How to compile and install the latest version of PHP 8 from source code on Debian

Updating or installing the latest version of PHP from scratch is quite a daunting task so I wrote this guide to make the task easier for both my self and others.

I will try to keep this guide up to date and the current version of the guide is for installing PHP 8.0.2 on a server running Debian 10 Buster. In most parts, all you have to do to install an other version of PHP is to run a search and replace for the version number in this guide and replace it with the version of PHP that you want to install.

The following is tested on production servers running Debian 8 Jessie, Debian 9 Stretch and Debian 10 Buster but for this guide I installed a fresh version of Debian 10 Buster on a virtual server using Microsoft’s Hyper-V to ensure it works on a fresh system.

Please note that all of the following steps are run using the ”root” account.

If this is the first time you are about to compile and install PHP from source you probably need to install some prerequisites, so we start by doing that.

apt install build-essential net-tools autoconf libxml2-dev libssl-dev libbz2-dev libcurl4-openssl-dev libpng-dev libjpeg-dev 

Next, we create some folders and download and extract the source code from the PHP site to these folders.

mkdir /opt/php-8.0.2
mkdir /usr/local/src/php-8.0-build
cd /usr/local/src/php-8.0-build
wget https://www.php.net/distributions/php-8.0.2.tar.bz2 
tar jxf php-8.0.2.tar.bz2 
cd php-8.0.2

On a new server there might be required packages missing and these are the ones that I had to install prior to configuring the install script for PHP.

apt install pkg-config sqlite3 libsqlite3-dev libonig-dev libzip-dev

apt install libfcgi-dev libfcgi0ldbl libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-dev libxml2-dev libbz2-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libfreetype6-dev libkrb5-dev libpq-dev libxml2-dev libxslt1-dev libzip-dev libsqlite3-dev libonig-dev

To have the IMAP-extensions installed correctly you have to:

ln -s /usr/lib/libc-client.a /usr/lib/x86_64-linux-gnu/libc-client.a
cd /usr/include
ln -s x86_64-linux-gnu/curl
cd /usr/local/src/php-8.0-build/php-8.0.2

Then you should be set to configure the install script and this is the step where you should change your options according to what you need and don’t need. Mine is the following:

./configure --prefix=/opt/php-8.0.2 --enable-mbstring --with-curl --with-zlib --enable-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 --with-zip --with-pdo-mysql --with-mysqli --with-mysql-sock=/var/run/mysqld/mysqld.sock --with-jpeg=/usr --with-openssl --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=/lib/x86_64-linux-gnu --enable-ftp --enable-opcache --enable-fpm --without-sqlite3

If you get errors saying that some packages were missing, please Google for the missing packages and how to install them on your system and try to run the configure part again, rinse and repeat until it succeeds.

The next step is to actually make and install the binaries and you can tweak the ”make” part by letting it use X number of cores by setting the ”-j” parameter. In the following example 4 cores may be used by the compiler. If you have more available cores, you may increase the number.

make -j 4 
make install

When the binaries are installed, we copy some default production ready configuration files:

cp /usr/local/src/php-8.0-build/php-8.0.2/php.ini-production /opt/php-8.0.2/lib/php.ini
cp /opt/php-8.0.2/etc/php-fpm.conf.default /opt/php-8.0.2/etc/php-fpm.conf
cp /opt/php-8.0.2/etc/php-fpm.d/www.conf.default /opt/php-8.0.2/etc/php-fpm.d/www.conf

Using your preferred text editor, open the file ”/opt/php-8.0.2/etc/php-fpm.conf” and remove the ”;” from the line ”;pid = run/php-fpm.pid”.

I’m using ”vim” as text editor, but you may use ”nano” or what ever, so just replace ”vim” with your preferred editor.

vim /opt/php-8.0.2/etc/php-fpm.conf

Next, we set the port number on which this version of PHP will be available. You should use different port numbers for each version of PHP installed on your server.

Open ”/opt/php-8.0.2/etc/php-fpm.d/www.conf” and locate the line ”listen = 127.0.0.1:9000” and change the ”9000” to a free port number. I’m using 8000 for PHP 8.0.0 and 8002 for PHP 8.0.2 but it’s up to you and your environment what port you should use.

vim /opt/php-8.0.2/etc/php-fpm.d/www.conf

Next step is to configure ”systemd”, so that your version of PHP runs on system restarts.

Create a new file, ”/lib/systemd/system/php-8.0.2-fpm.service”, and copy and paste the following into it:

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

[Service]
Type=simple
PIDFile=/opt/php-8.0.2/var/run/php-fpm.pid
ExecStart=/opt/php-8.0.2/sbin/php-fpm --nodaemonize --fpm-config /opt/php-8.0.2/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

The last step before enabling this version of PHP is to tweak the PHP settings and you do that in the following file:

vim /opt/php-8.0.2/lib/php.ini

You don’t have to tweak the settings if you are fine with the default settings, which are set for production.

The last step is to start the PHP instance by enabling it in systemd:

systemctl enable php-8.0.2-fpm.service
systemctl daemon-reload
systemctl start php-8.0.2-fpm.service

To make your websites use the new version of PHP you have to make changes to your websites configuration files. I’m using Apache as web server and to make a website use the latest version of PHP is made possible by adding the following in the websites virtual host file in the <VirtualHost>-directory. For example:

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:PORT_NUMBER/path/to/example.com/public_html/$1

Make sure you replace ”PORT_NUMBER” with the port number you selected previously in ”/opt/php-8.0.2/etc/php-fpm.d/www.conf” and the path to where your websites public directory is located.

Reload Apache to activate the changes:

systemctl reload apache2.service

3 kommentarer

  • Peter Udem

    Thanks for this great tutorial. For me, always worked fine, but the last point I didn’t understand. Where exactly I have to put the ProxyPassMatch …. -line?

  • The line ProxyPassMatch should go in the configuration file for your domain/website, i.e /etc/apache/sites-available/example.com.conf

  • Javier

    Muy bueno el tutorial, aprendí muchisimo.

    No olvides hacer el link simbólico al excecutable:

    $ sudo ln -s /opt/php-8.0.0/bin/php /usr/bin/php

    de lo contrario no podrás usar php en la linea de comandos (php -v por ejemplo)

Lämna ett svar

Din e-postadress kommer inte publiceras. Obligatoriska fält är märkta *

Denna webbplats använder Akismet för att minska skräppost. Lär dig hur din kommentardata bearbetas.