Adding Password Authentication on Apache with Ubuntu 14.04

Published on Author craigzearfossLeave a comment
  • Update packages and make sure apache2-utils is installed.
    sudo apt-get update
    sudo apt-get install apache2 apache2-utils
    
  • Add the user to the password file. (Only use the -c parameter the first time to create the file.)
    sudo htpasswd -c /etc/apache2/.htpasswd USERNAME
    
  • Configure the access control within the virtual host definition /etc/apache2/sites-available/HOST_CONF_FILE. Add the lines in red.
    <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>
    
  • You can also configure password authentication in the .htaccess file. See the reference for instructions.

References:

Leave a Reply

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