以下是 Nginx 1.13.9 的詳細安裝步驟(基于 CentOS/Ubuntu 系統):
1. 安裝依賴
CentOS/RHEL
sudo yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
Ubuntu/Debian
sudo apt update && sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
2. 下載并解壓 Nginx 1.13.9
wget http://nginx.org/download/nginx-1.13.9.tar.gz
tar -zxvf nginx-1.13.9.tar.gz
cd nginx-1.13.9
3. 配置編譯選項
./configure \
--prefix=/data/opt/nginx-1.13.9 \
--sbin-path=/data/opt/nginx-1.13.9/sbin/nginx \
--conf-path=/data/opt/nginx-1.13.9/conf/nginx.conf \
--error-log-path=/data/opt/nginx-1.13.9/log/error.log \
--http-log-path=/data/opt/nginx-1.13.9/log/access.log \
--pid-path=/data/opt/nginx-1.13.9/run/nginx.pid \
--with-http_ssl_module \
--with-openssl=/data/opt/openssl-1.1.0l
4. 編譯并安裝
make && make install
5. 創建用戶和目錄
# 創建 Nginx 用戶和組
sudo groupadd -r nginx
sudo useradd -r -g nginx -s /bin/false -M nginx# 創建日志目錄
sudo mkdir -p /var/log/nginx
sudo chown -R nginx:nginx /var/log/nginx# 創建臨時目錄(如果需要)
sudo mkdir -p /var/cache/nginx
sudo chown -R nginx:nginx /var/cache/nginx
6. 配置 Systemd 服務(可選)
創建服務文件 /etc/systemd/system/nginx.service
:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target
7. 啟動并驗證
# 重新加載 Systemd
sudo systemctl daemon-reload# 啟動 Nginx
sudo systemctl start nginx# 設置開機自啟
sudo systemctl enable nginx# 驗證版本
nginx -v
# 輸出應為:nginx/1.13.9# 檢查配置文件
nginx -t
# 輸出應為:syntax is ok, test is successful
8. 開放防火墻(如需要)
# CentOS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload# Ubuntu
sudo ufw allow 'Nginx Full'
9. 訪問測試
在瀏覽器中訪問服務器 IP,看到以下頁面表示成功:
Welcome to nginx!
常見問題
1. 端口沖突
如果 80
端口被占用:
# 查找占用進程
sudo netstat -tulnp | grep :80# 停止沖突服務(例如 Apache)
sudo systemctl stop httpd
2. 配置文件路徑錯誤
如果修改過路徑,需確保所有配置文件(如 nginx.conf
)中的路徑一致:
# 檢查錯誤日志路徑
grep "error_log" /etc/nginx/nginx.conf
附:編譯參數說明
--prefix
: 安裝根目錄--sbin-path
: 可執行文件路徑--conf-path
: 主配置文件路徑--with-http_ssl_module
: 啟用 HTTPS 支持--with-http_v2_module
: 啟用 HTTP/2 協議
如果需要其他模塊(如 gzip
、proxy
),可在 ./configure
時添加對應參數。
/data/opt/nginx-1.13.9/sbin/nginx -s reload