In this article I’m setting up HTTP authentication on Apache and Nginx servers. It is pretty straightforward and should work for most of the environments.

Using Apache’s htpasswd Command

If you want to use Apache’s htpasswd command, check if it exists on your system:

which htpasswd
root@server1:~# which htpasswd
/usr/bin/htpasswd

If you get an output like the one above, everything is fine – htpasswd is already installed. If the command returns without any output, htpasswd does not exist on your system, and you must install it. On Debian/Ubuntu, it’s part of the apache2-utils package which we can install as follows:

apt-get install apache2-utils

I want to create the password file /var/www/www.mysite.com/.htpasswd now and store the user johndoe in it (you can give the password file any name you like – it’s not necessary to name it .htpasswd; I just named it .htpasswd because that’s the way password files are named under Apache):

htpasswd -c /var/www/www.mysite.com/.htpasswd johndoe

You will be asked for a password for the user johndoe. Please note that the -c switch makes that the file is created from scratch; if it didn’t exist before, it will be created; if it existed before, it will be overwritten with a new one, and all users from the old file will be lost! Therefore, if you want to add another user without deleting all existing users, use the htpasswd command without the -c switch:

htpasswd /var/www/www.mysiteOtherwise you will see a 401 Authorization Required error message:.com/.htpasswd till

The last command adds the user till to /var/www/www.mysite.com/.htpasswd so that we now have the users johndoe and till in it.

On Apache

<VirtualHost *:80>
ServerName www.mysite.com
...
 <Location />
  Allow from all
  AuthBasicProvider file
  AuthUserFile /var/www/www.mysite.com/.htpasswd
  AuthName authorization
  AuthType Basic
  Satisfy all
  require valid-user
 </Location>
</VirtualHost>

On Nginx

server {
 listen 80;
 server_name www.mysite.com;
 root /var/www/www.mysite.com/.htpasswd; 
 [...] 
 location / { 
#    try_files $uri $uri/ /index.php; # you might need to use it in cases like Magento
    auth_basic "Restricted"; 
    auth_basic_user_file /var/www/www.mysite.com/.htpasswd; 
 } 
[...] 
}

Testing

That’s it! You can now go to your test directory in a browser (http://www.mysite.com/), and you should be asked for a username and password:

Screen Shot 2014-11-07 at 10.26.59 AM

If you enter the correct username and password, you’ll be granted access. Otherwise you will see a 401 Authorization Required error message:

Screen Shot 2014-11-07 at 10.27.10 AM

That’s all. Happy Coding!

Set Up HTTP Authentication With Apache/Nginx on Linuxhttps://i1.wp.com/blog.mdnsolutions.com/wp-content/uploads/2014/11/HTTP-auth.png?fit=414%2C216&ssl=1https://i1.wp.com/blog.mdnsolutions.com/wp-content/uploads/2014/11/HTTP-auth.png?resize=150%2C108&ssl=1Renato MedinaApache2NginxApache,Authentication,HTTP,NginxIn this article I'm setting up HTTP authentication on Apache and Nginx servers. It is pretty straightforward and should work for most of the environments. Using Apache's htpasswd Command If you want to use Apache's htpasswd command, check if it exists on your system: which htpasswd root@server1:~# which htpasswd /usr/bin/htpasswd If you get an output...A Magento Craftsman in Melbourne