How to Locate and Use php.ini-development on Ubuntu LAMP for PHP Debugging


2 views

When setting up a LAMP stack on Ubuntu using sudo tasksel install lamp-server, you might notice that only /etc/php5/apache2/php.ini exists by default. Unlike some PHP distributions, Ubuntu's package doesn't include the php.ini-development and php.ini-production variants that are commonly referenced in PHP documentation.

The default php.ini in Ubuntu is configured for production use, meaning:

  • Error reporting is minimal (display_errors = Off)
  • Logging may be restricted
  • Development features like assert() are disabled

This makes debugging PHP scripts significantly harder during development.

Here's how to modify the existing php.ini for development purposes:

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

Key settings to change:

display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php_errors.log

If you prefer using the official development configuration:

wget https://raw.githubusercontent.com/php/php-src/master/php.ini-development
sudo mv php.ini-development /etc/php5/apache2/php.ini

For a more sophisticated setup, consider using different ini files:

sudo cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.production
sudo cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.development

# Then modify php.ini.development with development settings

Create a switch script:

#!/bin/bash
if [ "$1" == "dev" ]; then
    sudo ln -sf /etc/php5/apache2/php.ini.development /etc/php5/apache2/php.ini
elif [ "$1" == "prod" ]; then
    sudo ln -sf /etc/php5/apache2/php.ini.production /etc/php5/apache2/php.ini
fi
sudo service apache2 restart

Create a test script to verify error reporting:

<?php
// This should generate a warning if development mode is active
echo $undefined_variable;

Remember to restart Apache after any php.ini changes:

sudo service apache2 restart

When setting up a LAMP stack on Ubuntu (specifically Ubuntu 10.04.1 LTS in this case) using tasksel install lamp-server, you'll notice that only the main php.ini file exists in /etc/php5/apache2/. Unlike some PHP distributions, the Ubuntu package doesn't include the php.ini-development and php.ini-production variants by default.

The development configuration typically enables several crucial settings for debugging:

display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On

Whereas production settings prioritize security and performance:

display_errors = Off
display_startup_errors = Off
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
log_errors = On

Instead of searching for the missing file, you can create a development-ready configuration:

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

Make these essential changes:

; Enable error display
display_errors = On
display_startup_errors = On

; Set error reporting level
error_reporting = E_ALL | E_STRICT

; Enable logging
log_errors = On
error_log = /var/log/php_errors.log

Create a simple switching mechanism:

#!/bin/bash
# Switch PHP configuration between development and production
if [ "$1" = "dev" ]; then
    sudo cp /etc/php5/apache2/php.ini.development /etc/php5/apache2/php.ini
    echo "Development configuration activated"
elif [ "$1" = "prod" ]; then
    sudo cp /etc/php5/apache2/php.ini.production /etc/php5/apache2/php.ini
    echo "Production configuration activated"
else
    echo "Usage: phpconfig [dev|prod]"
fi
sudo service apache2 restart

Create a test file to verify settings:

<?php
// Force an error for testing
echo $undefinedVariable;
trigger_error("Test error message", E_USER_NOTICE);
phpinfo();
?>

For more flexibility, consider using .htaccess for development environments:

# In your development directory's .htaccess
php_flag display_errors on
php_value error_reporting -1