Custom PHP settings per directory with .user.ini files
Some hosting accounts use the PHP Selector instead of php.ini files to configure PHP settings. However, you can still specify custom PHP settings for a directory by using .user.ini files. This article explains how to set up and use .user.ini files on your account.
Using .user.ini files
When you create a .user.ini file, any PHP directives in the file are only active in that directory (and any subdirectories beneath it). This enables you to have different PHP settings for different directories. This may be necessary, for example, if you install two applications on your account that have different PHP requirements.
To enable custom settings in a .user.ini file, follow these steps:
- Use your preferred text editor to create a .user.ini file in the directory where you want to enable custom PHP settings.
For a list of directives you can use in PHP .ini files, please visit https://www.php.net/manual/en/ini.list.php.
More Information
To view the official PHP documentation, please visit https://www.php.net/docs.php.
Article Details
Other Articles in This Category
- What is PHP?
- PHP script basics
- Viewing PHP settings
- Changing PHP settings and versions
- Custom php.ini files
- Using php.ini directives
- Custom .htaccess files for PHP settings
- Using PHP directives in custom .htaccess files
- Run PHP scripts from cron jobs
- Using PHP to send e-mail messages
- PHP include paths
- PEAR packages
- Determining if a PHP function is available
- ‘500 Internal Server Error’ while running PHP
- ‘String could not be parsed as XML’ error message in PHP
- Using the Exif extension in PHP
- Common issues after PHP upgrade
- Using a custom php.ini file for cPanel cron jobs
- Using PHP sessions
- ionCube PHP Loader support
- Specifying the MySQL character set in PHP
- ‘Unable to allocate memory for pool’ error message in PHP
- Optimizing Symfony using APC
- Using the internationalization extension in PHP
- Installing PHP composer
- Determining if APC is installed on a server
- Using the MailChimp API with PHP
- ‘Allowed memory size exhausted’ error message in PHP
- Using ionCube Loader with different PHP versions
- Enabling PHP opcode caching
- Sending e-mail with PHPMailer
- Hardening PHP 7 and earlier versions
- Custom PHP settings per directory with .user.ini files
- ionCube PHP Loader unavailable on PHP 8.0
- Changing the PHP version for a specific directory
Related Articles
Grow Your Web Business
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you’ll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
Hosting
Php ini user directory
The configuration file ( php.ini ) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.
- SAPI module specific location ( PHPIniDir directive in Apache 2, -c command line option in CGI and CLI)
- The PHPRC environment variable.
- The location of the php.ini file can be set for different versions of PHP. The root of the registry keys depends on 32- or 64-bitness of the installed OS and PHP. For 32-bit PHP on a 32-bit OS or a 64-bit PHP on a 64-bit OS use [(HKEY_LOCAL_MACHINE\SOFTWARE\PHP] for 32-bit version of PHP on a 64-bit OS use [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] ] instead. For same bitness installation the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x] , where x, y and z mean the PHP major, minor and release versions. For 32 bit versions of PHP on a 64 bit OS the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x] , where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in any of these keys, the first one found will be used as the location of the php.ini (Windows only).
- [HKEY_LOCAL_MACHINE\SOFTWARE\PHP] or [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] , value of IniFilePath (Windows only).
- Current working directory (except CLI).
- The web server’s directory (for SAPI modules), or directory of PHP (otherwise in Windows).
- Windows directory ( C:\windows or C:\winnt ) (for Windows), or —with-config-file-path compile time option.
If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini ), it is used instead of php.ini . The SAPI name can be determined with php_sapi_name() .
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.
Using environment variables can be used in php.ini as shown below.
Example #1 php.ini Environment Variables
; PHP_MEMORY_LIMIT is taken from environment memory_limit = $
The php.ini directives handled by extensions are documented on the respective pages of the extensions themselves. A list of the core directives is available in the appendix. Not all PHP directives are necessarily documented in this manual: for a complete list of directives available in your PHP version, please read your well commented php.ini file. Alternatively, you may find » the latest php.ini from Git helpful too.
Example #2 php.ini example
; any text on a line after an unquoted semicolon (;) is ignored [php] ; section markers (text within square brackets) are also ignored ; Boolean values can be set to either: ; true, on, yes ; or false, off, no, none register_globals = off track_errors = yes ; you can enclose strings in double-quotes include_path = ".:/usr/local/lib/php" ; backslashes are treated the same as any other character include_path = ".;c:\php\lib"
It is possible to refer to existing .ini variables from within .ini files. Example: open_basedir = $ «:/new/dir» .
Scan directories
It is possible to configure PHP to scan for .ini files in a directory after reading php.ini . This can be done at compile time by setting the —with-config-file-scan-dir option. The scan directory can then be overridden at run time by setting the PHP_INI_SCAN_DIR environment variable.
It is possible to scan multiple directories by separating them with the platform-specific path separator ( ; on Windows, NetWare and RISC OS; : on all other platforms; the value PHP is using is available as the PATH_SEPARATOR constant). If a blank directory is given in PHP_INI_SCAN_DIR , PHP will also scan the directory given at compile time via —with-config-file-scan-dir.
Within each directory, PHP will scan all files ending in .ini in alphabetical order. A list of the files that were loaded, and in what order, is available by calling php_ini_scanned_files() , or by running PHP with the —ini option.
Assuming PHP is configured with --with-config-file-scan-dir=/etc/php.d, and that the path separator is . $ php PHP will load all files in /etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d php PHP will load all files in /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=:/usr/local/etc/php.d php PHP will load all files in /etc/php.d/*.ini, then /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d: php PHP will load all files in /usr/local/etc/php.d/*.ini, then /etc/php.d/*.ini as configuration files.
Setup
I have installed nginx 1.2.2 and PHP 5.4.4. I have configured PHP with nginx running as FastCGI. nginx config snippet:
I have a number of local virtual sites running on same system. To customize each ones requirements I have created .user.ini file in each sites root folder which as per the PHP documentation should be applicable on sub folders also. Please note this is a development environment and not live. So I have flexibility to customize a number of things.
Problem
When I access any site from the browser it works fine unless I do not dive into some sub folder. It seems that .user.ini does not work in sub directories — at least in my configuration. As per PHP document (http://www.php.net/manual/en/configuration.file.per-user.php):
In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root
Has anyone configured .user.ini under nginx — request you to please help me out. Note: Someone reported a bug in 2009: https://bugs.php.net/bug.php?id=50374
More Information (EDIT)
PHPInfo() says (in root as well as in sub folders):
* Server API: CGI/FastCGI
* user_ini.filename: .user.ini (local as well as master) php_sapi_name() says cgi-fcgi These values are present in phpinfo() from root folder and sub directories. So there is no difference in PHPInfo(). Further confirmation that it is running as CGI/FastCGI is that root folder is taking configurations from .user.ini but not sub folders.
// .user.ini auto_prepend_file = "E:\Websites\localhost\user.ini.php"
This may actually be a bug but has anyone configured PHP with nginx in this way and did not faced the problem. Requesting to please share version numbers and platform in this case.