Custom PHP.ini tips and tricks Updated: Feb 05, 07 | Custom PHP.ini tips and tricks
« Fight Blog Spam with ApacheRare XHTML elements use for SEO »
Describes in exhaustive detail how to change configuration settings and implement a custom php.ini file for use with the Apache Web Server.
### RUN PHP AS APACHE MODULE ### AddHandler application/x-httpd-php .php .htm ### RUN PHP AS CGI ### AddHandler php-cgi .php .htm ### CGI PHP WRAPPER FOR CUSTOM PHP.INI ### AddHandler phpini-cgi .php .htm Action phpini-cgi /cgi-bin/php5-custom-ini.cgi ### FAST-CGI SETUP WITH PHP-CGI WRAPPER FOR CUSTOM PHP.INI ### AddHandler fastcgi-script .fcgi AddHandler php-cgi .php .htm Action php-cgi /cgi-bin/php5-wrapper.fcgi ### CUSTOM PHP CGI BINARY SETUP ### AddHandler php-cgi .php .htm Action php-cgi /cgi-bin/php.cgi
in root .htaccess
SetEnv PHPRC /location/todir/containing/phpinifile
Place your php.ini file in the dir of your cgi’d php, in this case /cgi-bin/
htaccess might look something like this
AddHandler php-cgi .php .htm Action php-cgi /cgi-bin/php5.cgi
Create a wrapper script called phpini.cgi to export the directory that contains the php.ini file as PHPRC
#!/bin/sh export PHPRC=/home/site/askapache.com/inc exec /user/htdocs/cgi-bin/php5.cgi
In your .htaccess or httpd.conf file
AddHandler php-cgi .php Action php-cgi /cgi-bin/phpini.cgi
NOTE: Custom PHP.ini with FastCGI on DreamHost
You will have a shell wrapper script something like this:
#!/bin/sh export PHP_FCGI_CHILDREN=3 exec /user/htdocs/cgi-bin/php5.cgi
Change To
#!/bin/sh export PHP_FCGI_CHILDREN=3 exec /user/htdocs/cgi-bin/php.cgi -c /home/user/php.ini
NOTES:
open_basedir = ${open_basedir} ":/new/dir"SetEnv PHPRC /home/user1export PHPRC=/home/user1/home/user1/htdocs/cgi-bin/php.cgi -c /home/user1/php.ini$ php -c /custom/directory/custom-file.ini my_script.phpC:\windows or C:\winntFrom: PHP Runtime Configuration
NOTE: The Apache web server changes the directory to root at startup causing PHP to attempt to read php.ini from the root filesystem if it exists. If php-SAPI.ini exists (where SAPI is used SAPI, so the filename is e.g. php-cli.ini or php-apache.ini), it’s used instead of php.ini. SAPI name can be determined by php_sapi_name(). You can use also use the predefined PHP_SAPI constant instead of php_sapi_name()
If your server is running Windows click here
Specific to Powweb, but can be used elsewhere. In the folder above the htdocs (your ROOT) for the domain you want a custom php.ini file for, create an htaccess file with the following content
SetEnv PHPRC /home/users/web/bEXAMPLE/pow.EXAMPLE
Then create a blank php.ini also in your ROOT directory (/home/users/web/bEXAMPLE/pow.EXAMPLE). Next copy the powweb php.ini text to your php.ini file and customize it.
You can test to make sure you are using the new php.ini by running phpinfo(); If you want multiple php.ini files, then use .htaccess files to set the PHPRC variable to the directory that the php.ini file you want to use is in.
. |-- site1.com | `-- htdocs | | |-- cgi-bin | | | `-- dl.cgi | | `-- index.html | |-- phpsessions | |-- php.ini | `-- .htaccess |-- site2.org | `-- htdocs | | |-- cgi-bin | | | `-- dl.cgi | | `-- index.html | |-- phpsessions | |-- php.ini | `-- .htaccess `-- site3.net `-- htdocs | |-- cgi-bin | | `-- dl.cgi | `-- index.html |-- phpsession |-- php.ini `-- .htaccess
Remember to chmod 640 all .htaccess files, chmod 600 your php.ini files, chmod 600 your php flies, and chmod 705 your cgi scripts.. if you don’t want ftp users to be able to change the file than chmod 400.
If one of those is a show-stopper for you, you can easily switch to running PHP as an Apache module and not CGI, but be prepared for a bunch of potential security and ease-of-use issues! If you don’t know what any of these drawbacks mean, you’re fine just using the default setting of PHP-CGI and not worrying about anything!
When using PHP as an Apache module, you can also change the configuration settings using directives in Apache configuration files (e.g. httpd.conf) and .htaccess files. You will need “AllowOverride Options” or “AllowOverride All” privileges to do so.
With PHP 4 and PHP 5, there are several Apache directives that allow you to change the PHP configuration from within the Apache configuration files.
NOTE: With PHP 3, there are Apache directives that correspond to each configuration setting in the php3.ini name, except the name is prefixed by “php3_”.
php_value name value
Sets the value of the specified directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives. To clear a previously set value use none as the value.
NOTE: Don’t use php_value to set boolean values. php_flag (see below) should be used instead.
php_flag name on|off
Used to set a boolean configuration directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives.
php_admin_value name value
Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or virtualhost directives. To clear a previously set value use none as the value.
php_admin_flag name on|off
Used to set a boolean configuration directive. This can not be used in .htaccess files. Any directive type set with php_admin_flag can not be overridden by .htaccess or virtualhost directives.
<IfModule mod_php5.c> php_value include_path ".:/usr/local/lib/php" php_admin_flag safe_mode on </IfModule> <IfModule mod_php4.c> php_value include_path ".:/usr/local/lib/php" php_admin_flag safe_mode on </IfModule> <IfModule mod_php3.c> php3_include_path ".:/usr/local/lib/php" php3_safe_mode on </IfModule>
How to change configuration settings
When running PHP on Windows, the configuration values can be modified on a per-directory basis using the Windows registry. The configuration values are stored in the registry key HKLM\SOFTWARE\PHP\Per Directory Values, in the sub-keys corresponding to the path names. For example, configuration values for the directory c:\inetpub\wwwroot would be stored in the key HKLM\SOFTWARE\PHP\Per Directory Values\c\inetpub\wwwroot. The settings for the directory would be active for any script running from this directory or any subdirectory of it. The values under the key should have the name of the PHP configuration directive and the string value. PHP constants in the values are not parsed. However, only configuration values changeable in PHP_INI_USER can be set this way, PHP_INI_PERDIR values can not.
Regardless of how you run PHP, you can change certain values at runtime of your scripts through ini_set().
If you are interested in a complete list of configuration settings on your system with their current values, you can execute the phpinfo() function, and review the resulting page. You can also access the values of individual configuration directives at runtime using ini_get() or get_cfg_var().
add settings to a .htaccess file with ‘php_flag’ like this:
php_flag register_globals off php_flag magic_quotes_gpc on
In .htaccess, only true/false on/off flags can be set using php_flag. To set other values you need to use php_value, like this:
php_value upload_max_filesize 20M
PHP_INI_SYSTEM can be configured per-directory by placing it inside a per-directory block in httpd.conf
# Selectively enable APC for wildly popular directories # apc.enabled is Off in php.ini to reduce memory use <Directory /usr/local/apache2/htdocs/forum> php_flag apc.enabled On </Directory>
NOTE: In order for these settings to work in your htaccess file, you will need to add “Options” to your AllowOverride specifications for the directory/webserver if it’s not already allowed.
One of the most common reasons why you get 'No input file specified' (AKA 'the second most useful error message in the world') is that you have set 'doc_root' (in php.ini) to a value which is to the 'DocumentRoot' defined in the apache configuration. This is the same for other webservers. For example, on lighttpd, make sure the 'server.document-root' value is the same as what is defined as 'doc_root' in php.ini.
cgi.fix_pathinfo Provides real PATH_INFO/PATH_TRANSLATED support for CGI. PHP’s previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting this to 1 will cause PHP CGI to fix it’s paths to conform to the spec. A setting of zero causes PHP to behave as before. Default is zero. You should fix your scripts to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
cgi.force_redirect gi.force_redirect is necessary to provide security running PHP as a CGI under most web servers. Left undefined, PHP turns this on by default. You can turn it off at your own risk.
cgi.redirect_status_env If cgi.force_redirect is turned on, and you are not running under Apache or Netscape (iPlanet) web servers, you may need to set an environment variable name that PHP will look for to know it is OK to continue execution.
NOTE: Setting this variable may cause security issues, know what you are doing first.
fastcgi.impersonate FastCGI under IIS (on WINNT based OS) supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under. mod_fastcgi under Apache does not currently support this feature (03/17/2002) Set to 1 if running under IIS. Default is zero.
It was great reading the article.. very informative and neat..
Hi,
Nice Article. It pointed in the right direction. Just wanna ask how can i restrict users in changing “php_ini_system” directives if i set setenv phprc /user/home/directory/php.ini? And will only allow php_ini_all and php_ini_perdir directives?
@ Prasanna
Very cool. Thanks for that!
Hi,
I got this by adding a line PHPINIDir "conf" in httpd.conf file.
And created a directory "conf" under ServerRoot and place the file php.ini under that directory.
Now the default php.ini is getting override by the file(php.ini) which i kept under ServerRoot/conf directory.
Now i can able to load dynamic extensions in PHP through Apache module.
Thanks for your help.
Prasanna
Hi,
I am using PHP through Apache. I need to include dynamic extensions in PHP (ssh2.so).
All i need to know is how to specify the custom php.ini path while starting php through Apache.
Also i tried SetEnv PHPRC /home/users/web/bEXAMPLE/pow.EXAMPLE in .htaccess file and give All permission in httpd.conf file.
I am not clear in using PHPRC variable and how to use. Please clarify me
My versions:
Apache: 2.0.4.7
PHP:5.2.4
PHP5 CGI on top of PHP4 module was a really hard one. By searching around a bit, or quite a lot, I found finally this page “Custom PHP.ini tips and tricks“. Thanks guys, you really saved my [...]
hi,
Really very nice article.
You can find another one on the following link:
http://vijaymodi.wordpress.com/2007/03/03/phpini-settings/
Regards,
Vijay Modi
« Fight Blog Spam with ApacheRare XHTML elements use for SEO »
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, which lets you use/modify/re-post this content provided you follow the attribution guidelines in the license.
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. ~Tim Berners-Lee
Apache Software WebRing
Prev | Ring Hub | Join | Next
DCMI | XFN | XOXO 1.0 | hAtom | XDMP | GRDDL | Valid XHTML 1.1 | Valid CSS 2.1
W3C | WAI | © AskApache | License and Disclaimer | Terms of Service
It's very simple -Contact Webmaster | Glossary | Indianapolis, Indiana, U.S.A
you read the protocol
and write the code.
~Bill Joy
One of the most common reasons why you get ‘No input file specified’ (AKA ‘the second most useful error message in the world’) is that you have set ‘doc_root’ (in php.ini) to a value which is to the ‘DocumentRoot’ defined in the apache configuration. This is the same for other webservers. For example, on lighttpd, make sure the ’server.document-root’ value is the same as what is defined as ‘doc_root’ in php.ini.
In response to grange at club-internet dot fr: There are a couple of errors in the mod_rewrite directives given. I found that the following works:
RewriteEngine on RewriteCond %{ENV:REDIRECT_STATUS} !200 RewriteRule ^cgi-bin/php.cgi - [F]I removed the = from the RewriteCond and took out the leading / from the RewriteRule.
I have noticed that some people have noted that running PHP as a CGI program can run slowly compared with a compiled in module. Some have noted that they want to use FastCGI but are hesitant. I found that using the Apache 2’s CGID module was a great way to speed up performance almost to the same level as an “so”-installed PHP module but you get the added benefit of running each virtual host under it’s own user and group.
In my testing I got 44 pages per second using PHP as a module and I got roughly the same performance (within 5%) running PHP as a CGI program through CGID. CGID is also really easy to set up. Just add –enable-cgid to your Apache configure command and you’re good to go. Just set up PHP as a CGI normally. I’m sure that there’s extra RAM used for this method but RAM is as cheap as borscht anyways so it shouldn’t be a major factor when trying to speed up PHP CGI.
Here are my two cents of knowledge about php-cgi when running CGI script from prompt: If you get the “No input file specified.” error, create the environment variable “SCRIPT_FILENAME=C:\files\test.php”. If you get “Security Alert!” error and it tells you to create the REDIRECT_STATUS environment variable, it is because you have the SERVER_NAME variable set but not the REDIRECT_STATUS variable. Hence, if you have SERVER_NAME, you also need REDIRECT_STATUS, but not otherwise. And you pretty much should have SCRIPT_FILENAME at all time.
–enable-force-cgi-redirect won’t work in FastCGI mode : as of 4.3.10, it is only supported in CGI mode. However, you can achieve the same result with mod_rewrite under Apache :
RewriteEngine on RewriteCond %{ENV:REDIRECT_STATUS} !=200 RewriteRule /cgi-bin/path/to/php - [F]This will only allow internal redirection, thus forbidding direct HTTP access to php interpreter.
a replacement for suexec is suphp (http://www.suphp.org).
“suPHP is a tool for executing PHP scripts with the permissions of their owners. It consists of an Apache module (mod_suphp) and a setuid root binary (suphp) that is called by the Apache module to change the uid of the process executing the PHP interpreter.” (from the website)
I have setup a guide to installing PHP with SuEXEC in such a way that shebangs (!#/usr/bin/php4) are not needed. Hope this is of some help to you.
If you are using php per cgi and have additionally mod_gzip enabled you have to disable mod_gzip for the php cgi binary to use –enable-cgi-redirect. mod_gzip sets the REDIRECT_STATUS always to 200 which makes it impossible for the php binary to know when it was called directly or when it was called by a redirect.
When using php in cgi mode, it’s often a good idea to take a look at the apache suexec feature in addition to the –force-cgi-redirect option.
http://httpd.apache.org/docs/suexec.html
If you want to use suexec and reference your php interpreter via #!/usr/local/bin/php, be shure to compile php WITHOUT –enable-force-cgi-redirect.
The configuration option ‘–enable-force-cgi-redirect’ is supported by Zeus Web Server 3.3.8.2 (at least, that’s what I’ve tried it on - it make work on previous versions).
another clean solution is to hack suexec.c of apache and force all .php scripts to be executed with php compiled in cgi mode. suexec.c in place if (!(prg_info.st_mode & S_IXUSR)) { just if (!(prg_info.st_mode & S_IXUSR) & (strstr(cmd, ".php") == NULL)) { in place execv(cmd, &argv[3]); just if (strstr(cmd, ".php")) execl("/usr/local/bin/php", "php", cmd, NULL); else execv(cmd, &argv[3]);