What is php.ini file and how to configure it for PHP projects

What is PHP configuration file

The PHP configuration file is read whenever PHP starts up. For the server module versions of PHP, this takes place just once when the web server is started. For the CGI and CLI versions, it occurs on every invocation.

If changes that you made are not working, you should stop and restart the HTTPd. If changes still are not working or showing, you should find out the exact path of the php.ini file: use phpinfo() function to check the path to the configuration file.

The configuration file is described well in itself by comments. The keys are case sensitive while keyword values are not case sensitive; spaces and lines start with semicolons are neglected.

The Boolean may be shown by 1/0, Yes/No, On/Off, or True/False.

The default values in the php.ini-dist should result in an appropriate PHP set up that may be modified afterward.

A Few worth mentioning PHP ini settings

In the following section, we will explain a few important configuration file settings that may be required for the PHP parser:

php.ini file location

The best way to find location of PHP ini file is through the following script



 

Save and run this file in your browser. The displayed page will show you:

phpinfo1

short_open_tag = Off

Short open tags look like this: <? ?>. If you need to use XML functions then this option must be set to Off.

safe_mode = Off

if safe_mode is set as On, you might have compiled PHP with the –enable-safe-mode flag. Safe mode usually relevant to CGI use.

safe_mode_exec_dir = [DIR]

This setting provides that safe mode is ON; it can also be set with the –with-exec-dir flag during the Unix build process. PHP in safe mode executes external binaries only out of this directory. The default is /usr/local/bin. This is not related to serving normal web pages in PHP/HTML.

safe_mode_allowed_env_vars = [PHP_]

This option specifies which environment variables users may modify in safe mode. The default value is set to those variables prefixed with “PHP_”. If this directive’s value is empty, most variables can be changed.

safe_mode_protected_env_vars = [LD_LIBRARY_PATH]

This setting specifies, what environment variables users can’t change in safe mode, even if safe_mode_allowed_env_vars is set permissively.

disable_functions = [function1, function2…]

This is a good addition from PHP4. This enables users to disable selected functions for security measures. In previous versions, this could be done in C code from where PHP was written.

The System, files system, and network functions should probably be the first thing to do, as such it is not safe to allow write or alter files over HTTP.

max_execution_time = 30

This function does work in safe mode as such set_time_limit() function does not work in safe mode. The max_execution_time function is the way to set script time out in safe mode. You may also use Apache configuration to set timeout (if you are using Apache web server) however this will also apply to files other than PHP extension.

error_reporting = E_ALL & ~E_NOTICE

The default value is E_ALL & ~E_NOTICE, all errors except notices. The Machine, where you are developing should be set to default whereas production servers may even consider lesser value.

file_uploads = [on/off]

To allow uploading from PHP scripts turn ON this flag.

upload_tmp_dir = [DIR]

Do not uncomment this line unless you understand the implications of HTTP uploads!

How to increase PHP Upload limits

The default setting is set to 2MB or less in the php.ini file for uploading files.

upload_max_filesize = 2M

Find following directives in the php.ini file to change the upload limit.

e.g



Change as per your requirements. Please note, the  Apache web server does not require to restart after making these changes.

If the upload_max_filesize is larger than post_max_size, you must increase post_max_size so that it is bigger than the upload_max_size.

If the value of post_max_size is larger than memory_limit, you must increase memory_limit so that it is larger than post_max_size.

date.timezone in php.ini

Edit the configuration file using your favourite editor.

# vi /etc/php.ini

Change this:

[Date]

; Defines the default timezone used by the date functions

; date.timezone =

To this:

[Date]

; Defines the default timezone used by the date functions

date.timezone = “Europe/London”

Change setting as per your requirement.


Was this article helpful?

Related Articles

Leave A Comment?