Enable userdir Apache module on Ubuntu Linux and other Debian based distributions

For starters we need to enable this module using following command:

sudo a2enmod userdir

Now we will configure Apache module userdir by editing userdir.conf file like this:

sudo nano /etc/apache2/mods-enabled/userdir.conf

Next you should replace the contents of that configuration file with the following code:

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root
        <Directory /home/*/public_html>
		AllowOverride All
		Options MultiViews Indexes SymLinksIfOwnerMatch
			# Apache <= 2.2:
		        Order allow,deny
		        Allow from all
		        # Apache >= 2.4:
		        #Require all granted
		<LimitExcept GET POST OPTIONS>
			# Apache <= 2.2:
		        Order deny,allow
		        Deny from all
			# Apache >= 2.4:
			#Require all denied

With this setup Apache web server would serve HTML files from public_html directory of every user on your pc. This HTML files would be accessible using the http://example.com/~user/ syntax. Serving PHP files from user’s public_html directoriy is disabled by default for security reasons. If you want to enable PHP processing when using userdir this is what you need to do. First you edit following Apache configuration file in this way:

sudo nano /etc/apache2/mods-available/php5.conf

Now you need to comment out a few lines from <IfModule mod_userdir.c> to the next </IfModule>so you get something like this:

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
	SetHandler application/x-httpd-php
    <FilesMatch "\.phps$">
	SetHandler application/x-httpd-php-source
    # To re-enable php in user directories comment the following lines
    # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    #<IfModule mod_userdir.c>
    #    <Directory /home/*/public_html>
    #        php_admin_value engine Off
    #    </Directory>

Last thing you just restart Apache web server and create public_html directory inside your home directory like this:

sudo service apache2 restart
mkdir /home/$USER/public_html

sudo chown -R www-data:www-data /home/jhoni/public_html
sudo adduser jhoni www-data
sudo chmod -R 775 /home/jhoni/public_html

cd /home
chmod 755 jhoni


Leave a Comment

Your email address will not be published. Required fields are marked *