1. 前言
本文使用的操作系統(tǒng):
名稱 | 描述 | 文章 |
---|---|---|
Oracle VM VirtualBox | 虛擬機(jī)軟件 | VirtualBox 使用介紹 |
CentOS-7-x86_64-Minimal-2009.iso | CentOS 7.9 最小化安裝鏡像文件 | VirtualBox 安裝 CentOS 7 |
操作系統(tǒng)信息如下所示
cat /etc/redhat-release
2. 源碼包
進(jìn)入 nginx 官網(wǎng):https://nginx.org,查看最新穩(wěn)定版,復(fù)制鏈接地址,本文使用的是當(dāng)前最新穩(wěn)定版本 v1.24.0
https://nginx.org/download/nginx-1.24.0.tar.gz
3. 編譯安裝
本文將 nginx 源碼包存放在 /usr/local/src
目錄
# 進(jìn)入目錄
cd /usr/local/src
# 下載 nginx 源碼包
wget https://nginx.org/download/nginx-1.24.0.tar.gz
當(dāng)使用 wget 下載 nginx 源碼包時,提示命令不存在。使用 yum 安裝即可,然后重新使用 wget 下載 nginx 源碼包
yum install wget -y
解壓 nginx 源碼包,進(jìn)入源碼包目錄,執(zhí)行預(yù)編譯命令
nginx 的安裝目錄默認(rèn)是 /usr/local/nginx
,--prefix
配置項缺省時默認(rèn)就是該目錄,但不建議省略該參數(shù)選項
tar -zxf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure --prefix=/usr/local/nginx
當(dāng)預(yù)編譯出現(xiàn)以下報錯時,表示沒有 gcc 編譯器,使用 yum 安裝即可
nginx 是使用 c 語言編寫的程序,因此想要運(yùn)行 nginx 就需要安裝一個編譯工具。gcc 就是一個開源的編譯器集合,用于處理各種各樣的語言,其中就包含了 c 語言,運(yùn)行以下命令安裝即可
# 安裝 gcc 編譯器
yum install gcc -y
# 可通過以下命令來查看 gcc 是否安裝成功
gcc --version
當(dāng)預(yù)編譯出現(xiàn)以下報錯時,表示缺少 pcre(兼容正則表達(dá)式庫),使用 yum 安裝即可
nginx 在編譯過程中需要使用到 pcre 庫,因為在 nginx 的 Rewrite 模塊和 http 核心模塊都會使用到 pcre 正則表達(dá)式語法
# 安裝 pcre 庫
yum install pcre pcre-devel -y
# 可以通過以下命令來查看是否安裝成功
rpm -qa pcre pcre-devel
當(dāng)預(yù)編譯出現(xiàn)以下報錯時,表示缺少 zlib,使用 yum 安裝即可
zlib 庫提供了開發(fā)人員的壓縮算法,在 nginx 的各個模塊中需要使用 gzip 壓縮,所以我們也需要安裝其庫及源代碼
# 安裝 zlib 庫
yum install zlib zlib-devel -y
# 可以通過以下命令來查看是否安裝成功
rpm -qa zlib zlib-devel
當(dāng)看到以下內(nèi)容,表示預(yù)編譯成功,目前最小化安裝成功了,也就是使用最少的參數(shù)
當(dāng)我們配置 SSL 證書,實現(xiàn) HTTPS 訪問時,會將監(jiān)聽的端口改為 443 ssl
,重載配置發(fā)現(xiàn)報錯了
server {
listen 443 ssl;
server_name www.waterflosserreview.com;
}
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:36
這是因為沒有安裝 SSL 模塊,不支持配置 SSL,運(yùn)行以下命令安裝即可
# 安裝 openssl 庫
yum install openssl openssl-devel -y
# 可以通過以下命令來查看是否安裝成功
rpm -qa openssl openssl-devel
./configure --prefix=/usr/local/nginx --with-http_ssl_module
4. 安裝總結(jié)
環(huán)境準(zhǔn)備:安裝 wget 和 編譯 nginx 所需要的依賴包
yum install wget -y
yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
下載 nginx 源碼包
# 源碼包存放目錄
cd /usr/local/src
# 下載 nginx 源碼包
wget https://nginx.org/download/nginx-1.24.0.tar.gz
# 解壓縮 nginx 源碼包
tar -zxf nginx-1.24.0.tar.gz
# 進(jìn)入源碼包目錄
cd nginx-1.24.0
執(zhí)行編譯安裝
# 預(yù)編譯
./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module
# 編譯并安裝
make && make install
當(dāng) nginx 安裝成功后,/usr/local/nginx
目錄內(nèi)容如下所示
關(guān)閉防火墻
# 關(guān)閉防火墻狀態(tài)
systemctl stop firewalld
# 關(guān)閉防火墻開機(jī)自啟
systemctl disable firewalld
5. 啟動 nginx
進(jìn)入 /usr/local/nginx/sbin
目錄,運(yùn)行以下命令啟動 nginx 服務(wù)
# 進(jìn)入 nginx 安裝目錄下的 sbin 目錄
cd /usr/local/nginx/sbin
# 啟動 nginx 服務(wù),相對路徑寫法,./ 不能省略,表示執(zhí)行 nginx 文件
./nginx
也可以使用絕對路徑寫法
/usr/local/nginx/sbin/nginx
絕對路徑命令比較長,可以定義命令的別名簡化命令
# 定義命令別名
alias nginx=/usr/local/nginx/sbin/nginx
# 使用別名控制 nginx 服務(wù)啟停
nginx # 啟動
nginx -s stop # 停止
nginx -s reload # 重啟
命令補(bǔ)充:
./nginx -s stop # 快速停止
./nginx -s quit # 優(yōu)雅關(guān)閉,在關(guān)閉前完成已經(jīng)接受的連接請求
./nginx -s reload # 重新加載配置
使用 curl 命令測試訪問,看到以下內(nèi)容說明啟動成功
curl 127.0.0.1
6. 關(guān)閉防火墻
通過以下命令查看虛擬主機(jī)的局域網(wǎng) IP
ip addr | grep 192.168
目前局域網(wǎng)內(nèi)其他電腦無法訪問虛擬主機(jī),如下所示,這是因為防火墻是開啟狀態(tài)
運(yùn)行以下命令,即可關(guān)閉防火墻,如果只是使用虛擬機(jī)進(jìn)行測試,可以直接關(guān)閉防火墻
在正式環(huán)境中,可以開啟防火墻,只需要開放相應(yīng)端口即可,點擊查看防火墻命令更多用法
# 查看防火墻狀態(tài)(running|not running)
firewall-cmd --state
# 關(guān)閉防火墻狀態(tài)
systemctl stop firewalld
# 關(guān)閉防火墻開機(jī)自啟
systemctl disable firewalld
此時,就可以發(fā)現(xiàn)能訪問了
7. 設(shè)置系統(tǒng)服務(wù)
創(chuàng)建服務(wù)腳本
vi /usr/lib/systemd/system/nginx.service
服務(wù)腳本內(nèi)容
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
運(yùn)行以下命令,創(chuàng)建的 nginx 系統(tǒng)服務(wù)生效
# 重新加載系統(tǒng)服務(wù)
systemctl daemon-reload
nginx 系統(tǒng)服務(wù)相關(guān)命令,可以更加方便的管理 nginx 服務(wù)
# 查看 nginx 服務(wù)狀態(tài)
systemctl status nginx
# 啟動 nginx 服務(wù)
systemctl start nginx
# 關(guān)閉 nginx 服務(wù)
systemctl stop nginx
# 重載 nginx 配置
systemctl reload nginx
# 啟用 nginx 服務(wù)開機(jī)自啟
systemctl enable nginx
# 關(guān)閉 nginx 服務(wù)開機(jī)自啟
systemctl disable nginx
8. 卸載 nginx
步驟一:停止 nginx 服務(wù)
/usr/local/nginx/sbin/nginx -s stop
步驟二:將安裝的 nginx 刪除
rm -rf /usr/local/nginx
步驟三:將安裝包之前編譯的環(huán)境清除掉
cd /usr/local/src/nginx-1.24.0
make clean