溫馨提示×

CentOS鏡像如何配置Web服務器

小樊
35
2025-08-12 22:22:18
欄目: 云計算

以下是基于CentOS鏡像配置Web服務器的步驟(以Apache和Nginx為例):

一、基礎環境準備

  1. 安裝CentOS系統

    • 使用CentOS鏡像文件通過虛擬機或物理機安裝系統,安裝過程中可配置網絡、分區等。
    • 更新系統:sudo yum update -y。
  2. 安裝Web服務器軟件

    • Apache
      sudo yum install httpd -y  
      sudo systemctl start httpd  
      sudo systemctl enable httpd  
      
    • Nginx
      sudo yum install epel-release -y  # 安裝EPEL倉庫(若需最新版Nginx)  
      sudo yum install nginx -y  
      sudo systemctl start nginx  
      sudo systemctl enable nginx  
      

二、配置Web服務器

1. Apache配置

  • 創建虛擬主機
    編輯配置文件 /etc/httpd/conf.d/虛擬主機名.conf,添加以下內容:
    <VirtualHost *:80>  
        ServerName example.com  
        DocumentRoot "/var/www/html/example.com"  
        ErrorLog "/var/log/httpd/example.com-error.log"  
        CustomLog "/var/log/httpd/example.com-access.log" combined  
    </VirtualHost>  
    
  • 設置目錄權限
    sudo mkdir -p /var/www/html/example.com  
    sudo chown -R apache:apache /var/www/html/example.com  
    sudo chmod -R 755 /var/www/html  
    

2. Nginx配置

  • 創建服務器塊
    編輯配置文件 /etc/nginx/conf.d/虛擬主機名.conf,添加以下內容:
    server {  
        listen 80;  
        server_name example.com;  
        root /var/www/html/example.com;  
        index index.html index.htm;  
        location / {  
            try_files $uri $uri/ =404;  
        }  
    }  
    
  • 設置目錄權限
    sudo mkdir -p /var/www/html/example.com  
    sudo chown -R nginx:nginx /var/www/html/example.com  
    sudo chmod -R 755 /var/www/html  
    

三、開放防火墻與測試

  1. 允許HTTP/HTTPS流量
    sudo firewall-cmd --permanent --add-service=http  
    sudo firewall-cmd --permanent --add-service=https  
    sudo firewall-cmd --reload  
    
  2. 測試訪問
    • 通過瀏覽器輸入服務器IP或域名,查看默認頁面或虛擬主機頁面是否正常顯示。

四、可選配置

  • 安裝SSL證書:使用Let’s Encrypt免費證書,配置HTTPS(以Apache為例):
    sudo certbot --apache  
    
  • 配置負載均衡/高可用:可結合Nginx反向代理或Docker實現多服務器負載均衡。

以上步驟基于CentOS默認鏡像,根據實際需求調整軟件版本或配置參數即可。

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