在 CentOS 系统中,我们通常使用 Apache HTTP 服务器(httpd)来配置虚拟主机。以下是创建和配置 Apache 虚拟主机的步骤:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo mkdir /var/www/example.com
sudo chown apache:apache /var/www/example.com
echo "<h1>Welcome to Example.com!</h1>" | sudo tee /var/www/example.com/index.html
sudo vi /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
请将 example.com 替换为您自己的域名。
保存并关闭配置文件。
重启 Apache 服务以应用更改:
sudo systemctl restart httpd
/etc/hosts 文件,以便在本地计算机上解析新创建的域名:127.0.0.1 example.com www.example.com
现在,您应该可以在浏览器中访问 http://example.com,并看到 “Welcome to Example.com!” 的消息。
请注意,为了使这些更改生效,您可能需要清除浏览器缓存或尝试使用其他设备或网络进行访问。