在Apache中配置虚拟主机(Virtual Host)可以让您在同一台服务器上托管多个网站。以下是配置虚拟主机的基本步骤:
首先,确保启用了mod_vhost_alias和mod_rewrite模块。您可以通过以下命令启用这些模块:
sudo a2enmod vhost_alias
sudo a2enmod rewrite
然后重启Apache服务:
sudo systemctl restart apache2
在/etc/apache2/sites-available/目录下创建一个新的虚拟主机配置文件。例如,创建一个名为example.com.conf的文件:
sudo nano /etc/apache2/sites-available/example.com.conf
在新创建的配置文件中添加以下内容:
<VirtualHost *:80>
ServerAdmin webmaster@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}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
ServerAdmin:管理员的电子邮件地址。ServerName:主域名。ServerAlias:可选的别名域名。DocumentRoot:网站的根目录。<Directory>:指定网站根目录的访问权限。ErrorLog 和 CustomLog:日志文件的路径。使用a2ensite命令启用新创建的虚拟主机配置文件:
sudo a2ensite example.com.conf
如果您不需要默认的Apache站点,可以禁用它:
sudo a2dissite 000-default.conf
最后,重启Apache服务以应用更改:
sudo systemctl restart apache2
确保您的域名(例如example.com)指向您的服务器IP地址。您可以在域名注册商的控制面板中进行配置。
打开浏览器并访问您的域名,确保网站正确显示。
通过以上步骤,您就可以在Apache中成功配置虚拟主机了。根据需要,您可以创建多个虚拟主机配置文件,并为每个网站配置不同的域名和文档根目录。