一、安裝依賴包
1、安裝gcc,nginx編譯依賴於gcc
yum install -y gcc-c++
2、安裝pcre,PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫
nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫
yum install -y pcre-devel
3、安裝zlib,zlib庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip
yum install -y zlib-devel
4、安裝openssl,OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel
二、nginx安裝
1、下載 wget https://nginx.org/download/nginx-1.16.1.tar.gz
2、解壓 tar -zxvf nginx-1.16.1.tar.gz
3、編譯 nginx安裝根目錄下執行命令
./configure
make
4、安裝 make install
注意:安裝成功後nginx服務默認會安裝在/usr/local/nginx目錄下
三、啓動及配置
1、啓動
cd /usr/local/nginx
./nginx
訪問http://localhost:80,如果頁面Welcome to nginx!則說明服務啓動成功
2、配置
2-1、配置開機啓動
2-1-1、在系統服務目錄/lib/systemd/system/下創建nginx.service
2-1-2、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
2-1-3、設置開機啓動 systemctl enable nginx.service
停止開機啓動 systemctl disable nginx.service
2-2、修改nginx啓動端口
nginx默認啓動端口爲80,可通過修改配置文件實現端口切換。
先切換到/usr/local/nginx/conf/下,編輯nginx.conf,修改截圖所示端口號
然後重新加載配置文件 ./nginx -s reload