在 CentOS 系统下设置 Apache2 虚拟主机,你需要遵循以下步骤:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
mywebsite
的目录:sudo mkdir /var/www/mywebsite
sudo chown -R apache:apache /var/www/mywebsite
/etc/httpd/conf/httpd.conf
文件中添加以下内容,以启用虚拟主机配置:IncludeOptional conf.d/*.conf
mywebsite.conf
的文件:sudo vi /etc/httpd/conf.d/mywebsite.conf
mywebsite.conf
文件中添加以下内容,根据需要进行修改:<VirtualHost *:80>
ServerAdmin webmaster@mywebsite.com
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/mywebsite
<Directory /var/www/mywebsite>
Options Indexes FollowSymLinks
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
更新你的 DNS 设置,将域名(例如:mywebsite.com)指向你的服务器 IP 地址。
在你的服务器上创建一个简单的 HTML 文件,以测试虚拟主机是否正常工作。例如,在 /var/www/mywebsite
目录中创建一个名为 index.html
的文件,并添加以下内容:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
</body>
</html>
现在你已经在 CentOS 系统下成功设置了 Apache2 虚拟主机。你可以根据需要创建更多的虚拟主机配置文件,并为每个站点指定不同的域名和目录。