
How to Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 24
Setting up PHP 7.4 on Ubuntu 24 might seem straightforward, but there are nuances that can trip up even experienced developers, especially since Ubuntu 24.04 ships with PHP 8.x by default. Whether you’re maintaining legacy applications or working with specific frameworks that haven’t fully migrated to PHP 8, knowing how to properly install and configure PHP 7.4 is crucial. This guide walks you through the complete process of installing PHP 7.4, configuring Apache/Nginx, setting up essential extensions, and creating a robust local development environment that mirrors production setups.
Why PHP 7.4 Still Matters in 2024
While PHP 8.x offers significant performance improvements and new features, PHP 7.4 remains relevant for several reasons. Many enterprise applications and WordPress sites still run on PHP 7.4, and some legacy codebases aren’t ready for the breaking changes introduced in PHP 8. Additionally, certain third-party libraries and frameworks may not be fully compatible with newer PHP versions.
PHP 7.4 introduced several important features including typed properties, arrow functions, and null coalescing assignment operators. It strikes a balance between modern PHP capabilities and backward compatibility, making it an ideal choice for developers working with mixed environments.
Prerequisites and System Preparation
Before diving into the installation, ensure your Ubuntu 24.04 system is up to date and ready. You’ll need sudo privileges and a basic understanding of command-line operations.
sudo apt update && sudo apt upgrade -y
sudo apt install software-properties-common apt-transport-https ca-certificates -y
Check if PHP is already installed on your system:
php -v
If PHP 8.x appears, don’t worry – we’ll configure multiple PHP versions to coexist peacefully.
Installing PHP 7.4 Using Ondrej’s PPA
Ubuntu 24.04 doesn’t include PHP 7.4 in its official repositories, so we’ll use Ondrej Surý’s well-maintained PPA. This repository is trusted by the PHP community and provides reliable packages for older PHP versions.
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Install PHP 7.4 along with essential modules:
sudo apt install php7.4 php7.4-cli php7.4-fpm php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath -y
Verify the installation:
php7.4 -v
You should see output similar to:
PHP 7.4.33 (cli) (built: Nov 13 2023 09:45:52) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0
Configuring Apache with PHP 7.4
If you’re using Apache as your web server, you’ll need to configure it to work with PHP 7.4. First, install Apache if it’s not already present:
sudo apt install apache2 -y
Install the Apache PHP 7.4 module:
sudo apt install libapache2-mod-php7.4 -y
If you have multiple PHP versions installed, disable other versions and enable PHP 7.4:
sudo a2dismod php8.1 php8.2 php8.3
sudo a2enmod php7.4
sudo systemctl restart apache2
Create a test file to verify PHP is working:
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
Visit http://localhost/info.php
in your browser to confirm PHP 7.4 is running properly. Remember to delete this file afterward for security reasons.
Setting Up Nginx with PHP-FPM 7.4
For those preferring Nginx, the setup involves configuring PHP-FPM (FastCGI Process Manager). Install Nginx first:
sudo apt install nginx -y
Ensure PHP 7.4 FPM is installed and running:
sudo systemctl status php7.4-fpm
sudo systemctl enable php7.4-fpm
sudo systemctl start php7.4-fpm
Configure a basic Nginx server block for your development environment:
sudo nano /etc/nginx/sites-available/php74-dev
Add this configuration:
server {
listen 80;
server_name php74.local;
root /var/www/php74-dev;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Enable the site and create the document root:
sudo ln -s /etc/nginx/sites-available/php74-dev /etc/nginx/sites-enabled/
sudo mkdir -p /var/www/php74-dev
sudo chown -R www-data:www-data /var/www/php74-dev
sudo nginx -t
sudo systemctl reload nginx
Add the domain to your hosts file:
echo "127.0.0.1 php74.local" | sudo tee -a /etc/hosts
Managing Multiple PHP Versions
One of the powerful features of this setup is the ability to switch between PHP versions seamlessly. Ubuntu’s update-alternatives
system makes this process straightforward.
Set up alternatives for PHP CLI:
sudo update-alternatives --install /usr/bin/php php /usr/bin/php7.4 74
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.1 81
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.2 82
Switch between versions interactively:
sudo update-alternatives --config php
You can also create version-specific aliases in your .bashrc
or .zshrc
:
alias php74='/usr/bin/php7.4'
alias php81='/usr/bin/php8.1'
alias php82='/usr/bin/php8.2'
Essential PHP Extensions and Configuration
A proper development environment requires additional PHP extensions beyond the basic installation. Here’s a comprehensive list of commonly needed extensions:
sudo apt install php7.4-dev php7.4-xdebug php7.4-imagick php7.4-redis php7.4-memcached php7.4-sqlite3 php7.4-pgsql php7.4-ldap php7.4-soap php7.4-intl -y
Extension | Purpose | Common Use Cases |
---|---|---|
php7.4-xdebug | Debugging and profiling | Step debugging, code coverage, performance profiling |
php7.4-imagick | Image manipulation | Image processing, thumbnail generation, format conversion |
php7.4-redis | Redis client | Caching, session storage, pub/sub messaging |
php7.4-memcached | Memcached client | Distributed caching, session management |
php7.4-intl | Internationalization | Unicode handling, locale-specific operations |
Configure PHP settings for development. Edit the PHP configuration file:
sudo nano /etc/php/7.4/cli/php.ini
For web server configurations:
# For Apache
sudo nano /etc/php/7.4/apache2/php.ini
# For Nginx (PHP-FPM)
sudo nano /etc/php/7.4/fpm/php.ini
Recommended development settings:
; Development settings
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 64M
max_execution_time = 300
max_input_vars = 3000
; OpCache settings for development
opcache.enable = 1
opcache.revalidate_freq = 0
opcache.validate_timestamps = 1
opcache.max_accelerated_files = 10000
opcache.memory_consumption = 192
Configuring Xdebug for Development
Xdebug is essential for professional PHP development. Configure it properly for your development workflow:
sudo nano /etc/php/7.4/mods-available/xdebug.ini
Add comprehensive Xdebug configuration:
zend_extension=xdebug.so
; Xdebug 3 configuration for PHP 7.4
xdebug.mode = debug,coverage,profile
xdebug.start_with_request = trigger
xdebug.client_host = 127.0.0.1
xdebug.client_port = 9003
xdebug.discover_client_host = false
xdebug.idekey = VSCODE
; Profiling
xdebug.output_dir = /tmp/xdebug
xdebug.profiler_output_name = cachegrind.out.%t-%s
; Coverage
xdebug.coverage_enable = 1
; Logging
xdebug.log = /tmp/xdebug/xdebug.log
xdebug.log_level = 7
Create the output directory and restart services:
sudo mkdir -p /tmp/xdebug
sudo chmod 777 /tmp/xdebug
sudo systemctl restart apache2 # or nginx and php7.4-fpm
Database Integration and Testing
A complete development environment needs database connectivity. Install and configure MySQL and PostgreSQL support:
# Install MySQL server and client
sudo apt install mysql-server mysql-client -y
# Install PostgreSQL
sudo apt install postgresql postgresql-contrib -y
# Install SQLite for lightweight testing
sudo apt install sqlite3 -y
Test database connections with a simple PHP script:
<?php
// MySQL connection test
try {
$mysql = new PDO('mysql:host=localhost;dbname=test', 'root', '');
echo "MySQL: Connected successfully\n";
} catch(PDOException $e) {
echo "MySQL: Connection failed - " . $e->getMessage() . "\n";
}
// PostgreSQL connection test
try {
$pgsql = new PDO('pgsql:host=localhost;dbname=postgres', 'postgres', '');
echo "PostgreSQL: Connected successfully\n";
} catch(PDOException $e) {
echo "PostgreSQL: Connection failed - " . $e->getMessage() . "\n";
}
// SQLite connection test
try {
$sqlite = new PDO('sqlite:/tmp/test.db');
echo "SQLite: Connected successfully\n";
} catch(PDOException $e) {
echo "SQLite: Connection failed - " . $e->getMessage() . "\n";
}
?>
Composer and Dependency Management
Install Composer globally for PHP package management:
curl -sS https://getcomposer.org/installer | php7.4
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
Verify Composer installation:
composer --version
For projects requiring specific PHP versions, use Composer’s platform requirements:
{
"require": {
"php": "^7.4"
},
"config": {
"platform": {
"php": "7.4.33"
}
}
}
Performance Optimization and Benchmarking
Configure PHP 7.4 for optimal development performance. These settings balance functionality with speed:
; OPcache optimization
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.fast_shutdown=1
opcache.enable_cli=1
; Realpath cache
realpath_cache_size=4096K
realpath_cache_ttl=600
Benchmark your PHP setup with a simple performance test:
<?php
$start = microtime(true);
// CPU intensive task
for($i = 0; $i < 1000000; $i++) {
$result = sin($i) * cos($i);
}
// Memory allocation test
$array = [];
for($i = 0; $i < 100000; $i++) {
$array[] = str_repeat('test', 10);
}
$end = microtime(true);
$memory = memory_get_peak_usage(true) / 1024 / 1024;
echo "Execution time: " . round($end - $start, 4) . " seconds\n";
echo "Peak memory usage: " . round($memory, 2) . " MB\n";
echo "PHP version: " . PHP_VERSION . "\n";
?>
Configuration | Execution Time | Memory Usage | Use Case |
---|---|---|---|
Default PHP 7.4 | ~0.15s | ~12MB | Basic development |
With OPcache | ~0.12s | ~11MB | Production-like testing |
Debug mode + Xdebug | ~0.25s | ~15MB | Active debugging |
Common Issues and Troubleshooting
Even with careful setup, you might encounter issues. Here are solutions to common problems:
- Port conflicts: If port 80 is occupied, configure alternative ports or stop conflicting services
- Permission issues: Ensure proper ownership of web directories (
sudo chown -R www-data:www-data /var/www
) - Extension loading errors: Check PHP error logs at
/var/log/php7.4-fpm.log
or/var/log/apache2/error.log
- Memory limits: Increase memory_limit in php.ini for resource-intensive applications
- SSL certificate issues: For HTTPS development, create self-signed certificates or use tools like mkcert
Debug PHP-FPM issues:
sudo systemctl status php7.4-fpm
sudo journalctl -u php7.4-fpm -f
tail -f /var/log/php7.4-fpm.log
Check loaded extensions and configuration:
php7.4 -m # List loaded modules
php7.4 --ini # Show configuration file locations
php7.4 -i | grep -i extension_dir # Show extension directory
Development Tools and IDE Integration
Configure your development environment with popular PHP tools. Install PHPUnit for testing:
composer global require phpunit/phpunit:^9.0
echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Install PHP CodeSniffer for code quality:
composer global require squizlabs/php_codesniffer
phpcs --version
For Visual Studio Code users, create a .vscode/settings.json
file in your project:
{
"php.validate.executablePath": "/usr/bin/php7.4",
"php.debug.executablePath": "/usr/bin/php7.4",
"php.executablePath": "/usr/bin/php7.4"
}
Production Deployment Considerations
When deploying applications developed with this setup, consider these factors:
- Security hardening: Disable unnecessary extensions and functions in production
- Error handling: Set
display_errors = Off
and configure proper logging - Performance tuning: Enable OPcache with production-optimized settings
- Monitoring: Implement APM tools like New Relic or Datadog for production monitoring
For scalable hosting solutions that support PHP 7.4, consider robust infrastructure options. Whether you need flexible VPS services for development staging or powerful dedicated servers for production workloads, proper hosting infrastructure ensures your PHP applications perform optimally.
Advanced Configuration and Optimization
Fine-tune your PHP 7.4 environment for specific use cases. For high-traffic applications, configure PHP-FPM pools:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
Optimize pool settings based on your system resources:
[www]
user = www-data
group = www-data
listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.process_idle_timeout = 10s
pm.max_requests = 500
Enable slow log for performance monitoring:
slowlog = /var/log/php7.4-fpm-slow.log
request_slowlog_timeout = 5s
This comprehensive setup provides a solid foundation for PHP 7.4 development on Ubuntu 24. The configuration balances performance, functionality, and ease of use while maintaining compatibility with modern development practices. Regular maintenance, security updates, and monitoring will ensure your development environment remains reliable and secure.
For additional information about PHP configuration and best practices, consult the official PHP documentation and the Ubuntu Server Guide.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.