在 CentOS 系统中,使用 Apache 添加虚拟主机的步骤如下:
sudo yum install httpd sudo systemctl start httpd sudo systemctl enable httpd mywebsite 的目录:sudo mkdir /var/www/mywebsite sudo chown apache:apache /var/www/mywebsite index.html:echo "<h1>Welcome to My Website</h1>" | sudo tee /var/www/mywebsite/index.html vi 或 nano)创建一个名为 mywebsite.conf 的文件:sudo vi /etc/httpd/conf.d/mywebsite.conf <VirtualHost *:80> ServerAdmin webmaster@mywebsite.com ServerName mywebsite.com ServerAlias www.mywebsite.com DocumentRoot /var/www/mywebsite <Directory /var/www/mywebsite> AllowOverride All Require all granted </Directory> ErrorLog /var/log/httpd/mywebsite-error.log CustomLog /var/log/httpd/mywebsite-access.log combined </VirtualHost> 保存并关闭配置文件。
重启 Apache 服务以应用更改:
sudo systemctl restart httpd /etc/hosts 文件,以便在本地计算机上解析新创建的域名。在文件末尾添加以下行(使用您自己的 IP 地址替换 192.168.1.100):192.168.1.100 mywebsite.com www.mywebsite.com http://mywebsite.com,您应该能看到新创建的虚拟主机的欢迎页面。请注意,这些步骤假设您已经拥有一个有效的域名。如果您还没有域名,您可以使用本地 /etc/hosts 文件进行测试,如上所述。但是,对于实际部署,您需要将域名解析到您的服务器 IP 地址。这通常是通过购买域名并在域名注册商处配置 DNS 记录来完成的。