nginx-001-安装及配置教程

123 阅读2分钟

nginx.png

1.安装教程

1.1 下载地址

nginx.org/en/download…

1.2 安装说明

PS:本文以Centos-7作为安装环境

  1. 文件上传至 /usr/local 文件夹
  2. 安装需要的相关依赖
yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre-devel

相关依赖说明: gcc: gcc-c++编译环境
gzip模块需要:   zlib 库
rewrite模块需要:pcre 库
ssl功能需要:    openssl库

  1. 创建nginx用户和用户组
groupadd nginx
useradd nginx -g nginx -s /sbin/nologin -M
  1. 配置nginx
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module

# 查看是否配置成功
echo $?
# 0 说明成功

make && make install

# 添加到环境变量
vim /etc/profile.d/nginx.sh
# export PATH=$PATH:/usr/local/nginx/sbin

/usr/local/nginx/sbin/nginx    启动nginx
/usr/local/nginx/sbin/nginx -s reload    #重载配置
/usr/local/nginx/sbin/nginx -s stop    #停止
/usr/local/nginx/sbin/nginx -s quit    #关闭

--user=nginx(用户名) --group=nginx(组) --prefix=/usr/local/nginx(程序安装路径) --with-http_stub_status_module (网页状态查看) --with-http_ssl_module (ssl模块) --with-http_realip_module (后台Nginx服务器记录原始客户端的IP地址 ) --with-http_gzip_static_module (压缩模块)

  1. 系统服务启动脚本 二选一
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

或者

[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

ps: PIDFile:PID路径,如果是按照上面编译时候设置的路径,那就在/usr/local/nginx/logs/nginx.pid; ExecStartPre:在执行ExecStart之前的操作,先删除有关nginx的PID也就是停止nginx,然后再检查nginx -t配置文件是否正确; ExecStart:nginx启动操作,如果是按照上面编译时候设置的路径,那就在/usr/local/nginx/sbin/nginx; ExecReload:nginx重启操作,这里HUP是平滑重启,重新加载配置文件; ExecStop:nginx停止操作,实际是使用了QUIT从容关闭nginx;