Ubuntu 22.04 安装 Apache HTTP Server 指南(apt 在线与离线)

在 Ubuntu 22.04 服务器上,通过 apt 在线安装与 dpkg 离线安装两种方式部署 Apache HTTP Server(apache2 2.4.52),并完成虚拟主机、模块管理与服务自启配置

Apache HTTP ServerApacheweb服务器httpd安装部署Web 服务器与反向代理Web 服务器2026/09/29

Ubuntu 22.04 安装 Apache HTTP Server 指南(apt 在线与离线)

目标:在 Ubuntu 22.04 服务器上,通过 apt 在线安装与 dpkg 离线安装两种方式部署 Apache HTTP Server(apache2 2.4.52),并完成虚拟主机、模块启用、日志路径与服务自启配置;已在 Ubuntu 22.04.5(x86_64)实测通过。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(x86_64)
  • Apache HTTP Server:2.4.52(apt 安装,包名 apache2)
  • 工作目录:/opt/apache(示例)
  • 服务名:apache2
  • 默认站点:000-default.conf
  • 站点根目录:/var/www/html
  • 全局配置:/etc/apache2/apache2.conf

二、方式一:apt 在线安装(推荐)

2.1 更新软件源

apt update

回显(部分):

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
...
Reading package lists... Done

2.2 安装 apache2

apt install -y apache2

回显(部分):

Enabling conf serve-cgi-bin.
Enabling site 000-default.
Processing triggers for libc-bin (2.35-0ubuntu3.14) ...

2.3 验证版本

apache2 -v

回显:

Server version: Apache/2.4.52 (Ubuntu)
Server built:   2026-07-06T16:41:46

2.4 启动服务并设置开机自启

systemctl enable apache2
systemctl start apache2
systemctl status apache2

回显(部分):

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since ...

💡 说明:Debian/Ubuntu 系列默认监听所有地址的 80 端口;启动后可直接 curl http://127.0.0.1/ 验证。

2.5 验证访问

curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1/

回显:

HTTP 200

查看默认首页:

curl -s http://127.0.0.1/ | head -8

回显:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <!--
    Modified from the Debian original for Ubuntu
    Last updated: 2022-03-22

2.6 查看进程与监听

ps -ef | grep apache2 | grep -v grep

回显:

root        3098       1  0 18:16 ?        00:00:00 /usr/sbin/apache2 -k start
www-data    3101    3098  0 18:16 ?        00:00:00 /usr/sbin/apache2 -k start
www-data    3102    3098  0 18:16 ?        00:00:00 /usr/sbin/apache2 -k start
ss -ltnp | grep :80

回显:

LISTEN 0      511          0.0.0.0:80        0.0.0.0:*    users:(("apache2",pid=3098,fd=4))

三、方式二:dpkg 离线安装

适用于内网环境。在联网机器上先下载 apache2 及其依赖包,再拷贝到目标服务器安装。

3.1 联网机器下载依赖包

apt-get install -y --download-only apache2

下载的 .deb 缓存在 /var/cache/apt/archives/,核心包如下:

  • apache2_2.4.52-1ubuntu4.23_amd64.deb
  • apache2-bin_2.4.52-1ubuntu4.23_amd64.deb
  • apache2-data_2.4.52-1ubuntu4.23_amd64.deb
  • apache2-utils_2.4.52-1ubuntu4.23_amd64.deb
  • libapr1、libaprutil1 等依赖

将所有 .deb 拷入目标服务器一个目录(如 /opt/debs):

scp /var/cache/apt/archives/*.deb user@server:/opt/debs/

3.2 目标服务器离线安装

cd /opt/debs
dpkg -i *.deb

若出现依赖报错,执行补装:

apt-get -f install -y

回显(部分):

Setting up apache2 (2.4.52-1ubuntu4.23) ...
Processing triggers for libc-bin (2.35-0ubuntu3.14) ...

3.3 启动并验证

systemctl enable --now apache2
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1/

回显:

HTTP 200

四、关键文件路径

内容路径
主配置文件/etc/apache2/apache2.conf
端口配置/etc/apache2/ports.conf
站点可用/etc/apache2/sites-available/
站点启用/etc/apache2/sites-enabled/
模块可用/etc/apache2/mods-available/
模块启用/etc/apache2/mods-enabled/
全局片段/etc/apache2/conf-available/(symlink 到 conf-enabled)
站点根目录/var/www/html/
错误日志/var/log/apache2/error.log
访问日志/var/log/apache2/access.log
PID 文件/var/run/apache2/apache2.pid

五、常用模块启用示例

启用 rewrite 模块并重启:

a2enmod rewrite
systemctl restart apache2

回显(模块部分):

To activate the new configuration, you need to run:
  systemctl restart apache2

验证模块加载:

apache2ctl -M | grep rewrite

回显:

 rewrite_module (shared)

六、自定义虚拟主机示例

创建站点配置:

cat > /etc/apache2/sites-available/myapp.conf <<'EOF'
<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/myapp
    ErrorLog ${APACHE_LOG_DIR}/myapp_error.log
    CustomLog ${APACHE_LOG_DIR}/myapp_access.log combined
</VirtualHost>
EOF

启用站点并重载:

a2ensite myapp
systemctl reload apache2
apachectl -S

回显(部分):

VirtualHost configuration:
*:80                   192.168.100.33 (/etc/apache2/sites-enabled/000-default.conf:1)
*:80                   www.example.com (/etc/apache2/sites-enabled/myapp.conf:1)
ServerRoot: "/etc/apache2"

七、常见问题

7.1 ServerName 警告

启动日志出现:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name

修复:在 apache2.conf 末尾加入:

echo "ServerName www.example.com" >> /etc/apache2/apache2.conf
apache2ctl configtest

回显:

Syntax OK

7.2 端口被占用

(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

说明 80 端口已被其他服务(如 nginx)占用,需停掉占用进程或修改 ports.conf 中监听端口。

八、卸载

apt purge -y apache2 apache2-bin apache2-data apache2-utils
apt autoremove -y

九、参考链接