1. 安裝Nginx
在CentOS上安裝Nginx有兩種常見方式:YUM源安裝(快速便捷,推薦新手)和源碼編譯安裝(靈活自定義,適合有特定需求的情況)。
YUM源安裝步驟:
sudo yum update -y
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
源碼編譯安裝步驟(可選):
sudo yum install -y gcc make pcre-devel openssl-devel zlib-devel
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre
make && sudo make install
sudo nano /etc/systemd/system/nginx.service
內容如下:[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
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
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
2. 基礎配置
Nginx的主配置文件位于/etc/nginx/nginx.conf
(YUM安裝)或/usr/local/nginx/conf/nginx.conf
(源碼安裝),虛擬主機配置通常放在/etc/nginx/conf.d/
(YUM)或/usr/local/nginx/conf/conf.d/
(源碼)目錄下。
核心配置說明:
user nginx; # 運行用戶(默認nginx)
worker_processes auto; # 工作進程數(auto自動匹配CPU核心數)
error_log /var/log/nginx/error.log warn; # 錯誤日志路徑和級別
pid /var/run/nginx.pid; # 主進程PID文件路徑
events {
worker_connections 1024; # 每個工作進程最大并發連接數
use epoll; # 事件模型(Linux下推薦epoll)
multi_accept on; # 允許一個進程同時接受多個連接
}
http {
include /etc/nginx/mime.types; # 引入MIME類型文件
default_type application/octet-stream; # 默認MIME類型
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; # 訪問日志格式
access_log /var/log/nginx/access.log main; # 訪問日志路徑
sendfile on; # 開啟高效文件傳輸
keepalive_timeout 65; # 長連接超時時間(秒)
gzip on; # 啟用GZIP壓縮
include /etc/nginx/conf.d/*.conf; # 包含虛擬主機配置
}
server {
listen 80; # 監聽80端口(HTTP)
server_name example.com www.example.com; # 域名(可多個,空格分隔)
root /var/www/html/example.com; # 網站根目錄
index index.html index.htm; # 默認首頁文件
location / {
try_files $uri $uri/ =404; # 嘗試查找文件,不存在則返回404
}
error_page 404 /404.html; # 自定義404錯誤頁面
location = /404.html {
root /var/www/html; # 錯誤頁面路徑
}
}
3. 虛擬主機配置
Nginx支持基于域名的虛擬主機(最常用),通過不同server_name
區分多個網站。
步驟:
sudo mkdir -p /var/www/html/example.com
sudo echo "Welcome to Example.com" > /var/www/html/example.com/index.html
example.com.conf
):sudo nano /etc/nginx/conf.d/example.com.conf
粘貼上述server
塊配置,修改server_name
和root
為你的域名和目錄。sudo nginx -t # 檢查配置是否有語法錯誤
sudo systemctl reload nginx # 重載配置(不中斷服務)
4. 防火墻設置
如果服務器啟用了防火墻(如firewalld
),需要開放HTTP(80)和HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http # 永久開放HTTP
sudo firewall-cmd --permanent --add-service=https # 永久開放HTTPS
sudo firewall-cmd --reload # 重載防火墻規則
若需臨時關閉防火墻(僅測試用,生產環境不推薦):
sudo systemctl stop firewalld
5. SSL配置(可選,啟用HTTPS)
使用Let’s Encrypt免費獲取SSL證書,步驟如下:
sudo yum install -y certbot python3-certbot-nginx
example.com
為你的域名):sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run # 測試續期
6. 常用命令
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl reload nginx
(不中斷服務)sudo systemctl status nginx
sudo nginx -t
sudo tail -f /var/log/nginx/error.log
通過以上步驟,你可以在CentOS上完成Nginx的安裝、基礎配置及虛擬主機設置,滿足常見的Web服務需求。