Giới thiệu
Trong lab này, bạn sẽ học cách cài đặt, cấu hình và quản lý Apache HTTP Server trên hệ thống Ubuntu 22.04. Lab này bao gồm các bước cần thiết, bao gồm cài đặt gói Apache, khởi động, dừng và khởi động lại server, và cấu hình virtual host. Các hướng dẫn được cung cấp mang tính thực tế và có thể dễ dàng làm theo, làm cho lab này trở thành một nguồn tài nguyên giá trị cho bất kỳ ai quan tâm đến việc quản trị web server bằng giao diện dòng lệnh Linux.
Cài đặt Apache HTTP Server trên Ubuntu 22.04
Trong bước này, chúng ta sẽ cài đặt Apache HTTP Server trên container Ubuntu 22.04 Docker.
Đầu tiên, hãy cập nhật chỉ mục gói:
sudo apt-get update
Ví dụ đầu ra:
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
Tiếp theo, cài đặt gói Apache HTTP Server:
sudo apt update
sudo apt-get install -y apache2
Ví dụ đầu ra:
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) ...
Sau khi quá trình cài đặt hoàn tất, chúng ta có thể xác minh rằng Apache HTTP Server đang chạy:
sudo systemctl status apache2
Ví dụ đầu ra:
● 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
Đầu ra cho thấy Apache HTTP Server đang chạy và hoạt động.
Khởi động, Dừng và Khởi động lại Apache HTTP Server
Trong bước này, chúng ta sẽ học cách khởi động, dừng và khởi động lại Apache HTTP Server.
Đầu tiên, hãy khởi động Apache HTTP Server:
sudo systemctl start apache2
Ví dụ đầu ra:
Để xác minh Apache HTTP Server đang chạy, chúng ta có thể sử dụng lệnh systemctl status
:
sudo systemctl status apache2
Ví dụ đầu ra:
● 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
Tiếp theo, hãy dừng Apache HTTP Server:
sudo systemctl stop apache2
Ví dụ đầu ra:
Để xác minh Apache HTTP Server đã dừng, chúng ta có thể sử dụng lại lệnh systemctl status
:
sudo systemctl status apache2
Ví dụ đầu ra:
● 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)
Cuối cùng, hãy khởi động lại Apache HTTP Server:
sudo systemctl restart apache2
Ví dụ đầu ra:
Xác minh Apache HTTP Server đang chạy lại:
sudo systemctl status apache2
Ví dụ đầu ra:
● 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
Đầu ra cho thấy Apache HTTP Server đang chạy lại sau khi khởi động lại.
Cấu hình Apache Virtual Hosts
Trong bước này, chúng ta sẽ cấu hình Apache virtual host để lưu trữ nhiều website trên cùng một server.
Đầu tiên, hãy tạo hai thư mục cho virtual host của chúng ta:
sudo mkdir -p /var/www/example.com /var/www/example.org
Tiếp theo, tạo một file index.html mặc định cho mỗi virtual host:
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
Bây giờ, hãy cấu hình Apache virtual host. Mở file cấu hình Apache mặc định:
sudo nano /etc/apache2/sites-available/000-default.conf
Thay thế nội dung của file bằng nội dung sau:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
</VirtualHost>
<VirtualHost *:80>
ServerName example.org
DocumentRoot /var/www/example.org
</VirtualHost>
Lưu và đóng file.
Bật cấu hình virtual host mới:
sudo a2ensite 000-default.conf
Cuối cùng, khởi động lại Apache HTTP Server để áp dụng các thay đổi:
sudo systemctl restart apache2
Bây giờ, bạn có thể truy cập http://example.com
và http://example.org
trong trình duyệt web của mình để xem các trang chào mừng tương ứng.
Tổng kết
Trong lab này, chúng ta đã học cách cài đặt Apache HTTP Server trên Ubuntu 22.04, khởi động, dừng và khởi động lại server, và cấu hình Apache virtual host. Đầu tiên, chúng ta đã cập nhật chỉ mục gói và sau đó cài đặt gói Apache HTTP Server. Sau đó, chúng ta đã khám phá các lệnh để quản lý dịch vụ Apache, chẳng hạn như khởi động, dừng và khởi động lại nó. Cuối cùng, chúng ta đã cấu hình Apache virtual host để lưu trữ nhiều website trên cùng một server.