安裝包:可自行前往我的飛書下載
Docs
也可以進入?nginx?官網,下載自己所需適應版本
nginx
開始安裝nginx
1. 創建準備目錄
cd /opt mkdir soft module # 創建軟件包和源碼解壓目錄
2. 安裝依賴環境
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
3. 安裝 PCRE(Nginx 依賴)
# 解壓 PCRE cd /opt/soft tar -zxvf pcre-8.37.tar.gz -C ../module/# 編譯安裝 cd /opt/module/pcre-8.37 ./configure --prefix=/usr/local/pcre8 make && make install
4. 安裝 Nginx
# 解壓 Nginx cd /opt/soft tar -zxvf nginx-1.20.2.tar.gz -C ../module/# 配置編譯選項 cd /opt/module/nginx-1.20.2 ./configure --prefix=/usr/local/nginx# 編譯并安裝 make && make install
5. 驗證安裝
# 檢查版本 /usr/local/nginx/sbin/nginx -v# 測試配置文件 /usr/local/nginx/sbin/nginx -t
6. 啟動 Nginx
/usr/local/nginx/sbin/nginx# 檢查進程(要有master和worker) ps -ef | grep nginx# 測試訪問 curl http://localhost
靜態站點部署
1.創建站點目錄和文件
# 進入Nginx的HTML目錄
cd /usr/local/nginx/html# 創建hello子目錄
mkdir hello# 創建hello.html文件(使用vim或直接echo)
cat > hello/hello.html <<EOF
<html><header><title>hello</title></header><body><h1>Hello Nginx</h1></body>
</html>
EOF
?2.修改Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
在http塊內添加server配置:nginx
server {listen 10010;location / {root html/hello;index hello.html;}
}
3.檢查并啟動
# 測試配置文件語法
/usr/local/nginx/sbin/nginx -t# 重啟Nginx(如果已運行)
/usr/local/nginx/sbin/nginx -s reload# 首次啟動(如果未運行)
/usr/local/nginx/sbin/nginx
4.訪問測試
# 檢查端口監聽
netstat -tulnp | grep 10010# 測試訪問(本地)
curl http://localhost:10010# 或瀏覽器訪問
echo "訪問地址:http://服務器IP:10010"
5.防火墻設置(如需)
# 開放10010端口 firewall-cmd --add-port=10010/tcp --permanent firewall-cmd --reload# 或iptables iptables -I INPUT -p tcp --dport 10010 -j ACCEPT service iptables save
關鍵點說明
-
路徑關系:
-
配置文件中的
root html/hello
實際指向/usr/local/nginx/html/hello/
-
必須確保hello.html位于該目錄
-
-
權限問題:
chown -R nobody:nobody /usr/local/nginx/html/hello chmod -R 755 /usr/local/nginx/html/hello
-
備選配置方案:
nginx
server {listen 10010;root /usr/local/nginx/html/hello;index hello.html;location / {try_files $uri $uri/ =404;} }
遇到問題時可通過查看錯誤日志排查:
tail -f /usr/local/nginx/logs/error.log
常見問題處理
1. 端口沖突(80 端口被占用)
netstat -tulnp | grep 80 # 查看占用進程 systemctl stop httpd # 停止 Apache(示例)
2. 啟動失敗(缺少 pid 文件)
# 強制停止殘留進程 killall nginx# 重新啟動 /usr/local/nginx/sbin/nginx
3. 重新加載配置
/usr/local/nginx/sbin/nginx -s reload
卸載方法
# 停止服務 /usr/local/nginx/sbin/nginx -s stop# 刪除文件 rm -rf /usr/local/nginx /usr/local/pcre8