Installing PHP 7 on Ubuntu 16.04

Installing PHP 7 on Ubuntu 16.04

In my last post Compiling PHP 7 on CentOS I showed how to compile PHP 7 on CentOS. In this post I will use the basis of that post and transfer the instructions for Ubuntu Linux. This will be a truncated example so if you want the details please refer to that post. This post was also inspired by Lorna Mitchell in her post http://www.lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu which she shows how to add ppa repository for multiple php versions, though this a good and quick way, compiling your own PHP you can compile different versions and run these at the same without having to switch and also it is good the learn to compile software ourselves as this gives us control how it is compiled and installed, it is relatively quick as this once learned can be done in 10 minutes and if you write a bash script can be automated too.

I am using Ubuntu 16.04 which is now using systemd instead of init.d to start services so this is what I will be showing you setting up PHP-FPM in systemd so it will start automatically when the system starts.

so without further ado we need some dependencies installed I will be configuring PHP the same as my last post so, alter this according to your needs

sudo apt-get install -y \
    build-essential \
    git-core \
    autoconf \
    bison \
    libxml2-dev \
    libbz2-dev \
    libmcrypt-dev \
    libcurl4-openssl-dev \
    libltdl-dev \
    libpng12-dev \
    libpspell-dev \
    libreadline-dev \
    libicu-dev \
    libxml2-dev \
    libpng-dev \
    libmcrypt-dev \
    libfreetype6 \
    libfreetype6-dev \
    imagemagick \
    libmagickwand-dev \
    zlib1g-dev \
    cmake

next download the PHP source code from Github

git clone https://github.com/php/php-src.git

now make the target build directory

sudo mkdir /usr/local/php7

charge into the source code directory

cd php-src

you have to now checkout the latest stable code, at the time of writing this it was 7.0.6 so I would do

git checkout PHP-7.0.6

Once that has done we need to configure and build PHP with ( alter this according to your needs)

./buildconf --force
./configure --prefix=/usr/local/php7 \
                  --with-config-file-path=/usr/local/php7/etc \
                  --with-config-file-scan-dir=/usr/local/php7/etc/conf.d \
                  --enable-bcmath \
                  --with-bz2 \
                  --with-curl \
                  --enable-filter \
                  --enable-fpm \
                  --with-gd \
                  --enable-gd-native-ttf \
                  --with-freetype-dir \
                  --with-jpeg-dir \
                  --with-png-dir \
                  --enable-intl \
                  --enable-mbstring \
                  --with-mcrypt \
                  --enable-mysqlnd \
                  --with-mysqli \
                  --with-mysql-sock=/var/run/mysqld/mysqld.sock \
                  --with-pdo-mysql=mysqlnd \
                  --with-pdo-sqlite \
                  --enable-opcache \
                  --with-openssl \
                  --enable-simplexml \
                  --with-sqlite3 \
                  --enable-xmlreader \
                  --enable-xmlwriter \
                  --enable-zip \
                  --with-zlib
make -j4
sudo make install

For more details what these mean see Compiling PHP 7 on CentOS. Next we will want to install the config files into our directory.

sudo cp -v php.ini-production /usr/local/php7/lib/php.ini

Make the module ini file

sudo mkdir /usr/local/php7/etc/conf.d
sudo nano /usr/local/php7/etc/conf.d/modules.ini

And put this in it

# Zend OPcache
zend_extension=opcache.so

now press Ctrl + o and Ctrl + x to save and exit file. Next we have to configure the fpm con files so

sudo mv -v /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
sudo mv -v /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf

now we need to tell fpm to use unix socket and which user to run under the default user is 'www-data' so if you are ok with that then you can leave it, my example I will change the defaults so:

sudo nano /usr/local/php7/etc/php-fpm.d/www.conf

and changes these values my user is 'ubuntu', but use the user your php scripts run under.

...

user = ubuntu
group = ubuntu

...

listen = /var/run/php/php7-fpm.sock

Don't forget to make the directory for the socket file

sudo mkdir /var/run/php

Because I like to have check to config file before starting and restarting the service we need to add a couple of files for this. First make the directory for the temp file, (these scripts were taken from the deb and adjusted for my setup)

sudo mkdir /user/local/php7/lib/tempfiles.d

Then make a file in this directory and add some content to it by

sudo nano /usr/local/php7/lib/tmpfiles.d/php7-fpm.conf

and add this to it

#Type Path        Mode UID      GID      Age Argument
    d /run/php    0755 www-data www-data -   -

now press Ctrl + o and Ctrl + x to save and exit file. Now by the same procedure we make the check conf file

sudo nano /usr/local/php7/php7-fpm-checkconf

and add this to it

#!/bin/sh
set -e
CONFFILE=${1:-/usr/local/php7/etc/php-fpm.conf}

do_tmpfiles() {
    local type path mode user group

    [ "$1" != no ] && V=-v

    TMPFILES=/usr/local/php7/lib/tmpfiles.d/php7.0-fpm.conf

    if [ -r "$TMPFILES" ]; then
	while read type path mode user group age argument; do
	    if [ "$type" = "d" ]; then
		mkdir $V -p "$path"
		chmod $V "$mode" "$path"
		chown $V "$user:$group" "$path"
	    fi
	done < "$TMPFILES"
    fi
}

do_tmpfiles no

errors=$(/usr/local/php7/sbin/php7-fpm --fpm-config "$CONFFILE" -t 2>&1 | grep "\[ERROR\]" || true);
if [ -n "$errors" ]; then
    echo "Please fix your configuration file..."
    echo $errors
    exit 1
fi
exit 0

Save and exit the file then make it executable by

sudo chmod +x /usr/local/php7/php7-fpm-checkconf

Now we want to make the systemd file by

sudo nano /lib/systemd/system/php7-fpm.service

and add this to it

[Unit]
Description=The PHP 7 FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/run/php/php7-fpm.pid
ExecStartPre=/usr/local/php7/php7-fpm-checkconf
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
ExecReload=/usr/local/php7/php7-fpm-checkconf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

save and exit the file and install the service by

sudo systemctl daemon-reload
sudo systemctl enable php7-fpm

And if all is done right we should be able to start the service with

sudo service php7-fpm start

so that should be it, hopefully I didn't miss anything

Let me know in the comments. Happy PHPing!


17/05/2016 14:12:00 Shaun Freeman Filed Under: Linux Linux, PHP, php-fpm, Ubuntu

Twitter Feed
Shaun Freeman @Zendmaster

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/lSFWmpHTX1 Patrick Stewart talks about meeting Sting on the set of DUNE (Funny to the EXTREME)

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/pmXSmod4ti Anonymous - This will Change Everything You Know... (2018-2019)

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/GkwTCvBfes Will Artificial Intelligence Take Over The World?

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/Y1ulafmsC6 Frank Abagnale: "Catch Me If You Can" | Talks at Google

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/NBdW2xFnqD ETS2: Special Transport DLC Trailer