在 CentOS 系统中,我们通常使用 Apache HTTP 服务器(httpd)来配置虚拟主机。以下是创建和配置 Apache 虚拟主机的步骤:
- 安装 Apache 服务:
sudo yum install httpd
- 启动 Apache 服务并设置开机启动:
sudo systemctl start httpd sudo systemctl enable httpd
- 创建一个新的目录作为虚拟主机的根目录:
sudo mkdir /var/www/example.com
- 更改新创建的目录的属主和属组为 apache:
sudo chown apache:apache /var/www/example.com
- 在虚拟主机根目录中创建一个简单的 HTML 文件:
echo "Welcome to Example.com!
" | sudo tee /var/www/example.com/index.html
- 创建一个新的虚拟主机配置文件:
sudo vi /etc/httpd/conf.d/example.com.conf
- 在新创建的配置文件中添加以下内容:
ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com AllowOverride All Require all granted ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined
请将 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!” 的消息。
请注意,为了使这些更改生效,您可能需要清除浏览器缓存或尝试使用其他设备或网络进行访问。