How To Configure & Optimise PHP on Ubuntu 14.04 LTS Production Server ( Part III )

Previously in guru series of tutorials we went through initial server setup and configuring Apache, now let us have a look at configuring and optimising PHP.


It’s already the most widely used server side scripting language on the web and it’s still growing. Initially developed to be the programming language for the web it has grown into a general purpose programming language. We have already seen the ways to install various versions of PHP in Part I but we need to customise the setup.


This is the main configuration file for PHP. It has a list of directives that are used to control how PHP interprets and runs your applications. By default the php.ini file is located at /etc/php5/apache2/php.ini on Ubuntu 14.04 running Apache 2.X.X. There are also separate php.ini files for CLI and CGI versions located at /etc/php5/cli/php.ini and /etc/php5/cgi/php.ini.

Loaded Configuration

Following command can be used to find the loaded configuration file.

sudo php -i | grep 'Configuration File'

phpinfo function

Another method to find php.ini file is to create a php file in your root directory with the following snippet and use a browser to see the output.

<?php phpinfo(INFO_GENERAL); ?>

Your would find the Loaded configuration File listed among details as shown in the screenshot below.

Additionally, if you wanted view all the information by calling the function without the constants.

<?php phpinfo(INFO_GENERAL); ?>

phpinfo constants
You can customise the output of the function using these constant parameters

Constant Output
INFO_CONFIGURATION Current Local and Master values for PHP directives.
INFO_MODULES Loaded modules and their respective settings.
INFO_ENVIRONMENT Environment Variable information that’s also available in $_ENV.
INFO_VARIABLES Shows all predefined variables from EGPCS (Environment, GET, POST, Cookie, Server).
INFO_LICENSE PHP License information.
INFO_ALL Shows all of the above.

Use the function to know your PHP environment variables. It is key for safety and performance of your server.

Backup php.ini

Always backup your php.ini files before your make changes to them. In-case your need to start fresh, you can recover both development and production php.ini from /usr/share/php5

You can copy them to your php.ini location using the following command and to get started again.

sudo cp /usr/share/php5/php.ini-production /etc/php5/apache2/php.ini
sudo cp /usr/share/php5/php.ini-production.cli /etc/php5/cli/php.ini

Edit php.ini

Once you know the loaded configuration file we can proceed to make the necessary changes to our PHP settings. The following command opens the php.ini file for editing

sudo nano path_loaded_php_ini_file


sudo nano /etc/php5/apache2/php.ini

This will open the php.ini file

[PHP] ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;; ; PHP's initialization file, generally called php.ini, is responsible for ; configuring many of the aspects of PHP's behavior. ; PHP attempts to find and load this configuration from a number of locations. ; The following is a summary of its search order: ; 1. SAPI module specific location. ; 2. The PHPRC environment variable. (As of PHP 5.2.0) ; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0) ; 4. Current working directory (except CLI) ; 5. The web server's directory (for SAPI modules), or directory of PHP ; (otherwise in Windows) ; 6. The directory from the --with-config-file-path compile time option, or the ; Windows directory (C:windows or C:winnt) ; See the PHP docs for more specific information.

Make the necessary changes and then use the following keyboard shortcuts
ctrl + o to save.
Ctrl + x to exit.

php.ini in Virtualhost

php.ini directives can also be defined inside your virtual host or Apache config files as follows.

php_value name_of_php_ini_directive value_for_directive


<VirtualHost *:80>
  DocumentRoot "C:/path/to/doc/root"
  ErrorLog "logs/"
  php_value post_max_size 50M
  php_value upload_max_filesize 50M

Optimise PHP

It’s important to optimise your php.ini settings to utilise the resources well and improve performance. There are numerous directives listed here that you can use to customise PHP but we are going to go through only a few that are of utmost importance to improve performance.

Maximum execution time

This is the maximum time in seconds that a script is executed.


Maximum input time

Maximum amount of time each script may spend parsing request data. It’s a good idea to limit this time on productions servers in order to eliminate unexpectedly long running scripts.


Memory limit
It is a very important directive that limits the memory a script may consume. It prevents the server from running out of memory through memory leaks or intensive usage. Make sure that this value is larger than post_max_size so that regular uploads won’t runout of memory.


Post max size

Maximum size of POST data that PHP will accept.For example if you intend to import a database through phpMyAdmin this setting will limit the size of the database that can be uploaded.This must also be larger than upload_max_filesize.


Upload max filesize
Maximum allowed size for uploaded files.


Edit and save php.ini. Then restart Apache using the command.

sudo service apache2 restart

for the new php.ini setting to be reflected.


Your php.ini file can also have directives for PHP extensions but that is for another set of tutorials.We will go through configuring and optimising MySQL ( MariaDB ) in next series as a follow up.

Subscribe and stay connected.

Leave a Reply

Your email address will not be published. Required fields are marked *