Thursday, October 25, 2007

PHP Setting Configuration Through .htaccess

Many components in Joomla give you exciting functionalities to your website. Most components can be configured properly through the Joomla backend and you are set to use it. However, there are situations (quite often!) that demands configuration at the server level. There situations are generally solved by configuring the PHP setting.

To configure it properly, there are 2 things you have to know:
1) Unless you are the administrator of the server, you cannot directly edit the php.ini file which contains the PHP settings. The way to configure it is to do it throught the .htaccess method. At the root of your web directory (ie. /public_html or /httpdocs or /var/www/html or whatever), there is a file named .htaccess which Apache uses for configuring setting for your website to run. Yes, I said Apache... if you are using IIS, then my post is not going to help you. We will add changes to this file to configure the PHP settings.

2) Know what to change. You can't have a solution without knowing what's the problem, right? Common sense 101. Anyway, that's why I have prepared a list for you to check out if you experienced some of the common problems that most other people will encounter. Here it is:

Problem: Blank screen when Joomla loads up

Analysis: This is a huge one. There are hundreds of reasons why it is blank... database failed, website down, domain not pointing correctly, your internet connection is down, PHP coding errors, blah blah blah... However, Joomla will generally at least throw a notice/warning/error to you if it is loaded to your browser and some server PHP setting is set to NOT display any error messages. Hence, you can try this solution.

Solution: Add "php_flag display_errors on" to your .htaccess file. It will make the server throw error messages on the screen if there is an error with the codes.


Problem: Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 1200 bytes) in /public_html/components/whatever/phpthumb.functions.php on line 334

Analysis: This is a php error message. Note the particular file and line number. This message tells you that the code in the script needs more memory to run. Now, generally well-written scripts do not need extra memories to run. Note the word generally. There are cases where more memory is really needed such as loading and processing a huge file or database. My advice will be to google the problem to see if it is a configuration problem or something like before you use my solution below to increase your PHP memory allocation to the script.

Solution: Give more memory for your script to run. Add "php_value memory_limit 10M" to your .htaccess file. Change 10M to your desired amount. Care!


Problem: Uploading a large file causes problem

Analysis: This is from my own experience. For example, I am using the forum component Fireboard for one of my client's website and we need to let users upload a 5MB music files to the forum. 1MB music file is ok but > 3MB is a big problem. After some time trying to find the solution, I realized that file upload is limited by PHP setting. Hence, you would need to increase the file upload if you want users to upload a bigger size file.

Solution: Add "php_value upload_max_filesize 7M" and "php_value post_max_size 7M" to your .htaccess file. Put the 2 codes in separate lines (ie. don't put them in a single sentence/line). You can change 7M to your desired size.

An explanation here for the 2 codes: upload_max_filesize determine the maximum size of the file that can be uploaded to your server. post_max_size is the maximum size that your server can receive from a form post request. Hence, post_max_size is the sum of all post variables plus the file which means strictly speaking post_max_size should be larger than upload_max_filesize. However, most other post variables are small in size and we do not expect the uploaded file to be larger than 5MB (the limit is set in the Fireboard forum), we just leave the 2 values the same. If you don't understand what I am talking about, just use the code.


Once you are done adding the extra codes to .htaccess, upload them to your web directory and check it out. If it works, I am happy for you. =) There are various other PHP settings that you can use for the .htaccess and here's a short list of the common ones that I use for my Joomla websites:

php_flag register_globals off (Solves the Joomla backend error warning)
php_flag magic_quotes_gpc on (Solves the Joomla backend error warning)
php_value session.save_path '/tmp/' (Solves session problems - sometimes)

No comments: