Using PHP 7.2 on an Ubuntu server is highly recommended over previous PHP versions for several reasons, first being security. Active Support for PHP 7.2 goes until November 30th, 2019 and Security Support until Nov 30, 2020. Older versions like 7.0 and anything 5.6 and below are no longer getting any support and can leave open security holes on a server if they are not replaced. Another main reason to upgrade is the big performance increase over previous versions when PHP 7.2 is installed and is using the OPcache module. This can greatly decrease the time it takes for your webpage to load! If you are developing a site locally or launching it on one of Liquid Web’s Ubuntu VPS or Dedicated Servers, using PHP 7.2 or newer would be the way to go.
Pre-flight: Check the Server Environment
- Correct Permissions: First, make sure you have proper root or sudo permissions to be able to manage the system and applications on the server. If you only have access to a non-root user on the system, then you may have to get more permissions from the server admin.
- Existing PHP Versions: Check for an existing version of PHP. If you selected the default LAMP stack when first installing the Ubuntu image onto the server, it defaults to PHP 7.0. If you used a different image or did a plain install, then you may not have any version of PHP on the server. To check the PHP version access the terminal and run:
user@test:~# php -v
PHP 7.0.30-0ubuntu0.16.04.1 (cli) ( NTS )
If you see an older PHP version, then you will have to install the new version and switch the version Apache connects to later. If it shows “Command php not found” then there is no PHP version currently installed.
- Creating a Quick Backup: If you do have an existing PHP version, you may want to make a backup of any configurations before doing the upgrade in case there are issues (like if your site code wasn’t compatible) and you need to downgrade again. You can check on the current configuration directory by running:
user@test:~# php --ini
Configuration File (php.ini) Path: /etc/php/7.0/cli
Loaded Configuration File: /etc/php/7.0/cli/php.ini
Scan for additional .ini files in: /etc/php/7.0/cli/conf.d
user@test:~# sudo cp -a /etc/php/7.0/ /etc/php/7.0.backup
Also, you can save a list of existing PHP modules by running:
user@test:~# php -m > /tmp/7.0.modulelist.txt
This can help when comparing the existing modules to the modules in PHP 7.2.
Installing PHP 7.2
The official repository for PHP on Ubuntu is from Ondřej Surý on launchpad. To add it to your system, you need to run the following command:
user@test:~# sudo add-apt-repository ppa:ondrej/php
Press enter when it prompts you to. This allows your system to use the repository as a source for new software. The next command grabs the list of available software packages for your system:
user@test:~# sudo apt-get update
After that you can proceed with the install:
user@test:~# sudo apt-get install php7.2
It should show several different packages that it will install and how much total disk space they will use. When it prompts you, type ‘Y’ and submit it. After a few moments the install should be complete, check the PHP version with:
user@test:~# php -v
It should now show PHP 7.2, but now we need to have Apache configured to use the newer version.
Configuring Apache to Use PHP 7.2
Ubuntu 16.04 uses a few different commands to help manage Apache modules so the way it utilizes a specific PHP version depends on which module is loaded. You can view the list of available modules by running:
user@test:~# ls /etc/apache2/mods-available/php*
You may have other modules as well, the default LAMP stack install would have 7.0 and the new 7.2 install we just made, but running the next command shows that 7.0 is still active:
user@test:~# ls /etc/apache2/mods-enabled/php*
user@test:~# sudo a2dismod php7.0
user@test:~# sudo a2enmod php7.2
Before restarting Apache, check the Apache configuration syntax by running:
user@test:~# apachectl -t
If it says the syntax is OK, then restart Apache:
user@test:~# sudo service apache2 restart
Verifying Apache’s PHP Version
Now to check your work, run the previous command again to see which module is loaded and it should show the PHP 7.2 version:
user@test:~# ls /etc/apache2/mods-enabled/php*
If you do not see the same results as shown in the last few commands, recheck the spelling and syntax of the last few commands and make sure sudo was entered if you are not running as the root user.
Then to test it on your site, we recommend making a phpinfo file you can view on your domain. This shows the PHP version as well as information about current configurations, variables, and all the added modules. Create a new .php file in your site’s home folder or in Apache’s document root (/var/www/html by default) and include this code:
<?php phpinfo(); ?>
Then if you visit the page in the browser it should look something like this:
If you had a site already up and running on the previous PHP version, test it out now and see if there are any issues. If your code isn’t compatible or showing errors, you can try to diagnose further or if you need the site back up right away you can downgrade the PHP version again by running the a2dismod and a2enmod commands to disable PHP 7.2 and then enable 7.0 or whichever previous version you had on the server then restart Apache again.
Some sites may now show an error saying a module is missing and can’t run, or if you are developing a new site and have a list of modules that are needed, then the next section can help you add new modules.
Adding Modules to PHP 7.2
The switch to PHP 7.2 doesn’t automatically keep all the same modules from previous PHP versions. If you don’t need to compare the list to a previous PHP version then to install any basic modules you do need, first type the comannd below. But don’t press enter, if you press the TAB key twice you should get a list of available modules specific to 7.2::
@test:~# sudo apt-get install php7.2[tab][tab]
php7.2 php7.2-enchant php7.2-mbstring php7.2-snmp
php7.2-bcmath php7.2-fpm php7.2-mysql php7.2-soap
php7.2-bz2 php7.2-gd php7.2-odbc php7.2-sqlite3
php7.2-cgi php7.2-gmp php7.2-opcache php7.2-sybase
php7.2-cli php7.2-imap php7.2-pgsql php7.2-tidy
php7.2-common php7.2-interbase php7.2-phpdbg php7.2-xml
php7.2-curl php7.2-intl php7.2-pspell php7.2-xmlrpc
php7.2-dba php7.2-json php7.2-readline php7.2-xsl
php7.2-dev php7.2-ldap php7.2-recode php7.2-zip
This is not a complete list so you may need to look up how to install other modules you may need. You can type the modules you want, and you can add multiple in the same install command, like:
user@test:~# sudo apt-get install php7.2-opcache php7.2-mbstring php-memcached
If you did save the list of modules from a previous PHP version with the command earlier in the guide, you can save a list of the modules in PHP 7.2 and compare them with the previous version. Run:
php -m > /tmp/7.2.modulelist.txt
Then to compare it to the list that was created before, run:
user@test:~# diff /tmp/7.0.modulelist.txt /tmp/7.2.modulelist.txt
Or if you want a more visual way to compare the two lists, run:
user@test:~# vimdiff /tmp/7.0.modulelist.txt /tmp/7.2.modulelist.txt
Which will show a page like this in the terminal:
In this example, the various MySQL modules and soap show up on the list of 7.0 modules on the left but are missing from the 7.2 version on the right, and the sodium module is on 7.2 but wasn’t in 7.0.
So for this example, we can install the missing modules with:
user@test:~# sudo apt-get install php7.2-mysql php7.2-soap
After that installs, we can save an updated list of modules and can compare it again:
user@test:~# php -m > /tmp/7.2.modulelist.updated.txt
diff /tmp/7.0.modulelist.txt /tmp/7.2.modulelist.updated.txt
Only the sodium module is different between the module lists, so it is now ready for development! Hopefully, this guide helped with your upgrade to PHP 7.2! If you had any issues or need further help with your Liquid Web Ubuntu Server, please let our Linux Support Team know!