.htaccess on LAMP 14.04?

Posted June 7, 2014 24.5k views
Trying to lock down www/html/ using .htaccess on a LAMP 14.04 deployment, and having a tough time. Documentation is all over the place without a clear answer. Tried updating 000-default.conf Tried updating .htaccess to point to .htpasswd Did lots of restarts of apache2 Nothing worked. Suggestions?
  • I don’t know if you still need this but…

    I had problems with this as well. First make sure you have a new droplet with the LAMP template.

    The solution:

    1. Upload your website to the /var/www/html directory
    2. chmod -R 777 /var/www/html
    3. Check your htaccess file with: cat /var/www/html/.htaccess
    4. If the htaccess is not correct modify the file so it contains the correct rules
    5. Enable the mod rewrite: sudo a2enmod rewrite
    6. Restart your server: sudo service apache2 restart
    7. It should be working now! Don’t touch the apache config files.
  • You should also:

    1. Search for <Directory /var/www/>...</directory> in /etc/apache2/apache2.conf
    2. Change AllowOverride None to AllowOverride ALL
    3. then restart apache by using sudo service apache2 restart
  • Thank you. It worked for me :)

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers
In order to enable an .htaccess file, there are a few steps you need to take. In /etc/apache2/sites-available/000-default.conf you need to add:
        <Directory "/var/www/html">
            AllowOverride All
Then create the file /var/www/html/.htaccess with the contents:
AuthUserFile /etc/htaccess/.htpasswd
AuthName "Authorization Required"
AuthType Basic
require valid-user
Then run:
sudo mkdir /etc/htaccess/
sudo htpasswd -c /etc/htaccess/.htpasswd user1
And finally, restart apache:
sudo service apache2 restart
Well... In this tutorial [ ] we learn to create a user and give you root privileges ok. Good. I did it correctly and now looked at the file and is equal to the tutorial. I'm trying to use this option suggested by @astarr and the answer I have is can not create directory '/etc/htaccess/': Permission denied Can I get using root?
by Justin Ellingwood
When you start a new server, there are a few steps that you should take every time to add some basic security and set a solid foundation. In this guide, we'll walk you through the basic steps necessary to hit the ground running with Ubuntu 14.04.