網上通用的方法一般是兩種,yum和編譯安裝
一、yum方式
yum我沒測試,是以不做評論,感興趣可以參考以下兩個文章
centos6 ,7 使用yum 安裝最新nginx版本 這裡需要注意的是,新增的nginx.repo檔案中必須要加上“[nginx]”這一段
CentOS 7 YUM 安裝 Nginx 這篇文章看的雲山霧罩,是以不加評論
二、編譯方式
我在這裡采取的是編譯安裝的方式,步驟見下
2.1:安裝前提
在安裝nginx前,需要確定系統安裝了g++、gcc、openssl-devel、pcre-devel和zlib-devel軟體。安裝必須軟體:
安裝指令如下
[[email protected] /]# yum install gcc-c++
[[email protected] /]# yum -y install zlib zlib-developenssl openssl-devel pcre pcre-devel
檢查系統安裝的Nginx:
[[email protected] /]# find-name nginx
./usr/local/nginx/sbin/nginx
解除安裝原有的Nginx:
[[email protected] /]# yum remove nginx
下載下傳安裝包
方式一:從https://nginx.org/en/download.html直接下載下傳
方式二:
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
下載下傳後,将安裝包放到期望路徑,比如我的是/usr/local/下。然後解壓縮 tar -vxf nginx*。解壓後進入nginx-1.12.2
進入nginx-1.12.2目錄後,執行./configure。如果有特殊需求,可以執行個性化配置,如下
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注:将臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立temp及nginx目錄
但是我沒有測試
編譯安裝:
make
make install
查找安裝路徑:
whereis nginx
啟動、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
:此方式停止步驟是待nginx程序處理任務完畢進行停止。
./nginx -s quit
:此方式相當于先查出nginx程序id再使用kill指令強制殺掉程序。
./nginx -s stop
查詢nginx程序:
ps aux|grep nginx
重新開機 nginx
1.先停止再啟動(推薦):
對 nginx 進行重新開機相當于先停止再啟動,即先執行停止指令再執行啟動指令。如下:
./nginx -s quit
./nginx
2.重新加載配置檔案:
當 ngin x的配置檔案 nginx.conf 修改後,要想讓配置生效需要重新開機 nginx,使用
-s reload
不用先停止 ngin x再啟動 nginx 即可将配置資訊在 nginx 中生效,如下:
./nginx -s reload
啟動成功後,在浏覽器可以看到這樣的頁面:

開機自啟動
即在
rc.local
增加啟動代碼就可以了。
增加一行
/usr/local/nginx/sbin/nginx
設定執行權限:
到這裡,nginx就安裝完畢了,啟動、停止、重新開機操作也都完成了,當然,你也可以添加為系統服務,我這裡就不在示範了
三、坑們
解壓了nginx的安裝包後,我迫不及待的将nginx-1.12.2檔案夾改名為nginx。然後執行./configure和make
這樣在執行到make install的時候就産生報錯,内容如下:
根據報錯資訊查了半天,完全沒有頭緒,後來看到一個文章nginx源碼編譯安裝出現“make[1]: Leaving directory `/usr/local/nginx’“解決辦法 作者在裡面說當make install執行完畢之後,要檢查一下nginx檔案夾是否存在。
然後我就想,是否我不應該解壓後就改名,因為編譯之後會生成一個名為“nginx”的檔案夾,如果我改名了,就會有沖突?
是以我删掉之前的nginx檔案夾,重新解壓縮nginx的壓縮包,而且不再改名,再執行 1: ./configure 2: make 3:make install就能正确執行完畢了。問題解決!