lauantai 13. huhtikuuta 2013

User homepages on Apache + php + password protection

I didn't test if the user homepages are enabled by default but setting it on makes this happen: anything you put in public_html in your home directory is published on the web.

Turn the mod on and restart service:

$ sudo a2enmod userdir
$ sudo service apache2 restart

create a folder named "public_html" on your home directory and put "index.html" there with some HTML code.

Check your name if you don't know it:

$ whoami

Then surf to http://localhost/~yourname


To allow php for users.

$ sudo apt-get install libapache2-mod-php5
$ cd /etc/apache2/mods-enabled/
$ sudoedit php5.conf 
Comment the lines out from FilesMatch 
This is simple php file to test if it is working
<html>
<head>
<title>This website supports php!</title>
</head>
<body>
<?php
print "this is php, 1+2 is ".(1+2);
?>
</body>
</html>

$ sudo service apache2 restart



 Password protected site.

Again create a new folder for your site which you want to have password protection.

$ mkdir password
$ htpasswd -c .htpasswd xubuntu
-c = create. makes a new file, if a file exists it is overwritten. This creates hidden file .htpasswd for user xubuntu.

$ sudoedit /etc/apache2/sites-available/default

Edit apache default settings by adding this:

   <Directory /home/xubuntu/public_html/password/>
        AllowOverride AuthConfig
        AuthName "Please insert your login data."
        AuthType Basic
        AuthUserFile /home/xubuntu/public_html/password/.htpasswd
        AuthGroupFile /dev/null
        require user xubuntu
    </Directory>

$ sudo service apache2 restart

And for me this also worked. Original instructions: http://kontsu.wordpress.com/2012/09/27/virtual-hosting-with-apache-2/

UPDATE

It is better to create .htaccess file under the website folder and add:

AuthUserFile /home/location/.htpasswd
AuthType Basic
AuthName "Login"
Require valid-user







Ei kommentteja:

Lähetä kommentti