Finally, the third part of our LAMP tutorial series: how to install PHP on Ubuntu. In this tutorial, we’ll show you how to install various versions of PHP, including PHP 7, and the latest PHP 7.2
This tutorial should work for any Ubuntu release and other Ubuntu-based releases. Ubuntu 14.04, Ubuntu 16.04, Ubuntu 18.04, even Ubuntu 17.10. It was tested and written for Ubuntu 16.04.
For the first part of our LAMP series, go to our How to Install and Optimize Apache on Ubuntu tutorial.
And for the second part, go to How to Install MySQL/MariaDB on Ubuntu.
Before we begin installing PHP on Ubuntu
- PHP has different versions and releases you can use. Starting from the oldest that is currently supported – PHP 5.6, and onto PHP 7, PHP 7.1, and the latest – PHP 7.2. We’ll include instructions for PHP 7.2 and the default PHP version in the Ubuntu 16.04 repositories – PHP 7. We recommend that you install PHP 7.2 as it’s stable and has lots of improvements and new features. If you still use PHP 5.6, you definitely need to upgrade ASAP.
- You’ll obviously need an Ubuntu server. You can get one from Vultr. Their servers start at $2.5 per month. Or you can go with any other cloud server provider where you have root access to the server.
- You’ll also need root access to your server. Either use the root user or a user with sudo access. We’ll use the root user in our tutorial so there’s no need to execute each command with ‘sudo’, but if you’re not using the root user, you’ll need to do that.
- You’ll need SSH enabled if you use Ubuntu or an SSH client like MobaXterm if you use Windows.
- Check if PHP is already installed on your server. You can use the ‘which php’ command. If it gives you a result, it’s installed, if it doesn’t, PHP is not installed. You can also use the “php -v” command. If one version is installed, you can still upgrade to another.
- Some shared hosts have already implemented PHP 7.2 in their shared servers, like Hawk Host and SiteGround.
Now, onto our tutorial.
How to install PHP 7 on Ubuntu
Currently, as of January 2018, the default PHP release in the Ubuntu 16.04 repositories is PHP 7.0. We’ll show you how to install it using Ubuntu’s repository.
First, before you do anything else, you should update your Ubuntu server:
apt-get update && apt-get upgrade
Next, to install PHP, just run the following command:
apt-get install php
This command will install PHP 7.0, as well as some other dependencies:
To verify if PHP is installed, run the following command:
You should get a response similar to this:
And that’s it. PHP is installed on your Ubuntu server.
Install PHP 7.0 modules
You may need some additional packages and PHP modules in order for PHP to work with your applications. You can install the most commonly needed modules with:
apt-get install php-pear php7.0-dev php7.0-zip php7.0-curl php7.0-gd php7.0-mysql php7.0-mcrypt php7.0-xml libapache2-mod-php7.0
Depending on how and what you’re going to use, you may need additional PHP modules and packages. To check all the PHP modules available in Ubuntu, run:
apt-cache search --names-only ^php
You can tweak the command to only show ^php7.0- packages etc.
If you want to use the latest PHP version, follow the next instructions instead.
How to Install PHP 7.2 on Ubuntu 16.04, 18.04…
PHP 7.2 is the latest stable version of PHP and has many new features, improvements, and bug fixes. You should definitely use it if you want a better, faster website/application.
Of course, as always, first update Ubuntu:
apt-get update && apt-get upgrade
Add the PHP repository
You can use a third-party repository to install the latest version of PHP. We’ll use the repository by Ondřej Surý.
First, make sure you have the following package installed so you can add repositories:
apt-get install python-software-properties
Next, add the PHP repository from Ondřej:
And finally, update your package list:
Install PHP 7.2
After you’ve added the repository, you can install PHP 7.2 with the following command:
apt-get install php7.2
This command will install additional packages:
And that’s it. To check if PHP 7.2 is installed on your server, run the following command:
Install PHP 7.2 modules
You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:
apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml
And that’s all. You can now start using PHP on your Ubuntu server.
If you want to further tweak and configure your PHP, read our instructions below.
How to change the PHP version you’re using
If you have multiple PHP versions installed on your Ubuntu server, you can change what version is the default one.
To set PHP 7.0 as the default, run:
update-alternatives --set php /usr/bin/php7.0
To set PHP 7.2 as the default, run:
update-alternatives --set php /usr/bin/php7.2
If you’re following our LAMP tutorials and you’re using Apache, you can configure Apache to use PHP 7.2 with the following command:
And then restart Apache for the changes to take effect:
systemctl restart apache2
How to upgrade to PHP 7.2 on Ubuntu
If you’re already using an older version of PHP with some of your applications, you can upgrade by:
- Backup everything.
- Install the newest PHP and required modules.
- Change the default version you’re using.
- (Optionally) Remove the older PHP
- (Required) Configure your software to use the new PHP version. You’ll most likely need to configure Nginx/Apache, and many other services/applications. If you’re not sure what you need to do, contact professionals and let them do it for you.
Speed up PHP by using an opcode cache
You can improve the performance of your PHP by using a caching method. We’ll use APCu, but there are other alternatives available.
If you have the ‘php-pear’ module installed (we included it in our instructions above), you can install APCu with the following command:
pecl install apcu
There are also other ways you can install APCu, including using a package.
To start using APCu, you should run the following command for PHP 7.2:
echo "extension=apcu.so" | tee -a /etc/php/7.2/mods-available/cache.ini
And the following command for PHP 7.0:
echo "extension=apcu.so" | tee -a /etc/php/7.0/mods-available/cache.ini
If you’re following our LAMP tutorials and you’re using Apache, create a symlink for the file you’ve just created.
For PHP 7.2:
ln -s /etc/php/7.2/mods-available/cache.ini /etc/php/7.2/apache2/conf.d/30-cache.ini
For PHP 7.0:
ln -s /etc/php/7.0/mods-available/cache.ini /etc/php/7.0/apache2/conf.d/30-cache.ini
And finally, reload Apache for the changes to take effect:
systemctl restart apache2
To further configure APCu and how it works, you can add some additional lines to the cache.ini file you previously created. The best configuration depends on what kind of server you’re using, what applications you are using etc. Either google it and find a configuration that works for you, or contact professionals and let them do it for you.
That’s it for our basic setup. Of course, there are much more options and configurations you can do, but we’ll leave them for another tutorial.