溫馨提示×

centos虛擬機Web服務器搭建

小樊
58
2025-07-14 14:00:09
欄目: 云計算

在CentOS虛擬機上搭建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. 配置防火墻 如果你使用的是firewalld,需要允許HTTP和HTTPS流量:

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

使用Nginx搭建Web服務器

  1. 更新系統

    sudo yum update -y
    
  2. 安裝Nginx

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

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

    sudo systemctl enable nginx
    
  5. 配置防火墻 同樣,如果你使用的是firewalld,需要允許HTTP和HTTPS流量:

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

配置虛擬主機

無論是Apache還是Nginx,都可以配置多個虛擬主機來托管不同的網站。

Apache虛擬主機配置

  1. 編輯Apache配置文件:

    sudo vi /etc/httpd/conf/httpd.conf
    
  2. 添加虛擬主機配置:

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

    sudo systemctl restart httpd
    

Nginx虛擬主機配置

  1. 編輯Nginx配置文件:

    sudo vi /etc/nginx/nginx.conf
    
  2. 添加虛擬主機配置:

    server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
    
        root /var/www/html/yourdomain;
        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虛擬機上成功搭建并配置一個Web服務器。

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