目录
一、Ubuntu/Debian 安装 二、CentOS/RHEL 安装 三、Windows 安装 四、基本配置 五、虚拟主机配置 六、常用模块启用 七、防火墙配置 八、服务管理命令 九、验证安装 十、常见问题排查
一、Ubuntu/Debian 安装
1. 更新软件源
sudo apt update
2. 安装 Apache
sudo apt install apache2 -y
3. 确认安装版本
apache2 -v
# 输出示例:Server version: Apache/2.4.52 (Ubuntu)
4. 启动并设置开机自启
sudo systemctl start apache2
sudo systemctl enable apache2
5. 验证服务状态
sudo systemctl status apache2
二、CentOS/RHEL 安装
1. 更新软件源
sudo yum update -y
# CentOS 8+ / RHEL 8+ 使用:
# sudo dnf update -y
2. 安装 Apache(CentOS 中包名为 httpd)
sudo yum install httpd -y
# CentOS 8+:
# sudo dnf install httpd -y
3. 确认安装版本
httpd -v
# 输出示例:Server version: Apache/2.4.37 (centos)
4. 启动并设置开机自启
sudo systemctl start httpd
sudo systemctl enable httpd
5. 验证服务状态
sudo systemctl status httpd
三、Windows 安装
1. 下载 Apache
访问 Apache Lounge 下载 Windows 版本(选择 VC17 编译版本)。
2. 解压安装
将下载的 zip 文件解压到 C:\Apache24(推荐保持默认路径)。
3. 修改配置文件
编辑 C:\Apache24\conf\httpd.conf,确认以下配置:
Define SRVROOT "C:/Apache24"
ServerRoot "${SRVROOT}"
Listen 80
ServerName localhost:80
4. 安装为系统服务
以管理员身份打开命令提示符:
cd C:\Apache24\bin
httpd.exe -k install
5. 启动服务
httpd.exe -k start
或通过 services.msc 找到 Apache2.4 服务手动启动。
四、基本配置
主配置文件位置
/etc/apache2/apache2.conf | |
/etc/httpd/conf/httpd.conf | |
C:\Apache24\conf\httpd.conf |
网站根目录
/var/www/html | |
/var/www/html | |
C:\Apache24\htdocs |
修改网站根目录(示例)
以 Ubuntu 为例,编辑配置文件:
sudo nano /etc/apache2/sites-available/000-default.conf
修改 DocumentRoot 指向你的目录:
DocumentRoot /home/user/mywebsite
同时需要授权目录访问权限,编辑 apache2.conf:
<Directory /home/user/mywebsite>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
重启服务生效:
sudo systemctl restart apache2
五、虚拟主机配置
Ubuntu/Debian
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
3. 创建虚拟主机配置文件
sudo nano /etc/apache2/sites-available/example.com.conf
写入以下内容:
<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 # 禁用默认站点(可选)
sudo systemctl restart apache2
CentOS/RHEL
编辑 /etc/httpd/conf.d/example.com.conf:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
重启生效:
sudo systemctl restart httpd
六、常用模块启用
Ubuntu/Debian 模块管理
# 查看已启用模块
apache2ctl -M
# 启用 SSL 模块
sudo a2enmod ssl
# 启用 URL 重写模块
sudo a2enmod rewrite
# 启用 Headers 模块
sudo a2enmod headers
# 启用缓存模块
sudo a2enmod cache
sudo a2enmod cache_disk
# 禁用模块
sudo a2dismod 模块名
# 重启生效
sudo systemctl restart apache2
CentOS/RHEL 模块管理
模块配置在 /etc/httpd/conf.modules.d/ 目录下,编辑对应的 .conf 文件:
# 启用 SSL
sudo yum install mod_ssl -y # 安装后自动启用
# 启用 rewrite(通常默认已安装,确认配置即可)
grep -r "LoadModule rewrite_module" /etc/httpd/conf.modules.d/
配置 HTTPS(SSL)
1. 安装 Certbot 获取免费 SSL 证书
# Ubuntu
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache
# CentOS
sudo yum install certbot python3-certbot-apache -y
sudo certbot --apache
2. 手动配置 SSL 虚拟主机
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/private/example.com.key
</VirtualHost>
3. HTTP 自动跳转 HTTPS
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
七、防火墙配置
UFW(Ubuntu)
# 放行 HTTP 和 HTTPS
sudo ufw allow 'Apache Full'
# 或分别放行:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# 查看防火墙状态
sudo ufw status
Firewalld(CentOS/RHEL)
# 放行 HTTP 和 HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
# 重新加载防火墙
sudo firewall-cmd --reload
# 查看已放行服务
sudo firewall-cmd --list-services
iptables(通用)
# 放行 HTTP (80) 和 HTTPS (443)
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 保存规则
sudo iptables-save | sudo tee /etc/iptables/rules.v4
八、服务管理命令
Ubuntu/Debian(apache2)
# 启动
sudo systemctl start apache2
# 停止
sudo systemctl stop apache2
# 重启
sudo systemctl restart apache2
# 优雅重启(不断开现有连接)
sudo systemctl reload apache2
# 查看状态
sudo systemctl status apache2
# 设置开机自启
sudo systemctl enable apache2
# 取消开机自启
sudo systemctl disable apache2
CentOS/RHEL(httpd)
# 启动
sudo systemctl start httpd
# 停止
sudo systemctl stop httpd
# 重启
sudo systemctl restart httpd
# 优雅重启
sudo systemctl reload httpd
# 查看状态
sudo systemctl status httpd
# 设置开机自启
sudo systemctl enable httpd
Windows
# 启动
httpd.exe -k start
# 停止
httpd.exe -k stop
# 重启
httpd.exe -k restart
# 卸载服务
httpd.exe -k uninstall
九、验证安装
1. 检查服务运行状态
# Linux
sudo systemctl status apache2 # Ubuntu
sudo systemctl status httpd # CentOS
# 或检查端口监听
sudo ss -tlnp | grep :80
2. 浏览器访问
打开浏览器访问:
本机: http://localhost局域网: http://服务器IP地址公网: http://公网IP或域名
看到 Apache2 Ubuntu Default Page 或 Testing 123 页面即安装成功。
3. 命令行测试
curl -I http://localhost
# 应返回 HTTP/1.1 200 OK 及 Server: Apache/...
十、常见问题排查
❌ 端口被占用
症状: 启动失败,日志显示 Address already in use: AH00072
# 查看占用 80 端口的进程
sudo lsof -i :80
# 或
sudo ss -tlnp | grep :80
# 终止占用进程
sudo kill -9 <PID>
# 或修改监听端口
# 编辑配置文件,将 Listen 80 改为其他端口如 Listen 8080
❌ 权限不足(403 Forbidden)
症状: 访问网站返回 403 错误
# 检查目录权限
ls -la /var/www/html/
# 修复权限
sudo chown -R www-data:www-data /var/www/html/ # Ubuntu
sudo chown -R apache:apache /var/www/html/ # CentOS
sudo chmod -R 755 /var/www/html/
❌ 无法外网访问
排查步骤:
确认 Apache 正在监听
sudo ss -tlnp | grep :80确认防火墙已放行
sudo ufw status # Ubuntu
sudo firewall-cmd --list # CentOS确认云服务器安全组已放行 80/443 端口(阿里云/腾讯云/AWS等需在控制台配置)
确认 SELinux 未阻止(CentOS)
# 查看状态
getenforce
# 临时设置为宽松模式(调试用)
sudo setenforce 0
# 永久关闭(不推荐生产环境)
sudo nano /etc/selinux/config
# 将 SELINUX=enforcing 改为 SELINUX=disabled
❌ 配置语法错误
# 测试配置文件语法
sudo apache2ctl configtest # Ubuntu
sudo httpd -t # CentOS
# 查看错误日志
sudo tail -50 /var/log/apache2/error.log # Ubuntu
sudo tail -50 /var/log/httpd/error_log # CentOS
❌ .htaccess 不生效
确认目录配置中 AllowOverride 已开启:
<Directory /var/www/html>
AllowOverride All
</Directory>
并确保 rewrite 模块已启用:
sudo a2enmod rewrite
sudo systemctl restart apache2
📌 常用目录速查表
/etc/apache2/apache2.conf | /etc/httpd/conf/httpd.conf | |
/etc/apache2/sites-available/ | /etc/httpd/conf.d/ | |
/etc/apache2/mods-available/ | /etc/httpd/conf.modules.d/ | |
/var/www/html | /var/www/html | |
/var/log/apache2/error.log | /var/log/httpd/error_log | |
/var/log/apache2/access.log | /var/log/httpd/access_log |
📝 提示: 生产环境建议配置 HTTPS、限制目录列表(
Options -Indexes)、隐藏版本号(ServerTokens Prod),并定期更新安全补丁。
夜雨聆风