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.
FastCGI & Custom php.ini file per user
Found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later .. Big thanks for the useful info i found on Xplor Digital BLog | Web Development Blog.
I keep receiving the following error after following the steps noted.
[Thu Mar 18 17:30:49 2010] [error] [client 184.108.40.206] suexec failure: could not open log file
[Thu Mar 18 17:30:49 2010] [error] [client 220.127.116.11] fopen: Permission denied
[Thu Mar 18 17:30:49 2010] [error] [client 18.104.22.168] Premature end of script headers: php.fcgi
You are correct, I have updated the post. You need the shebang to tell the script which interpreter to use.