Running your CodeIgniter web application with PHP 7.4 on Ubuntu may be essential if your application is incompatible with newer PHP versions. This article provides a step-by-step guide to removing a newer version of PHP, installing PHP 7.4, and configuring it on your Ubuntu server.
Table of Contents:
- Why Use PHP 7.4 on Ubuntu?
- Step 1: Remove Existing PHP Version
- Step 2: Add the PHP PPA Repository
- Step 3: Install PHP 7.4 and Necessary Extensions
- Step 4: Set PHP 7.4 as the Default Version
- Step 5: Configure Your Web Server
- Step 6: Verify PHP Version
- Step 7: Test Your CodeIgniter Application
- Troubleshooting Guide
- Conclusion
Why Use PHP 7.4 on Ubuntu?
Many web applications, including CodeIgniter, are built to run smoothly on PHP 7.4 due to its stability and broad extension support. While newer versions of PHP provide additional features, your application may not yet support them, necessitating the use of PHP 7.4 on Ubuntu. This guide helps ensure your environment matches the required PHP version.
Step 1: Remove Existing PHP Version
If you have already installed a newer version of PHP (like PHP 8.x), it must be removed before installing PHP 7.4 on Ubuntu.
$ sudo apt remove php8.*
$ sudo apt autoremove
This command removes PHP 8.x and its dependencies, freeing up your system for PHP 7.4 installation.
Related: Ubuntu CodeIgniter Setup: A Step-by-Step Installation Guide
Step 2: Add the PHP PPA Repository
Since PHP 7.4 is not included in the default Ubuntu repositories, you need to add the Ondřej Surý PPA, which maintains older versions of PHP.
Add the PPA:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php sudo
apt update
This repository provides access to PHP 7.4 on Ubuntu along with other older PHP versions.
Step 3: Install PHP 7.4 and Necessary Extensions
Install PHP 7.4 along with the required extensions to run your CodeIgniter application. These extensions include mysql
, xml
, mbstring
, and more.
sudo apt install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-xml php7.4-mbstring php7.4-json php7.4-curl php7.4-zip php7.4-intl
Step 4: Set PHP 7.4 as the Default Version
After installing PHP 7.4, ensure that it is set as the default version on your system. If multiple versions of PHP are installed, you can configure Ubuntu to use PHP 7.4 on Ubuntu by default:
sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4
Step 5: Configure Your Web Server
Now that PHP 7.4 on Ubuntu is installed, you need to configure your web server to work with PHP 7.4. Instructions are provided below for both Apache and Nginx.
Apache
If you’re using Apache, disable the current PHP module (for versions like PHP 8.x) and enable PHP 7.4:
sudo a2dismod php8.*
sudo a2enmod php7.4
sudo systemctl restart apache2
Nginx
For Nginx, update your site configuration to point to the PHP 7.4 FPM socket:
Edit your site’s Nginx configuration file (usually located at /etc/nginx/sites-available/your-site
).
Update the fastcgi_pass
directive to use the PHP 7.4 socket:
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
Restart Nginx and PHP-FPM:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
Step 6: Verify PHP Version
To confirm that PHP 7.4 is installed correctly, run the following command:
php -v
You should see output like:
PHP 7.4.33 (cli) (built: ...)
Step 7: Test Your CodeIgniter Application
Now that PHP 7.4 on Ubuntu is installed, test your CodeIgniter application by navigating to your site in a browser. Ensure that all required PHP extensions are enabled for your application to function correctly. You can check installed PHP modules using:
$ php -m
If any necessary extensions are missing, install them using:
$ sudo apt install php7.4-<extension-name>
For more information on configuring PHP for CodeIgniter, refer to the official CodeIgniter documentation.
Troubleshooting Guide
This step-by-step guide, designed for Ubuntu users, will help you quickly resolve common issues like CAPTCHA generation and routing errors. Follow these solutions to boost your troubleshooting skills and streamline your development process today!
CodeIgniter Troubleshooting: A Comprehensive Guide for Ubuntu
Conclusion
Installing PHP 7.4 on Ubuntu ensures that your CodeIgniter web application runs on a compatible and stable version of PHP. By following this guide, you have successfully removed any newer PHP version, installed PHP 7.4, configured your web server, and verified that your environment is ready for CodeIgniter.
For additional tips on optimizing PHP for performance, check out our guide to securing your Termux environment and how to pause and play cvlc from the terminal for media server setups.