How to Compile PHP7.0/PHP7.1 From Source on Arch Linux
This tutorial shows you how to compile PHP7.0/PHP7.1 on Arch Linux and install it alongside PHP7.2. Arch Linux is a rolling-release distribution with the newest and greatest software. PHP7.2 landed in Arch Linux repository. Some web applications such as WordPress works well with PHP7.2, but others like NextCloud currently doesn’t support PHP7.2. The upcoming NextCloud version – NextCloud 13 – is said to be compatible with PHP7.2.
Yesterday I did an upgrade on my Arch Linux server, which resulted in PHP7.2 replacing PHP7.1 and my NextCloud web interface stopped working. The AUR (Arch User Repository) provides PHP7.0. I tried to install it with Yaourt, but the signature verification failed, so I had no other choice but to compile PHP7.0/PHP7.1 myself.
How to Compile PHP7.0/PHP7.1 on Arch Linux
First, upgrade all software.
sudo pacman -Syu
Install wget and download PHP source code. I use the latest version of PHP7.0. Replace the version number with your preferred version.
sudo pacman -S wget
wget http://php.net/distributions/php-7.0.26.tar.gz
Extract the tarball.
tar xvf php-7.0.26.tar.gz
Cd into the newly created directory.
cd php-7.0.26
Install build tools and dependencies.
sudo pacman -S gcc make bison gawk re2c libxml2 libwebp freetype2 c-client libmcrypt libxslt
Configure the environment with the following parameters. Copy the entire command and paste it to your terminal window.
./configure \ --prefix=/opt/php-7.0.26 \ --with-config-file-path=/opt/php-7.0.26/etc \ --with-zlib-dir \ --with-freetype-dir \ --enable-mbstring \ --with-libxml-dir=/usr \ --enable-soap \ --enable-calendar \ --with-curl \ --with-mcrypt \ --with-zlib \ --with-gd \ --disable-rpath \ --enable-inline-optimization \ --with-bz2 \ --with-zlib \ --enable-sockets \ --enable-sysvsem \ --enable-sysvshm \ --enable-pcntl \ --enable-mbregex \ --enable-exif \ --enable-bcmath \ --with-mhash \ --enable-zip \ --with-pcre-regex \ --with-pdo-mysql \ --with-mysqli \ --with-mysql-sock=/var/run/mysqld/mysqld.sock \ --with-jpeg-dir=/usr \ --with-png-dir=/usr \ --enable-gd-native-ttf \ --with-openssl \ --with-fpm-user=http \ --with-fpm-group=http \ --enable-ftp \ --with-imap \ --with-imap-ssl \ --with-kerberos \ --with-gettext \ --with-xmlrpc \ --with-xsl \ --enable-opcache \ --enable-fpm
When it’s done, let’s compile PHP7 with make command. Replace 2 with the number of CPU cores on your machine. By default make
uses 1 core, you can make the compile process faster by using all available cores.
make -j2
The compile process can take some time, depending on your CPU power. Once it’s finished, run a test.
make test
Now install PHP. The installation directory is /opt/php-7.0.26/
.
sudo make install
From the screenshot, we can see PHP-FPM binary is installed to /opt/php-7.0.26/sbin/
and PHP-FPM configuration directory is /opt/php-7.0.26/etc/
. Before running PHP-FPM, we need to create the PHP-FPM configuration file. There are two sample configuration files so we can just copy them.
sudo cp /opt/php-7.0.26/etc/php-fpm.conf.default /opt/php-7.0.26/etc/php-fpm.conf sudo cp /opt/php-7.0.26/etc/php-fpm.d/www.conf.default /opt/php-7.0.26/etc/php-fpm.d/www.conf
Edit the www.conf
configuration file.
sudo nano /opt/php-7.0.26/etc/php-fpm.d/www.conf
By default, PHP-FPM listens on TCP socket 127.0.0.1:9000, we can make it listen on Unix socket. Find the following line.
listen = 127.0.0.1:9000
Add a semicolon at the beginning to comment it out. Then add the following line below it.
listen = /run/php-fpm/php7.0-fpm.sock
Next, find the following 3 lines and uncomment them.
;listen.owner = http ;listen.group = http ;listen.mode = 0660
Save and close the file. Next copy the php.ini file.
sudo cp ~/php-7.0.26/php.ini-production /opt/php-7.0.26/etc/php.ini
Edit the file.
sudo nano /opt/php-7.0.26/etc/php.ini
Add the following at the bottom of the file.
include_path = "/opt/php-7.0.26/lib/php"
Save and close the file. Now we can start PHP-FPM with:
sudo /opt/php-7.0.26/sbin/php-fpm
Creat Systemd Service Unit
Create Systemd service unit for PHP7.0.
sudo nano /etc/systemd/system/php7.0-fpm.service
Copy and paste the following lines into the file.
[Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=simple PIDFile=/run/php-fpm/php7.0-fpm.pid ExecStart=/opt/php-7.0.26/sbin/php-fpm --nodaemonize --fpm-config /opt/php-7.0.26/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
Save and close the file. Now we can kill the current running PHP7.0-FPM and run it via systemd. Find the PID of PHP7.0-FPM master process.
ps aux | grep php-fpm
As you can see, the PID is 5066 on my machine. So I can kill it with:
sudo kill 5066
Note that the PHP7.2-FPM binary is also named php-fpm
.If you use pkill php-fpm
, then both PHP7.2-FPM and PHP7.0-FPM will stop. Now start php7.0-fpm via systemd.
sudo systemctl start php7.0-fpm
Enable auto-start at boot time.
sudo systemctl enable php7.0-fpm
Check its status.
systemctl status php-fpm
Make Nginx Use PHP7.0-FPM
To make Nginx use PHP7.0 instead of PHP7.2, open Nginx server block file, find the following line.
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
Change it to
fastcgi_pass unix:/run/php-fpm/php7.0-fpm.sock;
There may be more than one instance of this line. You need to change all of them. Then make http
(the web server user on Arch Linux) as the owner of /run/php-fpm/php7.0-fpm.sock
file.
sudo chown http:http /run/php-fpm/php7.0-fpm.sock
Test Nginx configuration.
sudo nginx -t
If the test if successful, reload Nginx for the changes to take effect.
sudo systemctl reload nginx
That’s it! I hope this tutorial helped you compile PHP7.0/7.1 from source on Arch Linux. As always, if you found this post useful, then subscribe to our free newsletter to get more useful tips and tricks.