在 CentOS 系统中设置 Apache2 虚拟主机,可以按照以下步骤进行操作:
首先,确保你已经安装了 Apache。如果没有安装,可以使用以下命令进行安装:
sudo yum install httpd
安装完成后,启动 Apache 服务并设置为开机自启:
sudo systemctl start httpd
sudo systemctl enable httpd
在 /etc/httpd/conf.d/
目录下创建一个新的虚拟主机配置文件,例如 example.com.conf
:
sudo vi /etc/httpd/conf.d/example.com.conf
在打开的文件中添加以下内容,根据你的需求进行修改:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /var/www/example.com/html
ServerName example.com
ServerAlias www.example.com
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
<Directory /var/www/example.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
ServerAdmin
: 管理员邮箱地址。DocumentRoot
: 网站文件的根目录。ServerName
: 主机名或域名。ServerAlias
: 别名。ErrorLog
: 错误日志文件路径。CustomLog
: 访问日志文件路径。<Directory>
: 目录配置,包括访问权限等。根据 DocumentRoot
指定的路径创建网站文件目录,并添加一些示例文件:
sudo mkdir -p /var/www/example.com/html
sudo echo "Welcome to Example.com" > /var/www/example.com/html/index.html
保存并关闭配置文件后,重启 Apache 服务以使配置生效:
sudo systemctl restart httpd
确保你的域名(例如 example.com
)已经指向你的服务器 IP 地址。你可以在你的域名注册商的管理面板中进行设置。
在浏览器中输入你的域名(例如 http://example.com
),应该能够看到你设置的欢迎页面。
通过以上步骤,你就可以在 CentOS 系统上成功设置 Apache2 虚拟主机了。如果你有多个虚拟主机,可以按照相同的步骤为每个虚拟主机创建一个配置文件,并确保每个配置文件的 ServerName
和 ServerAlias
是唯一的。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: CentOS如何监控Node.js应用状态