本文記錄Centos7上安裝Nginx詳細(xì)步驟
1、安裝依賴包
[root@localhost ~] yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl—devel
2、下載Nginx(地址:http://nginx.org/en/download.html)

3、下載完成后使用MobaXterm上傳到服務(wù)器 /user/install目錄下
4、解壓Nginx
[root@localhost ~] cd /user/install
[root@localhost soft] tar -zxvf nginx-1.17.0.tar.gz -C /opt
5、編譯和安裝Nginx
[root@localhost opt] cd nginx-1.17.0/
#指定安裝在/usr/local/nginx目錄下,并增加http_ssl_module模塊(如不需要可不加這個配置)
[root@localhost nginx-1.17.0] ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.17.0] make && make install
6、查看Nginx版本號
[root@localhost ~] cd /usr/local/nginx/sbin/
[root@localhost sbin] ./nginx -h

7、啟動和停止nginx
//啟動
[root@localhost sbin] ./nginx
//停止
[root@localhost sbin] ./nginx -s stop
//重啟
[root@localhost sbin] ./nginx -s reload
8、驗(yàn)證是否安裝成功
(1)輸入命令后,沒有任何提示,那我們?nèi)绾沃繬ginx服務(wù)已經(jīng)啟動了哪?可以使用Linux的組合命令,進(jìn)行查詢服務(wù)的運(yùn)行狀況。
ps aux | grep nginx
如果啟動成功會出現(xiàn)如下圖片中類似的結(jié)果

(2)在瀏覽器地址欄輸入服務(wù)器的IP(http://192.168.58.7),出現(xiàn)下圖界面表示安裝成功

二、配置Nginx
1、配置文件分開
在Linux中不同的用戶都可能用到Nginx,如果不同的用戶無法達(dá)成一個對nginx.conf編寫標(biāo)準(zhǔn),勢必會導(dǎo)致nginx.conf里的內(nèi)容變的相當(dāng)混亂,極難維護(hù)。所以新建一個文件夾,這個文件夾中分放不同用戶所需要反向代理的配置文件。
[root@localhost /]vim /usr/local/nginx/conf/nginx.conf
nginx.conf 文件盡量不做修改,只需在最末尾加上 include /usr/local/nginx/conf.d/*.conf;
2、編輯配置文件
/usr/local/nginx 目錄下新建文件夾conf.d,用于存放模塊配置
mkdir /usr/local/nginx/conf.d
新建文件yimall.conf,添加內(nèi)容如下
server {
listen 80;
server_name 192.168.197.90;
charset utf-8;
location /yimall {
alias /www/web/www.yimall.pro/dist;
try_files $uri $uri/ /index.html last;
index index.html;
}
access_log /www/logs/www.yimall.log;
error_log /www/logs/www.yimall.error.log;
}
向/www/web/www.yimall.pro/ 文件夾下上傳打包好的vue dist文件夾,測試訪問 http://192.168.197.90/yimall/
Tips:按照上述nginx配置,需要注意vue項(xiàng)目里的配置:
1.router的index.js中
mode: 'history',
base:'/yimall/'
2.vue.config.js中
// 部署應(yīng)用時的基本 URL
publicPath: '/yimall/',
3、查看端口號
在默認(rèn)情況下,Nginx啟動后會監(jiān)聽80端口,從而提供HTTP訪問,如果80端口已經(jīng)被占用則會啟動失敗。我么可以使用 netstat -tlnp 命令查看端口號的占用情況。
三、配置開機(jī)啟動
1、切換到/lib/systemd/system/目錄,創(chuàng)建nginx.service文件
cd /lib/systemd/system/
vim nginx.service
內(nèi)容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2、加入開機(jī)自啟動
systemctl enable nginx
如果不想開機(jī)自啟動了,可以使用下面的命令取消開機(jī)自啟動
systemctl disable nginx
查看nginx狀態(tài)
systemctl status nginx
如果沒有正常啟動,檢查80端口和防火墻再重啟nginx
pkill -9 nginx
systemctl start nginx
systemctl status nginx
3、服務(wù)的啟動/停止/刷新配置文件/查看狀態(tài)
啟動nginx服務(wù) systemctl start nginx.service
停止服務(wù) systemctl stop nginx.service
重新啟動服務(wù) systemctl restart nginx.service
查看所有已啟動的服務(wù) systemctl list-units --type=service
查看服務(wù)當(dāng)前狀態(tài) systemctl status nginx.service
設(shè)置開機(jī)自啟動 systemctl enable nginx.service
停止開機(jī)自啟動 systemctl disable nginx.service
4、一個常見的錯誤
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示執(zhí)行命令systemctl daemon-reload 即可
systemctl daemon-reload