LAMP 2019LinuxOpen SourcePHP

PHP – Building a LAMP Server (2019)

This is the eighth article in a series on building the ultimate LAMP Server. This covers installing PHP from source so you can run multiple versions and easily switch between them.

Estimated time (this post only): 1 to 4 hours
* varies depending on how much you install

Warning: this article is a work in progress and incomplete. This warning will be removed once it is done.

Why FPM?

The simplest way to install PHP is as an Apache module. However, this limits you to a single version of PHP. Installing as a CGI script allows you to run multiple versions easily. FastCGI is a better-performing implementation of CGI. FPM is a process manager to manage the FastCGI Server API for PHP. Different web hosts use different methods of managing multiple versions of PHP. Here are the two most common scenarios:

  • Offer each supported version of PHP as a CGI and/or FastCGI script. This is what I recommend.
  • Offer a default installation of PHP as a module for better performance and other versions as CGI/FastCGI scripts. This used to be the preferred method, but with the advances in server hardware, this is no longer as common.


Assuming you’ve downloaded all the prerequisites mentioned previously, you shouldn’t need any other packages. The PHP Group maintains extensive documentation on their website. You can read it for more details. But if you’ve been following this guide, you should already have most everything you need for most (if not all) extensions I’m covering here.

Create a user for FPM to use:

sudo useradd -rU php

For PostgreSQL support, you may need to do the following:

sudo ln -s /usr/pgsql-11/ /usr/local/pgsql

Download Source

You should test against the supported versions listed on the PHP Download Site. As of this writing, they have stopped releasing even security updates for PHP 7.0 and older. By the end of this year support will also be dropped for 7.1. If you need to develop for older versions of PHP, you can find them on the PHP Releases page.

The latest statistics from W3Techs show that the following are the most popular server-side scripting languages/versions (those with over 5% market share): PHP 5.6 (24.4%), 5.4 (10.7%), 5.3 (8.9%), 7.0 (8.6%), 7.2 (7.4%), 5.5 (7.3%), and 7.1 (6.7%). As you can see, despite support being dropped for PHP 5 and 7.0, they still are the most popular, so you should probably develop for them as well as the current versions.

5.6 is the most-used version right now, and the following will download it. Change the version number and instance name on the first line to whatever you want.

TMPVER="5.6.40" && TMPINSTNAME="php56"
cd ~
wget -O php-${TMPVER}.tar.bz2 \${TMPVER}.tar.bz2/from/this/mirror

Other versions you can try as of this date:

TMPVER="7.0.33" && TMPINSTNAME="php70"
TMPVER="7.1.26" && TMPINSTNAME="php71"
TMPVER="7.2.15" && TMPINSTNAME="php72"
TMPVER="7.3.3" && TMPINSTNAME="php73"

Verify the download against the sha256 checksum shown on the download page if desired:

sha256sum php-${TMPVER}.tar.bz2

Assuming the download is valid, extract it:

tar xvjf php-${TMPVER}.tar.bz2

Configure PHP

Depending on your requirements you may not wish to use the same options as below. Read this entire section before executing the following commands.

The following should be compatible with most PHP installations on shared hosts and provides support for PDO, MySQL/MariaDB, SQLite, and PostgreSQL (I put the database extensions last so you can remove anything you don’t need).

mkdir ~/php-$TMPVER/bld
cd ~/php-$TMPVER/bld

../configure --prefix=/opt/$TMPINSTNAME --datadir=/opt/$TMPINSTNAME/share \
--localstatedir=/opt/$TMPINSTNAME/var --with-libdir=lib64 --without-pear  \
--enable-cgi --enable-fpm --enable-bcmath=shared --with-bz2=shared \
--enable-calendar=shared --enable-ctype=shared --with-curl=shared \
--enable-dom=shared --enable-exif=shared --enable-fileinfo=shared \
--enable-filter=shared --with-freetype-dir=/usr --enable-ftp=shared \
--with-gd=shared --with-gettext=shared --with-gmp=shared --enable-hash \
--with-iconv=shared --with-imap=shared --with-imap-ssl --enable-intl=shared \
--with-jpeg-dir=/usr --enable-json=shared --with-kerberos \
--enable-libxml=shared --enable-mbregex --enable-mbregex-backtrack \
--enable-mbstring --with-mcrypt=shared --with-mhash=shared --enable-opcache \
--with-openssl=shared --enable-pcntl=shared --with-pspell=shared \
--with-pcre-regex --enable-phar=shared --with-png-dir=/usr \
--enable-posix=shared --with-readline=shared --enable-session=shared \
--enable-shmop=shared --enable-simplexml=shared --enable-soap=shared \
--enable-sockets=shared --enable-sysvmsg=shared --enable-sysvsem=shared \
--enable-sysvshm=shared --with-tidy=shared --enable-tokenizer=shared \
--enable-wddx=shared --enable-xml=shared --with-xmlrpc=shared \
--enable-xmlreader=shared --with-xsl=shared --enable-xmlwriter=shared \
--with-xpm-dir=/usr --enable-zip=shared --with-zlib=shared --with-zlib-dir=/usr \
--enable-pdo \
--with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,mysqlnd \
--with-sqlite3 --with-pdo-sqlite \
--with-pdo-pgsql=shared --with-pgsql=shared

If anything goes wrong in the configure or make process below, do the following before running configure again:

make clean

Configure For Other Databases

The following should should be added to the configure line for the other databases covered in the previous article as necessary (change paths as needed).

For Oracle:

--with-oci8=shared --with-pdo-oci=shared,/opt/oracle/product/18c/dbhomeXE


--with-mssql=shared --with-pdo-odbc=shared,unixODBC,/usr

The DB2 extensions must be installed via PECL, which will be covered later on.


--with-mssql=shared --with-pdo-dblib=shared

For Firebird:

--with-interbase=shared,/opt/firebird --with-pdo-firebird=shared,/opt/firebird

Install PHP

If all went well, you can now run make:

make test

The test phase will take a while. I have tried to avoid as many errors as possible, but you probably will still encounter a few. To minimize errors, start all databases you have compiled PHP for. Many database tests will still fail since you set custom passwords and install directories. Older versions of PHP may have bugged test scripts and sometimes the failures can be avoided by copying the updated scripts from the latest version of PHP. If you want to be thorough you can save the test output to a text file and go through each failed test result by appending the last line with something like > ~/phptest.log.

Now install:

sudo make install

Create a default php.ini file. You can choose between the development and production versions. The biggest difference is error reporting. With the development version, PHP errors will appear in your web browser. It’s unsafe to do this on any production system as it exposes PHP code to the public, but can be useful on a development machine. So run only one of the following commands as desired.

sudo cp ~/php-$TMPVER/php.ini-development /opt/$TMPINSTNAME/lib/php.ini
sudo cp ~/php-$TMPVER/php.ini-production /opt/$TMPINSTNAME/lib/php.ini

Make a default php-fpm.conf file:

sudo cp /opt/$TMPINSTNAME/etc/php-fpm.conf.default \
sudo mkdir /opt/$TMPINSTNAME/etc/fpm.d
sudo nano /opt/$TMPINSTNAME/etc/php-fpm.conf

Change a few things in this file. First, un-comment the following lines (remove the semicolon at the beginning):

pid = run/

Next edit the following line:

listen =

Change this to listen on a socket rather than a TCP port. Both methods have their advantages in terms of performance and security. If you stick with ports, each PHP instance has to be unique. Sockets are easier and arguably more secure:

listen = var/run/php.sock

You will see three lines that start with "listen." These determine the owner and permissions of the socket we set above. The default values won’t work because Apache isn’t running as the same user as PHP (your browser will return a 503 error). Assuming you are using the default values for Apache (you can check these in "httpd.conf"), un-comment and edit these three lines as follows:

listen.owner = daemon = daemon
listen.mode = 0660

Further down the file, find and un-comment "security.limit_extensions". For example:

security.limit_extensions = .php .php3 .php4 .php5

After saving that file, you also need to edit the Apache configuration:

sudo nano /opt/apache/conf/httpd.conf

Un-comment (remove the pound sign) from the following lines and save the file:

LoadModule proxy_module modules/
LoadModule proxy_fcgi_module modules/

Do some cleanup:

cd ~
rm php-${TMPVER}.tar.bz2
sudo mv php-$TMPVER /usr/local/src/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.