Bước 1: Tạo file .htpasswd
-----------------------------------
sudo htpasswd -c /nơi chứ file/.htpasswd tên
* sudo htpasswd /nơi chứ file/.htpasswd tên
sudo chmod 644 /nơi chứ file/.htpasswd
-----------------------------------
Bước 2: tạo file .htaccess nhập vào
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /nơi chứ file/.htpasswd
Require valid-user
sudo chmod 644 /nơi chứ file/.htaccess
sau đó bỏ vào thư mục html cần bảo vệ
-----------------------------------
Bước 3: Cấu hình Apache để hỗ trợ .htaccess
sudo /etc/apache2/apache2.conf
Tìm phần cấu hình cho thư mục nơi bạn muốn bảo vệ
<Directory "/var/www/html">
AllowOverride None
...
</Directory>
sửa thành
<Directory "/var/www/html">
AllowOverride All
...
</Directory>
-----------------------------------
sudo systemctl restart apache2
2024-10-09