I recently needed to increase the memory_limit, upload_max_filesize and post_max_size values to something higher than the measly amount set by a particular webhost's default php.ini file. This account was set up with cPanel and overriding the php.ini defaults the usual way (by adding various lines to the .htaccess or settings.php files) didn't work at all. If you've had the same experience, you may find that this how-to works for you.

This how-to is for any version of Drupal running on Linux and Apache. No Drupal modules are needed, but the devel module certainly helps.

1. Get and modify your custom php.ini file

It's best to use a php.ini file that somewhat resembles the one already running on your server. You can probably find one at /usr/local/lib/php.ini or /usr/local/Zend/etc/php.ini and just copy it into your account's web folder (it may be called "public_html" or "www" or "htdocs" -- you know the one).

Edit your php.ini file and save your changes. There are handbook pages on increasing memory and upload size if you need help on the syntax.

2. Creating your CGI script

Now create a small script and put it in your cgi-bin directory. In your web folder, create another folder called "cgi-bin" if it's not there already. Using your preferred text editor, create a file name "php.cgi" and put the following into it:

#!/bin/sh
exec /usr/local/cpanel/cgi-sys/php5 -c /path/to/drupal/

If you don't have cPanel on your account, try /etc/php5 instead. In any case, replace the /path/to/drupal/ part with the full path to your Drupal installation, such as /home/youraccount/public_html/ or /home/youraccount/public_html/drupal/

Since this is a script that needs to run as an executable file, use the chmod command and type this at the command line:

chmod -x php.cgi

Don't have shell access? That's outside the scope of this how-to, but you can use an FTP program that can change permissions of files, ask your webhost to make the php.cgi script executable for you, or make it executable with some PHP code (perhaps with the devel module's Execute PHP block).

3. Modifying your .htaccess file

Add this one line to your .htaccess file:

Action application/x-httpd-php5 /cgi-bin/php.cgi

This tells Apache to perform an action each time it encounters a file of file type "application/x-httpd-php5" -- if you're using a version of PHP older than PHP5, you'll need to change this to "application/x-httpd-php" (or upgrade to PHP5 -- ask your webhost).

Finally, scroll down to the part that says "# Protect files and directories from prying eyes" and add "ini|" to the <FilesMatch> directive so it looks something like:

<FilesMatch "(\.(ini|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.p        hp)?|xtmpl)|code-style\.pl|Entries.*|Repository|Root|Tag|Template)$">
  Order allow,deny
</FilesMatch>

Notice the "ini|" on the first line? This will prevent your custom php.ini file (and anything else ending in .ini) from being accessible to the entire world. (The character after the "ini" part is a pipe, by the way, not an L.)

4. Test your site

Load up your website and see if it works. If it does, good job! If it doesn't, go back to step 2 or 3 and check to see what version of PHP your site is using. You can also ask your webhost for help.

If you increased your post_max_size or upload_max_size, then look at your admin/settings/uploads admin page and see how big your uploads can be. If you increased your memory_limit, check phpinfo or devel module's phpinfo() page at admin/logs/status/php

Comments

graper’s picture

I run many sites on cpanel powered servers and I've found that just have the php.ini file in the drupal folder itself is enough to allow the php scripts in that folder to run properly with the new settings. I have my cpanel servers setup with SuPHP, so there may be differences if setup with FAST-cgi or some other setting other then SuPHP.

I found it strange that my custom php script in a different folder that does nothing but phpinfo() pulls up the default settings of the server while the system info module pulls up the new settings, obviously because of the main index.php being the running script

Granville
Kirkham Systems

karolus’s picture

One of my clients (multisite presence) also has a Web host using cPanel.

All I needed to add with a php.ini file with the following code:

upload_max_filesize = 50M
post_max_size = 50M

Logged into site immediately after posting, all was good to go...

EllECTRONC’s picture

Thank you very much!

In php.cgi I used:

#!/bin/sh
exec /usr/local/bin/php -c /home2/mysite/public_html/

and /usr/local/bin/php i.e path to php we can find in php.ini or via phpinfo() (all the same)

But I can't use command line and I've set chmod to 111. It's right? Or it will be enough 110?

zoo’s picture

Hello,

is this workaround supposed to work with fastcgi too?
Because I tried, but the php.ini doesn't load (with cgi instead it works). Thanks

cgi-bin/php.cgi

#!/bin/sh
exec /usr/local/cpanel/cgi-sys/php5 -c /path/to/drupal/

/.htaccess

Action application/x-httpd-php5 /cgi-bin/php.cgi
zoo’s picture

Hello bibstha,

Thank You very much for your link ( http://bibekshrestha.com.np/blogs/post/2010/02/05/using-custom-phpini-cp... ).

I just tried to tweak the .ini file as suggested. I took these steps:

1. copied the "/usr/local/lib/php.ini" to the "public_html/cgi-bin" directory
2. created a custom "php.cgi" executable file, within the "public_html/cgi-bin" folder (chmod 777)
3. changed the root .htaccess file, with the "action" line at the very top.

Nonetheless, visiting the info.php file it keeps saying "Loaded Configuration File: /usr/local/lib/php.ini" instead of "Loaded Configuration File: /home/[username]/public_html/cgi-bin/php.ini".

Any ideas? Thank You again for sharing your solution.

bye