Edit and save php.ini over terminal

Edit and save php.ini over terminal

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Persistence Paradox in PHP Configuration on Ubuntu

On Ubuntu systems, particularly older releases like 14.04 where the /etc/php5/cli/php.ini resides, a common misconception exists regarding how configuration files are managed. Developers often assume that opening a file with a text editor via the terminal—using commands like nano /etc/php5/cli/php.ini or vi—implies the changes will persist after closing the session. This intuition holds true for standard file systems, but server environments introduce a layer of complexity that can lead to lost configuration changes if not handled correctly.

The core issue stems from how package managers like apt-get handle system files. When you install PHP via the official Ubuntu repositories, the package manager stores the original php.ini within the package data structure. If you edit these files directly and then run an update or upgrade command, the package manager will often restore the default configuration file, effectively overwriting your manual edits. This is a deliberate safety mechanism to ensure that system upgrades don't leave servers in an unstable state due to unexpected configuration drift.

To save your edits permanently without risking them being wiped out by future updates, you must understand the distinction between modifying the source package file and creating a persistent configuration override. The most reliable method involves using a backup of the original file as a template rather than editing the live system file directly.

Consider the following workflow which ensures persistence across upgrades:

# Backup the original configuration first
sudo cp /etc/php5/cli/php.ini /etc/php5/cli/php.ini.orig

# Edit the backup or create a new one based on it
sudo nano /etc/php5/cli/php.ini.orig

However, editing the live file at /etc/php5/cli/php.ini is acceptable if you ensure the system knows to ignore updates that touch this specific path. In many modern Debian-based systems, the recommended approach is to drop configuration files into a separate directory or use dpkg-divert. For PHP 5 on Ubuntu 14.04 specifically, the standard practice was often to edit the file directly but ensure you do not run apt-get upgrade without checking for package conflicts, or more safely, to configure the system to drop files.

A robust alternative that avoids direct modification of the managed file is placing a custom configuration file in /etc/php5/cli/conf.d/. This directory contains individual .ini files that are loaded after the main php.ini. If you need to change settings like upload_max_filesize or memory_limit, creating a new file in this directory and adding your directives there allows you to bypass the main php.ini entirely for specific parameters.

# Create a custom config file
sudo nano /etc/php5/cli/conf.d/99-custom.ini

# Add your settings here, for example:
upload_max_filesize = 10M
memory_limit = 256M

This method offers significant advantages. If the PHP package is updated, any changes to this specific .ini file you created will not be overwritten because the package manager generally leaves files in conf.d/ alone unless they are explicitly listed for replacement. Furthermore, using numbered prefixes (like 99-) ensures your custom settings are loaded after the default configuration, allowing them to override defaults without needing to comment out lines in the main file.

Another critical step often overlooked is verifying that the changes actually take effect. Editing a file is only half the battle; you must reload the PHP environment. For CLI tools, this usually means restarting any processes using the CLI version of PHP or simply running php -v to confirm the new values are read. If you are using a web server like Apache or Nginx with PHP-FPM, you must restart those services as well.

sudo service php5-fpm restart
sudo service apache2 restart

Finally, while direct editing of /etc/php5/cli/php.ini is technically possible and widely done in production environments that are rarely upgraded, the risk of data loss during an OS upgrade remains. By leveraging the conf.d/ directory or utilizing package diversion tools when necessary, you create a separation between system-managed defaults and your custom requirements. This architectural choice ensures that your configuration survives the inevitable lifecycle updates of your operating system, maintaining stability without requiring manual intervention every time a security patch is applied.

Our network