Confusion in suPHP, suPHPexec and Apache...

Confusion in suPHP, suPHPexec and Apache suEXEC

What is phpsuexec?

Phpsuexec is a deprecated feature in cPanel where php is setup as cgi instead of apache module. All shared hosting servers have been updated from phpsuexec to suPHP. This applies to such clients that are still using phpsuexec on their VPSs/Dedicated servers. Phpsuexec brings a new level of security to the way php is used.

  1. php scripts execute using the permissions of userid of the account holder instead of user “nobody”
  2. world writable folders (chmod 777) is not required for file uploads through php
  3. The php file need to have ownership of the user to execute in the user account (by default it is)
  4. php file (script) does not need 755 permissions. 644 is fine. In fact 400 or 600 is ok too (especially good for sensitive information)
  5. php_flag or php_value can NOT be used in .htaccess files (It will result in Internal Server Error)
  6. The php flags that do not work in .htaccess can be moved to php.ini file in the same folder where php script exists. However, the php flag/value will be in the format of php.ini and not that of .htaccess. For example this from .htaccess:

    php_flag register_globals off

    will go into php.ini as:

    register_globals off

    If php.ini exists in the folder where the php scripts exists, it will take all values from it (and nothing will be taken from main php.ini

  7. If the folder that contains a php file/script is world writable (chmod 777), it will result in Internal Server Error. This is similar to cgi/perl scripts under suexec that do not like such permissions for security reason. The normal folder permissions should be 755
  8. Apache specific php functions do not work:

    http://www.php.net/manual/en/ref.apache.php

  9. If your .htaccess file contains “Options” directive, it should have + or – with the directive to keep the ExecCGI active
  10. Symbolic links do not work for php scripts for security reasons
  11. Some web applications (OS commerce, ZenCart etc.) check if its configure.php file is writable (since php is being executed with userid it should be), so it will complain that its writable. Please change the permissions to 444 via ssh. (chmod 444 /path/to/configure.php)
  12. HTTP authentication via php code does not work. However you can continue to use it via .htaccess or password protected folder feature of the control panel
  13. If you use “AddType application/x-httpd-php” in .htaccess, it should be set to “AddHandler application/x-httpd-php”

Similarly if you are using ForceType in .htaccess to force a file to be treated as php, you will need to change it to SetHandler.

What is suPHP and what is being changed from phpsuexec to suPHP?

suPHP is a tool for executing PHP scripts with the permissions of their owners. Currently our servers use phpsuexec which also executes PHP with the permission of their owners. However these are two different tools and there are some improvements with moving to suPHP.

Once suPHP is available on your server, you can login to your control panel and find a link ‘PHP Configuration’ under ‘Software/Services’. On that page:

  1. You can switch your account’s php to php4 or php5
  2. You can read how to configure php and how suPHP works. It is similar to phpsuexec as explained above excluding some improvements mentioned below
  3. Download server wide php.ini for php4 or php5 and customize it for your own needs. You may need to do this regularly to keep your php settings in synch with server level settings e.g after Zend Optimizer upgrade

Changes from phpsuexec to suPHP:

  1. By default php CLI is php5. Here are the paths for your reference:
    /usr/bin/php (php5 cgi)
    /usr/local/bin/php (php5 cli)
    /usr/php4/bin/php (php4 cgi)
    /usr/local/php4/bin/php (php4 cli)
  2. There are some significant improvments in suphp such as:
    * HTTP based authentication auth works via php
    * symbolic links to php files also work
    * permissions of public_html does not need to be changed for using shared SSL with php5
    * Custom error pages will work with both php4 and php5
  3. ionCube PHP Loader will be available server wide alongwith Zend Optimizer. If you use custom php.ini you will need to update it by downloading it from your control panel so that latest Zend Optimizer can load for your scripts as well
  4. If you are setting up custom php settings, the custom php.ini file will be required in a folder where the php script needs to execute. Or you can place php.ini anywhere and have this directive in public_html/.htaccess:

    suPHP_ConfigPath /home/username/php5-config

    where username is your cpanel account username, and php5-config is just a folder name (you can name it anything) and it will pick php.ini from that folder. Yes, you can have php.ini outside of your webroot in suPHP. This is a new feature

  5. To activate php5 on a subfolder or in your whole account, this directive was added in .htaccess in phpsuexec

    AddHandler application/x-httpd-php5 .php .php3 .phtml

    or a variant of it. Now this must be proceeded by the marker comment to block cpanel from changing your settings:

    # Use PHP5 as default

  6. AddHandler application/x-httpd-php5 .php .php3 .phtml
    Or if you use control panel to activate php5 (upgraded servers), then you do not need to manually add the above directive

Other updates

  1. For semi-dedicated clients, ffmpeg will be available via both php4 and php5
  2. mod_gzip is being installed as well on all servers

Differences between phpsuexec and regular php

When using the common PHP installation on a webserver, php runs as the user nobody and it doesn’t require the execute flag to be enabled.

The problem on this is that if mod_openbasedir is not installed, every user will be able to read your php files because everyone is virtually sharing the same username (nobody).

As most of you already know, PHP Files are not meant to be read, but parsed, and that is where the problem resides. PHP Files have to be parsed, otherwise everyone who is able to read your php file will see settings that you would probably want to keep private, such as your MySQL username and password.

PHPSUEXEC fixes all this because it requires php to be run as the file owner’s username. (for example: andre)

This is not everything it fixes though. PHPSUEXEC is also here to fix file ownership problems.

This has been a common issue on a few Content Management Systems such as Joomla and also on the popular blog software WordPress.

It also adds security to your files as you can use permissions such as 600 or 700 in your files and your visitors will still be able to view them (parsed) in their browsers.

PHPSUEXEC will also refuse to serve any pages that are at security risk, for example with 777 as permissions. (will generate an Internal Server Error)

PHP as an Apache Module

When PHP runs as an Apache module, PHP files work under the Apache user/group known as “nobody”. For example, when a PHP file needs to write to another file or create/remove a file, it does so under the name “nobody”. In order to allow “nobody” to do this, you need to set specific permissions on the file/directory, such as 777 – which translates to read/write/execute by user/group/world. This is insecure because you have not only allowed the webserver (Apache) to read/write to the file, you have also allowed everyone else on the server to read/write to the file as well!

Due to the above conditions, when a PHP file creates or uploads a new file under your account, the new file will be owned by the user “nobody”. If you FTP into your account, all files owned by “nobody” will not be available for you to move, rename or delete. In this case the only way to remove the “nobody” owned files would be through a file on the server or to contact support and ask for the file ownership to be changed back to your username.

PHP as a CGI with Suexec

When PHP runs as a CGI with Suexec, PHP files work under your user/group. PHP files no longer require loose permissions to function, now they will require strict permissions. Setting your directories or PHP files to 777 will cause them to produce a 500 Internal Server Error, this happens to protect your PHP files from being abused by outside sources.

Under PHPSuexec your directories and PHP files can have permissions no greater than 755 (read/write/execute by your username, read/execute by group/world). Since you own your files, your scripts can function in any directory your user has created and can’t be manipulated by any outside users, including “nobody”.

Now, when a PHP file creates or uploads a new file under your account, the new file will be owned by your username. You will no longer have to worry about the webserver taking over your files and even more important, you will no longer have to worry about a stranger reading or writing to your files either!

How To Enable WHM Apache PHP SuExec

By default PHP on WHM/Cpanel is loaded as DSO (Dynamic Shared Object) module and is run by the user “nobody” by default. Though this method of loading the PHP module is normally the fastest way to serve PHP request, running it as using user “nobody” will be a real pain in the ass if you are serving multiple sites run by multiple users, you will be for sure run into file permission problems.

This is where the SuExec comes in play, every executed PHP scripts will be executed by the user who owns the VirtualHost that is server the request, this method has a lot of drawbacks too on both speed and security.

Anyway, if you still want to enable it then read on below.

  1. Login to your Web Host Manager as root account then under the Service Configuration menu, look for the “Configure PHP and SuExec” and click on it
  2. On the “Configure PHP and SuExec” page, under “alter configuration” section, look for the PHP handlers and then change its values to “cgi” and then set the Apache SuExec to On. (by default the value is on)
  3. Finally, click on “Save new configuration” button and wait til the Apache server restarted and your done

To verify that SuExec is working as intended, try to upload a file or create a folder using an upload file script on PHP.

Troubleshooting Internal Server Errors (Error 500)

Everytime an internal server error occurs, it will be added to your Error Log in cPanel. (cPanel »» Error Log). This will usually give you a clue on where the error resides. In most cases it will be either a permission error on a bad command in your .htaccess file (remember that all php values have to go to your php.ini file).

Directories that need to be written onto will no longer require 777 as permissions and phpsuexec will refuse to write or read on directories exposed with such permissions. You will have to chmod them to 755 always.

To simplify it, just remember that you should never have a file or folder with world-writeable permissions, because you no longer have to.

MIMETypes

If you added a Mimetype to the system in order to run html files as php scripts (AddType as .htaccess command), you will have to remove it and add an ApacheHandler instead. This is easy to do though. Just log into your control panel, then click on Apache Handlers and add the following:

Extension: html (or htm) : AddHandler application/x-httpd-php

Tags: dso (mod_php), fastcgi, php handlers explained, suphp

Comments are closed.