1、執行yum命令安裝依賴
yum -y install pcre*
yum -y install openssl*
2、下載nginx
//如果沒有安裝wget,下載已編譯版本
yum install wget
//進入指定目錄
cd /usr/local/
//下載nginx 安裝包,“1.16.0”是指定的安裝版本,可以選擇自己需要或者最新的版本
wget http://nginx.org/download/nginx-1.16.0.tar.gz
3、編譯安裝
//通過tar解壓安裝包
tar -zxvf zlib-1.16.0.tar.gz
//進入nginx
cd nginx-1.16.0
//執行編譯
./configure
//編譯報錯誤的話比如:“C compiler cc is not found”,這個就是缺少編譯環境,安裝一下就可以了
yum -y install gcc make gcc-c++ openssl-devel wget
//編譯成功執行
make -j4 && make install
4、nginx測試
//在nginx可執行命令下目錄/usr/local/nginx/sbin執行
./nginx -t
//出現下面結果表示安裝成功
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
//也可以執行./nginx 啟動,然后在瀏覽器訪問此機器的 IP,如果瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功
//部分命令
//重啟:./nginx -s reload
//停止:./nginx -s stop
5、配置開機啟動-配置文件(如果先部署項目,跳過看第7條)
//進入目錄,編輯nginx文件
cd /etc/init.d/
vi nginx
//添加如下,注內容修改PATH字段, 匹配自己的安裝路徑,如果按這個流程安裝應該是一樣的
#!/bin/bash
# Startup script for the nginx Web Server
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# HTML files and CGI.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
NGINX_HOME=/usr/local/nginx/sbin
NGINX_CONF=/usr/local/nginx/conf
PHP_HOME=/usr/local/php-fcgi/bin
if [ ! -f "$NGINX_HOME/nginx" ]
then
echo "nginxserver startup: cannot start"
exit
fi
case "$1" in
'start')
$PHP_HOME/spawn-fcgi -a 127.0.0.1 -p 10080 -C 20 -u nobody -f $PHP_HOME/php-cgi
$NGINX_HOME/nginx -c $NGINX_CONF/nginx.conf
echo "nginx start successful"
;;
'stop')
killall -TERM php-cgi
killall -TERM nginx
;;
esac
6、配置開機啟動-啟動
//設置執行權限
chmod a+x /etc/init.d/nginx
//注冊成服務
chkconfig --add nginx
//重啟, 查看nginx服務是否自動啟動.
shutdown -h0 -r
netstat -apn|grep nginx
7、配置部署自己的項目,下面以一個node/vue前后端分離的項目為例:
//找到nginx。conf配置文件
cd /usr/local/nginx/conf/
//編輯文件
vi nginx.conf
//在server對象里改為自己要的端口,默認為80
listen 80;
//同樣的配置前端打包地址: root為vue打包后存放在服務器的地址
location / {
root /data/paulBlog/browseClient/dist;
index index.html index.htm;
}
//同樣的配置后端接口地址:proxy_pass 為后端接口地址
#解決跨域
location /api { # 自定義nginx接口前綴
proxy_pass http://127.0.0.1:3000; # 后台api接口地址
proxy_redirect default;
#設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
//整體文件如下

//通過命令:qw 保存成功后最好是重新啟動下nginx
/usr/local/nginx/sbin/nginx -s reload
8、在阿里雲后台配置安全組規制放出對應的端口,就可以通過阿里雲提供的IP訪問了。需要域名訪問的話,同樣在阿里雲上申請,解析,最后備案后,可通過域名訪問,備案挺麻煩的^**^