Posts | About

Installing PHP on Ubuntu Linux

May 20, 2019 by Areg Sarkissian

Introduction

In this post I will detail the steps to install PHP onto an Ubuntu 18.04 (bionic) OS.

The instructions here can be applied in general to other Linux distributions.

You can install and use the Ubuntu distribution on Windows 10 using the Windows Subsystem for Linux (WSL) with platform specific changes.

Instructions for enabling WSL and installing a Linux distribution can be found at https://docs.microsoft.com/en-us/windows/wsl/install-win10

Note the upcoming WSL2 will provide native Linux support on the Windows 10 platform.

Testing the install with Docker

If you have Docker installed on your machine you can bring up an Ubuntu container (or any other Linux container) running bash in interactive mode to test the installation before installing on your local machine.

Here is the command to run bash in the Ubuntu 18.04 container:

docker run -ti --rm ubuntu:bionic bash

The first time you run this command, it will take a little bit of time to download the docker image. Subsequent runs will be very quick.

Note: Any of the changes below that you make to the running container will be lost if you exit the container.

Installing PHP using the APT package manager

Before installing the PHP files we should update the Ubuntu apt package manager after which we can add the package repository from where the PHP installation package will be retrieved.

These steps are listed below:

#update the apt package manager
apt-get update
#install the package that allows us to add the PHP repository
apt-get install software-properties-common -y --no-install-recommends
#add the PHP package repostory
add-apt-repository ppa:ondrej/php -y

Now we are setup to install PHP.

Run the following bash commands to install the PHP CLI and the PHP-FPM service along with some PHP extensions

apt-get install -y --no-install-recommends \
php7.3 \
php7.3-fpm \
php7.3-mbstring \
php7.3-bcmath \
php7.3-xml \
php7.3-xmlrpc \
php7.3-zip \
php7.3-sqlite3 \
php7.3-mysql \
php7.3-pgsql \
php7.3-imap \
php7.3-readline \
php7.3-phpdbg \
php7.3-curl \
php7.3-dev \
php-pear \
php-ssh2 \
php-yaml \
php-memcached \
php-redis \
php-apcu \
php-xdebug \
php-xhprof \
curl \
unzip

Note: installing the php7.3 package will also install php7.3-cli, php7.3-common, php7.3-json, php7.3-opcache and other PHP extensions that we did not explicitly specify above.

Next run the following commands to clean up temporary files generated by the installation

apt-get autoremove -y && apt-get clean -y
rm -rf /tmp/* /var/tmp/* /usr/share/doc/*

Finally run the following to fix a PHP v7.3 JIT Compiler error when running the PHP Composer package manager:

sed -i 's/;pcre.jit=1/pcre.jit=0/g' /etc/php/7.3/cli/php.ini
sed -i 's/;pcre.jit=1/pcre.jit=0/g' /etc/php/7.3/fpm/php.ini

The above statements uncomment and set the value of the pcre.jit setting in the php.ini configuration files.

You can run the following commands before and after uncommenting the JIT setting to verify the settings are updated in their respective php.ini files.

cat /etc/php/7.3/cli/php.ini | grep ';pcre.jit=1'
cat /etc/php/7.3/fpm/php.ini | grep ';pcre.jit=1'

You can run the following commands to verify the installation:

The PHP installation directories

If we look in /usr/bin/ we will see the following symlink:

php -> /etc/alternatives/php

and if look in /etc/alternatives/ we will see the following symlink:

php -> /usr/bin/php7.3

Which shows that we are pointing to PHP v7.3.

You can run which php to see that the php command is located at /usr/bin/php.

This means that we must make sure we have the /usr/bin directory in our PATH environment variable.

Similarly if we run which php-fpm7.3 we will see it is found at /usr/sbin/php-fpm7.3 so we need to ensure /usr/sbin/ is in our PATH as well.

We can check that by executing:

echo $PATH

PHP and PHP FPM configuration files

The PHP CLI and PHP-FPM configuration files will be installed at the following locations:

/etc/php/7.3/cli/php.ini

/etc/php/7.3/fpm/php.ini

/etc/php/7.3/fpm/php-fpm.conf

/etc/php/7.3/fpm/pool.d/www.conf

PHP extension configuration files

Each PHP extension that we install will have its own configuration <extension-name>.ini file installed in /etc/php/7.3/mods-available/

Here are a few examples:

/etc/php/7.3/mods-available/opcache.ini
/etc/php/7.3/mods-available/xdebug.ini
/etc/php/7.3/mods-available/pdo_mysql.ini

The /etc/php/7.3/cli/conf.d/ and /etc/php/7.3/fpm/conf.d/ directories will have symlinks to the /etc/php/7.3/mods-available/ files.

If we take a look in /etc/php/7.3/cli/conf.d/ we will see for example the following two symlinks among many others.

10-opcache.ini -> /etc/php/7.3/mods-available/opcache.ini
20-xdebug.ini -> /etc/php/7.3/mods-available/xdebug.ini

The same symlinks will exist if we look in /etc/php/7.3/fpm/conf.d/.

Configuring extensions

Optionally we can configure xdebug for remote debugging and configure opcode caching:

echo 'xdebug.remote_enable=1' >> /etc/php/7.0/mods-available/xdebug.ini

echo 'extension=apcu.so' >> /etc/php/7.3/mods-available/cache.ini

We can enable the xdebug extension using the following command: phpenmod xdebug

Running PHP-FPM

Note: This section does not apply to the Ubuntu docker container used for testing the setup. This is because we can not run the PHP-FPM service using the systemctl command from within the docker container. The docker container only runs a single process and is not configured to run the systemd process manager.

We can check if our PHP-FPM configuration is correct before running the service:

php-fpm7.3 -t

We can start or restart the service by running:

systemctl restart php7.3-fpm

We can check the status of the service by running:

systemctl status php7.3-fpm

Conclusion

In this post I detailed the steps to take to install PHP on Ubuntu Linux. I also described the installation of PHP extensions and the PHP-FPM FastCGI process manager for PHP.

The following resources were used as a reference:

https://www.linode.com/docs/web-servers/lemp/how-to-install-a-lemp-server-on-ubuntu-16-04/

https://www.cloudbooklet.com/how-to-install-nginx-php-7-3-lemp-stack-on-ubuntu-18-04-google-cloud/

https://thishosting.rocks/install-php-on-ubuntu/

https://tecadmin.net/enable-disable-php-modules-ubuntu/

Thanks for reading.