溫馨提示×

nginx在centos怎樣設置

小樊
37
2025-10-15 07:10:21
欄目: 智能運維

1. 安裝Nginx
在CentOS上安裝Nginx有兩種常見方式:YUM源安裝(快速便捷,推薦新手)和源碼編譯安裝(靈活自定義,適合有特定需求的情況)。

YUM源安裝步驟

  • 更新系統并安裝EPEL倉庫(EPEL提供Nginx等額外軟件包):
    sudo yum update -y
    sudo yum install epel-release -y
    
  • 安裝Nginx:
    sudo yum install nginx -y
    
  • 啟動Nginx并設置開機自啟:
    sudo systemctl start nginx
    sudo systemctl enable nginx
    

源碼編譯安裝步驟(可選):

  • 安裝編譯工具和依賴:
    sudo yum install -y gcc make pcre-devel openssl-devel zlib-devel
    
  • 下載并解壓Nginx源碼(以1.25.3為例):
    wget http://nginx.org/download/nginx-1.25.3.tar.gz
    tar -zxvf nginx-1.25.3.tar.gz
    cd nginx-1.25.3
    
  • 配置編譯選項(指定安裝路徑、啟用SSL等模塊):
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre
    
  • 編譯并安裝:
    make && sudo make install
    
  • 創建systemd服務文件(方便管理):
    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/(源碼)目錄下。

核心配置說明

  • 全局塊(文件頂部):設置Nginx全局參數,如工作進程數、錯誤日志路徑:
    user nginx;                  # 運行用戶(默認nginx)
    worker_processes auto;       # 工作進程數(auto自動匹配CPU核心數)
    error_log /var/log/nginx/error.log warn;  # 錯誤日志路徑和級別
    pid /var/run/nginx.pid;      # 主進程PID文件路徑
    
  • events塊:配置連接處理方式,優化高并發性能:
    events {
        worker_connections 1024;   # 每個工作進程最大并發連接數
        use epoll;                 # 事件模型(Linux下推薦epoll)
        multi_accept on;           # 允許一個進程同時接受多個連接
    }
    
  • http塊:配置HTTP服務器參數,包含虛擬主機和MIME類型:
    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塊(虛擬主機):定義單個網站的配置,如監聽端口、域名、根目錄:
    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_nameroot為你的域名和目錄。
  • 測試配置語法并重載Nginx:
    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證書,步驟如下:

  • 安裝Certbot和Nginx插件:
    sudo yum install -y certbot python3-certbot-nginx
    
  • 獲取并安裝證書(替換example.com為你的域名):
    sudo certbot --nginx -d example.com -d www.example.com
    
  • 按提示完成配置(如選擇是否自動重定向HTTP到HTTPS)。
  • 證書自動續期(Let’s Encrypt有效期90天):
    sudo certbot renew --dry-run  # 測試續期
    

6. 常用命令

  • 啟動Nginx:sudo systemctl start nginx
  • 停止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服務需求。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女