How to Use the .user.ini
File in ServBay: A Guide to Localized PHP Configuration
ServBay is a robust, integrated local web development environment designed for developers, supporting a variety of languages and tech stacks—including flexible PHP version management and convenient configuration options. In real-world development scenarios, different projects may require different PHP settings, such as upload file size limits, memory restrictions, or error reporting levels. Directly editing the global php.ini
affects all projects, which can be inconvenient when managing multiple projects.
Fortunately, PHP offers a .user.ini
mechanism that allows developers to define custom PHP settings within specific directories and their subdirectories, without modifying the global php.ini
. ServBay fully supports .user.ini
, making it simple and efficient to fine-tune PHP configurations for individual projects or directories.
This article will explain in detail how the .user.ini
file works, how to use it within ServBay, and provide examples for setting custom PHP configurations for specific projects.
Introduction to the .user.ini
File
The .user.ini
file was introduced in PHP 5.3.0 as a more flexible and secure method for setting PHP directives compared to the traditional php_value
and php_flag
options in web server configurations like Apache's .htaccess
. It allows developers to place a .user.ini
file within a web-accessible directory, with configuration directives that only apply to PHP scripts within that directory and its subdirectories.
Key Features
- Scoped Settings: Directives in a
.user.ini
file are only effective within its directory and all subdirectories, and do not affect the global ServBayphp.ini
or other project configurations. - High Flexibility: Developers can define independent PHP configuration sets according to the specific requirements of each project.
- Easy Management: No need to alter global server or PHP config files, reducing the risk of configuration conflicts and making project settings more independent and portable.
- No Restart Needed: Unlike editing
php.ini
(which typically requires a PHP-FPM or web server restart), changes to.user.ini
usually take effect automatically after the interval specified byuser_ini.cache_ttl
(default is typically 300 seconds, or 5 minutes).
Configurable Directives
Not all PHP configuration directives can be set within a .user.ini
file. Which directives can be set depends on their "changeable mode." Only those with modes PHP_INI_USER
, PHP_INI_PERDIR
, or PHP_INI_ALL
can be modified in .user.ini
.
Common directives that can be set in .user.ini
include:
upload_max_filesize
post_max_size
memory_limit
display_errors
log_errors
max_execution_time
session.save_path
date.timezone
Directives with the PHP_INI_SYSTEM
mode (such as extension_dir
, zend_extension
, disable_functions
, etc.) cannot be set in .user.ini
. These must be configured in the main php.ini
or the web server’s configuration.
For a full list of directives and their changeable modes, consult the PHP Manual: List of php.ini directives.
Using .user.ini
in ServBay
The process of using a .user.ini
file in ServBay is straightforward. Below are the specific steps, using upload_max_filesize
and memory_limit
as examples.
Assume your website’s root directory is at the default ServBay path /Applications/ServBay/www/
, and you want to set custom PHP configurations for a project named myproject
, whose web root is /Applications/ServBay/www/myproject/public
.
Step 1: Identify the Target Directory
First, determine the directory where you want to apply your customized PHP settings. This is typically the web server root directory for your project (like the public
directory in Laravel or Symfony), or a specific subdirectory that requires special configuration.
Example: /Applications/ServBay/www/myproject/public
Step 2: Create or Edit the .user.ini
File
Create a new file called .user.ini
in the target directory (or edit it if it already exists). Note: Files that start with a dot, like .user.ini
, are hidden in most file systems. You might need to adjust your file manager settings to see hidden files.
You can create the file via terminal:
bash
cd /Applications/ServBay/www/myproject/public
touch .user.ini
1
2
2
Next, open the .user.ini
file with a text editor and add the PHP directives you need—one per line. For example:
ini
; Set maximum file upload size to 20MB
upload_max_filesize = 20M
; Set PHP script memory limit to 256MB
memory_limit = 256M
; Enable error display (recommended for development only)
display_errors = On
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Save and close the .user.ini
file.
Step 3: Wait for the Configuration to Take Effect or Force a Refresh
Changes made in .user.ini
will not take effect instantly—PHP caches these files for performance reasons. By default, the user_ini.cache_ttl
directive governs the cache duration, which is typically 300 seconds (5 minutes). The changes will be applied automatically after the cache expires.
If you want your changes to take effect immediately, you can try the following methods (effectiveness may vary depending on your specific version and configuration of ServBay):
- Restart the relevant PHP-FPM process: In the ServBay application interface, find the PHP version used by your site, then try stopping and starting the corresponding PHP-FPM service.
- Restart ServBay: This is the most thorough method but will interrupt all local services.
- Wait for the cache to expire: This is the recommended approach, especially if the changes are not urgent.
Step 4: Verify That the .user.ini
File Is in Effect
To check that your .user.ini
settings have been applied, you can create a simple PHP file to display PHP configuration information.
In the same directory as your .user.ini
(e.g., /Applications/ServBay/www/myproject/public
), create a file called info.php
with the following content:
php
<?php
phpinfo();
?>
1
2
3
2
3
Save the info.php
file.
Then, access the corresponding info.php
file on your ServBay website through your browser. For instance, if your project’s domain in ServBay is set to myproject.servbay.demo
and info.php
is in the public
directory, visit https://myproject.servbay.demo/info.php
.
On the phpinfo()
output page, look for the directives you set in .user.ini
, such as upload_max_filesize
, memory_limit
, and display_errors
. You should see two columns: Master Value
and Local Value
.
Master Value
shows the value from the globalphp.ini
.Local Value
shows the value currently effective for that script, which may come from.user.ini
or web server configuration.
If the Local Value
matches what you specified in .user.ini
(e.g., 20M
, 256M
, On
), your .user.ini
file is working as intended.
Important Note: After verifying, for security reasons, make sure to delete or restrict access to info.php
, as it exposes detailed server configuration information.
Additional Notes and Considerations
- File Naming and Location: The
.user.ini
file must be named exactly.user.ini
and placed in the directory where you want the configuration to apply. It will affect that directory and all its subdirectories. - Directive Modes: Only certain directive modes can be set in
.user.ini
. Attempting to setPHP_INI_SYSTEM
mode directives will not work and may not show any error. Refer to the PHP Manual to confirm. - Caching: Pay attention to your
user_ini.cache_ttl
setting. If you frequently edit.user.ini
and need to see results immediately, you can temporarily setuser_ini.cache_ttl = 5
in the globalphp.ini
. However, setting a very low value is not recommended in production as it may impact performance. Editing the globalphp.ini
requires restarting the PHP-FPM service. - Precedence: PHP configuration precedence generally follows: Core default <
php.ini
(and extra.ini
files) < web server config (php_value
/php_flag
if using Apache/nginx module) <.user.ini
<ini_set()
function calls..user.ini
settings override those in the globalphp.ini
, but can be further overridden byini_set()
in scripts. - Security: Avoid storing sensitive information inside
.user.ini
. While it only impacts the current directory, it is still located within a web-accessible path (even though most servers are configured to deny direct access).
Frequently Asked Questions (FAQ)
Q: I created a .user.ini
file and added settings, but they don't seem to take effect. Why?
A: Possible reasons include:
- Incorrect filename or location: Ensure it is named
.user.ini
and placed in the correct directory. - Unsupported directive mode: You may have tried to set directives that cannot be configured in
.user.ini
(modes other thanPHP_INI_USER
orPHP_INI_PERDIR
). Check the PHP Manual. - Cache not expired: Wait for the duration set by
user_ini.cache_ttl
(default 5 minutes) or try restarting PHP-FPM. - Syntax error: Double-check your
.user.ini
for syntax mistakes. - Overridden by higher precedence: Check for scripts that use
ini_set()
which may override your settings.
Q: What's the difference between a .user.ini
file and the global php.ini
file?
A: The global php.ini
affects the entire PHP environment (or all apps using a specific PHP version) and applies its settings to all PHP scripts unless a lower-precedence configuration overrides it. The .user.ini
file only affects scripts within its directory and subdirectories and can only set certain directive modes. The main advantage of .user.ini
lies in its local scope and the ability to avoid changing global settings.
Conclusion
Leveraging the .user.ini
file in ServBay is the recommended way to manage project-specific PHP configurations. It offers great flexibility, allowing developers to easily tailor their PHP environment to meet the unique needs of each project without impacting other projects or the global ServBay settings. With the guidance and examples provided in this article, you should be well-equipped to use .user.ini
effectively in your ServBay projects, improving both your development efficiency and environment management. ServBay is designed to empower developers with powerful and flexible local tools—and excellent support for .user.ini
is an integral part of this philosophy.