溫馨提示×

CentOS Minimal如何搭建Web服務器

小樊
50
2025-02-20 17:03:18
欄目: 云計算

在CentOS Minimal上搭建Web服務器,通常會選擇安裝Apache或Nginx。以下是使用這兩種Web服務器的步驟:

安裝Apache Web服務器

  1. 更新系統包

    sudo yum update -y
    
  2. 安裝Apache

    sudo yum install httpd -y
    
  3. 啟動Apache服務

    sudo systemctl start httpd
    
  4. 設置Apache開機自啟

    sudo systemctl enable httpd
    
  5. 檢查Apache狀態

    sudo systemctl status httpd
    
  6. 配置防火墻 如果你啟用了防火墻,需要允許HTTP和HTTPS流量:

    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    
  7. 訪問Web服務器 打開瀏覽器,訪問服務器的IP地址或域名,你應該能看到Apache的默認頁面。

安裝Nginx Web服務器

  1. 更新系統包

    sudo yum update -y
    
  2. 安裝EPEL倉庫(如果尚未安裝)

    sudo yum install epel-release -y
    
  3. 安裝Nginx

    sudo yum install nginx -y
    
  4. 啟動Nginx服務

    sudo systemctl start nginx
    
  5. 設置Nginx開機自啟

    sudo systemctl enable nginx
    
  6. 檢查Nginx狀態

    sudo systemctl status nginx
    
  7. 配置防火墻 如果你啟用了防火墻,需要允許HTTP和HTTPS流量:

    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    
  8. 訪問Web服務器 打開瀏覽器,訪問服務器的IP地址或域名,你應該能看到Nginx的默認頁面。

配置虛擬主機

無論是Apache還是Nginx,你都可以通過配置虛擬主機來管理多個網站。

Apache虛擬主機配置

  1. 創建虛擬主機配置文件

    sudo vi /etc/httpd/conf.d/yourdomain.conf
    
  2. 添加以下內容

    <VirtualHost *:80>
        ServerAdmin webmaster@yourdomain.com
        DocumentRoot /var/www/yourdomain/html
        ServerName yourdomain.com
        ServerAlias www.yourdomain.com
        ErrorLog /var/log/httpd/yourdomain-error.log
        CustomLog /var/log/httpd/yourdomain-access.log combined
    </VirtualHost>
    
  3. 重啟Apache服務

    sudo systemctl restart httpd
    

Nginx虛擬主機配置

  1. 創建虛擬主機配置文件

    sudo vi /etc/nginx/conf.d/yourdomain.conf
    
  2. 添加以下內容

    server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
    
        root /var/www/yourdomain/html;
        index index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        error_log /var/log/nginx/yourdomain-error.log;
        access_log /var/log/nginx/yourdomain-access.log;
    }
    
  3. 重啟Nginx服務

    sudo systemctl restart nginx
    

通過以上步驟,你可以在CentOS Minimal上成功搭建并配置一個Web服務器。

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