Tuesday, June 24, 2014

Enable userdir apache module on Ubunty

What is userdir? 

userdir is module on apache2 web server that enables users (apart from root user) on multiuser system like linux, accessible public directory on web server. For every user on the system there is corresponding address. To be more precise if there are users, john, peter on system corresponding addresses would be: 

http://hostname/~john/    --> mapped to /home/john/public_html/
http://hostname/~peter/   --> mapped to /home/peter/public_html/

So every user on the system can have their own web site.

Why do I need apache2 userdir mod?

If you have multiple web projects you are working on and don't want to bother with root account all the time userdir is the way to go. 

Ok so how do I enable this userdir mod? 

If you have apache2 web server installed, then userdir module is available, you just have to enable it. To do this, open your terminal (CTRL + ALT + T) and enter following command: 

sudo  a2enmod userdir

This command will enable userdir module, now you need to create public_html directory under your 'home' path. Your home path will be /home/yourusername/ so that's where you create new directory
like so: 

mkdir public_html

and add privileges 

chmod 755 public_html

After this step you need to restart your apache2 server so the changes are applied. 

sudo service apache2 restart

Ok now when you create something in public_html dir you can access it via web browser. So create file:

index.html and fill it with some text. It will be accessible via http from web browser:

http://localhost/~youusername/index.html

That's nice but php is not interpreting my code, what to do? 

By default php interpreter is disabled from userdirs so you'll have to enable that as well. To do so you have to edit php5.conf file:

sudo gedit /etc/apache2/mods-enabled/php5.conf

and you'll see something like this: 


<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>


Comment last 5 lines (from <IfModule> .. </IfModule>)  and you should get:


<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>


Now save the changes (CTRL +S)  and restart apache2 web server again.

sudo service apache2 restart

Now your php files under userdir will be interpreted by php interpreter.  More information on apache2 userdir can be found http://httpd.apache.org/docs/2.2/mod/mod_userdir.html.



No comments:

Post a Comment