[Linux] PHP not working in userdir (public_html)

Today I wanted to give my users possibility to test their PHP scripts, but without all the fuss with creating virtual hosts for each one of them. My first and obvious choice was userdir – user creates public_html directory in his home dir, puts there files, and those files are accessible via http://servername/~username/ URL. To enable this behavior you only have to enable userdir module (a2enmod userdir), and remember to set correct permissions to the userdir (chmod +x $HOME) and public_html (chmod 755 $HOME/public_html). I did this, and everything was working fine, except PHP scripts – browser wanted to download them instead of displaying proper processed content. It appeared that apache in Debian has by default PHP disabled for userdirs. To enable scripting in this dirctory, open file /etc/apache2/mods-enabled/php5.conf, find that piece of code:

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

and disable it, either by deleting or by commenting it out (precede each line with # sign). You can also change php_admin_value engine setting to On, but if you do that, you will be unable to turn off PHP engine in .htaccess files.

47 thoughts on “[Linux] PHP not working in userdir (public_html)”

    1. You can add next entry in config, enabling the php engine just for this user. Whole block would look like this:

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

      This should work. If it’s not, drop me a comment and I’ll test it thoroughly.

  1. Unfortunately it took a lot more than 2 minutes for me to find this site, but luckily I found it at last. Thanks a lot! – Funny thing that I have never seen this documented elsewhere…

  2. Whoever thought this setting was a great idea to introduce needs to be shot in front of their families.

    Assuming you had scripts in your home dir, this setting comes with an update and from then on your serving those scripts AS SOURCE, not as parsed script. It’s not even a sechole, I’d almost consider it a deliberate backdoor.

  3. php_admin_value engine On

    after I setted engine On,however .php file in /home/user/test.php also downloaded by my browser.I’ll appreciate it if you explain how does this directive work.
    Thanks.

  4. if i comment out the IfModule in php5.conf and restart apache, php workd fine.

    however, if i set it up like leafnode mentioned above, it does not work.

    I also read that setting it up this way is considered bad practice, and you should use a php-in-homedirs.conf file instead. i tried this way, and it does not work.

    the only way i can get php to work in public_html is by commenting out IfModule in php5.conf.

    can anyone please explain how to set up aphp-in-homedirs.conf file?

  5. Thank’s. Your Post helps me very much with the configuration in apache2 and php5. I only need comment the lines from php5.conf that apache interpreted the php in my public_html.

  6. I did eneable php on my server this way for users. Just to drop a line to all who said they needed to clear cache… you don’t just restart apache server: sudo service apache2 restart, that schould do the trick.

  7. This is crazy. Ten years later and this is still not better documented. I have working on this for hours, I tried nginx, lighttpd, and apache and none worked. Then I found this is tried apache again and it worked. How is this not documented in any basic server guides or anything for Ubuntu?

Leave a Reply

Your email address will not be published.