I was recently asked to look at allowing custom php.ini files on a system where they were using FastCGI (fcgid) with SuExec to load php pages. This was a cPanel server using PHP5x, and after some research, I was able able to come up with a solution.
Basically, what we will be doing, is telling our php interpreter (fastcgi) to use a different configuration for a specific user. We are going to do this through the use of .htaccess files, and a new action script. I am going to assume that you already have everything installed and working correctly. If not, you need to get things running prior to doing the following. If you need a hand getting things installed correctly, feel free to leave a comment at the end of this post.
Let’s get started by ensuring that you have logged into your server as the root user, or, if need be, add the sudo command to any of the statements that you need to execute below. I am going to be using the user named “chris” in the directions below. Make sure you use the correct user name for the user on your own system! The users in this case are also all under the /home directory, make changes if yours are not.
It’s time to get started. Remember, we are going to allow the user “chris” to use its own php.ini file. All documents are served out of the /home/chris/public_html folder, so we are going to start by creating/editing our .htaccess file that is there. I prefer VIM, you can use your own editor of choice.
root@server [~] # vim /home/chris/public_html/.htaccess
Now, place the following code at the top of the file:
AddHandler php-fastcgi .php
Action php-fastcgi /cgi-bin/php.fcgi
What this does, is tell the web server to use the php5-fastcgi script to execute .php files. The next step, is to create that file, and place our configuration within it. We place this file within the cgi-bin folder of the users home directory.
root@server [~] # echo '#!/bin/sh' >> /home/chris/public_html/cgi-bin/php.fcgi
root@server [~] # echo 'exec /usr/local/cpanel/cgi-sys/php5' >> /home/chris/public_html/cgi-bin/php.fcgi
This places the correct statement within the file. Because the file is being loaded from the /home/chris/public_html/cgi-bin/ folder, this is where it will look for a php.ini file. So, you would now want to place your customized php.ini file in the /home/chris/public_html/cgi-bin/ folder. A simple way to do this on a cPanel server, is to copy to existing one being used, then modify it after to your liking. To do this, run the following:
root@server [~] # cp /usr/local/lib/php.ini /home/chris/public_html/cgi-bin
Ok, permissions. Because we have done all of this as root, permissions for a couple of files may need modifying. The first one, is the script call itself. This is because this server is running SuExec, which executes scripts with the owners permissions. Because the file currently has root as the owner, it will flag up a 500 error. To fix this, we give it the owner and group of the user, as well as execute permissions:
root@server [~] # chown chris.chris /home/chris/public_html/cgi-bin/php.fcgi
root@server [~] # chmod 700 /home/chris/public_html/cgi-bin/php.fcgi
We might as well check the owner of that .htaccess file we created too:
root@server [~] # chown chris.chris /home/chris/public_html/.htaccess
Now, everything should be ready to go. How do we see if it worked? Easiest way, is to use phpinfo. Make sure of course, that this is not disabled in you php.ini file. Create a new file within your public_html folder of your website. Lets call it phpinfo.php. Within that file, place the following:
Once that is done, navigate your browser to the page you just created, and check the under the “Loaded Configuration File” section. It should state something along the lines of “/home/chris/public_html/cgi-bin/php.ini”. If so, your all set!
There are a few other ways to do this, including keeping the php.ini file outside the users directory. If you would like more information, please let me know.