# ubuntu / centos linux安装nginx

> Ubuntu安装 nginx 一键安装 sudo apt-get install nginx启动命令： cd /usr/local/nginx/sbin/ sudo /etc/init.d/nginx start //启动 sudo /et…

- 原文：https://www.l2qq.com/article/90
- 发布：2019-11-06T02:18:28Z
- 更新：2020-06-12T09:51:01Z
- 作者：l2qq

---

Ubuntu安装 nginx
一键安装
```
sudo apt-get install nginx
```
启动命令：

```
cd /usr/local/nginx/sbin/
sudo /etc/init.d/nginx start //启动
sudo /etc/init.d/nginx stop //停止
sudo /etc/init.d/nginx restart //重启
```
http://nginx.org/en/download.html
### 1、官方网站下载Stable版本（正式环境）
Nginx官网提供了三个类型的版本
Mainline version：Mainline 是 Nginx 目前主力在做的版本，可以说是开发版
Stable version：最新稳定版，生产环境上建议使用的版本
Legacy versions：遗留的老版本的稳定版
```
wget http://nginx.org/download/nginx-1.16.1.tar.gz
```

### 2、安装gcc
nginx是C语言开发，建议在linux上运行。
安装nginx需要先将官网下载的源码进行编译，编译依赖gcc环境，如果没有gcc环境，需要安装gcc
```
yum install gcc-c++
```

2.PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库，包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式，所以需要在linux上安装pcre库。
```
#注：pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
yum install -y pcre pcre-devel
```

3.zlib
zlib库提供了很多种压缩和解压缩的方式，nginx使用zlib对http包的内容进行gzip，所以需要在linux上安装zlib库。
```
yum install -y zlib zlib-devel
```

4.openssl
OpenSSL 是一个强大的安全套接字层密码库，囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议，并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议，还支持https（即在ssl协议上传输http），所以需要在linux安装openssl库。
```
yum install -y openssl openssl-devel
```
5、编译安装nginx
将nginx-1.16.1.tar.gz拷贝至linux服务器，或使用命令下载：
```
wget http://nginx.org/download/nginx-1.16.1.tar.gz
```
6、解压：

```
tar -zxvf nginx-1.16.1.tar.gz
cd nginx
```
7、配置：

注意：要拼接一行
```
./configure --prefix=/usr/local/nginx -with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module
```
```
./configure --prefix=/usr/local/nginx
--prefix=/usr/local/nginx
--conf-path=/etc/nginx/nginx.conf
--user=nginx --group=nginx
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--with-http_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-client-body-temp-path=/var/tmp/nginx/client
--http-proxy-temp-path=/var/tmp/nginx/proxy
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
```
进入目录，执行命令：

```
make
```
生成Makefile再进行安装：
```
make install
```
8、启动Nginx
```
#切换到安装位置,也就是之前配置configure的cd /usr/local/nginx#启动#注：nginx.conf 是 nginx 的默认配置文件。也可以使用 nginx -c 指定配置文件启动sbin/nginx#启动失败按照提示创建对应client目录#启动成功查看nginx进程（master进程和worker进程）ps -ef | grep nginx
```
测试访问
```
停止nginx
方式1，快速停止： cd /usr/local/nginx/sbin
./nginx -s stop
此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
方式2，完整停止(建议使用)： cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。
重启nginx
方式1，先停止再启动（建议使用）：
对nginx进行重启相当于先停止nginx再启动nginx，即先执行停止命令再执行启动命令。
如下：
./nginx -s quit
./nginx
方式2，重新加载配置文件：
当nginx的配置文件nginx.conf修改后，要想让配置生效需要重启nginx，使用-s reload不用先停止
```
