Thiết lập Password Authentication Apache trên Ubuntu 14.04

Cài Đặt Apache Utilites

  • sudo apt-get update
  • sudo apt-get install apache2 apache2-utils

Tạo Password File

  • sudo htpasswd -c /etc/apache2/.htpasswd <username>

Selection_009– Kiểm tra file .htpasswd

  • sudo vi /etc/apache2/.htpasswd

Selection_010 Selection_011

Cấu hình Apache Password Authentication

1. Dùng Virtual Host

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/truongnguyen/www/lara5111
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /home/truongnguyen/www/lara5111>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride All
         Order allow,deny
         allow from all
         AuthType Basic
         AuthName "Login"
         AuthUserFile /etc/apache2/.htpasswd
         Require valid-user
    </Directory>
</VirtualHost>

- Restart apche
- sudo service apache2 restart

2. Dùng .htaccess

Thêm 4 dòng code vào đầu file .htaccess

AuthUserFile /etc/apache2/.htpasswd
AuthType Basic
AuthName "Login"
Require valid-user
Done ^_^