一丶官網 http://nginx.org/en/download.html 至於安裝那個版本首先要看清楚版本代表什么意思
Nginx官網提供了三個類型的版本
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版
Stable version:最新穩定版,生產環境上建議使用的版本(毫無疑問,生產環境用於次版本)
Legacy versions:遺留的老版本的穩定版
編譯安裝前所需要的准備:
1.GCC編譯器
首先檢查GCC是否安裝,命令:gcc -v ,如果顯示有相關版本信息,則說明已經安裝好,沒有就安裝:
yum install -y gcc # -y參數表示一直確認安裝
2.PCRE庫
Nginx的HTTP模塊要用它來解析正則表達式。
yum install -y pcre pcre-devel
pcre-devel是使用PCRE做二次開發時所需要的開發庫。類似的你可以想到安裝LAMP時安裝的php-devel。
3.zlib庫
gzip格式的壓縮會用到它。
yum install -y zlib zlib-devel
4.OpenSSL庫
yum install -y openssl openssl-devel
wget http://nginx.org/download/nginx-1.14.0.tar.gz #這里安裝的是1.14.0生產穩定版本
解壓安裝
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
-
./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre #生成 Makefile,為下一步的編譯做准備
-
make #編譯
-
make install #安裝
附(Nginx部分控制命令):
默認Nginx安裝在/usr/local/nginx/中,因此
-
/usr/local/nginx/sbin/nginx #默認啟動方式 start
-
/usr/local/nginx/sbin/nginx -t #測試配置信息
-
/usr/local/nginx/sbin/nginx -v #顯示版本信息,-V(大V)顯示編譯時的參數
-
/usr/local/nginx/sbin/nginx -s stop #快速停止服務
-
/usr/local/nginx/sbin/nginx -s quit #正常停止服務
- /usr/local/nginx/sbin/nginx -s reload #重啟
Q: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
A: [root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

如果不行記得要打開端口
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,沒有此參數重啟后失效)
重新載入
firewall-cmd --reload
可選 創建方便啟動命令
創建nginx啟動命令腳本
vi /etc/init.d/nginx
插入以下內容:
#! /bin/bash # chkconfig: - 85 15 PATH=/usr/local/nginx DESC="nginx daemon" NAME=nginx DAEMON=$PATH/sbin/$NAME CONFIGFILE=$PATH/conf/$NAME.conf PIDFILE=$PATH/logs/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop() { $DAEMON -s stop || echo -n "nginx not running" } do_reload() { $DAEMON -s reload || echo -n "nginx can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0
設置執行權限 chmod a+x /etc/init.d/nginx
注冊成服務 chkconfig --add nginx
設置開機啟動 chkconfig nginx on