凌峰创科服务平台

Apache Linux服务器搭建步骤详解?

目录

  1. 准备工作
    • 选择 Linux 发行版
    • 更新系统
    • 获取服务器权限
  2. 安装 Apache HTTP Server
    • 在 Ubuntu/Debian 上安装
    • 在 CentOS/RHEL 上安装
    • 验证安装
  3. 基本配置
    • 目录结构解析
    • 修改服务器名称
    • 配置虚拟主机(托管多个网站)
    • 启用/禁用站点
  4. 安全与优化
    • 配置防火墙
    • 启用 HTTPS (SSL/TLS)
    • 基本性能优化
  5. 常见问题与故障排查
    • 如何查看 Apache 错误日志?
    • 如何重启 Apache 服务?
    • 端口被占用怎么办?

准备工作

a. 选择 Linux 发行版

  • Ubuntu/Debian: 社区活跃,软件包丰富,非常适合初学者和开发者,我们将使用 apt 包管理器。
  • CentOS/RHEL/Rocky Linux/AlmaLinux: 企业级稳定性,默认使用较新的软件包,我们将使用 yumdnf 包管理器。

本指南会同时提供两种系统的命令。

Apache Linux服务器搭建步骤详解?-图1
(图片来源网络,侵删)

b. 更新系统

在安装任何新软件之前,保持系统软件包是最新的好习惯。

对于 Ubuntu/Debian:

sudo apt update
sudo apt upgrade -y

对于 CentOS/RHEL:

# 对于 CentOS 7/RHEL 7
sudo yum update -y
# 对于 CentOS 8/RHEL 8/Rocky Linux/AlmaLinux
sudo dnf update -y

c. 获取服务器权限

你需要一个拥有 sudo 权限的非 root 用户,或者直接使用 root 用户,为了安全,推荐使用 sudo 用户。

Apache Linux服务器搭建步骤详解?-图2
(图片来源网络,侵删)

安装 Apache HTTP Server

Apache 的官方名称是 "Apache HTTP Server"。

a. 在 Ubuntu/Debian 上安装

Apache 在 Ubuntu/Debian 中的包名是 apache2

sudo apt install apache2 -y

b. 在 CentOS/RHEL 上安装

在 CentOS/RHEL 中,包名是 httpd

# 对于 CentOS 7/RHEL 7
sudo yum install httpd -y
# 对于 CentOS 8/RHEL 8/Rocky Linux/AlmaLinux
sudo dnf install httpd -y

c. 验证安装

安装完成后,Apache 服务通常会自动启动,我们可以通过以下方式验证:

Apache Linux服务器搭建步骤详解?-图3
(图片来源网络,侵删)
  1. 检查服务状态 Ubuntu/Debian:

    sudo systemctl status apache2

    CentOS/RHEL:

    sudo systemctl status httpd

    如果看到绿色的 active (running) 字样,说明服务正在运行。

  2. 测试网页访问 Apache 默认会监听 80 端口,打开你的浏览器,输入你的服务器 IP 地址:

    http://你的服务器IP地址

    如果看到 "It works!" 或者 "Apache2 Ubuntu Default Page" 这样的页面,恭喜你,Apache 已经成功安装并运行了!

    如何找到你的服务器 IP 地址?

    # Ubuntu/Debian
    hostname -I
    # CentOS/RHEL
    ip addr show

基本配置

a. 目录结构解析

了解 Apache 的目录结构是配置的关键。

  • 网站根目录:

    • Ubuntu/Debian: /var/www/html/
    • CentOS/RHEL: /var/www/html/ 这是存放你网站文件(如 index.html)的地方。
  • 配置文件目录:

    • Ubuntu/Debian: /etc/apache2/
    • CentOS/RHEL: /etc/httpd/
  • 站点配置文件目录:

    • Ubuntu/Debian: /etc/apache2/sites-available/ (存放可用站点配置) 和 /etc/apache2/sites-enabled/ (存放已启用的站点配置,通常是软链接)
    • CentOS/RHEL: /etc/httpd/conf.d/ (所有以 .conf 结尾的文件都会被自动加载)
  • 日志文件目录:

    • Ubuntu/Debian: /var/log/apache2/
    • CentOS/RHEL: /var/log/httpd/

b. 修改服务器名称

为了方便管理,建议在配置文件中设置一个服务器名称(ServerName)。

Ubuntu/Debian:

# 编辑主配置文件
sudo nano /etc/apache2/apache2.conf

在文件末尾添加或修改:

ServerName your_domain_or_ip:80

然后重启 Apache:

sudo systemctl restart apache2

CentOS/RHEL:

# 编辑主配置文件
sudo nano /etc/httpd/conf/httpd.conf

找到 ServerName 这一行(默认可能被注释掉),取消注释并修改:

ServerName your_domain_or_ip:80

然后重启 Apache:

sudo systemctl restart httpd

c. 配置虚拟主机(托管多个网站)

虚拟主机允许你在一台服务器上托管多个独立的网站,我们以 example.com 为例,创建一个新的站点。

  1. 创建网站目录

    sudo mkdir -p /var/www/example.com
  2. 创建一个测试首页

    sudo nano /var/www/example.com/index.html
    <!DOCTYPE html>
    <html>
    <head>
        <title>Welcome to example.com!</title>
    </head>
    <body>
        <h1>Success! Your example.com server block is working!</h1>
    </body>
    </html>

    修改文件所有者,让 Web 服务器可以读取:

    sudo chown -R www-data:www-data /var/www/example.com # Ubuntu/Debian
    sudo chown -R apache:apache /var/www/example.com    # CentOS/RHEL
  3. 创建站点配置文件 Ubuntu/Debian:

    sudo nano /etc/apache2/sites-available/example.com.conf

    输入以下配置:

    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com
        <Directory /var/www/example.com>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

    CentOS/RHEL:

    sudo nano /etc/httpd/conf.d/example.com.conf

    输入与上面几乎相同的配置(注意日志路径变量不同):

    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com
        <Directory /var/www/example.com>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        ErrorLog /var/log/httpd/example.com_error.log
        CustomLog /var/log/httpd/example.com_access.log combined
    </VirtualHost>
  4. 启用站点 Ubuntu/Debian: 使用 a2ensite 工具创建符号链接。

    sudo a2ensite example.com.conf

    禁用站点则用 a2dissite example.com.conf

    CentOS/RHEL: 只需保存文件即可,Apache 会自动加载 conf.d 目录下的所有 .conf 文件。

  5. 测试并重启 Apache 在重启前,最好检查一下配置文件语法是否正确。 Ubuntu/Debian:

    sudo apache2ctl configtest

    CentOS/RHEL:

    sudo apachectl configtest

    如果输出 Syntax OK,则重启服务: Ubuntu/Debian:

    sudo systemctl restart apache2

    CentOS/RHEL:

    sudo systemctl restart httpd

访问 http://example.com (或你配置的域名) 就能看到你创建的测试页面了。


安全与优化

分享:
扫描分享到社交APP
上一篇
下一篇