在 CentOS 上搭建 Apache 网站,一般步骤如下(以 CentOS 7 / 8 为例,适用于大多数情况):
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
查看状态:
sudo systemctl status httpd
如果开启了 firewalld:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问服务器 IP:
http://服务器IP
看到 Apache Test Page 即成功。
sudo mkdir -p /var/www/html/my site
sudo chown -R apache:apache /var/www/html/my site
sudo chmod -R 755 /var/www/html/my site
sudo vi /var/www/html/my site/index.html
内容示例:
<html>
<body>
<h1>Hello Apache on CentOS</h1>
</body>
</html>
sudo vi /etc/httpd/conf.d/mysite.conf
示例:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html/my site
<Directory /var/www/html/my site>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/mysite-error.log
CustomLog /var/log/httpd/mysite-access.log combined
</VirtualHost>
sudo systemctl restart httpd
sudo yum install php php-mysql -y
sudo systemctl restart httpd
sudo setsebool -P httpd_read_user_content 1
或临时关闭测试:
sudo setenforce 0
如果你需要:
可以告诉我,我可以给你更具体的配置。