Recently I needed to debug an issue that was happening on a staging site that is protected by basic authentication. To try and recreate the issue I needed to password protect my local development site in the same way.
I’m using MAMP Pro for my local sites and as I couldn’t find a post about setting up basic auth on MAMP Pro, I thought I’d best document it – even just for myself googling in the future.
Create the Password
To create the username and password file we can use the htpasswd
tool that comes installed on a Mac by default.
Choose where you want to store the password file and make sure the directory exists:
mkdir /Users/igp/web
Then run the htpasswd
command using the -c
argument to create the file, passing the username:
htpasswd -c /Users/igp/web/.htpasswd iain
You will be asked to supply a password and then confirm it. That’s it. Now we need to tell MAMP Pro to use it for a site.
Configuring MAMP Pro
I just wanted to password protect one of my sites in MAMP, so it needs to be set up as a host. Select the host and click the Apache tab and you should see a text box for ‘Additional parameters for <VirtualHost> directive:`. I’m using MAMP Pro 4.4.1 so things might vary on your version.
In that box add the following, making sure you change the path of ‘AuthUserFile’ to match your path:
<Location />
Deny from all
AuthUserFile /Users/igp/web/.htpasswd
AuthName "Restricted Area"
AuthType Basic
Satisfy Any
require valid-user
</Location>
Save the settings and get MAMP to restart the servers.
And that’s it! The next time you access the site it will ask you for a username and password before you can access it.