介绍
在本实验中,你将学习如何在 Ubuntu 22.04 系统上安装、配置和管理 Apache HTTP Server。实验涵盖了关键步骤,包括安装 Apache 软件包、启动、停止和重启服务器,以及配置虚拟主机。提供的指令实用且易于遵循,使本实验成为任何对使用 Linux 命令行界面进行 Web 服务器管理感兴趣的人的宝贵资源。
在 Ubuntu 22.04 上安装 Apache HTTP Server
在这一步中,我们将在 Ubuntu 22.04 Docker 容器中安装 Apache HTTP Server。
首先,更新软件包索引:
sudo apt-get update
示例输出:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done
接下来,安装 Apache HTTP Server 软件包:
sudo apt update
sudo apt-get install -y apache2
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libexpat1 libxml2 procps
Suggested packages:
www-browser
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libexpat1 libxml2 procps
0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,679 kB of archives.
After this operation, 7,542 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libapr1 amd64 1.7.0-6ubuntu0.22.04.1 [94.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 libaprutil1 amd64 1.6.1-4ubuntu2 [87.1 kB]
...
Selecting previously unselected package apache2.
(Reading database ... 14342 files and directories currently installed.)
Preparing to unpack .../apache2_2.4.52-1ubuntu4.1_amd64.deb ...
Unpacking apache2 (2.4.52-1ubuntu4.1) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../apache2-bin_2.4.52-1ubuntu4.1_amd64.deb ...
Unpacking apache2-bin (2.4.52-1ubuntu4.1) ...
...
Setting up apache2 (2.4.52-1ubuntu4.1) ...
Creating config file /etc/apache2/apache2.conf with new version
Creating config file /etc/apache2/ports.conf with new version
...
Processing triggers for systemd (249.11-0ubuntu3.6) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for ufw (0.36.1-2ubuntu1) ...
安装完成后,我们可以验证 Apache HTTP Server 是否正在运行:
sudo systemctl status apache2
示例输出:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-28 06:29:07 UTC; 10s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1196 (apache2)
Tasks: 55 (limit: 4686)
Memory: 7.1M
CPU: 86ms
CGroup: /system.slice/apache2.service
├─1196 /usr/sbin/apache2 -k start
├─1197 /usr/sbin/apache2 -k start
└─1198 /usr/sbin/apache2 -k start
输出显示 Apache HTTP Server 正在运行且处于活动状态。
启动、停止和重启 Apache HTTP Server
在这一步中,我们将学习如何启动、停止和重启 Apache HTTP Server。
首先,启动 Apache HTTP Server:
sudo systemctl start apache2
示例输出:
为了验证 Apache HTTP Server 是否正在运行,我们可以使用 systemctl status 命令:
sudo systemctl status apache2
示例输出:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-28 06:29:07 UTC; 10s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1196 (apache2)
Tasks: 55 (limit: 4686)
Memory: 7.1M
CPU: 86ms
CGroup: /system.slice/apache2.service
├─1196 /usr/sbin/apache2 -k start
├─1197 /usr/sbin/apache2 -k start
└─1198 /usr/sbin/apache2 -k start
接下来,停止 Apache HTTP Server:
sudo systemctl stop apache2
示例输出:
为了验证 Apache HTTP Server 是否已停止,我们可以再次使用 systemctl status 命令:
sudo systemctl status apache2
示例输出:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2023-04-28 06:29:17 UTC; 5s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Process: 1321 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
Main PID: 1196 (code=exited, status=0/SUCCESS)
最后,重启 Apache HTTP Server:
sudo systemctl restart apache2
示例输出:
验证 Apache HTTP Server 是否再次运行:
sudo systemctl status apache2
示例输出:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-28 06:29:22 UTC; 5s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Process: 1321 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
Process: 1358 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1364 (apache2)
Tasks: 55 (limit: 4686)
Memory: 7.1M
CPU: 86ms
CGroup: /system.slice/apache2.service
├─1364 /usr/sbin/apache2 -k start
├─1365 /usr/sbin/apache2 -k start
└─1366 /usr/sbin/apache2 -k start
输出显示 Apache HTTP Server 在重启后再次运行。
配置 Apache 虚拟主机
在这一步中,我们将配置 Apache 虚拟主机,以便在同一服务器上托管多个网站。
首先,为我们的虚拟主机创建两个目录:
sudo mkdir -p /var/www/example.com /var/www/example.org
接下来,为每个虚拟主机创建一个默认的 index.html 文件:
echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/index.html
echo "<h1>Welcome to example.org</h1>" | sudo tee /var/www/example.org/index.html
现在,让我们配置 Apache 虚拟主机。打开默认的 Apache 配置文件:
sudo nano /etc/apache2/sites-available/000-default.conf
将文件内容替换为以下内容:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
</VirtualHost>
<VirtualHost *:80>
ServerName example.org
DocumentRoot /var/www/example.org
</VirtualHost>
保存并关闭文件。
启用新的虚拟主机配置:
sudo a2ensite 000-default.conf
最后,重启 Apache HTTP Server 以应用更改:
sudo systemctl restart apache2
现在,你可以在浏览器中访问 http://example.com 和 http://example.org 来查看各自的欢迎页面。
总结
在本实验中,我们学习了如何在 Ubuntu 22.04 上安装 Apache HTTP Server,启动、停止和重启服务器,以及配置 Apache 虚拟主机。我们首先更新了软件包索引,然后安装了 Apache HTTP Server 软件包。接着,我们探索了管理 Apache 服务的命令,例如启动、停止和重启它。最后,我们配置了 Apache 虚拟主机,以便在同一服务器上托管多个网站。



