Individual php.ini files for PHP FCGI and PHP CGI

Miraenda

Well-Known Member
Jul 28, 2004
243
5
168
Coralville, Iowa USA
cPanel Access Level
Root Administrator
Since I figured these might be helpful to people running either FCGI or CGI handlers on PHP, I'm going to post my guides that I have on my own forum here. If this is in the wrong spot for such guides, please feel free to move them. I hope these will be helpful :)

PHP FCGI Individual php.ini File

The following steps will allow a custom php.ini file on an account when using FCGI as the PHP handler.

Copy and Edit default php.ini

Code:
cd /home/user/public-html/cgi-bin
cp /usr/local/lib/php.ini /home/user/public_html/cgi-bin
vi php.ini
As a test change one of the variables like register_globals from:

Code:
register_globals = Off
To:

Code:
register_globals = On
If yours was On, then do the reverse. This is simply to test it changes from the global php.ini file.

Create php.fcgi file to load custom php.ini

Code:
vi php.fcgi
Please note that you should still be in /home/user/public_html/cgi-bin location.

Put into file:

Code:
#!/bin/sh
export PHP_FCGI_CHILDREN=1
export PHP_FCGI_MAX_REQUESTS=10
exec /usr/local/cpanel/cgi-sys/php5
Please note the path to php5 is due to using PHP5 on this system. If you are using php4, it might be /usr/local/cpanel/cgi-sys/php4 or some other path. Check /etc/httpd/conf/php.conf to see what it shows for cgi-sys path for your version of PHP.

Save file, then made executable:

Code:
chmod +x /home/user/public_html/cgi-bin/php.fcgi
Change ownership of files to correct user:

Code:
chown -R user:user /home/user/public_html/cgi-bin/
Edit .htaccess to point to php.fcgi wrapper

Code:
cd /home/user/public_html
vi .htaccess
Put at top of file:

Code:
AddHandler php5-fastcgi .php
Action php5-fastcgi /cgi-bin/php.fcgi
Again, here php5 is listed.

Add paths to php.conf file

Add the following lines to /usr/local/apache/conf/php.conf file:

Code:
Action php5-fastcgi /cgi-bin/php.fcgi
AddType application/x-httpd-php .php
Yours will probably look similar to the following after those lines are added:

Code:
# Fastcgi configuration for PHP5
LoadModule fcgid_module modules/mod_fcgid.so
MaxRequestsPerProcess 500
AddHandler fcgid-script .php5 .php4 .php .php3 .php2 .phtml
Action php5-fastcgi /cgi-bin/php.fcgi
AddType application/x-httpd-php .php
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php5
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php4
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php3
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .php2
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .phtml
Distill and rebuild Apache, then restart Apache

Run these commands:

Code:
/usr/local/cpanel/bin/apache_conf_distiller --update
/scripts/rebuildhttpdconf
/etc/init.d/httpd restart
Load phpinfo page on account

Code:
cd /home/user/public_html
vi php.php
In file put:

Code:
<?php
phpinfo();
?>
Change ownership to the user:

Code:
chown user:user php.php
Load the file at http://domain.com/php.php to see the results.

Your phpinfo file should now show register_globals changed as well as have this at the top defining the new php.ini path:

Code:
Loaded Configuration File /home/user/public_html/cgi-bin/php.ini
-----
PHP CGI Individual php.ini File

The following steps will allow a custom php.ini file on an account when using CGI as the PHP handler.

Copy and Edit default php.ini

Code:
cd /home/user/public-html/cgi-bin
cp /usr/local/lib/php.ini /home/user/public_html/cgi-bin
vi php.ini
As a test change one of the variables like register_globals from:

Code:
register_globals = Off
To:

Code:
register_globals = On
If yours was On, then do the reverse. This is simply to test it changes from the global php.ini file.

Create php.cgi file to load custom php.ini

Code:
vi php.cgi
Please note that you should still be in /home/user/public_html/cgi-bin location.

Put into file:

Code:
#!/bin/sh
/usr/local/cpanel/cgi-sys/php5 -c /home/user/public_html/cgi-bin/
Please note the path to php5 is due to using PHP5 on this system. If you are using php4, it might be /usr/local/cpanel/cgi-sys/php4 or some other path. Check /etc/httpd/conf/php.conf to see what it shows for cgi-sys path for your version of PHP.

Save file, then made executable:

Code:
chmod +x /home/user/public_html/cgi-bin/php.cgi
Change ownership of files to correct user:

Code:
chown -R user:user /home/user/public_html/cgi-bin/
Edit .htaccess to point to php.cgi wrapper

Code:
cd /home/user/public_html
vi .htaccess
Put at top of file:

Code:
Action application/x-httpd-php5 /cgi-bin/php.cgi
Again, here php5 is listed. If your PHP is different, use the Action listed for your version of PHP in /etc/httpd/conf/php.conf file.

Load phpinfo page on account

Code:
cd /home/admin/public_html
vi php.php
In file put:

Code:
<?php
phpinfo();
?>
Change ownership to the user:

Code:
chown user:user php.php
Load the file at http://domain.com/php.php to see the results.

Your phpinfo file should now show register_globals changed as well as have this at the top defining the new php.ini path:

Code:
Loaded Configuration File /home/user/public_html/cgi-bin/php.ini
 

lanquest-it

Member
Apr 9, 2008
10
0
51
Almost a year on and I cannot believe this is still an issue when installing or upgrading this product.

I've just posted a link to this workaround here
/http://forums.kayako.com/threads/sorry-fusion-cant-be-installed-checking-if-magic-quotes-gpc-is-disabled.23644/#post-124861 (Sorry! Fusion can't be installed - Checking if magic quotes GPC is disabled | Kayako Community Forums)

With PHP 5 evolving, why are we still going through this pain?. :confused:
 

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
I don't understand the request being made. Kayako isn't a cPanel product, so if this concerns Kayako and what it requires, then this would need to be taken up with their support system. Likewise the fact CGI and FCGI do not default allow individual php.ini files would not be the fault of cPanel itself, but the very essence of how these PHP handlers work. Any system using CGI or FCGI PHP handlers (not just cPanel) will have to configure individual php.ini files in a similar manner. Now, the options I provided to enable individual php.ini files under CGI or FCGI do work provided the hosting provider hasn't restricted them from being used.

There are many hosts who use suPHP, which has a more lax system for allowing individual php.ini files, so you could certainly try to find a server using suPHP instead.

Also, of note, when I say "I provided" above, this thread was originally made using my non-staff account at the time, so I was the original poster (to avoid any confusion).
 

Pooch

Registered
Jul 26, 2011
1
0
51
Thanks for the guide. Very helpful. One thing worth mentioning... if you have suExec enabled it's particular about the permissions of the cgi-bin directory and scripts. Do a chmod -R 755 cgi-bin. If you get a 500 error like I did, check your permissions, and remember to check your logs too.
 

juliovedovatto

Registered
Aug 20, 2011
3
0
51
Great artcile, contratz.

You know what would be great? A guide discribing how to make cpanel create these files automatically when a New Account or Subdomain was created in CPanel. This will avoid problems like this:

Not Found

The requested URL /cgi-bin/php.fcgi/foo.php was not found on this server.
This error is generated because I created New Account and not created the specified fastcgi files.

Where I can find a way to make the creation of php.fcgi/php.ini into user's cgi-bin directory automated?
 

syndicated

Member
Jun 1, 2010
14
0
51
Okay I followed this exactly to the tee and spend about 5 hours trying to understand why it would not WORK. I even put in a support ticket and Cpanel support could not figure it out either.

So here are some CRITICAL STEPS MISSING from the above tutorial.

First the ACCOUNT itself must be a NEW account and you MUST ENABLE CGI-ACCESS from the SETTINGS.

If Cgi is not enabled in the account setup then you will get SUPHP and many other errors.

Second - the permissions and group permissions must be PERFECT. The first account I tried this on I did from
a backup from another server and it would not work. So if you

1. Create a new account and choose ENABLE on CGI Privilege
2. Make sure to +755 the pgp.fcgi
3. Make sure your groups are OK

Then it works 100% every time and ive messed this up for about 20 hours so I have fiddled with every possible variable.

Its funny how 1 stupid setting can RUIN your whole day - even Cpanel support didn't think to check that stupid CGI permission in the account page. So before you kill yourself in frustration make sure its ON.

Thanks to the support forums, we seem to solve more crap then staff sometimes!
 

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
If you are changing something from the default settings, then you should try to note this in the ticket or on the server. Unchecking CGI on account creation is not something done very often. Since you are trying to create CGI-based files for the individual php.ini files to work for FCGI, then it's pretty important to note you've made that change to someone, I agree.
 

syndicated

Member
Jun 1, 2010
14
0
51
Yes but when you make a packages you choose many options, OTHER then doing this process, you NEVER need cgi-access so enabling it is just an extra security risk you don't need. I ran a server for 3 years under FCGI never needing it. I would never think to go back and check something I set years ago in WHM. And though everyone bashed their heads for hours to fix it nobody thought of it. Supports recommendation was to abandon the idea and undo any changes. One of the things you do when you debug is backstep over all possible reasons things don't work, but because this one is not on the list, its very easy to overlook it. Its like a hidden kill switch buried way back, and unless someone warns you, you can suffer the same fate as me. If it wasn't for my refusal to let it go, and continued experiments on my own, I would have failed. I doubt you could think of a single other item that effects this OTHER then that lol. There is nothing, its the last piece. Also support has full WHM access which is what they ask for. And the errors that get thrown have nothing to do with CGI enabled. There is no nice warning - SORRY CGI DISABLED or anything remotely like that. Maybe its something you want to add to the next update, often when features are disabled the errors contain a hint.
 

tylerl

Active Member
Dec 11, 2009
28
0
51
Here's a simpler solution:

Edit the file called /usr/local/cpanel/cgi-sys/php5 -- insert the line above exec /usr/bin/php as follows:

Code:
#!/bin/sh

# 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 ~/php.ini ] && exec /usr/bin/php -c ~/
exec /usr/bin/php
Copy the file over to /var/cpanel/conf/apache/wrappers/php5 as indicated at the top of the file.

Now, if any account has a php.ini file in their home directory (not public_html) then it will be used instead of the system-provided one.
 

mikelduke

Registered
May 15, 2013
1
0
1
cPanel Access Level
Root Administrator
Hi Tylerl,

Thank you for your solution...
If I can find the var/cpanel/conf/apache/ I do not have /wraappers/ do I have to create it?? and put the php5 file inside this folder?

Is that the line we have to add in the file ?
"
[ -f ~/php.ini ] && exec /usr/bin/php -c ~/
"
Thank you

Frank
 

DavidR

Well-Known Member
Feb 25, 2003
177
0
166
Here's a simpler solution:

Edit the file called /usr/local/cpanel/cgi-sys/php5 -- insert the line above exec /usr/bin/php as follows:

Code:
#!/bin/sh

# 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 ~/php.ini ] && exec /usr/bin/php -c ~/
exec /usr/bin/php
Copy the file over to /var/cpanel/conf/apache/wrappers/php5 as indicated at the top of the file.

Now, if any account has a php.ini file in their home directory (not public_html) then it will be used instead of the system-provided one.

Does this work with FCGI?
 

grvulture

Registered
Nov 4, 2009
4
0
51
Yes, it does work with FCGI. Just tried it, and it's a much simpler solution indeed
Where with the original solution I was getting 500 Internal Server error on all php pages.
 

johnzena

Registered
Oct 13, 2011
1
0
51
cPanel Access Level
Website Owner
If you don't have a wrappers directory or php5 file just create them. The file doesn't have an extension php5 not php.txt .htm .bin etc just php