编译安装
源码安装包下载地址:http://nginx.org/en/download.html
解压并进入解压后的目录,执行以下命令:
# 执行该命令可能会报错,通常为系统缺少某种依赖包,按错误提示使用 yum 安装即可
# 注意,安装缺少的依赖包时,需一并安装对应的 devel
# 该命令执行成功后,生成 Makefile 文件
./configure --prefix=/usr/local/nginx
# 编译和安装
# 该命令执行成功后,在 /usr/local/nginx 目录下生成安装后的文件
make && make install
启动与测试
# 执行 /usr/local/nginx/sbin 目录下的 nginx 命令即可
/usr/local/nginx/sbin nginx
# 查看启动后的进程
netstat -anp | grep nginx
# nginx 默认绑定的是 80 端口,因此直接访问服务器 ip 地址即可进行验证
# 访问不通时,可关闭防火墙后重试
# CentOS 7 查看防火墙状态
systemctl status firewalld
# CentOS 7 关闭防火墙
systemctl stop firewalld
通过 signals 控制 nginx
nginx 可以通过向主进程的进程 id 发送信号的方式进行控制,主进程的进程 id 默认被写入 /usr/local/nginx/logs/nginx.pid 文件(/usr/local/nginx 是我测试时的安装路径);主进程支持以下信号:
TERM, INT:立即停止 nginx
QUIT:优雅的停止 nginx
HUP:更新配置,以新的配置启动新的工作进程,优雅的关闭旧的工作进程
USR1:启用新的日志文件
USR2:升级可执行文件
WINCH:优雅的关闭工作进程
使用示例
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
支持的命令行参数
[root@base sbin]# ./nginx -?
nginx version: nginx/1.14.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
-? | -h:打印帮助信息
-c file:使用指定的配置文件替代默认配置文件
-g directives:设置全局配置指令,示例:nginx -g "pid /var/run/nginx.pid; worker_processes `sysctl -n hw.ncpu`;"
-p prefix:设置 nginx 目录前缀,即保存服务器文件的目录,默认为 /usr/local/nginx
-q:suppress non-error messages during configuration testing
-t:测试配置文件,nginx 检查配置是否正确,然后尝试打开配置文件中引用的文件
-T:same as -t, but additionally dump configuration files to standard output (1.9.2)
-v:打印 nginx 版本
-V:打印nginx版本、编译器版本和配置参数
-s signal:向主进程发送信号,参数信号支持如下:
- stop:理解停止 nginx
- quit:优雅的停止 nginx
- reload:更新配置,以新的配置启动新的工作进程,优雅的关闭旧的工作进程
- reopen:启用新的日志文件