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.


28 Responses to “[Linux] PHP not working in userdir (public_html)”  

  1. 1 Eduardo

    Thanks !!

    It’s help me !!

    Greetings from Argentina.

    Eduardo.

  2. 2 Sami

    Thanks, it worked for me too, I commented this 5 line out :)

  3. 3 Alan

    I don’t know how I overlooked that… thanks for the tip. :)

  4. 4 Heino

    just switched from opensuse to try unbuntu – and this tip was usefull to me :)

  5. 5 TeemuJ

    Thanks, that helped a lot :)

  6. 6 Coolmax

    Ok, but if I want to leave this option unchanged, and enable php only for one user. How can I get this?

  7. 7 leafnode

    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.

  8. 8 Jabba

    Thanks, it helped me. However, it didn’t work immediately. I figured out a bit later that I had to delete the browser cache too.

  9. 9 Stu

    Good tip – problem identified, googled and fixed in under 2 mins. Thank you :)

  10. 10 alejo

    Thank you!! it’s was very useful to me ;)

  11. 11 Nicklas Jarnesjö

    Just what I was looking for! Thx!

  12. 12 docker

    Lifesaver… Thanks! Beware of the browser cache, as Jabba said!

  13. 13 Olli Räisänen

    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…

  14. 14 Dragan

    Thank you.
    Greetings from Montenegro.

  15. 15 yavuz

    thanks. it worked :)

  16. 16 jake

    Thanks! This helped

  17. 17 treaki

    Thanks a lot to!

  18. 18 TheNice

    Thanks man. That helped!

  19. 19 会跳舞的鞋子

    thanks very much.
    it’s what I want now .
    ^_^

  20. 20 bilge öz

    Thank you so and so and so much i forgat this one at new installation on lamp

    From Turkey
    Çok teşekkürler ..

  21. 21 Misael

    Thank you very much. Its saves my time.

  22. 22 Trevor

    many thanks, still useful!

    Trevor, UK

  23. 23 Adrie

    Can I enable php as non-super user, of do I need admin support?

  24. 24 leafnode

    @Adrie: there is possibility to enable PHP via .htaccess file, but only if admin allowed you to do so in main Apache config file.

  25. 25 Mike

    Thanks, saved a large amount of head scratching

    Mike

  26. 26 turkey

    thank you this solution

  27. 27 Timothy

    Thank U, I was looking for this whole day. It worked

  28. 28 CrazyCat

    I saved a lot of time with your post, thanks a lot

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image



Recent Comments:

RSS