基本安裝
- 版本:
Centos8 安裝 1.18.0 的版本
Centos7 安裝1.8.0 版本
- 解壓
tar -zxvf nginx-*.tar.gz
- 編譯
進入到解壓後的nginx 目錄
./configure --prefix=/usr/local/nginx
- prefix 後面對應的是安裝的路徑
- 如果需要配置https,編譯時需要加上https 模塊,編譯命令如下:
./configure --prefix=/usr/local/nginx --with-http_ssl_module
- 安裝
make && make install
- 配置
vim /usr/local/nginx/conf/
- 基礎命令
/usr/local/nginx/sbin/nginx //啓動
/usr/local/nginx/sbin/nginx -s reload //重啓
/usr/local/nginx/sbin/nginx -s stop //停止
/usr/local/nginx/sbin/nginx -t //查看配置是否正確
/usr/local/nginx/sbin/nginx -V //查看版本
- 查看是否啓動成功
netstat -unltp | grep nginx
- netstat 可使用yum -y install net-tools 安裝
開機自啓動
- 簡單配置
vim /etc/rc.d/rc.local
末尾加入:
/usr/local/nginx/sbin/nginx
賦予權限
chmod +x /etc/rc.d/rc.local
- 更好的解決方案(推薦使用)
新建nginx.service 文件
vim /lib/systemd/system/nginx.service
輸入以下內容並保存:
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
通過以下命令進行控制nginx 的啓動與停止以及自啓動
systemctl start nginx.service //啓動nginx服務
systemctl stop nginx.service //停止服務
systemctl restart nginx.service //重新啓動服務
systemctl list-units --type=service //查看所有已啓動的服務
systemctl status nginx.service //查看服務當前狀態
systemctl enable nginx.service //設置開機自啓動
systemctl disable nginx.service //停止開機自啓動
- 問題:systemctl status nginx.service 顯示faile 或者 inactive,先殺死nginx進程 然後重新啓動
pkill -9 nginx
ps aux | grep nginx
systemctl start nginx
如果發現啓動不了,按提示運行 systemctl daemon-reload
- 設置開機自啓動
systemctl enable nginx.service
再次 reboot ,開機後輸入再次查nginx 狀態是否啓動成功,如果轉發的地址爲域名比如,t2.tianditu.com ,參考第4步。
https 配置證書
- 使用openssl 工具生成證書
- 證書位置:/usr/local/nginx/conf
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/nginx/conf/nginx.key -out /usr/local/nginx/conf/nginx.crt
- openssl 可通過yum 安裝
- 配置https 反向代理
prox_pass 參數爲域名時
配置開機自啓動時需要解析域名t2.tianditu.com,虛擬機重啓使用 systemctl status nginx.service 查看nginx 狀態,發現報錯:(手動啓動沒有此問題)
host not found in upstream "xxx.xx.xx"
解決