debian

Debian Apache如何管理虚拟主机

小樊
46
2025-08-15 01:50:57
栏目: 云计算

在Debian上管理Apache虚拟主机,主要通过配置文件实现,步骤如下:

  1. 安装Apache

    sudo apt update && sudo apt install apache2  
    
  2. 启用必要模块

    sudo a2enmod rewrite ssl  # 启用重写和SSL模块(如需HTTPS)  
    sudo systemctl restart apache2  
    
  3. 创建虚拟主机配置文件
    /etc/apache2/sites-available/目录下新建文件(如example.com.conf),内容示例:

    <VirtualHost *:80>  
        ServerAdmin admin@example.com  
        ServerName example.com  
        ServerAlias www.example.com  
        DocumentRoot /var/www/example.com  
        <Directory /var/www/example.com>  
            Options Indexes FollowSymLinks  
            AllowOverride All  
            Require all granted  
        </Directory>  
        ErrorLog ${APACHE_LOG_DIR}/example.com-error.log  
        CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined  
    </VirtualHost>  
    
  4. 启用虚拟主机

    sudo a2ensite example.com.conf  # 启用配置  
    sudo a2dissite 000-default.conf  # 可选:禁用默认站点  
    sudo systemctl restart apache2  # 重启生效  
    
  5. 配置DNS
    确保域名解析指向服务器IP(需在域名管理面板操作)。

  6. SSL配置(可选)
    使用Let’s Encrypt免费证书:

    sudo apt install certbot python3-certbot-apache  
    sudo certbot --apache -d example.com -d www.example.com  
    

管理命令

通过以上步骤可完成虚拟主机的添加、启用及管理,支持多站点隔离配置。

0
看了该问题的人还看了