CentOS7安裝Nginx-1.16.1穩定版


安裝依賴環境

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

 

1、下載安裝包:

#cd /usr/local/src
#wget http://nginx.org/download/nginx-1.16.1.tar.gz

 

2、解壓:

#tar -zxvf nginx-1.16.1.tar.gz

 

3、配置環境:

#cd nginx-1.16.1
#./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_ssl_module

 

nginx大部分常用模塊,編譯時./configure --help以--without開頭的都默認安裝。

--prefix=PATH : 指定nginx的安裝目錄。默認 /usr/local/nginx
--conf-path=PATH : 設置nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啟動,通過命令行中的-c選項。默認為prefix/conf/nginx.conf
--user=name: 設置nginx工作進程的用戶。安裝完成后,可以隨時在nginx.conf配置文件更改user指令。默認的用戶名是nobody。--group=name類似
--with-pcre : 設置PCRE庫的源碼路徑,如果已通過yum方式安裝,使用--with-pcre自動找到庫文件。使用--with-pcre=PATH時,需要從PCRE網站下載pcre庫的源碼(版本4.4 – 8.30)並解壓,剩下的就交給Nginx的./configure和make來完成。perl正則表達式使用在location指令和 ngx_http_rewrite_module模塊中。
--with-zlib=PATH : 指定 zlib(版本1.1.3 – 1.2.5)的源碼解壓目錄。在默認就啟用的網絡傳輸壓縮模塊ngx_http_gzip_module時需要使用zlib 。
--with-http_ssl_module : 使用https協議模塊。默認情況下,該模塊沒有被構建。前提是openssl與openssl-devel已安裝
--with-http_stub_status_module : 用來監控 Nginx 的當前狀態
--with-http_realip_module : 通過這個模塊允許我們改變客戶端請求頭中客戶端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意義在於能夠使得后台服務器記錄原始客戶端的IP地址
--add-module=PATH : 添加第三方外部模塊,如nginx-sticky-module-ng或緩存模塊。每次添加新的模塊都要重新編譯(Tengine可以在新加入module時無需重新編譯

4、編譯安裝:

# make && make install

這里對解壓完成后的部分目錄和文件做個簡單的介紹:

src 該目錄存放了Nginx的所有源碼;
man 該目錄存放了Nginx的幫助文檔;
html 該目錄存放了兩個html文件。這兩個文件與Nginx服務器的運行相關,這兩個文件的作用會在下文
給出,這里不做贅述;
conf 該目錄存放的是Nginx服務器的配置文件,包含Nginx服務器的基本配置文件;
auto 該目錄存放了大量腳本文件,和configure腳本程序有關;
configure 該文件是Nginx軟件的自動腳本程序。運行configure腳本一般會完成兩項工作:
一是檢查環境,根據環境檢查結果生成C代碼;二是生成編譯代碼需要的Makefile文件。
5、配置開機自啟動:
在目錄/usr/lib/systemd/system下創建文件nginx.service,文件內容如下:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

6、命令

設置開機啟動:
# systemctl enable nginx 啟動Nginx: # systemctl start nginx 停止Nginx: # systemctl stop nginx 重啟Nginx: # systemctl reload nginx

 


不帶服務的操作命令
## 檢查配置文件是否正確
# /usr/local/nginx/sbin/nginx -t
# ./sbin/nginx -v # 查看版本
# ./sbin/nginx -V # 可以看到編譯選項

## 啟動、關閉
# ./sbin/nginx # 默認配置文件 conf/nginx.conf,-c 指定
# ./sbin/nginx -s stop
或 pkill nginx

## 重啟,不會改變啟動時指定的配置文件
# ./sbin/nginx -s reload
或 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM