安裝環境說明
- 系統centos7
- Nginx版本 nginx-1.12.0.tar.gz
- Nginx安裝包百度雲鏈接 密碼:6q92
安裝依賴包
# yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libxslt libxslt-devel gd-devel geoip geoip-devel –y
# yum install gd fontconfig-devel freetype-devel libX11-devel libXpm-devel libjpeg-devel libpng-devel -y
創建用戶
# useradd -M -s /sbin/nologin nginx #創建用戶
編譯安裝
# cd nginx-1.12.0/ #進入解壓文件夾
# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-mail \
--with-mail_ssl_module \
--with-stream_ssl_module
# make && make install
系統服務配置
# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP /usr/local/nginx/logs/nginx.pid
ExecStop=/bin/kill -s QUIT /usr/local/nginx/logs/nginx.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
啓動命令
# systemctl enable nginx #設置開啓自啓動
# systemctl start nginx #啓動nginx服務
# systemctl stop nginx
報錯解決
啓動Nginx後,查看狀態,發現雖然正在Nginx正在運行,但是提示Failed to read PID from file /run/nginx.pid: Invalid argument 這個錯誤
- 造成的原因是
因爲 nginx 啓動需要一點點時間,而 systemd 在 nginx 完成啓動前就去讀取
pid file 造成讀取 pid 失敗
解決方法很簡單,讓 systemd 在執行 ExecStart 的指令後等待一點點時間即可如果你的 nginx 啓動需要時間更長,可以把 sleep 時間改長一點
# mkdir -p /etc/systemd/system/nginx.service.d
# printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
# systemctl daemon-reload #重新加載配置文件
# systemctl restart nginx.service #重新啓動