PHP.ini / .htaccess. How to set them up

You may at times have a custom requirement for the php.ini file, however, the global php.ini file cannot be changed at will as it affects everyone on the server. You are able to create your own php.ini file so that you can override settings as needed.

For instance, Drupal (from about version 5.x) requires MBString to be turned off. However, MBString is a requirement for displaying foreign language characters, like Chinese.

The solution is an extremely simple 2 step process:

Step 1: Create your php.ini file

Create a file called php.ini (lower case) in the public_html directory of your account. Enter any info you need here.

So, for instance, in our Drupal example, we’d have a file called php.ini with mb_string turned off. For certain sites like wordpress, you may need to increase the upload file size limit. You may get the following error: The uploaded file exceeds the upload_max_filesize directive in php.ini, so you can increase that. You may also need to turn off settings like Magic Quotes = off

Add the following lines to php.ini:

php_flag mbstring.encoding_translation Off

upload_max_filesize = 16M
post_max_size = 18M
max_execution_time = 300
max_input_time = 300

magic_quotes_sybase = Off
magic_quotes_runtime = Off
magic_quotes_gpc = Off

That’s it for the php.ini file, BUT there is a problem. This php.ini file applies only to the public_html directory. You will need to copy this file to every directory (folder) on your site.

Fortunately, we can use .htaccess so that we DO NOT have to copy this file to all folders. Using .htaccess we can tell the entire website to use our php.ini file from public_html.

Step 2: Create your .htaccess file

In the same folder as your php.ini file, create a file called .htaccess (note the dot in front of the file name). In .htaccess, add the following code:

<IfModule mod_suphp.c>

suPHP_ConfigPath /home/username/public_html
<Files php.ini>

order allow,deny
deny from all

</Files>

</IfModule>

NOTE: the username part. That needs to be changed to the username of your account. You can see this in cpanel in the left nav under the heading: Home Directory (its usually the first 8 characters of your domain name.

The above code tells the server to use the php.ini file found in public_html. The deny from all line tells php to use this file, but not let anyone download it.

That’s it, your php should now work as you want it to!

Please post any questions in the comments section below.

 

facebook google twiter linkedin linkedin linkedin linkedin linkedin

Leave a Reply

Have a Quesion?

Questions? We're here. Send us a message!

Questions, issues or concerns? I'd love to help you!

Click ENTER to chat