hoatranlab.io.vn Zalo: 0917516878 Hotline: 0917516878 [email protected]
HoaTranLab Logo HoaTranLab
Linux Bible 11th Edition • Chapter 17

Configuring a Web Server

Tìm hiểu cách cài đặt và cấu hình Apache HTTP Server (httpd) trên Linux, quản lý virtual hosts, bảo mật SSL/TLS với mod_ssl, và tích hợp với SELinux và firewall.

Mục tiêu chương

Nắm vững kiến thức về Apache Web Server trên Linux

Cài đặt Apache httpd

Cài đặt httpd-core và các gói liên quan, khởi động service với systemctl enable/start, kiểm tra trạng thái và xem file cấu hình chính.

httpd.conf Configuration

Hiểu các directive quan trọng: ServerName, DocumentRoot, DirectoryIndex, Listen, User/Group, ErrorLog, và các Directory blocks.

Virtual Hosts

Cấu hình name-based virtual hosting để phục vụ nhiều domain từ một server, tạo VirtualHost blocks trong /etc/httpd/conf.d/.

SSL/TLS với mod_ssl

Cài mod_ssl, tạo self-signed certificate với openssl, cấu hình HTTPS trên port 443 để mã hóa dữ liệu truyền tải.

SELinux cho Apache

Quản lý file contexts (httpd_sys_content_t), SELinux Booleans cho Apache, và xử lý các vấn đề SELinux ngăn chặn truy cập.

Firewall cho Apache

Mở port 80 (HTTP) và port 443 (HTTPS) trong firewalld, kiểm tra với ss command, và cho phép truy cập từ mạng ngoài.

Lý thuyết

Cài đặt và khởi động Apache

Apache HTTP Server được phân phối qua gói httpd-core trong Fedora/RHEL. Để học, nên cài thêm httpd-manual để xem tài liệu tại http://localhost/manual.

Terminal — Cài đặt Apache

# Fedora/RHEL — cài nhóm Web Server

$ sudo dnf groupinstall "Web Server"

# Hoặc cài riêng lẻ

$ sudo dnf install httpd httpd-manual mod_ssl

# Enable và start Apache

$ sudo systemctl enable httpd.service

$ sudo systemctl start httpd.service

$ sudo systemctl status httpd.service

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)

Active: active (running) since 2025-07-28 18:58:36 EDT; 3s ago

Main PID: 6058 (/usr/sbin/httpd)

Status: "Total requests: 10; Idle/Busy workers 100/0"

File cấu hình httpd.conf

File cấu hình chính là /etc/httpd/conf/httpd.conf. Các file .conf trong /etc/httpd/conf.d/ cũng được nạp tự động.

/etc/httpd/conf/httpd.conf

# Server identity

ServerRoot "/etc/httpd"

Listen 80

ServerName www.example.com:80

ServerAdmin [email protected]

# Process identity

User apache

Group apache

# Content locations

DocumentRoot "/var/www/html"

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

# Default index files

DirectoryIndex index.html index.php

# Logging

ErrorLog logs/error_log

CustomLog "logs/access_log" combined

# Include additional configs

Include conf.d/*.conf

# Directory security

<Directory "/var/www/html">

Options Indexes FollowSymLinks

AllowOverride None

Require all granted

</Directory>

Cấu hình Virtual Hosts

Virtual hosts cho phép phục vụ nhiều website từ một Apache server. Tạo file .conf riêng cho mỗi virtual host trong /etc/httpd/conf.d/.

/etc/httpd/conf.d/example.org.conf

<VirtualHost *:80>

ServerAdmin [email protected]

ServerName www.example.org

ServerAlias web.example.org

DocumentRoot /var/www/html/example.org/

DirectoryIndex index.php index.html index.htm

ErrorLog logs/example.org_error_log

CustomLog logs/example.org_access_log combined

</VirtualHost>

# Tạo thư mục content

$ sudo mkdir -p /var/www/html/example.org

$ echo "<h1>Welcome to example.org</h1>" | sudo tee /var/www/html/example.org/index.html

# Test cú pháp và restart

$ sudo apachectl configtest

Syntax OK

$ sudo apachectl graceful

SSL/TLS với mod_ssl

Để bảo mật kết nối, cài mod_ssl và tạo certificate. File cấu hình SSL là /etc/httpd/conf.d/ssl.conf.

Terminal — SSL/TLS Setup

# Cài mod_ssl

$ sudo dnf install mod_ssl openssl

# Tạo self-signed certificate (365 ngày)

$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \

-keyout /etc/pki/tls/private/apache.key \

-out /etc/pki/tls/certs/apache.crt

Country Name (2 letter code) [XX]: VN

Common Name (e.g. server FQDN): www.example.com

# Cấu hình VirtualHost SSL trong ssl.conf

<VirtualHost _default_:443>

ServerName www.example.com

DocumentRoot "/var/www/html"

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/apache.crt

SSLCertificateKeyFile /etc/pki/tls/private/apache.key

</VirtualHost>

# Kiểm tra ports

$ sudo ss -tupln | grep httpd

tcp6 LISTEN :::80 :::* users:(("httpd",pid=6058))

tcp6 LISTEN :::443 :::* users:(("httpd",pid=6058))

Firewall và SELinux cho Apache

Cần mở port 80 và 443 trong firewall, và đảm bảo SELinux file contexts đúng cho nội dung web. Files trong /var/www/html tự động có context httpd_sys_content_t.

Terminal — Firewall & SELinux

# Mở firewall cho HTTP và HTTPS

$ sudo firewall-cmd --permanent --add-service=http

success

$ sudo firewall-cmd --permanent --add-service=https

success

$ sudo firewall-cmd --reload

success

# Kiểm tra SELinux context

$ ls -Z /var/www/html/

system_u:object_r:httpd_sys_content_t:s0 index.html

# Nếu đặt nội dung ở vị trí khác, cần set context

$ sudo semanage fcontext -a -t httpd_sys_content_t "/mysite(/.*)?"

$ sudo restorecon -Rv /mysite

# SELinux Booleans hữu ích

$ sudo setsebool -P httpd_can_network_connect on

$ sudo setsebool -P httpd_enable_homedirs on

# Xem tất cả booleans liên quan Apache

$ getsebool -a | grep httpd

Lab Thực Hành

Kịch bản: Thiết lập Apache Web Server với Virtual Host và HTTPS trên Fedora Linux

1

Cài đặt Apache và mod_ssl

Cài đặt các gói cần thiết và khởi động Apache.

Terminal

$ sudo dnf install -y httpd httpd-manual mod_ssl openssl

Installed: httpd-2.4.64-2.fc42.x86_64

$ sudo systemctl enable --now httpd

Created symlink ...multi-user.target.wants/httpd.service

$ curl -s http://localhost | head -5

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">

2

Tạo trang web cơ bản

Tạo nội dung trong DocumentRoot và kiểm tra hiển thị.

Terminal

$ sudo bash -c 'cat > /var/www/html/index.html <<EOF

<html><body><h1>My Apache Server</h1></body></html>

EOF'

$ ls -Z /var/www/html/index.html

system_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html

$ curl http://localhost

<html><body><h1>My Apache Server</h1></body></html>

3

Mở firewall

Cho phép HTTP và HTTPS qua firewalld.

Terminal

$ sudo firewall-cmd --permanent --add-service={http,https}

success

$ sudo firewall-cmd --reload && sudo firewall-cmd --list-services

cockpit dhcpv6-client http https ssh

4

Tạo Virtual Host cho mysite.local

Cấu hình name-based virtual host để phục vụ trang web riêng.

/etc/httpd/conf.d/mysite.conf

# Tạo thư mục nội dung

$ sudo mkdir -p /var/www/html/mysite

$ echo "<h1>Welcome to mysite.local</h1>" | sudo tee /var/www/html/mysite/index.html

# Tạo file virtual host

$ sudo nano /etc/httpd/conf.d/mysite.conf

<VirtualHost *:80>

ServerName mysite.local

DocumentRoot /var/www/html/mysite

DirectoryIndex index.html

</VirtualHost>

$ sudo apachectl configtest && sudo systemctl reload httpd

Syntax OK

5

Cấu hình HTTPS với self-signed cert

Tạo certificate và cấu hình SSL virtual host.

Terminal

$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \

-keyout /etc/pki/tls/private/mysite.key \

-out /etc/pki/tls/certs/mysite.crt \

-subj "/CN=mysite.local/O=Test/C=VN"

Generating a RSA private key ... writing new private key

$ curl -k https://localhost

<h1>My Apache Server</h1>

6

Kiểm tra log và debug

Xem log Apache để phát hiện lỗi và xử lý sự cố.

Terminal

$ sudo tail -f /var/log/httpd/error_log

[Mon Jan 15 10:05:00 2025] [notice] Apache/2.4.64 configured

$ sudo tail -f /var/log/httpd/access_log

192.168.1.50 - - [15/Jan/2025:10:05:10 +0000] "GET / HTTP/1.1" 200 45

$ sudo journalctl -u httpd -n 20

# SELinux deny check

$ sudo ausearch -m avc -ts recent | grep httpd

Câu hỏi ôn tập

1

DocumentRoot mặc định của Apache trên RHEL/Fedora là gì? User nào chạy tiến trình httpd?

DocumentRoot mặc định là /var/www/html. Tiến trình httpd chạy với user và group apache (được định nghĩa bởi directive User và Group trong httpd.conf). Root chỉ khởi động daemon ban đầu, sau đó worker processes drop xuống quyền apache.

2

Lệnh nào kiểm tra cú pháp httpd.conf trước khi restart Apache?

sudo apachectl configtest — kiểm tra cú pháp, output "Syntax OK" nếu hợp lệ. Dùng apachectl graceful để reload mà không ngắt kết nối hiện có.

3

Virtual Host là gì và tạo ở đâu trên Fedora/RHEL?

Virtual Host cho phép phục vụ nhiều website từ một Apache server, phân biệt qua domain name (name-based). Trên Fedora/RHEL, tạo file .conf trong /etc/httpd/conf.d/ với VirtualHost block chứa ServerName, DocumentRoot, v.v.

4

mod_ssl cần thiết cho điều gì? File cấu hình SSL nằm ở đâu?

mod_ssl cung cấp SSL/TLS encryption cho HTTPS (port 443), mã hóa dữ liệu giữa client và server. File cấu hình SSL là /etc/httpd/conf.d/ssl.conf. Cần cài thêm gói mod_ssl và có certificate file.

5

Tại sao Apache không phục vụ file khi đặt content ở /mystuff thay vì /var/www/html?

Vì SELinux. Files ở /var/www/html tự động có context httpd_sys_content_t, nhưng /mystuff không. Cần chạy: semanage fcontext -a -t httpd_sys_content_t "/mystuff(/.*)?"restorecon -Rv /mystuff.

6

Directive DirectoryIndex dùng để làm gì? Ví dụ thực tế?

DirectoryIndex xác định file nào Apache phục vụ khi client request thư mục (không chỉ định file). Ví dụ: DirectoryIndex index.html index.php — Apache sẽ tìm index.html trước, nếu không có thì tìm index.php.

7

Log files của Apache nằm ở đâu? Làm sao xem real-time?

Log files tại /var/log/httpd/error_log (lỗi) và /var/log/httpd/access_log (truy cập). Xem real-time: sudo tail -f /var/log/httpd/error_log. Dùng journalctl -u httpd cho systemd journal.

8

Lệnh ss nào kiểm tra Apache đang lắng nghe trên port đúng?

sudo ss -tupln | grep httpd — hiển thị tcp ports 80 và 443 với process httpd. Output: tcp6 LISTEN :::80 :::* users:(("httpd",pid=6058))