下面给你一份在 CentOS 上配置 Apache(httpd)的实用指南,从安装到基本配置、防火墙、虚拟主机,适合服务器新手。
sudo yum install httpd -y
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
查看状态:
systemctl status httpd
如果启用了 firewalld:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache Test Page 即成功。
/var/www/html
放一个测试页:
echo "Hello Apache" > /var/www/html/index.html
/etc/httpd/conf/httpd.conf
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R apache:apache /var/www/example.com
sudo vi /etc/httpd/conf.d/example.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo systemctl restart httpd
| 配置项 | 说明 |
|---|---|
Listen 80 |
监听端口 |
DocumentRoot |
网站根目录 |
ServerName |
域名 |
DirectoryIndex |
默认首页 |
sudo yum install php php-cli -y
sudo systemctl restart httpd
测试:
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
tail -f /var/log/httpd/error_log
chown -R apache:apache /var/www
chmod -R 755 /var/www
如果你告诉我:
我可以直接给你一份定制版配置。