How To Upgrade PHP 7.0 to PHP 8.0 Version On Ubuntu 18.04 LAMP Stack?

Developer, WordPress

How To Upgrade PHP 7.0 to PHP 8.0 Version On Ubuntu 18.04 LAMP Stack?

How to upgrade PHP 7 to PHP 8

Many beginner users face these common issues on the server while upgrading the web development environment for its applications. And 80% of users failed while upgrading this requirement because third-party blogs and tutorials do not cover every error, rollback, and troubleshooting method.

In this article, we will go through the steps to upgrade PHP 7.x to PHP 8.0 on an Ubuntu 18.04 LAMP stack with all error and troubleshooting points. But, before we start we need to know Ubuntu 18.04 LAMP Stack.

What is Ubuntu 18.04 LAMP Stack?

Ubuntu 18.04 LAMP stack is a popular web development environment that consists of the following components:

  • Ubuntu 18.04 LTS (Long Term Support) operating system
  • Apache HTTP Server: A free and open-source web server software that is widely used to serve web pages.
  • MySQL or MariaDB: A relational database management system that is used to store and manage data for web applications.
  • PHP: A popular server-side scripting language that is used to build dynamic web applications.

The acronym “LAMP” is formed from the first letter of each of the above components. Together, these components provide a powerful platform for web development and are used by many developers and organizations around the world.

By using a LAMP stack, developers can create and test web applications in a local development environment, before deploying them to a production server. This helps to ensure that the application is stable, secure, and meets the requirements of the end-users.

Is there any problem I face if I upgrade PHP to the higher version on Ubuntu 18.04 LAMP?

Upgrading PHP from version 7.2 to 8.0 on Ubuntu 18.04 LAMP stack may introduce some compatibility issues with your existing PHP applications, especially if they are using deprecated functions or features that have been removed in PHP 8.0.

Here are some potential issues that you may face:

  1. Deprecated features: Some PHP functions, extensions, or features that were deprecated in PHP 7.2 may have been removed in PHP 8.0. If your code relies on these deprecated features, it may not work properly on PHP 8.0 and you may need to update your code.
  2. Extension compatibility: Some PHP extensions that were compatible with PHP 7.2 may not be compatible with PHP 8.0, or may require updates or recompilation to work properly. You may need to check if your extensions are compatible with PHP 8.0 and update them if necessary.
  3. Configuration changes: PHP 8.0 may introduce some configuration changes, such as changes to the default values of some PHP settings or the removal of some deprecated settings. You may need to review your PHP configuration and update it accordingly.
  4. Application compatibility: Your existing PHP applications may not be fully compatible with PHP 8.0, and you may need to update them to ensure they work properly on the new version.

It is always recommended to test your applications on a staging or development environment before upgrad PHP on your production server. This will allow you to identify any compatibility issues and address them before they impact your production environment.

Here is the complete solution with error and troubleshooting while upgrading the PHP 7 to PHP 8

1. Add the ondrej/php PPA to your system by running the following commands:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

2. Update your system's package list:

sudo apt update

3. Install PHP 8.0 and its required extensions:

sudo apt install php8.0 php8.0-common php8.0-cli php8.0-fpm php8.0-mysql php8.0-xml php8.0-curl php8.0-gd php8.0-mbstring php8.0-opcache php8.0-zip

4. Verify that PHP 8.0 is installed:

php -v

You will get output like that:

PHP 8.0.28 (cli) (built: Feb 14 2023 18:32:25) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.28, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.28, Copyright (c), by Zend Technologies

5. Restart the Apache web server:

sudo systemctl restart apache2

6. After completing these steps, your Ubuntu 18.04 LAMP stack should now be running PHP 8.0. You can verify this by creating a PHP info file and accessing it through a web browser:

sudo nano /var/www/html/info.php

7. Add the following content to the file:

<?php
phpinfo();
?>

OR

you can create same php file using any file editor and upload your directoy /var/www/html/

8. Save and close the file. Access the file in your web browser by going to:

http://your-server-ip/info.php
OR
http://domain-name/info.php (if your IP is pointing to your server IP address)

9. This should display the PHP info page, which should show that PHP 8.0 is now installed and running on your system.
Check Server PHP Version

Error and Troubleshooting

Error 1

If I run the following command:

sudo apt install php8.0 php8.0-common php8.0-cli php8.0-fpm php8.0-mysql php8.0-xml php8.0-curl php8.0-gd php8.0-mbstring php8.0-opcache php8.0-zip

I getting the following errors

E: Unable to locate package php8.0
E: Couldn't find any package by glob 'php8.0'
E: Couldn't find any package by regex 'php8.0'
E: Unable to locate package php8.0-common
E: Couldn't find any package by glob 'php8.0-common'
E: Couldn't find any package by regex 'php8.0-common'
E: Unable to locate package php8.0-cli
E: Couldn't find any package by glob 'php8.0-cli'
E: Couldn't find any package by regex 'php8.0-cli'
E: Unable to locate package php8.0-fpm
E: Couldn't find any package by glob 'php8.0-fpm'
E: Couldn't find any package by regex 'php8.0-fpm'
E: Unable to locate package php8.0-mysql
E: Couldn't find any package by glob 'php8.0-mysql'
E: Couldn't find any package by regex 'php8.0-mysql'
E: Unable to locate package php8.0-xml
E: Couldn't find any package by glob 'php8.0-xml'
E: Couldn't find any package by regex 'php8.0-xml'
E: Unable to locate package php8.0-curl
E: Couldn't find any package by glob 'php8.0-curl'
E: Couldn't find any package by regex 'php8.0-curl'
E: Unable to locate package php8.0-gd
E: Couldn't find any package by glob 'php8.0-gd'
E: Couldn't find any package by regex 'php8.0-gd'
E: Unable to locate package php8.0-mbstring
E: Couldn't find any package by glob 'php8.0-mbstring'
E: Couldn't find any package by regex 'php8.0-mbstring'
E: Unable to locate package php8.0-opcache
E: Couldn't find any package by glob 'php8.0-opcache'
E: Couldn't find any package by regex 'php8.0-opcache'
E: Unable to locate package php8.0-zip
E: Couldn't find any package by glob 'php8.0-zip' E: Couldn't find any package by regex 'php8.0-zip'

Solution

It seems that the PPA for PHP 8.0 is not added to your Ubuntu 18.04 system yet. Here are the steps to add the PPA and install PHP 8.0:

1. Install the software-properties-common package:

sudo apt update
sudo apt install software-properties-common

2. Add the ondrej/php PPA:

sudo add-apt-repository ppa:ondrej/php

3. Update your system's package list:

sudo apt update

4. Install PHP 8.0 and its required extensions:

sudo apt install php8.0 php8.0-common php8.0-cli php8.0-fpm php8.0-mysql php8.0-xml php8.0-curl php8.0-gd php8.0-mbstring php8.0-opcache php8.0-zip

5. Verify that PHP 8.0 is installed:

php -v

This should display the PHP version information, indicating that PHP 8.0 is now the default version.

6. Restart the Apache web server:

sudo systemctl restart apache2

After completing these steps, your Ubuntu 18.04 LAMP stack should now be running PHP 8.0.

Note: Make sure to test your applications thoroughly after the upgrade PHP to ensure that they work as expected. Some code changes may be required to ensure compatibility with the new version of PHP.

Issue that may occur during this process

PHP 8.0 version is installed but after checking using the info.php file it’s showing the 7.2 version.

Troubleshooting the issue

This issue may occur if Apache is still configured to use PHP 7.2. To resolve this issue, you can follow these steps:

1. Disable PHP 7.2 module:

sudo a2dismod php7.2

2. Enable PHP 8.0 module:

sudo a2enmod php8.0

3. Restart Apache web server:

sudo systemctl restart apache2

4. Create a new PHP file with phpinfo() function to check PHP version:

sudo nano /var/www/html/info.php

Add the following code to the file:

<?php phpinfo(); ?>

5. Save and close the file.

6. Access the info.php file from your web browser by visiting http://your_server_ip/info.php.

You should now see the PHP 8.0 version information on the page.

Note: Remember to remove the info.php file when you are done testing as it may contain sensitive information about your server configuration.

Rollback PHP Version 8.0 to 7.2

To roll back to PHP 7.2, you can follow these steps:

1. Disable PHP 8.0 module:

sudo a2dismod php8.0

2. Enable PHP 7.2 module:

sudo a2enmod php7.2

3. Restart Apache web server:

sudo systemctl restart apache2

After completing these steps, Apache will use PHP 7.2 instead of PHP 8.0. You can verify this by creating a new PHP file with phpinfo() the function and checking the version information. Note that you should remove the info.php file that you created earlier as it may contain sensitive information about your server configuration.

Error 2

while running this command sudo a2enmod php8.0

Showing error ERROR: Module php8.0 does not exist!

Solution

If you are getting an error message saying “ERROR: Module php8.0 does not exist!” while running the command sudo a2enmod php8.0, it could mean that the PHP 8.0 module was not installed properly.

You can try reinstalling the PHP 8.0 module by running the following command:

sudo apt install libapache2-mod-php8.0

After the installation is complete, you can try enabling the PHP 8.0 module again with the following command:

sudo a2enmod php8.0

Finally, restart the Apache web server with the following command:

sudo systemctl restart apache2

Now you should be able to use PHP 8.0 on your server.

Conclusion

Upgrading PHP 7.0 to PHP 8.0 on an Ubuntu 18.04 LAMP stack is a relatively straightforward process that can be accomplished by following the steps outlined in this article. By upgrading to PHP 8.0, you can take advantage of the latest features and performance improvements, while ensuring the security and stability of your web application. It is important to note that before upgrading, you should always back up your files and database to avoid any potential data loss. With the steps outlined in this article, you can confidently upgrade PHP version and take your web application to the next level.

Alexia Barlier
Faraz Frank

Hi! I am Faraz Frank. A freelance WordPress developer.