Sam
Sam

Reputation: 2084

How do I add HTTP authentication to a PHP-based Heroku app?

I am attempting to add HTTP authentication to my php-based Heroku app. I found a great Github gist here that provides code for creating .htaccess and .htpasswd files in my Heroku directory. I currently have both files within the /app/ directory (so .htaccess is located at /app/.htaccess and .htpasswd is located at /app/.htpasswd). However, when I navigate to my app and enter in the username and password, I get a 500 internal server error. Checking the Heroku logfile, I can see only that my .htpasswd file was not found:

(2)No such file or directory: [IP removed] AH01620: Could not open password file: /app/.htpasswd

Here is the full contents of my .htaccess file:

AuthUserFile /app/.htpasswd AuthType Basic AuthName "My Files" Require valid-user

If I run bash on heroku and 'ls -a', I can see that my .htpasswd file is definitely in the /app/ directory.

Someone has any idea what is going on here? Is there something that I'm missing in terms of getting this set up?

Upvotes: 2

Views: 3801

Answers (1)

Sam
Sam

Reputation: 2084

Figured it out! I needed to create the .htpasswd file in my project's main directory (on my local machine), then add the file to my git repo and push the whole thing to Heroku. For some reason, when I used "heroku run bash" and then executed the command to add the .htpasswd file, it seems that Heroku added that file on a temporary dyno / instance and not on the permanent version of my app.

For other rookies who are trying to do this and need an easy end-to-end explanation:

  • Create an .htaccess file in the main directory of your git repo (on your local machine) per the instructions at https://gist.github.com/bbrewer97202/3316425. For my app, I left out the /www part of the AuthFile line because there is no /www/ folder on my Heroku instance.
  • Using terminal on your local machine, create an .htpasswd file in the main directory of your git repo by typing 'htpasswd -c .htpasswd [username]' once you are in that directory.
  • Commit everything to git and then push to the Heroku server.

Upvotes: 4

Related Questions