Article

Table of Contents
Theme:
Was this article helpful?

1  out of  1 found this helpful

Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $600 by adding new articles!

Upgrade From PHP 7 to PHP 8 on Ubuntu 20.04 with Apache

Author: Chester Doran

Last Updated: Tue, Aug 3, 2021
PHP System Admin Ubuntu Web Servers

Introduction

This guide will show you how to migrate from PHP 7.x to PHP 8 on an Apache web server. Check the migration guide for new features and incompatible changes.

Prerequisites

Before proceeding, it is recommended to make a backup of your server. Then, test the backup by deploying a new instance from the backup, then verify that the test instance boots and has the correct data. If you do not make a backup before proceeding, you risk losing your data.

1. List Installed PHP Modules

Before upgrading PHP, find all the PHP 7.x modules currently installed on the server. These will need to be upgraded along with PHP core to their respective 8 versions.

Take note of the version number of the current PHP installation; this will be needed later.

$ dpkg -l | grep php

Output:
php-common                                  install
php7.x-cli                                  install
php7.x-curl                                 install
[...]

2. Install PHP 8

Ubuntu may not yet have PHP version 8 in its official repositories, and you can install PHP 8 from the ondrej/php repository, a long-time and community-trusted repository for Ubuntu PHP packages.

Add the necessary repository.

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

Install PHP 8.

$ sudo apt install php8.0

3. Install Modules

Install related PHP modules; below are some of the most frequently used. Please refer back to step 1 and manually install any modules necessary that are missing, replacing 7.x with 8.0.

$ sudo apt install php8.0-common php8.0-fpm php8.0-mysql php8.0-gmp php8.0-xml php8.0-xmlrpc php8.0-curl php8.0-mbstring php8.0-gd php8.0-dev php8.0-imap php8.0-opcache php8.0-readline php8.0-soap php8.0-zip php8.0-intl php8.0-cli libapache2-mod-php8.0

Once complete, restart PHP

$ sudo systemctl restart php8.0-fpm.service

Verify PHP has been installed correctly.

$ php -v

3. Enable PHP 8 in Apache

The a2enmod and a2dismod scripts can be used for enabling and disabling PHP versions in Apache.

  1. Disable the previous PHP version installed. Replace 7.x with the version number noted in step 1.

    $ sudo a2dismod php7.x
    
  2. Enable PHP 8.

    $ sudo a2enmod php8.0
    
  3. Restart the Apache web server.

    $ sudo systemctl restart apache2.service
    

4. Verify Apache is Using PHP 8

  1. Navigate to the document root of a website on the server. For this example the document root is /var/www/html.

    $ cd /var/www/html
    
  2. Create a PHP file.

    $ sudo nano phpinfo.php
    
  3. Add the following content to the file.

    <?php
      phpinfo();
    ?>
    
  4. Open a browser and access the file at http://[ip-address]/phpinfo.php or http://[domain]/phpinfo.php and verify it displays the correct PHP version.

Remember to remove phpinfo.php when finished to prevent exposing sensitive information about your server.

Conclusion

You have now successfully migrated from PHP 7.x to PHP 8 on your Apache web server.

Want to contribute?

You could earn up to $600 by adding new articles