Upgrading Ubuntu 16.04 to 18.04 & PHP 7.0 to 7.2 for WordPress

  • Website
  • Published Dec 12, 2018 Updated Jun 18, 2021

This post describes how I upgraded our webserver running WordPress on Apache from Ubuntu 16.04.5 LTS to 18.04.1 LTS. Please see this article for more information on the server’s installation and configuration.

Backup

Before you begin, create a checkpoint (snapshot) in Hyper-V Manager. If anything goes wrong, a checkpoint makes it trivially easy to get back to the last working state.

Installing all Available Updates

sudo apt update
sudo apt dist-upgrade
sudo apt autoremove

Reboot and check Apache’s error log:

sudo shutdown -r now
tail /var/log/apache2/error.log

Upgrading to Ubuntu 18.04.1

sudo do-release-upgrade

During the upgrade process:

  • When asked whether to install the updated version of /etc/sysctl.conf, select “yes”
  • When asked whether to install the updated version of /etc/apache2/apache2.conf, select “no”
  • When asked whether to install the updated version of /etc/logrotate.d/apache2, select “yes”
  • When asked whether to install the updated version of /etc/ssh/sshd_config, select “keep the local version”
  • When asked whether to install the updated version of security.conf, select “no”

Upgrade Package Changes

Packages no Longer Supported

  • ntp
  • tcpd

You may want to uninstall these packages once the upgrade is finished by running the commands:

sudo apt remove ntp
sudo apt remove tcpd
sudo apt autoremove

Removed Packages

  • curl
  • systemd-shim
  • libapache2-modsecurity

Upgrade PHP Package Name Changes

In the upgrade from Ubuntu 16.04 to 18.04 the PHP version is upgraded from 7.0 to 7.2, which is a good thing. What is not so great is that the names of all the PHP packages change from php7.0-* to *php7.2-**. Due to that name change, Apache’s PHP configuration is broken after the upgrade and must be fixed manually.

Additionally, the upgrade routine is not clever enough to upgrade any manually installed PHP packages. The 7.0 packages are uninstalled instead of replacing them with their 7.2 counterparts. Run the following command to generate a list of all installed PHP packages so you can reinstall them for the new version in a later step:

dpkg -l | grep php | tee packages.txt

Migrating from PHP 7.0 to PHP 7.2

Apache Configuration

Enable the PHP 7.2 module:

sudo a2enmod php7.2
sudo service apache2 restart

Installing Missing PHP 7.2 Modules

Add all the modules from the file packages.txt generated earlier.

sudo apt install php7.2-mysql php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-opcache php7.2-tidy php7.2-xml php7.2-xmlrpc php7.2-zip php7.2-bcmath
sudo apt autoremove
sudo service apache2 restart
sudo service php7.2-fpm restart

Note: mcrypt is not available any more with PHP 7.2.

PHP 7.2 Hardening and Optimization

Edit the PHP configuration. If you are using FPM it should be located in /etc/php/7.2/fpm/php.ini. Otherwise try /etc/php/7.2/apache2/php.ini:

Add the following to disable_functions: exec,system,shell_exec,passthrough

Configure PHP’s opcache by setting:

opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=10
opcache.max_accelerated_files=10000

Increase the upload limit:

upload_max_filesize = 50M
post_max_size = 50M

Restart PHP-FPM:

sudo service php7.2-fpm restart

Increasing PHP Resources

Depending on your workload it may be necessary to increase the resources allocated to PHP.

Edit the PHP configuration. Modify the following settings:

; Default: 30
max_execution_time = 120
; Default: 128M
memory_limit = 256M

Restart PHP-FPM (or Apache, if you are not using PHP FPM):

sudo service php7.2-fpm restart

Fixing PHP Errors

PHP Warning “Illegal string offset”

Cause: a string variable is used like an array, e.g.:

$var[index] = "value";

Fix it by adding an array check:

// DATE PHP 7.2 compat: added check if $var actually is an array
if (is_array ($var))
   $var[index] = "value";

Removing Obsolete PHP Directories

Clean up remainders from earlier migrations:

sudo rm -r /etc/php5
sudo rm -r /etc/php/7.0

Adjusting the Logrotate Configuration

Edit /etc/logrotate.d/apache2 so that it says:

rotate 30
dateext

Re-enabling the mod_pagespeed Repository

This was disabled during the upgrade.

sudo rm /etc/apt/sources.list.d/mod-pagespeed.list
sudo mv /etc/apt/sources.list.d/mod-pagespeed.list.distUpgrade /etc/apt/sources.list.d/mod-pagespeed.list

Checking for errors

Check Apache’s error log:

tail /var/log/apache2/error.log

Comments

Related Posts

Guide: WordPress on Dockerized Apache on Hetzner Cloud

Guide: WordPress on Dockerized Apache on Hetzner Cloud
If you’ve followed this blog for a while, you may have noticed that I’ve used a traditionally installed (i.e., not dockerized) LAMP stack for its server setup since 2014. Only recently did I switch to Docker containers. Why? Maintenance. Much facilitated maintenance. If you take a look at the articles I’ve written over the years describing how to upgrade to newer versions of Ubuntu or - god beware - PHP, you can’t help but realize what a godawful PITA it all is. Switching to Docker enforces (or at least strongly encourages) a strict separation of (public) code and (personal) configuration. With this new setup, upgrading from one PHP version to another involves nothing more than changing a version number in a text file.
Website

Latest Posts

Scripted WordPress to Hugo Migration

Scripted WordPress to Hugo Migration
After having published in WordPress for almost 20 years, it was time for a change. This site is now rendered by Hugo, a static website generator built for Markdown content hosted in a Git repository. The migration from WordPress (HTML) to Hugo (Markdown) was far from trivial. Since I couldn’t find any tool for the job, I developed my own set of migration scripts that fully automate the migration process. You can find them on GitHub along with extensive documentation.
Website