Customize PHP per domain in cPanel

PHP is a scripting language commonly used to build websites and web applications.
PHP natvie core can be customized to better meet the needs of your website or chosen Content Management System (CMS). This customization can fulfill many purposes, from increasing the memory available to changing security settings and other stuffs.

While there are literally thousands of possible combinations, developers can usually find a happy balance that allows all of websites to function without affecting each other. When this is the case, it is always recommended to simply make the necessary changes through WHM by choosing the PHP Configuration Editor option under the Service Configuration heading:

cPanel currently offers four different PHP handlers; four different interpreters for PHP to use to communicate with the web server (generally Apache). Each of these handlers has it’s own story and it’s own pros and cons, details that are better left for an article of their own. The important thing to note is that while all four of these handlers allow for customizing PHP for individual domains, the steps to accomplish this end differ considerably. To make this often necessary task a little easier, we have assembled the following instructions customized for each PHP handler.

Steps to customizing PHP for each handler per domain

CGI and FastCGI require the custom php.ini to contain a full PHP configuration, while DSO and suPHP can contain only those parameters you wish to override. For consistency and to facilitate easier switching between modes, however, we will recommend that regardless of the PHP handler you choose to use a full PHP configuration file. The default location for the main php.ini file on your cPanel server to copy should be /usr/local/lib/php.ini

The DSO handler does not allow for more than one php.ini per Apache instance. Instead, you may make minor adjustments in a .htaccess file located in the public_html folder for the domain needing customization.

  • suPHP

suPHP is the cPanel default and most commonly used PHP handler. It also comes pre-configured to allow a custom php.ini file placed wherever you need to change PHP configuration values. This “convenience”, however, is limited to affecting only the folder where the php.ini file resides. So to affect multiple folders or sub-folders you would need multiple copies of this file; one file per folder. This is easily resolved however by following the following easy steps:

    1. Create a copy of the full PHP configuration file (php.ini) inside of the domain’s public_html folder (ie: /home/cpanlusr/public_html/
    2. In the same folder, create or edit the .htaccessfile and place the following code at the top, being sure to update the second line of code to match your current folder location:
      <IfModule mod_suphp.c>
      	suPHP_ConfigPath /home/<cPanel user>/public_html/php.ini
      <Files php.ini>
      	order allow,deny
      	deny from all
    3. Your custom php.ini file should now function for both the public_html/ folder it resides in and any folder within as well.
    • Create a copy of the full PHP configuration file (php.ini) inside of the domain’s public_html folder (ie: /home/cpanlusr/public_html/). Connecting as the root user and running the following command in your favorite terminal application should do the trick. Be sure to update the second folder path to match where you want the new php.ini file created:
      cp /usr/local/lib/php.ini /home/cpanlusr/public_html/cgi-bin/
      NOTE: If you need a php.ini file for more than one domain, go ahead and repeat this step as many times as you need.
    • Next we’re going to be editing the cPanel PHP wrapper script located at /usr/local/cpanel/cgi-sys/php5. Before editing any server file, however, it is a good idea to create a backup first. Run the following command in your terminal application to do so:
      cp -frp /usr/local/cpanel/cgi-sys/php5 /usr/local/cpanel/cgi-sys/php5.bk
    • Now that we have a backup, we can start editing the original file to meet our needs. Using vi, vim, nano, or any other command line editor, open the file for editing:
      nano /usr/local/cpanel/cgi-sys/php5
    • Before making any modifications, the file should look something like this:
      # If you customize the contents of this wrapper script, place
      # a copy at /var/cpanel/conf/apache/wrappers/php5
      # so that it will be reinstalled when Apache is updated or the
      # PHP handler configuration is changed
      exec /usr/bin/php

      We’re going to add a new line just before the last one at the bottom [[ -f ~/public_html/php.ini ]] && exec /usr/bin/php -c ~/public_html/php.ini so that the file now looks like:

      # If you customize the contents of this wrapper script, place
      # a copy at /var/cpanel/conf/apache/wrappers/php5
      # so that it will be reinstalled when Apache is updated or the
      # PHP handler configuration is changed
      [[ -f ~/public_html/php.ini ]] && exec /usr/bin/php -c ~/public_html/php.ini
      exec /usr/bin/php

      NOTE: If you’re just following along and not familiar with the nano editor, you will press Ctrl+X to close the file and press Y when asked if you want to save your changes.

    • Now I’m sure you couldn’t help but notice the instructions that were inside that file saying “If you customize the contents of this wrapper script…”. Sure enough, we’re going to follow those instrutions. Now, what those instructions don’t tell you is that neither that file nor the path provided typically exist by default so you’ll want to run both of the following commands to make sure you create your copy correctly:
      mkdir -p /var/cpanel/conf/apache/wrappers
      cp -frp /usr/local/cpanel/cgi-sys/php5 /var/cpanel/conf/apache/wrappers/php5
    • Just to clarify, this is copying our modified version, not the backup we created earlier!
    • Last step, and this one is simple; all you need to do is restart Apache. Assuming you’re still connected to the server in your terminal application, you can run the following command to do this:
      service httpd restart
      If you closed the terminal or would just rather do this through WHM you can do so by clicking the HTTP Server (Apache) button under the Restart Services section
Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Related Posts