Drupalist

Changing PHP's configuration file, php.ini

[This post is about making changes in PHP's configuration files when you have full command-line access to the underlying Ubuntu 10 server. The commands should be identical for other Unix-based web servers.]

An important step in setting up hosting of any web site is tailoring PHP to the needs of the code you will be running. You do this by changing PHP's initialization files which have the extension .ini. There is at least one initialization file, called php.ini, and there may be others.

Step 1: Use phpinfo() to find the .ini files being used

With any text editor, create a file called phpinfo.php in your web root directory, which I will assume is /var/www. Here is the contents of /var/www/phpinfo.php:

<?php
phpinfo();
?>

[Note: Remove this file when finished with it, or before the server goes live. You want to use it to give you information (and not hackers) about how your server initializes PHP.]

Go to http://example.com/phpinfo.php with any web browser. Going to this page should cause the script phpinfo.php to run, giving you something that looks like this. The four entries "Configuration file (php.ini) path", "Loaded Configuration File", "Scan this dir for additional .ini files" and "Additional .ini files parsed" give you the information you need.

Step 2: Change the appropriate .ini file

Now you need to find the .ini file that has the parameter you want to change. This will usually be php.ini, but keep in mind that you the parameter you want to change might be in one of the other .ini files, or that you may want to make a new .ini file to hold the new setting.

Suppose we want to change memory_limit. Here I assume the standard case of a scan directory link in the directory of the loaded configuration file and give a command which returns the file name and line number of any match.

cd /etc/php5/apache2
egrep -rHn memory_limit *.ini

Edit the appropriate .ini file and make the change, remembering that you need to become root to do this.

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

Step 3: Give the web server permission to read the .ini files

The server may have been unable to read the .ini file before you edited it (!). And even if it could before, your editing this file as root may have made the web server unable to read it. The easiest make the file readable by the web server is to change its group to the web server's group. This is setting is in /etc/apache2/envvars and is www-data by default.

sudo chgrp www-data php.ini

Back to Step 1 to make sure the change took effect

You need to go back to Step 1 and look for the new value you just entered being used. The new value must appear in the HTML output of your phpinfo.php script when run by your web server, or it is not being used when your web server starts PHP. Please comment and share your experience.

Comments

A faster way to get feedback

Check out the recent blog post http://crotown.com/content/getting-phpinfo-output-any-drupal-site. It notes that all you have to do to check the output of PHP's phpinfo() command is go to the Drupal path admin/reports/status/php.

Drupal SEO