分享知識 傳遞快樂
1、線上安裝
nginx的一些子產品依賴一些lib庫,在安裝nginx之前,須先安裝這些lib庫,
依賴庫主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 是以執行如下指令安裝
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
wget http://nginx.org/download/nginx-1.9.14.tar.gz
tar xvzf nginx-1.9.14.tar.gz
./configure
make && make install
cd /usr/local/nginx/sbin
./nginx
或者在/opt/nginx/目錄下執行
./configure --prefix=/opt/nginx && make && make install
2、離線安裝
1)先去官網下載下傳nginx檔案
nginx-1.12.0.tar.gz
2)解壓
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0
3)配置
其實在 nginx-1.12.0 版本中你就不需要去配置相關東西,預設就可以了。當然,如果你要自己配置目錄也是可以的。
(1)使用預設配置
./configure
最好跟上編譯後的位址,這樣友善管理。
(2)自定義配置
./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目錄
3)編譯安裝
make
make install
或者
make && make install
4)查找安裝路徑:
whereis nginx
nginx-whereis.png
5)啟動、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop 此方式相當于先查出nginx程序id再使用kill指令強制殺掉程序。
./nginx -s quit 此方式停止步驟是待nginx程序處理任務完畢進行停止。
./nginx -s reload
6)查詢nginx程序:
ps aux|grep nginx
注意
先停止再啟動(推薦):
對 nginx 進行重新開機相當于先停止再啟動,即先執行停止指令再執行啟動指令。如下:
./nginx -s quit
./nginx
重新加載配置檔案:
當 nginx 的配置檔案 nginx.conf 修改後,要想讓配置生效需要重新開機 nginx,使用-s reload不用先停止 ngin x再啟動 nginx 即可将配置資訊在 nginx 中生效,如下:
./nginx -s reload
啟動成功後,在浏覽器可以看到這樣的頁面:
nginx-welcome.png
開機自啟動
即在rc.local增加啟動代碼就可以了。
vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx
設定執行權限:
chmod 755 rc.local
nginx-rclocal.png
到這裡,nginx就安裝完畢了,啟動、停止、重新開機操作也都完成了,當然,你也可以添加為系統服務,我這裡就不在示範了。
————————————