Command Summary
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get autoremove
sudo apt-get install php7.0
sudo apt-get install php7.0-fpm
sudo apt-get install php7.0-mysql
Check php version by: php -v
Make sure apache2 starts: sudo service apache2 restart
Remove conflicting modules by: sudo a2dismod php5_noncompat_mod
Introduction
In this tutorial we show how to upgrade your LAMP stack‘s PHP to the latest version (7.X). This latest PHP version will be the recommended one for wordpress in mid-2017.
Why Upgrading?
Constantly upgrading your software stack has the following advantages:
- Fix security loopholes.
- Improve performance.
- Stay compatible with well-maintained applications and plugins.
Of course, upgrading stacks can also break some legacy software you use in your website. But then you should consider stop using these software, as they might introduce security threats and performance issues to your site.
As of PHP7, WordPress will start listing PHP7 as the recommended requirement in mid 2017 (source). I personally also saw a great speed improvement on my testing wordpress sites.
Upgrading Process Outline
The generate upgrading process is as follows:
- Backup your site and data.
- Add PHP7 repository.
- Do a regular Ubuntu package upgrade.
- Install PHP7.
- Install PHP7 packages relating to other software on the stack (apache2 and MySQL).
- Check PHP version.
- Check and restart apache2.
- Check if all site’s components work (main application and plugins).
Upgrading Process in Detail
Backup your site and data
Upgrading stack software is considered major changes to your server. Thus you should backup your site and data in case anything happens.
For wordpress I recommend using the Duplicator plugin. It backs up both the site and MySQL data for you all at once, and makes re-installation very easy.
Add PHP7 repository
You have to manually add the PHP7 repository so ubuntu’s package manager can find it. Do it by using add-apt-repository
.
sudo add-apt-repository ppa:ondrej/php
Do a regular Ubuntu package upgrade
Do the standard three apt-get
commands for Ubuntu. See here for more details.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get autoremove
Install PHP7
Install PHP7 package directly by:
sudo apt-get install php7.0
Install PHP7 packages relating to other software on the stack (apache2 and MySQL)
In LAMP stack we use apache2 and MySQL as well. Thus we also have to install the PHP7 packages interfacing to them if needed.
sudo apt-get install php7.0-mysql
Checking PHP Version
You should now check if the upgrade is successful. First thing to check is PHP’s version:
php -v
You should see the returned text starts with “PHP 7.X.Y...
”
Checking PHP Apache Version
You then should check if apache2 works correctly AND it is using the right PHP version. To check if apache2 is working, do a restart:
sudo service apache2 restart
Then check if apache2 is using PHP7. This can be done by several ways:
apache2ctl -M
- Many wordpress security plugins will display the current using PHP version, like WP All-in-one Security plugin.
Troubleshooting
In my case after I installed PHP7 apache2 still used the old version one. I had to manually enable the 7 version and disable the old version by:
sudo a2dismod php[old version number.]
sudo a2enmod php7.0
Even after this my apache2 failed to stop. By inspecting the enabled modules I found that I had to manually disable another PHP old version related modules.