Proteksi apache dengan htpasswd dan .htaccess

Apache webserver mempunyai suatu file konfigurasi .htaccess yang bisa memproteksi direktori-direktori dalam webserver dari user yang melakukan browsing terhadap direktori tersebut. User akan diminta username dan password ketika mereka berusaha mengakses direktori tersebut. Proteksi ini berlaku secara recrusive terhadap subdirektori-subdirektori yang ada di dalam direktori yang terdapat file .htaccess.

Diasumsikan bahwa Apache Webserver terinstal pada /www. Sehingga file konfigurasi, file binary dan ServerRoot-nya terdapat pada direktori-direktori /www/conf, /www/bin
dan /www/htdocs (default).

Untuk mengaktifkan .htaccess kita harus melakukan pengeditan terhadap file konfigurasi apache (httpd.conf) yang terdapat pada /www/conf/http.conf.

[corey@slipknot corey]$ su
Password:
[root@slipknot corey]# vi /www/conf/httpd.conf

Carilah entry di bawah ini :



# This controls which options the .htaccess files in directories can
# override. Can also be “All”, or any combination of “Options”, “FileInfo”,
# “AuthConfig”, and “Limit”
#
AllowOverride None



Editlah entry diatas menjadi :

AllowOveride Authconfig

Sekarang tentukan direktori mana yang akan kita proteksi, misalkan direktori tersebut terdapat di /www/htdocs/restricted, buat suatu file dengan nama .htaccess dalam direktori tersebut.

[root@slipknot corey]# vi /www/htdocs/restricted/.htaccess

Masukkan entry dibawah ini :

AuthName “Username and Password”
AuthUserFile /www/htdocs/restricted/.htpasswd
AuthType Basic
AuthGroupFile /dev/null
require user corey ratm

Keterangan :

– AuthName “Username and Password”. String “Username and Password” adalah pesan yang akan muncul
ketika user mencoba untuk masuk kedalam direktori restricted.
– AuthUserFile /www/htdocs/restricted/.htpasswd. Path yang merujuk pada file .htpasswd. Dalam kasus ini berarti file .htpasswd terdapat pada direktori /www/htdocs/restricted.
– AuthType Basic. Tipe autentifikasi yang digunakan adalah autentifikasi basic.
– AuthGroupFile /dev/null. Disini kita menggunakan single login, jadi kita tidak menentukkan
group mana yang bisa login, kita hanya memakai nama user saja.
– require user corey ratm. Username yang berhak adalah corey dan ratm.

Setelah file .htaccess tersebut dibuat, sekarang kita buat file .htpasswd dengan program htpasswd yang terdapat pada /www/bin.

[root@slipknot corey]# /www/bin/htpasswd -c /www/htdocs/restricted/.htpasswd corey
New password:
Re-type new password:
Adding password for user corey
[root@slipknot corey]# /www/bin/htpasswd /www/htdocs/restricted/.htpasswd ratm
New password:
Re-type new password:
Adding password for user ratm

Untuk mendapatkan keterangan tentang cara membuat file .htpasswd, masukkan option -h

[root@slipknot corey]# /www/bin/htpasswd -h
Usage:
htpasswd [-cmdps] passwordfile username
htpasswd -b[cmdps] passwordfile username password

htpasswd -n[mdps] username
htpasswd -nb[mdps] username password
-c Create a new file.
-n Don’t update file; display results on stdout.
-m Force MD5 encryption of the password.
-d Force CRYPT encryption of the password (default).
-p Do not encrypt the password (plaintext).
-s Force SHA encryption of the password.
-b Use the password from the command line rather than prompting for it.
On Windows, TPF and NetWare systems the ‘-m’ flag is used by default.
On all other systems, the ‘-p’ flag will probably not work.

Restart Apache :

[root@slipknot corey]# /www/bin/apachectl restart
/www/bin/apachectl restart: httpd restarted

Tes konfigurasi kita tadi.

[corey@slipknot corey]$ lynx http://localhost/restricted
Alert!: Access without authorization denied — retrying
Username for ‘Username and Password’ at server ‘localhost’:corey
Password: ****

…..

Commands: Use arrow keys to move, ‘?’ for help, ‘q’ to quit, ‘<-‘ to go back.
Arrow keys: Up and Down to move. Right to follow a link; Left to go back.
H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list

Sip! berhasil! 🙂

Note: Terima kasih Yorriz atas tulisan yang sangat menarik ini. Sering-sering nulis yah 🙂

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *