in Apache, Linux, PHP

Apache: Enable PHP in UserDir

Here is a tip to PHP developers that uses the Debian family of distros: how enable the execution of PHP scripts in the public_html folder. Apparently, this feature is disabled by default in the Debian based distros. Before start, I’m assuming that you already have both Apache and PHP installed, configured and running.

The interest in using Apache’s mod_userdir is to increase the system security at the same time making easy the file handling. All the files inside the public_html folder present in your home dir will be accessible through Apache.

As root, run the following commands. First, we enable the userdir mod:

# a2enmod userdir

The script will enable the module and will tell you do restart Apache. At next, create the public_html folder using your normal user, inside your Home, and define its permissions:

$ mkdir public_html
$ chmod 711 public_html

Now, as root, define the Home’s folders permissions(this is a critical detail):

# chmod 711 /home /home/[your_login]

The next step is enable the execution of PHP scripts inside public_html. To do that, as root, open the file /etc/apache2/mods-enabled/php5.conf and find the following lines:

<IfModule mod_userdir.c>
  <Directory /home/*/public_html>
    php_admin_value engine Off

Comment them by putting a # in the beginning of each one. Save and close. Now restart the Apache server by running the following command as root:

# /etc/init.d/apache2 restart

The end! You can now run your PHP apps inside your public_html folder without being worried about doing weird tricks to read/write the default /var/www. 🙂