在 Linux 上安装和配置 Apache HTTP 服务器(通常称为 Apache)是搭建网站的基础步骤,以下是详细指南,适用于 Ubuntu/Debian 和 CentOS/RHEL 系统:

安装 Apache
Ubuntu/Debian 系统
# 更新软件包列表 sudo apt update # 安装 Apache sudo apt install apache2 # 启动服务并设置开机自启 sudo systemctl start apache2 sudo systemctl enable apache2
CentOS/RHEL 系统
# 更新软件包 sudo yum update # 安装 Apache(包名 httpd) sudo yum install httpd # 启动服务并设置开机自启 sudo systemctl start httpd sudo systemctl enable httpd
验证安装
- 打开浏览器访问服务器 IP 地址:
http://你的服务器IP - 或使用终端检查:
curl 127.0.0.1
若看到 Apache 默认欢迎页面,表示安装成功。

防火墙配置
Ubuntu/Debian (使用 UFW)
# 允许 HTTP/HTTPS 流量 sudo ufw allow 'Apache Full' # 或单独允许 80/443 端口 sudo ufw reload
CentOS/RHEL (使用 firewalld)
# 允许 HTTP/HTTPS sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
关键目录与文件
| 路径 | 用途 |
|---|---|
| Ubuntu/Debian | |
/etc/apache2/ |
主配置目录 |
/etc/apache2/apache2.conf |
全局配置文件 |
/etc/apache2/sites-available/ |
虚拟主机配置(未启用) |
/etc/apache2/sites-enabled/ |
已启用的虚拟主机(符号链接) |
/var/www/html/ |
默认网站根目录 |
| CentOS/RHEL | |
/etc/httpd/conf/httpd.conf |
主配置文件 |
/etc/httpd/conf.d/ |
附加配置/虚拟主机目录 |
/var/www/html/ |
默认网站根目录 |
配置虚拟主机(以 Ubuntu 为例)
(1) 创建网站目录
sudo mkdir -p /var/www/example.com/public_html
(2) 设置目录权限
sudo chown -R $USER:$USER /var/www/example.com/public_html sudo chmod -R 755 /var/www/example.com
(3) 创建虚拟主机配置文件
sudo nano /etc/apache2/sites-available/example.com.conf
```根据需求修改):
```apache
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
(4) 启用站点并重载配置
# 启用站点 sudo a2ensite example.com.conf # 禁用默认站点(可选) sudo a2dissite 000-default.conf # 重新加载 Apache sudo systemctl reload apache2
测试配置与排错
- 检查语法错误:
sudo apachectl configtest # 输出 Syntax OK 表示无错误
- 查看日志:
tail -f /var/log/apache2/error.log # Ubuntu/Debian tail -f /var/log/httpd/error_log # CentOS/RHEL
常用命令
| 命令 | 作用 |
|---|---|
sudo systemctl start apache2/httpd |
启动服务 |
sudo systemctl stop apache2/httpd |
停止服务 |
sudo systemctl restart apache2/httpd |
重启服务 |
sudo systemctl reload apache2/httpd |
重载配置(不中断连接) |
sudo systemctl status apache2/httpd |
查看服务状态 |
进阶配置
启用 HTTPS (使用 Let’s Encrypt)
# 安装 Certbot sudo apt install certbot python3-certbot-apache # Ubuntu sudo yum install certbot python3-certbot-apache # CentOS # 获取证书(自动修改配置) sudo certbot --apache -d example.com -d www.example.com
启用常用模块
# 启用 rewrite 模块(用于 URL 重写) sudo a2enmod rewrite # Ubuntu sudo ln -s /etc/httpd/conf.modules.d/00-rewrite.conf /etc/httpd/conf.d/ # CentOS # 重载服务生效 sudo systemctl reload apache2
问题排查
- 403 Forbidden 错误:
- 检查目录权限:确保
/var/www/目录有执行权限。 - 检查虚拟主机配置中的
DocumentRoot路径是否正确。
- 检查目录权限:确保
- 404 Not Found 错误:
- 确认文件是否存在于网站根目录。
- 检查虚拟主机配置中的域名是否正确。
- 端口冲突:
- 使用
sudo netstat -tulpn | grep ':80'检查是否有其他程序占用 80 端口。
- 使用
按照以上步骤,您已成功安装并配置 Apache,根据实际需求调整虚拟主机、SSL 或模块配置即可。

图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/295323.html

