PHP settings are controlled by a file named ‘php.ini’. The configuration file is read when Apache web server starts. To change the default PHP settings, you should edit the ‘php.ini’ file and restart your web server.
Out-of-the-box, PHP settings work pretty well but customizing the configuration file allows you to select what can work best for you depending on your hardware architecture and software design.
In this guide, we are going to show you how to edit the basic PHP settings on your Apache web server running on Ubuntu 18.04 VPS.
- A VPS Plan (visit HostAdvice's VPS reviews for the best VPS providers)
- A non-root user with sudo privileges
- Apache web server
Step 1: Determine the Location of your php.ini File
From time to time, you will need to manage the critical php.ini file. The file location may vary a bit depending on the PHP version running on your server.
The configuration file is located on:
For instance, if you are running PHP 7.0, the file will be located on:
The same case applies to PHP 7.2. You can find the configuration file at:
Step 2: Opening the PHP configuration file for editing
Once you determine the location of your file, the next step is editing it using a nano editor. Use the command below to open the file:
$ sudo nano php_ini_file
$ sudo nano /etc/php/7.2/apache2/php.ini
Step 3: Making changes on the php.ini file
As mentioned at the beginning of the article, the default PHP settings may work for the majority of websites or web applications. However, your environment may demand some values to be tweaked a bit to ensure that your website is running smoothly.
In most cases, you will be editing the below PHP settings:
This sets the maximum execution time in seconds that a PHP script is allowed to run before it is terminated. Sometimes, you might have demanding scripts that should run for a few minutes and you need to change this value. The default value is 30 seconds but you can set it to a larger value
Change to any value e.g. 1800
The default value for this directive is 2M (two Megabytes). This value controls the maximum size of files that you upload using PHP scripts. Sometimes, it is necessary to change this value if you anticipate uploading big files.
For instance, if you are uploading a large database via phpMyAdmin, you will need to change this value.
Change to a large value e.g. 16M
This value limits the amount of data allowed on post data. It usually affects PHP scripts that use a lot of web forms. The value also controls files uploaded via a PHP script, hence, it should always be larger than ‘upload_max_filesize’. The default value for ‘post_max_size’ is 8M.
Customize it depending on your needs e.g.
The default value for PHP 7.2 ‘memory_limit’ is 128M. Sometimes, poorly written PHP scripts may consume a lot of server’s memory and affect other applications running on your VPS. To avoid this, PHP ‘memory_limit’ controls the amount of memory allocated to a script.
memory_limit = 128M
Custom value example
memory_limit = 256M
You can also use -1 if you want to allocate an unlimited amount of memory to your PHP script depending on the available RAM on your VPS
memory_limit = -1
PHP Error Reporting Settings
You can control the behavior of error reporting in PHP using the below directives:
display_errors: Set this value to ‘On’ or ‘Off’ depending on whether you want PHP to display errors when scripts are run. In PHP 7.2 the default value is ‘Off’
display_errors = Off
You can turn error reporting on by changing the value to ‘On’:
display_errors = On
log_errors: This value tells whether errors from a script should be saved on the server’s log file. Instead of displaying errors to regular users in a production environment, you should log them. The default value in PHP 7.2 is ‘On’
log_errors = On
You can switch error logging off by changing the value to:
log_errors = Off
error_reporting: This directive dictates the error reporting level. For PHP versions greater than 5.3, the default value is ‘E_ALL & ~E_DEPRECATED & ~E_STRICT’
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
You may change the value depending on the errors that you want to be reported. For instance, to include notices, use the value below
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE
PHP Date/Time settings
You can also change the default timezone used by PHP scripts.
Find the line:
$ ; date.timezone=
Uncomment it by removing the semicolon and then enter your preferred time zone. You can check the list of support time zones on the official PHP website (http://php.net/manual/en/timezones.php)
For instance, if you want to change the time zone to New York City, use the value below:
Once you finish editing the php.ini file, press CTRL + X, Y and hit Enter to save the changes. You should also restart Apache for the settings to be reloaded using the command below:
$ sudo service apache2 restart
In this guide, we have discussed how to locate and edit PHP settings on your Ubuntu 18.04 server. We have also taken you through the basic settings that you should tweak to optimize the performance of your website or web applications. We believe that the changes you make on your PHP configuration file will help you to have a smoother environment for running your websites.