PHP ini Per Directory: Setup Guide
In this article
This guide explains how to override PHP configuration settings for a specific directory (or a whole site) using a php.ini file placed in that directory. You'll learn the exact steps, the prerequisites, common pitfalls, and how to troubleshoot when your changes don't take effect. This is useful for shared hosting, multi-site setups, or when you need different settings (like upload size or error reporting) for different parts of your application.
What You'll Need
- A web server with PHP installed (Apache, Nginx, LiteSpeed, etc.).
- Access to the directory where your PHP files live (via FTP, cPanel, or a terminal).
- Ability to create and edit files (a text editor like Notepad, VS Code, or nano).
- Basic understanding of PHP configuration directives (e.g.,
upload_max_filesize,display_errors).
Step-by-Step
- Check if your server supports per-directory PHP config. This method works only if PHP runs as an Apache module (mod_php) or if your hosting uses
suPHP,FastCGI, orLiteSpeedwith support for.user.inifiles. If you're on Nginx with PHP-FPM, you may need to use a different approach (see Tips). - Create a
php.inifile in the target directory. Using a file manager or terminal, navigate to the directory where you want to apply the settings. Create a new file named exactlyphp.ini(or.user.iniif your server uses that). - Add the desired configuration directives. Open the file in a text editor and add lines in the standard
key = valueformat. For example:upload_max_filesize = 20Mpost_max_size = 25Mdisplay_errors = On
Each directive must be on its own line. You can include any PHP_INI_PERDIR or PHP_INI_ALL directive (see Common Mistakes for which ones work). - Save the file and upload it if needed. If you're using FTP, upload the file to the target directory. If you're using a terminal, just save the file.
- Restart the web server (if required). For Apache with mod_php, changes to
php.initake effect immediately. For PHP-FPM or FastCGI, you may need to restart the PHP service or the web server. On shared hosting, a simple refresh might work, but if not, try restarting via your control panel. - Verify the settings. Create a temporary PHP file in that directory with
<?php phpinfo(); ?>and open it in a browser. Look for the Loaded Configuration File or Scan this dir for additional .ini files to confirm your file is being read. Also check the specific directive values you changed. - Remove the test file. After verifying, delete the
phpinfo()file to avoid exposing sensitive server details.
Common Mistakes
- Using directives that are not allowed per directory. PHP has three modes:
PHP_INI_PERDIR,PHP_INI_SYSTEM, andPHP_INI_ALL. OnlyPHP_INI_PERDIRandPHP_INI_ALLcan be set in a per-directoryphp.ini. Directives likedisable_functionsorextensionarePHP_INI_SYSTEMand will be ignored or cause a server error. - Naming the file incorrectly. Some servers use
.user.iniinstead ofphp.ini. Check your hosting documentation. Also, ensure the file name is exactly that – no extra spaces or extensions. - Forgetting about parent directories. Per-directory
php.inifiles apply to the directory they are in and all subdirectories. If you have a conflicting file in a subdirectory, it will override the parent. Plan your directory structure accordingly. - Not checking the PHP version. Some directives may be deprecated or removed in newer PHP versions. Always test after making changes.
- Assuming it works on all server types. If your server runs PHP as CGI (not mod_php), the
php.iniin the directory may be ignored. You might need to use.htaccesswithphp_valuedirectives instead (if allowed).
Tips
- Use
.user.inifor PHP-FPM. If you're on Nginx or Apache with PHP-FPM, the recommended method is to use a.user.inifile. The syntax is the same, but the file name is different. This works with PHP 5.3.3 and later. - Start with a minimal set of changes. Only override what you need. Too many directives can make debugging harder.
- Keep a backup. Before making changes, copy the original
php.ini(if any) so you can revert quickly. - Use absolute paths if needed. For directives like
include_path, use absolute paths to avoid confusion. - Test with a simple script. Instead of
phpinfo(), you can use<?php echo ini_get('upload_max_filesize'); ?>to quickly check a single value.
Troubleshooting
- Changes not taking effect. First, confirm the file is named correctly and is in the right directory. Then check if your server uses
.user.iniinstead. If you're on shared hosting, the server might have a globalphp.inithat overrides yours – you can't overridePHP_INI_SYSTEMdirectives. Also, try restarting the web server or PHP-FPM service. - Getting a 500 Internal Server Error. This usually means a syntax error in your
php.ini. Check for missing semicolons (though they are not required, they are allowed), incorrect directive names, or invalid values. Remove the file temporarily to see if the error goes away. - File is ignored completely. Your server might not support per-directory
php.iniat all. Check your hosting control panel or contact support. Alternatively, try using.htaccesswithphp_valuedirectives (ifAllowOverrideis enabled). - Only some directives work. As mentioned, only
PHP_INI_PERDIRandPHP_INI_ALLdirectives are allowed. If a directive isPHP_INI_SYSTEM, it will be ignored. Check the PHP manual for the directive's change mode. - Changes affect all subdirectories unexpectedly. That's by design. If you want to exclude a subdirectory, you can place a
php.iniin that subdirectory with the original values, or use a different approach like.htaccessfor that subdirectory.
Frequently Asked Questions
Can I use a php.ini file in any directory?
Not always. It depends on the server configuration. Apache with mod_php supports it, and many shared hosts do. Nginx with PHP-FPM typically requires .user.ini files. Check your hosting documentation.
What is the difference between php.ini and .user.ini?
php.ini is the traditional file used by Apache mod_php. .user.ini is used by PHP-FPM/FastCGI and works similarly, but the file name is different. The syntax is the same.
How do I know which directives I can set?
Look up the directive in the PHP manual. It will list the change mode (PHP_INI_PERDIR, PHP_INI_SYSTEM, etc.). Only PERDIR and ALL are allowed in per-directory files.
Will my changes affect subdirectories?
Yes, unless a subdirectory has its own php.ini that overrides them. The settings cascade down.
Do I need to restart the web server after changing php.ini?
For Apache with mod_php, no – changes take effect immediately. For PHP-FPM, you may need to restart the PHP-FPM service. On shared hosting, a refresh might be enough, but if not, contact support.
Can I use .htaccess instead?
Yes, if your server allows it. You can use php_value and php_flag directives in .htaccess to set per-directory settings. However, this is not recommended if you have many settings, as it can slow down the server.