CentOS Apache 安装与网站配置教程
- 安装 Apache
使用 yum 命令安装 Apache:
sudo yum install httpd
- 启动 Apache
使用 systemctl 命令启动 Apache:
sudo systemctl start httpd
- 设置开机自启动
使用 systemctl 命令设置 Apache 在系统启动时自动启动:
sudo systemctl enable httpd
- 配置防火墙
如果开启了防火墙,需要允许 HTTP 和 HTTPS 流量通过。使用 firewall-cmd 命令添加规则:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
- 创建网站目录
在 /var/www 目录下创建一个新目录,用于存放网站文件:
sudo mkdir /var/www/example.com
- 配置虚拟主机
在 Apache 配置目录下创建一个新的虚拟主机配置文件:
sudo nano /etc/httpd/conf.d/example.com.conf
添加以下内容:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
保存并退出文件。
- 重启 Apache
使用 systemctl 命令重新启动 Apache:
sudo systemctl restart httpd
现在可以通过浏览器访问 http://example.com 来查看网站是否正常运行。
原文地址: https://www.cveoy.top/t/topic/oYTZ 著作权归作者所有。请勿转载和采集!