Centos7 Nginx安裝部署

安裝環境說明

安裝依賴包

# 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

  • 形成的緣由是

由於 nginx 啓動須要一點點時間,而 systemd 在 nginx 完成啓動前就去讀取
pid file 形成讀取 pid 失敗
解決方法很簡單,讓 systemd 在執行 ExecStart 的指令後等待一點點時間便可若是你的 nginx 啓動須要時間更長,能夠把 sleep 時間改長一點vim

# 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   #從新啓動
相關文章
相關標籤/搜索