Nginx 1.28.0 源碼編譯安裝與自定義網(wǎng)頁部署詳細指南
更新時間:2026年05月11日 09:42:39 作者:Lw老王要學習
本文介紹了Nginx1.28.0的安裝過程,提供了自定義HTML文件部署步驟、安裝后核心目錄說明、常用管理命令、部署前端項目示例等內(nèi)容,感興趣的朋友跟隨小編一起看看吧
一、前置說明
- 安裝包:
nginx-1.28.0.tar.gz,已上傳至/root目錄 - 系統(tǒng):CentOS 7.x(適配你當前的環(huán)境)
- 安裝方式:源碼編譯安裝,默認安裝到
/usr/local/nginx - 前置依賴:gcc、pcre、zlib、openssl(腳本自動安裝)
二、一鍵安裝腳本(推薦直接使用)
vim /root/nginx_install.sh
- 腳本如下:
#!/bin/bash
# Nginx 1.28.0 源碼編譯安裝腳本(CentOS 7.x,root執(zhí)行)
# ==================== 配置參數(shù) ====================
NGINX_VERSION="1.28.0"
NGINX_PACKAGE="/root/nginx-${NGINX_VERSION}.tar.gz"
INSTALL_PATH="/usr/local/nginx"
# =================================================
# 1. 檢查root權(quán)限
if [ $UID -ne 0 ]; then
echo "請使用root用戶執(zhí)行此腳本!"
exit 1
fi
# 2. 檢查安裝包是否存在
if [ ! -f "${NGINX_PACKAGE}" ]; then
echo "錯誤:/root目錄下未找到nginx-${NGINX_VERSION}.tar.gz安裝包!"
exit 1
fi
# 3. 安裝編譯依賴
echo "===== 安裝編譯依賴 ====="
yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 4. 解壓源碼包
echo "===== 解壓Nginx源碼包 ====="
cd /root
tar -zxf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}
# 5. 配置編譯參數(shù)(含SSL、gzip、rewrite等核心模塊)
echo "===== 配置編譯參數(shù) ====="
./configure \
--prefix=${INSTALL_PATH} \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-pcre
# 6. 編譯并安裝
echo "===== 編譯并安裝Nginx ====="
make && make install
# 7. 配置環(huán)境變量(方便直接調(diào)用nginx命令)
echo "===== 配置環(huán)境變量 ====="
if ! grep -q "nginx" /etc/profile; then
echo "export PATH=\$PATH:${INSTALL_PATH}/sbin" >> /etc/profile
fi
source /etc/profile
# 8. 創(chuàng)建系統(tǒng)服務(systemd管理,開機自啟)
echo "===== 創(chuàng)建Nginx系統(tǒng)服務 ====="
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx Web Server
After=network.target
[Service]
Type=forking
ExecStart=${INSTALL_PATH}/sbin/nginx
ExecReload=${INSTALL_PATH}/sbin/nginx -s reload
ExecStop=${INSTALL_PATH}/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 重載systemd配置并設置開機自啟
systemctl daemon-reload
systemctl enable nginx
# 9. 開放防火墻端口(80/443)
echo "===== 開放防火墻端口 ====="
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
# 10. 啟動Nginx并驗證
echo "===== 啟動Nginx并驗證 ====="
systemctl start nginx
sleep 2
if ps aux | grep nginx | grep -v grep > /dev/null; then
echo "====================================="
echo "? Nginx ${NGINX_VERSION} 安裝成功!"
echo "安裝路徑:${INSTALL_PATH}"
echo "配置文件:${INSTALL_PATH}/conf/nginx.conf"
echo "網(wǎng)頁目錄:${INSTALL_PATH}/html"
echo "啟動命令:systemctl start nginx"
echo "訪問地址:http://你的服務器IP"
echo "====================================="
else
echo "? Nginx 啟動失敗,請檢查安裝日志!"
exit 1
fi賦權(quán)執(zhí)行腳本+重新加載環(huán)境變量
chmod +x nginx_install.sh sh /root/nginx_install.sh source /etc/profile
三、手動分步安裝流程(可選)
步驟1:安裝編譯依賴
yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
步驟2:解壓源碼包
cd /root tar -zxf nginx-1.28.0.tar.gz cd nginx-1.28.0
步驟3:配置編譯參數(shù)
./configure \ --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-pcre
步驟4:編譯并安裝
make && make install
步驟5:配置環(huán)境變量
echo "export PATH=\$PATH:/usr/local/nginx/sbin" >> /etc/profile source /etc/profile
步驟6:創(chuàng)建systemd服務
cat > /usr/lib/systemd/system/nginx.service << EOF [Unit] Description=Nginx Web Server 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 stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF # 重載配置并設置開機自啟 systemctl daemon-reload systemctl enable nginx
步驟7:開放防火墻端口
firewall-cmd --add-service=http --permanent firewall-cmd --add-service=https --permanent firewall-cmd --reload
步驟8:啟動并驗證
# 啟動Nginx systemctl start nginx # 驗證進程 ps aux | grep nginx # 驗證端口(80端口) ss -tlnp | grep 80 # 本地訪問測試 curl http://127.0.0.1

步驟9、自定義HTML文件部署步驟
替換默認首頁(快速測試)
如果僅需替換Nginx默認的歡迎頁,直接將自定義的index.html覆蓋默認文件即可:
# 備份默認首頁(可選) cp /usr/local/nginx/html/index.html /usr/local/nginx/html/index.html.bak # 將本地自定義的index.html上傳到服務器(示例用scp,也可通過rz、FTP等方式) scp /本地路徑/index.html root@你的服務器IP:/usr/local/nginx/html/ # 刷新頁面即可訪問(無需重啟Nginx)
----#####我這里用的xftp¥¥¥¥¥----

四、安裝后核心目錄說明
| 目錄/文件 | 作用說明 |
|---|---|
/usr/local/nginx/conf/nginx.conf | 主配置文件,所有反向代理、站點配置都在這里 |
/usr/local/nginx/conf/conf.d/ | 自定義站點配置目錄(可手動創(chuàng)建,在nginx.conf中include) |
/usr/local/nginx/html/ | 默認網(wǎng)頁根目錄,部署前端項目時將文件放在這里 |
/usr/local/nginx/logs/ | 日志目錄,包含access.log(訪問日志)和error.log(錯誤日志) |
/usr/local/nginx/sbin/nginx | Nginx主程序,可直接執(zhí)行命令(如nginx -s reload重載配置) |
五、常用管理命令
# 啟動Nginx systemctl start nginx # 停止Nginx systemctl stop nginx # 重啟Nginx systemctl restart nginx # 重載配置(修改配置文件后用,不中斷服務) systemctl reload nginx # 或直接用主程序 nginx -s reload # 檢查配置文件語法 nginx -t # 查看Nginx狀態(tài) systemctl status nginx # 查看Nginx進程 ps aux | grep nginx # 查看端口占用 ss -tlnp | grep nginx
六、部署前端項目示例
- 將前端打包文件上傳至
/usr/local/nginx/html/你的項目目錄(如/usr/local/nginx/html/frontend) - 編輯配置文件
/usr/local/nginx/conf/nginx.conf,添加站點配置:
server {
listen 80;
server_name 你的服務器IP;
location / {
root /usr/local/nginx/html/frontend;
index index.html index.htm;
try_files $uri $uri/ /index.html; # 解決SPA路由刷新404問題
}
# 反向代理后端接口示例
location /api {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}- 檢查配置并重載:
nginx -t && nginx -s reload
七、常見問題排查
- 端口被占用:執(zhí)行
ss -tlnp | grep 80查看占用進程,停止占用進程或修改Nginx監(jiān)聽端口 - 配置文件語法錯誤:
nginx -t會明確提示錯誤位置,根據(jù)提示修改配置文件 - 訪問403 Forbidden:檢查網(wǎng)頁目錄權(quán)限,確保Nginx進程用戶(默認nobody)有讀取權(quán)限
- 無法訪問:檢查防火墻是否開放80/443端口,服務器安全組是否放行
到此這篇關于Nginx 1.28.0 源碼編譯安裝與自定義網(wǎng)頁部署簡明指南的文章就介紹到這了,更多相關nginx 1.28.0 源碼編譯安裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx接收Http協(xié)議請求轉(zhuǎn)發(fā)使用Https協(xié)議的問題
這篇文章主要介紹了Nginx接收Http協(xié)議請求轉(zhuǎn)發(fā)使用Https協(xié)議,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
nginx php-fpm環(huán)境中chroot功能的配置使用方法
這篇文章主要介紹了nginx php-fpm環(huán)境中chroot功能的配置使用方法,此方法是比禁用PHP敏感函數(shù)更好的一個安全防護手手段,需要的朋友可以參考下2014-05-05

