1、安装各种依赖
#gcc安装,nginx源码编译需要
yum install gcc-c++
#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
yum install -y pcre pcre-devel
#zlib安装,nginx 使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel
#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
yum install -y openssl openssl-devel
2、下载
(1)直接官网下载【官网链接】
当前稳定版本是1.20.1
(2)使用wget命令下载(推荐)
#下载版本号可根据目前官网最新稳定版自行调整
wget -c http://nginx.org/download/nginx-1.20.1.tar.gz
3、安装
#根目录使用ls命令可以看到下载的nginx压缩包,然后解压
tar -zxvf nginx-1.20.1.tar.gz
#解压后进入目录
cd nginx-1.20.1
#使用默认配置
./configure
#编译安装
make
make install
#查找安装路径,默认都是这个路径
[root@VM_0_12_centos ~]# whereis nginx
nginx: /usr/local/nginx
#启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx #启动
./nginx -s stop #停止,直接查找nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit #退出停止,等待nginx进程处理完任务再进行停止
./nginx -s reload #重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效
#重启nginx,建议先停止,再启动
./nginx -s stop
./nginx
#查看nginx进程,如下返回,即为成功
[root@VM_0_12_centos ~]# ps aux|grep nginx
root 5984 0.0 0.0 112708 976 pts/1 R+ 14:41 0:00 grep --color=auto nginx
root 18198 0.0 0.0 20552 612 ? Ss 11:28 0:00 nginx: master process ./nginx
nobody 18199 0.0 0.0 23088 1632 ? S 11:28 0:00 nginx: worker process
4、开机自启动
#在rc.local增加启动代码即可
vi /etc/rc.local
#增加一行 /usr/local/nginx/sbin/nginx,增加后保存
#设置执行权限
cd /etc
chmod 755 rc.local
5、创建全局命令
#nginx 全局命令
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
6、nginx 常用命令
- 以特定目录下的配置文件启动:
nginx -c /特定目录/nginx.conf
- 重新加载配置:
nginx -s reload
执行这个命令后,master进程会等待worker进程处理完当前请求,然后根据最新配置重新创建新的worker进程,完成Nginx配置的热更新。- 立即停止服务:
nginx -s stop
- 从容停止服务:
nginx -s quit
执行该命令后,Nginx在完成当前工作任务后再停止。- 检查配置文件是否正确:
nginx -t
- 检查特定目录的配置文件是否正确:
nginx -t -c /特定目录/nginx.conf
- 查看版本信息:
nginx -v
7、nginx.conf配置实例