Apache2 per user web directories – Ubuntu 14.04

This tutorial helps in configuration of Apache 2 per user web directories. This enables Apache2 to serve we pages home user directories under a specific directory /home/username/public_html

Users accessing the page will in turn visit “http://server-name-or-ip-address/~username”  

Steps :

Install Apache in ubuntu . :

sudo apt-get install apache2 -y

Install php5 :

 sudo apt-get install php5

Install Some addition php5 components :

sudo apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Enable userdir conf in mod-enabled

sudo ln -s /etc/apache2/mods-available/userdir.load /etc/apache2/mods-enabled/
sudo ln -s /etc/apache2/mods-available/userdir.conf /etc/apache2/mods-enabled/

Lets create folder public_html in the user’s home directory. This can be done by a local user/staff/student

mkdir public_html

Now edit userdir.conf to set directory location and user info etc

 sudo vim /etc/apache2/mods-enabled/userdir.conf
<IfModule mod_userdir.c>
     UserDir public_html
     UserDir disabled root

 <Directory /home/*/public_html>
     DirectoryIndex index.php index index.html default.html default.htm
     AllowOverride FileInfo AuthConfig Limit Indexes
     Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
 <Limit GET POST OPTIONS>
     Require all granted
 </Limit>
 <LimitExcept GET POST OPTIONS>
     Require all denied
 </LimitExcept>
 </Directory>
</IfModule>

Now Lets enable php execution in local home directories (The edited file is below with the required lines commented out)

sudo vim /etc/apache2/mods-available/php5.conf
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
 SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
 SetHandler application/x-httpd-php-source
 # Deny access to raw php sources by default
 # To re-enable it's recommended to enable access to the files
 # only in specific virtual host or directory
 Order Deny,Allow
 Deny from all
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[345]?|t|tml|ps)$">
 Order Deny,Allow
 Deny from all
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
# 
# 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_flag engine Off
# </Directory>
#</IfModule>

Make Sure that file looks like mine .

now restart apache2

 sudo apachectl restart

Now lets create a index.php file in public_html folder

vim index.php

and put inside that file

<?php  phpinfo(); ?>

now open browser and enter address “http://server-name-or-ip-address/~username”

you will get phpinfo page

phpinfo

Main reference

https://www.computersnyou.com/2931/setup-per-user-web-directories-apache2-ubuntu-13-10/