Saturday, October 4, 2008

Php value configuration in .htaccess

Introduction

There are some php language settings that must be set for phpWebSite to function correctly. It is not our job here to survey every php runtime configuration setting. That is the job of the php on-line manual http://us3.php.net/manual/en/configuration.php. What this part of the guide strives to do is show you the settings that may make most new phpWebSite installations stumble when they are not set correctly. You must adjust these settings in the php.ini file. This is the primary place to make the changes. There are several other ways to make these changes should your site be supported by a web hosting company. Knowledge of these settings is helpful when you have to raise a support ticket with your selected web hosting company or if you have a Linux admin supporting you. It may be helpful to ask about these paramenters in a pre-sales question with a potential web hosting company. Their answers may help you decide to choose another company.

There are three ways to set these parameters. One is the php.ini file. A typical Linux distribution will place this file in /etc/php.ini,. The second way to set these values is by using the Apache .htaccess file that you can store in your phpWebSite Root directory. The third and final way to set these parameters is programmatically through the phpWebSite Core.php file. The file can be found in phpWebSite Root/core/Core.php.

Missing image
Warning.png
Warning

You should decide on one of the three different ways of setting these values. You will have to use your best judgement because each php runtime environment that phpWebSite can be installed in has its own variations. Take these settings as a general guideline for how to configure php in your web server's environment.

php.ini

In this section the runtime configuration parameters are examined in depth as far as phpWebSite is concerned.

Missing image
Caution.png
Caution

Note that setting the configuration parameters in the php.ini file will affect all the other php applications on the same server. A missconfiguration in one of these values will torpedo your whole site not to mention phpWebSite.

Missing image
Warning.png
Warning

As noted with the other two ways of making these changes you should only pick one of these and use it alone.

include_path

This is one of the most important configuration parameters to set for phpWebSite. Please review the phpWebSite Onion. phpWebSite implementations use include_path to set the php language path to include the phpWebSite Root/lib/pear library that is shipped with each phpWebSite release. You will want to edit the include_path , if you are having problems.

Missing image
Caution.png
Caution

Most new users struggle with this at first. An example error might be: "When I get to Step 4 to load http://www.yourdomain.edu/setup, I get the following error: "

Warning: main(../core/Core.php) [function.main]: failed to create stream:
Permission denied in /user2/rwj/public_html/phpws/setup/index.php on line 9

Fatal error: main() [function.main]: Failed opening required '../core/Core.php'
(include_path='.:/usr/local/php-4.3.1/lib/php') in /user2/rwj/public_html/phpws/setup/index.php on line 9

There are three things that are useful to have in the include_path. They are

* The dot ( . ) directory. The dot directory is also known as the current working directory.
* The pear library path that you will be using with your phpWebSite implementation. This will be phpWebSite Root/lib/pear/. As noted in other places, phpWebSite Root will vary from site to site depending on how you are hosting phpWebSite.
* The location of all the standard php system code. This could be /usr/lib/php or if your site is in transition to another version of the php language, then the site administrator may have installed your __stable__ version of the php language as /usr/lib/php4 . Putting all these considerations together your path should look something like the following. Also note that a colon ( : ) is required to separate all the values that we need assign to the include_path setting.

include_path = ".:phpWebSite Root/lib/pear/:/usr/lib/php4"

Please see http://us3.php.net/manual/en/ini.sect.path-directory.php#ini.include-path for more information.

The "failed to create stream: Permission denied" error message in the above example is the perfect segway into the session.save_path setting. Misconfigured settings for both the include_path and session.save_path settings may harmoniously blow up in your face together.

session.save_path

Session information is created for each person that hits your site. You should handle this variable with security concerns in mind. That being said the session.save_path can have a wide range of values. These may range from a simple directory like /tmp to special directories like /var/lib/php/session depending on your Linux distribution's philosophy. A web hosting company may create a special directory for this depending on their philosophy. A web hosting company's automated account creation scripts may place this directory some where in your account's web files directory.

Additional ideas govern the values stored in this setting. Let's say you set session.save_path to /tmp. Well you need to make sure the /tmp directory is located on a disc drive that has space. In addition, this directory should be writable by the web server. Periodic maintenance is also required on this directory. If you have a bizillion files in this directory, php may experience problems. This is not just a php issue. Other applications can perform poorly, if the temporary directory used to save working files is full of old files. After noting security concerns, space limitation, the php application mix on your server, and your determination to monitor the number of files in the directory that you are going to set this value to, then you may decide on /var/lib/php/session as the correct value.

session.save_path = /var/lib/php/session

Please see http://us3.php.net/manual/en/ref.session.php#ini.session.save-path for more information. The session.save_handler parameter may also come into play http://bugs.php.net/bug.php?id=25876. You may also want to consult your Linux distribution's documentation.

phpWebSite failed a session test. If your PHP installation is not configured to allow sessions,
you will not be able to run phpWebSite. Please check your session path in your php.ini file
and make sure your version of php supports superglobals(greater than version 4.1.2)


Missing image
Note.png
Image:Note.png

Note that only one of many session variables have been covered. Some of the other session.* settings may help you resolve problems. The related parameters all start with session. Hence, the session.* notation. A few of these session problems are may be answered in the Session Related Issues page. An example session configuration error message related to incorrect session.* settings is shown above. Special thanks go to sourceForge user akumaish for posting a response to this error message in the phpWebSite forums

Still having problems? Please review this short summary of trouble shooting ideas that were covered in this section.

1. Have you configured session.save_path to begin with in your php.ini file?
2. Do you have the correct directory for Linux or MS Windows? /tmp or C:\tmp.
3. Does the directory exist? /tmp or C:\tmp
4. Does the directory have read and write permissions for your webserver software--apache--to the temporary directory created for the session files?
5. After many days, weeks, of months of use, have you deleted old session files?
6. Do you have enough disk space available for the session files? If the disk drive where your temporary file is located runs out of space, then you will receive the "failed a session test" error. Having no disk space is just like not having created a temporary directory to store the files or having the correct permissions on the directory.


Path Summary

So far several sections of this page have covered information about correctly setting paths. Problems can arise on a correctly configured and working phpWebSite installation. Typical problems occur when key server software are upgraded. Typically the php.ini settings that cover paths that have been configured above should be rechecked if your site starts experiencing problems. Here's an example from a real life forum post:

* "I have changed my site URL but I now get an error message telling me the file is not in the "allowed path(s)." The error message is surprising as the path so far as I know has been removed. The conf/config.php file has been correctly modified. There must be some place that the script is still getting the old path information from. Can anybody help me figure where the next change should be?" (https://sourceforge.net/forum/message.php?msg_id=3824049)

* One proposed solution looked at the openbasedir php configuration settings. (https://sourceforge.net/forum/message.php?msg_id=3824145)

* Even though the openbasedir setting was not the answer, the answer provided a catalyst for a possible place to look: "The 'allowed paths' message refers to the hard-coded paths in your php.ini file. The php.ini settings were created initially by a wizard set-up by the ISP. The ISP allows me access to edit these settings as I'm running PHP in CGI mode. The "allowed path(s) problem is all sorted now, so thanks for the spark needed to solve the problem!" (https://sourceforge.net/forum/message.php?msg_id=3824224)

This real life story provides an example of how your functioning phpWebSite can start failing on you for no apparent and obvious reason. If your phpWebSite becomes important to you or others, then this is the time to look at both the Trouble Shooting With phpInfo page and Planning The Installation page. Write the settings of your site down now in a disciplined fashion before the installation, during the installation, or immediately after the installation. The information may not look important now but may be vital to your site's recovery when your Web hosting company pulls the rug out from under you! In this example, these two configuration pages would provide you with a before the problem occurred configuration and after the problem started configuration page. The "after the problem started configuration page" would be developed by using phpInfo.

Special thanks go to SourceForge user mb__mart, Malcolm, for posting back information on the solution to the "allowed path(s)" problem.

memory_limit

The Memory section of the Official Requirements part of this document provides some sample values. After you consult the memory table you may decide on 12M as the correct value. You would then edit your php.ini with this value.

memory_limit = 12M

Trial and error may be required to find the right value depending on the limitations of your phpWebSite installation environment. As noted in the memory table the number of modules that you Boost with the core phpWebSite system will greatly affect how this parameter should be set. If the value in the memory_limit parameter is set too low, then you will receive fatal runtime error messages

Fatal error: Allowed memory size of m bytes exhausted (tried to allocate e bytes) in Unknown on line n

* where m is the the memory limit the was exhausted.
* where e is the number of bytes that the memory limit was exceed by.
* where n is the line number in the phpWebSite system code where the php memory limit setting was exhausted.

The author of this section, Dr kludge, has had to use 16M so that the php squirrel mail application would function properly. Additional information can be found in the phpWebSite official FAQ http://phpwebsite.appstate.edu/index.php?module=faq&FAQ_op=view&FAQ_id=18. Please also see http://us3.php.net/manual/en/ini.sect.resource-limits.php#ini.memory-limit for more information.

max_execution_time

There are times when you will need to adjust max_execution_time. There have been times in past phpWebSite release cycles that this value had to be increased. Start with the default value of 30 first. Some users have set this as high as 120.

max_execution_time = 30

Please see http://us2.php.net/manual/en/ref.info.php#ini.max-execution-time for more information.

max_input_time

There are times when you will need to adjust max_input_time. Start with the default value of 60 first.

max_input_time = 60

Please see http://us3.php.net/manual/en/ref.info.php#ini.max-input-time for more information.

etc.magic_quotes_runtime

This is a setting required on some Linux distributions. You may need to set this value to Off if it was set to On.

etc.magic_quotes_runtime = Off

Please see http://phpwebsite.appstate.edu/index.php?module=faq&FAQ_op=view&FAQ_id=187 or http://us3.php.net/manual/en/ref.info.php#ini.magic-quotes-runtime for more information.

safe_mode_allowed_env_vars

This may be a useful setting when safe mode is set. You may need to set this value to TZ if you are having problems with safe_mode operation of your phpWebSite system.

safe_mode_allowed_env_vars = TZ

Please see http://us4.php.net/features.safe-mode or http://us4.php.net/manual/en/features.safe-mode.php#ini.safe-mode-allowed-env-vars for more information.

Restart The Web Server

I'll put this important reminder in the php.ini section. Anytime that you make a /etc/php.ini setting change, then you must restart the web server. Now friends and countryman, your author, would like to save you some embarassment. Yes friends there have been times when yours truely has made changes to the /etc/php.ini and has been pounding his head against the wall. The php.ini file changes were not working as advertised. Not only was he angry X-( and red faced :o he now can share those wonderful stories from the school of hard knocks with you. :\ Anytime that you make changes to your /etc/php.ini file and your web service is running, please issue this command.

su - root -c "/etc/rc.d/init.d/httpd restart"

Yep. The sample restart command is based off of a Red Hat Linux distribution. You should be able to find how to perform this command on your Linux distribution based on the example given. Missing image
Note.png
Note

Note: You will not have to perform this step for the other two php language environment configurations. Those options are dynamic because each option is evaluated each time your site is accessed.

Apache .htaccess File

This is the second example of how to configure your php language environment.

Missing image
Warning.png
Warning

As noted with the other two ways of making these changes you should only pick one of these and use it alone.

Normally, the Apache web server uses an httpd.conf file to store all of its settings. A Linux distribution may create the file in /etc/httpd/conf directory. This file and the rest of the /etc directory contents are off limits to most users on a Linux system except for the root user. The Apache software development team has created another mechanism to allow a user in a web hosting company supported environment to change some of the web server settings. This flexibility may be turned off or on as the web hosting company sees fit. Speaking of flexibility, an .htaccess file comes with a cost. The .htaccess file has to be parsed by the Apache web server each time a file is served out of the directory covered by one of these files. This may be be the perfect solution for configuring the php language when a web hosting company is involved. On the one hand, they will not let you edit the /etc/php.ini file. On the other hand, the WebHostingCompany may not let php application code use the ini_set(); function. So we will happly embrace the performance hit so that we can access all the power that the phpWebSite CMS provides our new site.

include_path

php_value include_path ".:PhpWebSiteRoot/lib/pear/:/usr/lib/php4"


session.save_path

php_value session.save_path "/tmp"


memory_limit

php_value memory_limit "12M"


max_execution_time

php_value max_execution_time "30"


max_input_time

php_value max_input_time "60"


etc.magic_quotes_runtime

php_value etc.magic_quotes_runtime "Off"


safe_mode_allowed_env_vars

php_value safe_mode_allowed_env_vars "TZ"


A Sample .htaccess File

php_value arg_separator.output "&"
php_value arg_separator.input "&"
php_value include_path ".:PhpWebSiteRoot/lib/pear/:/usr/lib/php4"
php_value session.save_path "/tmp"
php_value memory_limit "12M"
php_value max_execution_time "30"
php_value max_input_time "60"
php_value etc.magic_quotes_runtime "Off"


Programmatically Setting php.ini Values

This is the third and last example of how to configure your php language environment.

Missing image
Warning.png
Warning

As noted with the other two ways of making these changes you should only pick one of these and use it alone.

Information on the ini_set() function is located here http://us3.php.net/manual/en/function.ini-set.php. The php.ini section went into the details on each of these values. The only difference here is how the value is changed. For example memory_limit = 12M in the php.ini section will not be coded as ini_set("memory_limit", "12M");. You will want to add these ini_set() function calls to the Core.php program file. phpWebSite will dynamically set these values during the execution of the Core.php file. You can find this file in here phpWebSite Root/core/Core.php. The file may be all that you will have to modify when configuring the php language. Missing image
Note.png
Note

Note dynamic settings proposed in this section of the document may not be supported on your web server.

include_path

The only real comment to make here concerns the PHPWS_SOURCE_DIR part of the path. The Access The Setup URL section talks about the initial configuration file. As part of entering data in this first screen you will provide the phpWebSite Root directory information. For all intents and purposes the phpWebSite Root information that you supply via the setup screen will be stored in the PHPWS_SOURCE_DIR variable. The rest of the phpWebSite system will use this value. All of the code will function properly or die splendidly based on your thoughtful understanding of what phpWebSite Root means for your site. __Your judgement is crucial here!__

ini_set("include_path", ".:" . PHPWS_SOURCE_DIR . "lib/pear/:/usr/lib/php4");


session.save_path

ini_set("session.save_path", "/tmp");


memory_limit

ini_set("memory_limit", "12M");


max_execution_time

ini_set("max_execution_time", "30");


max_input_time

ini_set("max_input_time", "60");


etc.magic_quotes_runtime

ini_set("etc.magic_quotes_runtime", "Off");


safe_mode_allowed_env_vars

ini_set("safe_mode_allowed_env_vars, "TZ");


-------------

No comments:

Post a Comment