How to compile and install the latest version of PHP 7 from source code on Debian
Updating or installing the latest version of PHP from scratch is quite a daunting task and hence 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 7.4.10 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, download and extract the latest version of PHP from PHP’s website.
mkdir /opt/php-7.4.10 mkdir /usr/local/src/php-7.4-build cd /usr/local/src/php-7.4-build wget https://www.php.net/distributions/php-7.4.10.tar.bz2 tar jxf php-7.4.10.tar.bz2 cd php-7.4.10
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
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-7.4.10 --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-7.4-build/php-7.4.10/php.ini-production /opt/php-7.4.10/lib/php.ini cp /opt/php-7.4.10/etc/php-fpm.conf.default /opt/php-7.4.10/etc/php-fpm.conf cp /opt/php-7.4.10/etc/php-fpm.d/www.conf.default /opt/php-7.4.10/etc/php-fpm.d/www.conf
Using your preferred text editor, open the file ”/opt/php-7.4.10/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.
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-7.4.10/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:
Next step is to configure ”systemd”, so that your version of PHP runs on system restarts.
Create a new file, ”/lib/systemd/system/php-7.4.10-fpm.service”, and copy and paste the following into it:
[Unit] Description=The PHP 7.4.10 FastCGI Process Manager After=network.target [Service] Type=simple PIDFile=/opt/php-7.4.10/var/run/php-fpm.pid ExecStart=/opt/php-7.4.10/sbin/php-fpm --nodaemonize --fpm-config /opt/php-7.4.10/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:
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-7.4.10-fpm.service systemctl daemon-reload systemctl start php-7.4.10-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-7.4.10/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