在Debian上管理Apache虚拟主机,主要通过配置文件实现,步骤如下:
安装Apache
sudo apt update && sudo apt install apache2
启用必要模块
sudo a2enmod rewrite ssl # 启用重写和SSL模块(如需HTTPS)
sudo systemctl restart apache2
创建虚拟主机配置文件
在/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>
启用虚拟主机
sudo a2ensite example.com.conf # 启用配置
sudo a2dissite 000-default.conf # 可选:禁用默认站点
sudo systemctl restart apache2 # 重启生效
配置DNS
确保域名解析指向服务器IP(需在域名管理面板操作)。
SSL配置(可选)
使用Let’s Encrypt免费证书:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
管理命令:
sudo apache2ctl -S
sudo systemctl reload apache2
sudo a2dissite <配置文件名>
并重启服务。通过以上步骤可完成虚拟主机的添加、启用及管理,支持多站点隔离配置。