Skip to main content

Setup Password authentication for website with apache on ubuntu 14.04





In order to create the file that will store the passwords needed to access our restricted content, we will use a utility called htpasswd. This is found in the apache2-utils package within the Ubuntu repositories.

Update the local package cache and install the package by typing this command. We will take this opportunity to also grab the Apache2 server in case it is not yet installed on the server:

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


Create the Password File


We now have access to the htpasswd command. We can use this to create a password file that Apache can use to authenticate users. We will create a hidden file for this purpose called .htpasswd within our /etc/apache2 configuration directory.

The first time we use this utility, we need to add the -c option to create the specified file. We specify a username (sammy in this example) at the end of the command to create a new entry within the file:

    sudo htpasswd -c /etc/apache2/.htpasswd aditya


You will be asked to supply and confirm a password for the user.

 Leave out the -c argument for any additional users you wish to add:

    sudo htpasswd /etc/apache2/.htpasswd another_user

If we view the contents of the file, we can see the username and the encrypted password for each record:

    cat /etc/apache2/.htpasswd

Output
aditya:$apr1$lzxsIfXG$tmCvCfb49vpPFwKGVsuYz.
another_user:$apr1$p1E9MeAf$kiAhneUwr.MhAE2kKGYHK.



Configure Apache Password Authentication



Begin by opening up the virtual host file that you wish to add a restriction to. For our example, we'll be using the 000-default.conf file that holds the default virtual host installed through Ubuntu's apache package:

sudo nano /etc/apache2/sites-enabled/000-default.conf


Authentication is done on a per-directory basis. To set up authentication, you will need to target the directory you wish to restrict with a <Directory ___> block. In our example, we'll restrict the entire document root, but you can modify this listing to only target a specific directory within the web space:

 <VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/var/www/html">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </Directory>

</VirtualHost>



Save and close the file when you are finished. Restart Apache to implement your password policy:

    sudo service apache2 restart

If you wish to set up password protection using .htaccess files instead, you should begin by editing the main Apache configuration file to allow .htaccess files:

    sudo nano /etc/apache2/apache2.conf

Find the <Directory> block for the /var/www directory that holds the document root. Turn on .htaccess processing by changing the AllowOverride directive within that block from "None" to "All":


<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>



Save and close the file when you are finished.



Next, we need to add an .htaccess file to the directory we wish to restrict. In our demonstration, we'll restrict the entire document root (the entire website) which is based at /var/www/html, but you can place this file in any directory you wish to restrict access to:

    sudo nano /var/www/html/.htaccess

Put following value in .htaccess file that we have create under the directory -

AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user



Save and close the file. Restart the web server to password protect all content in or below the directory with the .htaccess file:

    sudo service apache2 restart

You should now have everything you need to set up basic authentication for your site.















 

Comments

Popular posts from this blog

how to take mails backup in outlook 2007

How to take backup of mails in outlook 2007. there is a very simple way to taking backup of mails 1.Opne outlook 2007 > tools >account > data files >open folder then close all other windows and copy outlook.pst file in a safe location To restore backup 1.Opne outlook 2007 > tools >account > data files > add > and give the location of outlook.pst file.

Configure Vsftpd with virtual users in Ubuntu 14.04

vsftpd is a GPL licensed FTP server for UNIX systems, including Linux. It is secure and extremely fast. It is stable. Don't take my word for it, though. Below, we will see evidence supporting all three assertions. We will also see a list of a few important sites which are happily using vsftpd. This demonstrates vsftpd is a mature and trusted solution. To configure vsftpd server  with virtual users we need to enable PAM for vsftpd. apt-get install vsftpd libpam-pwdfile Now configure vsftpd to use PAM authentication vim  /etc/vsftpd.conf then paste in the following listen=YES anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 local_root=/var/www chroot_local_user=YES allow_writeable_chroot=YES hide_ids=YES #virutal user settings user_config_dir=/etc/vsftpd_user_conf guest_enable=YES virtual_use_local_privs=YES pam_service_name=vsftpd nopriv_user=vsftpd guest_username=vsftpd Now create users - You can either use a data...

AWS Certification RoadMap