下面给你一份Linux 下 Apache(httpd)常见配置入门指南,以 CentOS / RHEL / Ubuntu 为主,涵盖安装、启动、虚拟主机、常用配置和排错。
sudo yum install httpd -y
# 或 CentOS 8+
sudo dnf install httpd -y
sudo apt update
sudo apt install apache2 -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2
/etc/httpd/conf/httpd.conf/etc/httpd/conf.d//var/www/html/etc/apache2/apache2.conf/etc/apache2/sites-available//etc/apache2/sites-enabled//var/www/html编辑主配置文件:
# CentOS
vi /etc/httpd/conf/httpd.conf
# Ubuntu
vi /etc/apache2/ports.conf
vi /etc/apache2/sites-available/000-default.conf
示例修改:
Listen 8080
DocumentRoot "/data/www"
<Directory "/data/www">
AllowOverride All
Require all granted
</Directory>
在 /etc/httpd/conf.d/ 新建文件:
vi /etc/httpd/conf.d/example.conf
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
重启:
systemctl restart httpd
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.conf
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
</VirtualHost>
启用站点:
a2ensite example.conf
systemctl restart apache2
a2enmod rewrite
systemctl restart apache2
主配置中确保:
LoadModule rewrite_module modules/mod_rewrite.so
# CentOS
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
# Ubuntu
ufw allow 80
setsebool -P httpd_can_network_connect 1
chcon -R -t httpd_sys_content_t /var/www
# 查看错误日志
tail -f /var/log/httpd/error_log # CentOS
tail -f /var/log/apache2/error.log # Ubuntu
# 配置语法检查
httpd -t # CentOS
apache2ctl -t # Ubuntu
echo "Hello Apache" > /var/www/html/index.html
http://服务器IP
如果你告诉我:
我可以给你一份针对性配置模板。