How to define your own PHP configuration

This post will show how you can define a custom PHP configuration for your website. A Custom PHP configuration will allow you to change server default configurations. This post mentions two options for adjusting configuration: user.ini and .htaccess.

The default server configuration (set using the php.ini file) will prevent your website from displaying errors. This setting in the php.ini is excellent in production environments but is cumbersome in a development environment.

What is the php.ini config file?

The php.ini file is a file that controls PHP settings on the server in the form of “directives”. PHP reads the php.ini file during startup and then configures the PHP environment accordingly. Some changes to the php.ini file will require a PHP restart.

It’s important to note that the php.ini affects the entire server and all the PHP sites hosted on it. Most hosting platforms will not allow you access to the php.ini.

Adjust the PHP configuration using the .htaccess file

Using the .htaccess file is an alternative method of setting PHP configuration. However, this method is not the preferred method for all PHP sites. You must use the .user.ini configuration file if your site is PHP 5.3.0+, and that is hosted using Apache GCI/FastCGI. Refer to the .htaccess-guide guide more information on configuring this file.

Adjust the PHP configruation using the .user.ini file

Instead of using the .htaccess file, set the PHP configuration within the .user.ini file. The .user.ini files are just like the .htaccess files, but there are some differences.

For instance, setting the PHP memory limit in .htaccess would require the following entry: php_value memory_limit 256M.

The PHP memory limit configuration in the .htaccess will result in the following error:

.htaccess: Invalid command ‘php_value’, perhaps misspelt or defined by a module not included in the server configuration.

The error will be available in your Apache site logs. Apache store this error log (typically) in the following path: /var/log/apache2/.  Alternatively, you would see a server 500 error.

Changing the old .htaccess configuration to .user.ini configuration is easy!

.htaccess configuration example:

php_value memory_limit 256M
php_value max_execution_time 300 

.user.ini configuration example:

memory_limit 256M
max_execution_time = 300 

The .user.ini file is configured on a per-directory basis and does not support all the php.ini directives.

Visit the list of php.ini directives page to see which directives apply to the .user.ini file. The configuration that applies to the .user.ini file will have a changeable mode value of “PHP_INI_PERDIR”.

There are about over 50 directives that can be set for the .user.ini file.

The php.ini file can change the requested filename of the user.ini file. The user_ini.filename directive can change the required name from .user.ini to something else. Be sure to check this configuration using a script.

Changes to your .user.ini file may take some time to take effect. The user_ini.cache_ttl directive in the php.ini typically caches the .user.ini for 5 minutes at a time. It is possible for the server admin to disable the .user.ini file.

Create a .user.ini file for custom PHP configuration

You can create a .user.ini file, which contains the configuration you would enter for the php.ini file.

To set your custom php.ini configuration:

Copy the php.ini file and make the changes to the configuration. As an example – you could enable PHP to display errors on the website. Remember to refer to the list of php.ini directives. Only some PHP directive may be configurable within the .user.ini file.

Rename the php.ini file to .user.ini and upload it to your website. Note that the dot is important to add. Adding the dot will hide the file when viewing it via the Linux shell. Using “ls-a” will show this file if it magically “disappears”!

To test, upload the following info.php script to your website:


The phpinfo() method will display the configuration setting on your website.

Look out for a section that looks similar to this image below, when you view your page in the browser. Note the differences between the master and local value in terms of displaying errors.

After uploading the .user.ini file:

phpinfo after user ini upload

Before uploading the .user.ini file:

phpinfo before user ini upload

You may also be interested in

About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.