Cài đặt máy chủ Apache Httpd trên CentOS/RHEL
sudo yum update httpd sudo yum install httpd
# Kích hoạt để Http tự động nạp sudo systemctl enable httpd # Chạy Httpd sudo systemctl start httpd # Kiểm tra sudo systemctl status httpd
Cấu hình VirtualHost trong Http Apache
Các file cấu hình của HTTPD APACHE có phần mở rộng .conf, vị trí file này có thể xác định
bằng lệnh
httpd -V
Server version: Apache/2.4.6 (CentOS)
Server built: Nov 10 2021 14:26:31
Server's Module Magic Number: 20120211:24
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
Ví dụ trên, thư mục gốc cấu hình là /etc/httpd/ file cấu hình đang là /etc/httpd/conf/httpd.conf.
Như vậy muốn tùy chỉnh cấu hình chỉ việc cập nhật file /etc/httpd/conf/httpd.conf, chú ý phải khởi động
lại apache sau khi thay đổi cấu hình
sudo systemctl restart httpd
Ngoài ra cũng lưu ý, thường trong file /etc/httpd/conf/httpd.conf gốc có chứa chỉ thị:
Include conf.modules.d/*.conf IncludeOptional conf.d/*.conf
Có nghĩa là khi file /etc/httpd/conf/httpd.conf được nạp, nó tự động nạp các file .conf
trong thư mục /etc/httpd/conf.modules.d và /etc/httpd/conf.d. Ví thế mà bạn
có thể viết cấu hình trực tiếp trong httpd.conf hoặc viết trong file có phần mở rộng .conf
đặt trong thư mục /etc/httpd/conf/. Ví dụ, bạn có thể tạo ra file /etc/httpd/conf/vhost.conf
để viết cấu hình các VirtualHost
VirtualHost (VHost)
Đó là cấu hình trong Apache để một máy chủ (một địa chỉ IP) chạy được nhiều website (nhiều domain). Với Apache thường tạo ra VHost dựa theo tên domain (Name-Based Virtual Host)
Mỗi Vhost được viết trong một khối thẻ <VirtualHost>, bạn có thể tạo ra nhiều khối <VirtualHost>
Ví dụ 1:
# cổng lắng nghe 80
<VirtualHost *:80>
# thư mục phụ vụ
DocumentRoot "/home/towebsite/public_html"
# thiết lập tên miền
ServerName domain.net
ServerAlias www.domain.net
# thiết lập file log
ErrorLog ${APACHE_LOG_DIR}domain.net-error.log
# CustomLog /dev/null combined -> không xuất log
CustomLog ${APACHE_LOG_DIR}domain.net-access.log common
</VirtualHost>
Thiết lập như trên, có thể truy cập các file tĩnh lưu trong thư mục /home/towebsite/public_html bằng tên miền domain.net
Ví dụ 2:
Trong VirtualHost có thể dùng chỉ thị <Directory> để cấu hình từng thư mục nhỏ, ví dụ cấu hình riêng cho thư
mục abc
# cổng lắng nghe 80
<VirtualHost *:80>
DocumentRoot "/home/towebsite/public_html"
ServerName domain.net
ServerAlias www.domain.net
ErrorLog ${APACHE_LOG_DIR}domain.net-error.log
CustomLog ${APACHE_LOG_DIR}domain.net-access.log common
<Directory /home/towebsite/public_html/abc>
# hủy nạp cấu hình trong .htaccess
AllowOverride None
# không cho duyệt thư mục
Options -Indexes -ExecCGI +FollowSymLinks -SymLinksIfOwnerMatch
# tối ưu senfile nếu cần
# EnableSendfile On
# Cấm truy cập thì thêm
# Require all denied
</Directory>
</VirtualHost>
Ví dụ 3:
Cấu hình để chạy ứng dụng PHP (PHP có dịch vụ PHP-FPM lắng nghe ở cổng 9000)
Đầu tiên mở file httpd.conf thêm vào:
# Thiết lập handler AddHandler "proxy:fcgi://127.0.0.1:9000" .php # Thiết lập nạp cấu hình tại conf/vhosts.confg Include conf/vhosts.conf # Tắt thông tin nhạy cảm ServerSignature Off ServerTokens Prod
Sau đó tạo khối VirtualHost như sau
<VirtualHost *:80>
DocumentRoot "/home/towebsite/public_html"
ServerName domain.net
ServerAlias www.domain.net
ErrorLog ${APACHE_LOG_DIR}domain.net-error.log
CustomLog ${APACHE_LOG_DIR}domain.net-access.log common
# Thiết lập mọi truy vấn đều chạy thông qua file index.php
<Directory /home/towebsite/public_html>
Options -Indexes -ExecCGI +FollowSymLinks -SymLinksIfOwnerMatch
DirectoryIndex index.php
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]
EnableSendfile On
AllowOverride None
</Directory>
<Directory /home/towebsite/public_html/abc>
# Các chỉ thị ...
</Directory>
</VirtualHost>
Lúc này, mọi truy vấn đến website đều thông qua index.php trừ các file tĩnh cấu hình riêng trong chỉ thị Directory khác
Chú ý Apache phải nạp rewrite_module, kiểm tra với lệnh
httpd -M
Nếu chưa bật trong cấu hình (httpd.cong hoặc trong thư mục conf.modules.d) phải nạp vào
LoadModule rewrite_module modules/mod_rewrite.so
Ví dụ 4:
VirtualHost với chứng chỉ SSL (lắng nghe ở cổng 443)
<VirtualHost *:443>
DocumentRoot "/home/towebsite/public_html"
ServerName domain.net
ServerAlias www.domain.net
ErrorLog ${APACHE_LOG_DIR}domain.net-error.log
CustomLog ${APACHE_LOG_DIR}domain.net-access.log common
# Kích hoạt SSL
SSLEngine on
# SSLProtocol all -SSLv2
# SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:!RC4+RSA:+HIGH:+MEDIUM:!LOW:!RC4
# thiết lập file chứng chỉ SSL (SSLCertificateFile và SSLCertificateKeyFile)
# Có thế lấy từ dịch vụ Let’s Encrypt
SSLCertificateFile /certtest/ca.crt
SSLCertificateKeyFile /certtest/ca.key
# Các chỉ thị khác
</VirtualHost>
Ví dụ 5:
Chuyển hướng cổng 80 về 443
<VirtualHost *:80>
ServerName domain.net
ServerAlias *.domain.net
RedirectMatch permanent ^/(.*)$ https://domain.net/$1
</VirtualHost>
Ví dụ 5:
Thiết lập Apache như là một Proxy, chuyển giao thức Http đến một cổng khác đang có địch vụ http chạy. Ví dụ như ứng dụng ASP.NET đang chạy và lắng nghe ở cổng 5000
<VirtualHost *:*>
RequestHeader set "X-Forwarded-Proto" expr="%{REQUEST_SCHEME}e"
</VirtualHost>
<VirtualHost *:80>
ServerName domain.net
ServerAlias www.domain.net
ErrorLog ${APACHE_LOG_DIR}domain.net-error.log
CustomLog ${APACHE_LOG_DIR}domain.net-access.log common
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>
Một số cấu hình khác
Chuyển hướng 301, viết trong VirtualHost
Redirect 301 /url-cu /url-mới
Nén nội dung trả về sử dụng mod_deflate
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text/ text/html text/plain text/xmk text/css application/json application/x-javascript application/javascript text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</IfModule>
Thiết lập thời gian cache trình duyệt
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 600 seconds"
ExpiresByType image/x-icon "access plus 172800 seconds"
ExpiresByType image/jpeg "access plus 172800 seconds"
ExpiresByType image/png "access plus 172800 seconds"
ExpiresByType image/gif "access plus 172800 seconds"
ExpiresByType application/x-shockwave-flash "access plus 172800 seconds"
ExpiresByType text/css "access plus 172800 seconds"
ExpiresByType text/javascript "access plus 172800 seconds"
ExpiresByType application/javascript "access plus 172800 seconds"
ExpiresByType application/x-javascript "access plus 172800 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
