这是一份为您准备的 Linux 下 Apache Web 服务器完整教程,本教程涵盖了从安装、基础配置、虚拟主机设置到安全优化的全过程。
前置假设:您使用的是常见的 Linux 发行版(如 Ubuntu/Debian 或 CentOS/RHEL)。
权限要求:大部分操作需要root权限或使用sudo。
第一步:安装 Apache
Ubuntu / Debian 系统
打开终端,执行以下命令:
# 更新软件包列表 sudo apt update # 安装 Apache2 sudo apt install apache2 # 启动 Apache 并设置开机自启 sudo systemctl start apache2 sudo systemctl enable apache2
CentOS / RHEL / Fedora 系统
# 安装 httpd (CentOS 7+ 使用 httpd) sudo yum install httpd # 或 sudo dnf install httpd # 启动 Apache 并设置开机自启 sudo systemctl start httpd sudo systemctl enable httpd
第二步:验证安装
安装完成后,您可以通过浏览器访问服务器的 IP 地址来验证:
http://你的服务器IP地址
如果看到 “It works!” 或 “Apache Test Page”,说明安装成功。
注意:如果无法访问,请检查防火墙设置(见第四步)。
第三步:基本目录结构
了解 Apache 的文件位置至关重要:
| 系统类型 | 网站根目录 | 主配置文件 | 模块配置目录 | 日志文件 |
|---|---|---|---|---|
| Ubuntu/Debian | /var/www/html |
/etc/apache2/apache2.conf |
/etc/apache2/sites-available/ |
/var/log/apache2/ |
|
CentOS/RHEL | /var/www/html | /etc/httpd/conf/httpd.conf | /etc/httpd/conf.d/ | /var/log/httpd/ |
第四步:防火墙配置
Apache 使用 80 (HTTP) 和 443 (HTTPS) 端口。
Ubuntu (使用 ufw)
sudo ufw allow 'Apache Full' sudo ufw reload
CentOS (使用 firewalld)
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
第五步:配置虚拟主机 (Virtual Hosts)
这是最实用的功能,允许在一台服务器上托管多个网站。
以 Ubuntu 为例:
-
创建网站目录:
sudo mkdir -p /var/www/example.com/html sudo chown -R $USER:$USER /var/www/example.com/html sudo chmod -R 755 /var/www/example.com
-
创建测试页面:
sudo nano /var/www/example.com/html/index.html
<html> <head><title>Welcome to Example.com!</title></head> <body><h1>Success! The example.com virtual host is working!</h1></body> </html>
-
创建虚拟主机配置文件:
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost :80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/html <Directory /var/www/example.com/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined </VirtualHost> -
启用站点并禁用默认站点:
sudo a2ensite example.com.conf sudo a2dissite 000-default.conf
-
测试配置并重载 Apache:
sudo apache2ctl configtest # 如果输出 Syntax OK,则执行: sudo systemctl reload apache2
CentOS 用户:在
/etc/httpd/conf.d/下创建.conf文件,内容类似,无需a2ensite命令,直接重启httpd即可。
第六步:常用管理命令
# 启动 Apache sudo systemctl start apache2 # Ubuntu sudo systemctl start httpd # CentOS # 停止 Apache sudo systemctl stop apache2 # 重启 Apache (应用配置更改) sudo systemctl restart apache2 # 重载 Apache (不中断现有连接) sudo systemctl reload apache2 # 检查 Apache 状态 sudo systemctl status apache2 # 测试配置文件语法 sudo apache2ctl configtest # Ubuntu sudo httpd -t # CentOS
第七步:安装 PHP 支持 (LAMP 栈)
Apache 本身只处理静态 HTML,要运行 PHP 应用(如 WordPress),需安装 PHP 模块。
Ubuntu:
sudo apt install php libapache2-mod-php php-mysql sudo systemctl restart apache2
CentOS:
sudo yum install php php-mysqlnd sudo systemctl restart httpd
测试 PHP:
在 /var/www/html/ 下创建 info.php:
<?php phpinfo(); ?>
访问 http://你的IP/info.php 查看 PHP 信息,测试后建议删除该文件以确保安全。
第八步:安全优化建议
-
隐藏版本信息:
编辑主配置文件,添加:ServerTokens Prod ServerSignature Off
-
禁止目录浏览:
确保Options中没有Indexes,或显式关闭:Options -Indexes
-
使用 HTTPS (SSL/TLS):
强烈建议安装 SSL 证书,推荐使用 Let’s Encrypt 免费证书:# 安装 certbot sudo apt install certbot python3-certbot-apache # Ubuntu sudo yum install certbot python3-certbot-apache # CentOS # 自动配置 SSL sudo certbot --apache -d example.com -d www.example.com
-
限制访问:
通过.htaccess或配置文件限制特定 IP 或路径的访问。
常见问题排查
| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| 403 Forbidden | 权限错误 | 检查文件所有者和权限 (chown, chmod) |
| 404 Not Found | 路径错误 | 检查 DocumentRoot 和文件是否存在 |
| 500 Internal Server Error | 配置错误/PHP 错误 | 查看错误日志 (/var/log/apache2/error.log) |
| 无法访问网站 | 防火墙/SELinux | 检查防火墙规则或 SELinux 状态 (setenforce 0 测试) |
下一步学习建议
- 学习 Nginx:对比 Apache 和 Nginx 的优缺点。
- 配置反向代理:使用 Apache 作为前端,后端连接 Node.js 或 Python 应用。
- 自动化部署:结合 Git 和 CI/CD 工具实现自动部署。
如果您有具体的配置需求(如配置 HTTPS、反向代理、负载均衡等),欢迎继续提问!
首发原创文章,作者:世雄 - 原生数据库架构专家,如若转载,请注明出处:https://idctop.com/article/484339.html



