Articles

How to Password Protect a Directory

If you want to limit access to one of your apps, you can place password protection on it using a .htaccess file.

First, open your text editor and create a file named .htaccess that contains the following (where APPNAME is the name of your app):

AuthType Basic
AuthName "Authentication Required"
AuthUserFile /srv/users/serverpilot/apps/APPNAME/public/.htpasswd
Require valid-user

Place that file in your app's public folder.

Next, SSH in to your server as the serverpilot user (or the system user the app belongs to). Run the following command to create your .htpasswd file. (Remember to replace APPNAME with the actual name of your app.)

touch ~/apps/APPNAME/public/.htpasswd

Now, use the htpasswd-sp command to add or change a password for a user yourname. (You can substitute any username for yourname).

htpasswd-sp ~/apps/APPNAME/public/.htpasswd yourname

Follow the prompts in the command line to add the new password.

When you visit the app through your browser, you'll now be prompted for a username and password.

Protecting a Subdirectory

You can also use these same steps to limit access to a subdirectory of your app.

Create a .htaccess file that contains the following (where DIRECTORY is the name of your subdirectory):

AuthType Basic
AuthName "Authentication Required"
AuthUserFile /srv/users/serverpilot/apps/APPNAME/public/DIRECTORY/.htpasswd
Require valid-user

Place that file that directory's folder.

SSH in to your server as the serverpilot user. Run the following command to create your .htpasswd file. (Remember to replace DIRECTORY with the actual name of your subdirectory.)

touch ~/apps/APPNAME/public/DIRECTORY/.htpasswd

Then, use the htpasswd-sp command to add or change a password for a user yourname. (You can substitute any username for yourname).

htpasswd-sp ~/apps/APPNAME/public/DIRECTORY/.htpasswd yourname

Follow the prompts in the command line to add the new password.

When you visit the subdirectory through your browser, you'll now be prompted for a username and password.