PHP on Ubuntu

posted in: Allgemein | 0

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 (the default in Ubuntu 18.04) 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 16.04

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.

Update Ubuntu

First, before you do anything else, you should update your Ubuntu server:

apt-get update && apt-get upgrade

Install PHP

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:

  • php-common
  • php7.0
  • php7.0-cli
  • php7.0-common
  • php7.0-fpm
  • php7.0-json
  • php7.0-opcache
  • php7.0-readline

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

php -v

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

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.

Update Ubuntu

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:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

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:

  • libapache2-mod-php7.2
  • libargon2-0
  • libsodium23
  • libssl1.1
  • php7.2-cli
  • php7.2-common
  • php7.2-json
  • php7.2-opcache
  • php7.2-readline

And that’s it. To check if PHP 7.2 is installed on your server, run the following command:

php -v

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 Install PHP 7.2 on Ubuntu 18.04

PHP 7.2 is included by default in Ubuntu’s repositories since version 18.04. So the instructions are pretty similar to PHP 7 for 16.04.

Update Ubuntu

Again, before doing anything, you should update your server:

apt-get update && apt-get upgrade

Install PHP 7.2

Next, to install PHP 7.2 on Ubuntu 18.04, just run the following command:

apt-get install php

This command will install PHP 7.2, as well as some other dependencies.

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

PHP 7.2.3-1ubuntu1 (cli) (built: Mar 14 2018 22:03:58) ( NTS )

And that’s it. PHP 7.2 is installed on your Ubuntu 18.04 server.

Install PHP 7.2 modules

These are the most common PHP 7.2 modules often used by php applications. You may need more or less, so check the requirements of the software you’re planning to use:

apt-get install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php

To check all the PHP modules available in Ubuntu, run:

apt-cache search --names-only ^php

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:

a2enmod php7.2

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:

  1. Backup everything.
  2. Install the newest PHP and required modules.
  3. Change the default version you’re using.
  4. (Optionally) Remove the older PHP
  5. (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.

 

 

Apache Configuration

Now enable few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with Apache server.

sudo a2enmod actions fastcgi alias proxy_fcgi

Get ready for the configuration of websites on your Apache server. For the testing purpose, I am configuring two websites to work with two different-2 PHP versions. First, create two directories on your server.

sudo mkdir /var/www/php56
sudo mkdir /var/www/php72

Now, create and index.php containing the phpinfo() function.

echo "<?php phpinfo(); ?>" > /var/www/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/php72/index.php

Let’s start the creation of VirtualHost. Apache keeps all the VirtualHost configuration files under /etc/apache2/sites-available with the extension .conf. Create a file for the first virtual host and edit in your favorite text editor.

sudo vim /etc/apache2/sites-available/php56.example.com.conf

Add the following content. Make sure to use correct ServerName and directory path according to your setup. This website is configured to work with PHP 5.6.

Similarly, create a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor:

sudo vim /etc/apache2/sites-available/php72.example.com.conf

Add the following content to file with proper ServerName and DocumentRoot.

You both of the websites are configured now. But they are still not active. Apache keeps active sites under /etc/apache2/sites-enabled directory. You can simply create a symbolic link of config files to this directory or use below command to do the same.

sudo a2ensite php56.example.com
sudo a2ensite php72.example.com

After making all the changes restart Apache to reload new settings changes.

sudo systemctl restart apache2

Your setup has been completed now. Go to next step to test your setup.