If you’re setting up PHP on a fresh Ubuntu machine, a solid configuration is key to running secure and performant applications. I have worked on dozens of Linux servers for custom web apps, Moodle, CMSs like WordPress, and more. Regardless of the size of your project, these must-do PHP settings should be part of every deployment. This guide assumes you’re running PHP in its standard mode with Apache (mod_php) or calling it via the CLI (Command Line Interface).
Install PHP with the Right Extensions
Start by installing PHP and its commonly needed extensions following the commands below:
sudo apt update
sudo apt install php libapache2-mod-php php-cli php-mysql php-curl php-mbstring php-xml php-zip php-bcmath php-gd php-soap unzip
Set the Correct Timezone in php.ini
Ms-configured time-zones lead to messy logs and broken time-related functions. Set it properly:
sudo nano /etc/php/*/apache2/php.ini
Look for ;date.timezone =
un-comment and set the correct time zone : date.timezone = Africa/Kampala
Also do the above for the CLI php.ini:
sudo nano /etc/php/*/cli/php.ini
Adjust memory_limit, upload_max_filesize, and post_max_size
Default values are often too small for modern PHP apps. Update them to something more practical inside the php.ini file
sudo nano /etc/php/*/apache2/php.ini
memory_limit = 512M
upload_max_filesize = 100M
post_max_size = 100M
Increase Execution and Input Time
If you’re dealing with longer-running scripts (file uploads, complex forms, cron jobs), bump these up:
max_execution_time = 300
max_input_time = 300
Enable and Optimize OPcache
OPcache dramatically improves performance by caching precompiled PHP scripts. First, make sure it’s installed:
sudo apt install php-opcache
Then edit your php.ini file
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.validate_timestamps=1
Restart Apache After Changes
Once you’ve made your changes to php.ini, restart Apache for them to take effect:
sudo systemctl restart apache2