下面我按最常见的 Linux 发行版(CentOS / Rocky / Ubuntu)给你一个从零到可访问网页的 Apache 配置指南。如果你有特定需求(虚拟主机、HTTPS、PHP、反向代理等),可以再告诉我。
sudo apt update
sudo apt install apache2 -y
sudo dnf install httpd -y
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl start httpd
sudo systemctl enable httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo ufw allow 80
sudo ufw allow 443
浏览器访问:
http://服务器IP
看到 Apache 默认欢迎页即成功。
/var/www/html/ # 默认网站目录
/etc/apache2/ # 配置目录
/var/www/html/
/etc/httpd/
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
echo "<h1>Hello Apache</h1>" > /var/www/example.com/index.html
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
启用站点:
sudo a2ensite example.com
sudo a2dissite 000-default
sudo systemctl reload apache2
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>
sudo systemctl restart httpd
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com
sudo dnf install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com
sudo apachectl configtest
journalctl -xe
sudo setsebool -P httpd_read_user_content 1
告诉我你的 Linux 版本 + 使用场景,我可以给你更精准的配置示例。