Hypertext Preprocessor abbreviated as PHP is a popular web scripting language. This is a server-side scripting language, this means that it is executed on the server before the HTML results are sent to the client-server. It holds a reputation for its versatility, simplicity and wide adoption.

There are several features and benefits associated with PHP, some of which include:

  • Extensive Frameworks and Libraries: PHP offers a rich ecosystem with innumerable frameworks and libraries to extend its functionality. These frameworks and libraries include Laravel, Symfony, and CodeIgniter.
  • Great Database Support: It supports a number of databases with built-in extensions. The supported databases include MySQL, PostgreSQL, Oracle, and SQLite
  • High Performance: With several improvements, PHP now provides high performance which makes it possible to handle high-traffic websites efficiently.
  • Active Community: The PHP community contribute to its growth through online resources, forums to provide help, tutorials, libraries, and best practices for developers.
  • Scalability: it has the ability to handle projects with varying sizes and complexity. This makes it easier for developers to create scalable web applications that easily integrate with other technologies.
  • Cost-effective: it being open-source, it reduces the cost of development.
  • Rich Feature Set: PHP provides a lot of comprehensive set of features for web development.
  • Wide Platform Support: it is compatible with several platforms such as Windows, Linux, macOS, and Unix. It also supports several web servers such as Apache, Nginx, and Microsoft IIS.

Generally, installing PHP on Ubuntu should not be difficult. However, the Ondřej Surý PPA repository that provides the latest PHP versions on Ubuntu seems not to be working on Ubuntu 18.04. This is what brings us to this guide on how to build and install PHP 8,7 on Ubuntu 18.04 (Bionic Beaver).

1. Install the Required Packages

To be able to build PHP8,7 on Ubuntu 18.04 (Bionic Beaver), you need to install all the required packages. First, update the system:

sudo apt update && sudo apt upgrade -y

If a reboot is required then perform it.

[ -e /var/run/reboot-required ] && sudo reboot

Now install the development packages:

sudo apt install git pkg-config build-essential autoconf bison libxml2-dev libsqlite3-dev openssl libcurl3 libpq-dev libxslt-dev libonig-dev -y

The next package required is not available in the default Ubuntu 18.04 repositories, so we will add the repo below

echo "deb http://security.ubuntu.com/ubuntu focal-security main" | sudo tee /etc/apt/sources.list.d/focal-security.list
sudo apt update

Now install the package:

sudo apt install re2c

Remove temporarily added repo list.

sudo rm /etc/apt/sources.list.d/focal-security.list

2. Download and Configure PHP Source Code

Now the next thing to do is to download the PHP source code. To do that, you need to clone the GitHub repository with the command below. You can use specific git branch.

# PHP 8.4 branch
git clone https://github.com/php/php-src.git --branch=PHP-8.4

# PHP 8.3 branch
git clone https://github.com/php/php-src.git --branch=PHP-8.3

# PHP 8.2 branch
git clone https://github.com/php/php-src.git --branch=PHP-8.2

# PHP 8.1 branch
git clone https://github.com/php/php-src.git --branch=PHP-8.1

# PHP 8.0 branch
git clone https://github.com/php/php-src.git --branch=PHP-8.0

# PHP 7.4 branch
git clone https://github.com/php/php-src.git --branch=PHP-7.4

# Master branch
git clone https://github.com/php/php-src.git --branch=master

Now switch to the directory:

cd php-src

For this guide, we will make some configurations before we compile the source code. First, generate the config using the command:

$ ./buildconf
buildconf: Checking installation
buildconf: autoconf version 2.69 (ok)
buildconf: Cleaning cache and configure files
buildconf: Rebuilding configure
buildconf: Rebuilding main/php_config.h.in
buildconf: Run ./configure to proceed with customizing the PHP build.

You can configure PHP with several desired options before the installation. This includes enabling/disabling extensions and setting the installation path.

To view all the available options, use:

./configure --help

Here, we will configure PHP using several options.

./configure \
 --prefix=/opt/php \
 --enable-cli \
 --enable-fpm \
 --enable-intl \
 --enable-opcache \
 --enable-sockets \
 --enable-soap \
 --with-freetype \
 --with-fpm-user=www-data \
 --with-fpm-group=www-data \
 --with-jpeg \
 --with-mysql-sock \
 --with-mysqli \
 --with-pdo-mysql \
 --with-pgsql \
 --with-xsl \
 --with-zlib

Sample Output:

Build and Install PHP 87 on Ubuntu 18.04 Bionic Beaver

3. Build PHP from Source Code

Now we are set to build PHP from source code on our Ubuntu 18.04 (Bionic Beaver). We will start the build with the command below:

To make the process faster, you can provide the number of cores identified with the command:

$ nproc
4

Now provide the core number during the build:

make -j$(nproc)

The process should proceed as shown:

Build and Install PHP 87 on Ubuntu 18.04 Bionic Beaver 1

4. Install PHP from Source Code

Once the build process is complete, run the installation:

sudo make install

Sample output:

Build and Install PHP 87 on Ubuntu 18.04 Bionic Beaver 2

Once complete, you will have PHP 8 installed in the /opt/php/php8. You can verify this with the command:

$ /opt/php/bin/php -v
PHP 8.1.27-dev (cli) (built: Nov 25 2023 12:03:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.27-dev, Copyright (c) Zend Technologies

Export this PATH:

echo "export PATH=\$PATH:/opt/php/bin/" | sudo tee -a /etc/profile
source /etc/profile

Now check the PHP version:

$ php -v
PHP 8.1.27-dev (cli) (built: Nov 25 2023 12:03:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.27-dev, Copyright (c) Zend Technologies

View the installed modules:

$ php -m
[PHP Modules]
Core
ctype
date
dom
fileinfo
filter
hash
iconv
intl
json
libxml
mysqli
mysqlnd
pcre
PDO
pdo_mysql
pdo_sqlite
pgsql
Phar
posix
random
Reflection
session
SimpleXML
soap
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
xsl
zlib

[Zend Modules]

5. Configure PHP

Now we need to identify the location of the PHP config after the installation. To achieve that, use the command:

$ php --ini
Configuration File (php.ini) Path: /opt/php/lib
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Here, you can clearly see that there is no configuration for PHP. We will create a config from the available sample php.ini-development or php.ini-production in the php-src directory.

sudo cp php.ini-production /opt/php/lib/php.ini

Now verify it the config is available:

$ php --ini
Configuration File (php.ini) Path: /opt/php/lib
Loaded Configuration File:         /opt/php/lib/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Now you can modify the configuration file as desired. The next thing we need to create is the PHP-FPM config file:

sudo cp /opt/php/etc/php-fpm.d/www.conf.default /opt/php/etc/php-fpm.d/www.conf

Modify the config:

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

Ensure that the below lines are added to the file.

;include=/opt/php/etc/php-fpm.d/*.conf
pid = run/php-fpm.pid
[www]
user = www-data
group = www-data
listen = 127.0.0.1:8999
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4

Save the file and create a service file for PHP-FPM

sudo vim /etc/systemd/system/php-fpm.service

Add these lines to the file:

[Unit]
Description=PHP FPM service
After=network.target

[Service]
PIDFile=%t/php-fpm.pid
ExecStartPre=/bin/mkdir --parents %t
ExecStart=/opt/php/sbin/php-fpm --fpm-config /opt/php/etc/php-fpm.conf --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
Type=simple

[Install]
WantedBy=multi-user.target

Now reload the system daemon:

sudo systemctl daemon-reload

Start and enable the service:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Check the status:

$ systemctl status php-fpm
 php-fpm.service - PHP FPM service
   Loaded: loaded (/etc/systemd/system/php-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2023-11-25 12:26:55 EAT; 4s ago
  Process: 18432 ExecStartPre=/bin/mkdir --parents /run (code=exited, status=0/SUCCESS)
 Main PID: 18433 (php-fpm)
    Tasks: 3 (limit: 1073)
   CGroup: /system.slice/php-fpm.service
           ├─18433 php-fpm: master process (/opt/php/etc/php-fpm.conf)
           ├─18443 php-fpm: pool www
           └─18444 php-fpm: pool www

Nov 25 12:26:55 Ubuntu-Bionic systemd[1]: Starting PHP FPM service...
Nov 25 12:26:55 Ubuntu-Bionic systemd[1]: Started PHP FPM service.

Verify if it is listening on the set port:

$ sudo ss -plunt|grep fpm
tcp    LISTEN   0        128             127.0.0.1:8999          0.0.0.0:*       users:(("php-fpm",pid=18444,fd=9),("php-fpm",pid=18443,fd=9),("php-fpm",pid=18433,fd=7))

6. Test PHP installation

Now we can test and validate that the installed PHP is working as desired. To achieve that you can use the desired web server:

Option 1: Using Apache

Begin by installing Apache and the required packages:

sudo apt install apache2 libapache2-mod-fcgid -y

Ensure that it is started and enabled:

sudo systemctl enable apache2 --now

Create a virtual host file that handles PHP via the FCGI port

sudo vim /etc/apache2/sites-available/test.conf

Add these lines to the file and replace them where required:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName test.computingforgeeks.com

     ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:8999/var/www/html/$1
     DirectoryIndex /index.php index.php

</VirtualHost>

Save the file and enable the site:

sudo a2ensite test

Also, enable other Apache extensions:

sudo a2enmod actions fcgid alias
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_fcgi

Restart apache:

sudo systemctl restart apache2

Now create a simple PHP file with the command:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Option 2: Using Nginx

You can also use the Nginx web server to serve the PHP files. First, install it:

sudo apt install nginx

Ensure that it is started and enabled:

sudo systemctl enable --now nginx

Next, create a config file:

sudo vim /etc/nginx/conf.d/test.conf

Add these lines to the file:

server {
    listen 80;
    server_name test.computingforgeeks.com;

    root /var/www/html;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:8999;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Restart nginx:

sudo systemctl restart nginx

Now create the file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

With any of thee above options used, verify if you can access the PHP file with the provided domain name http://domain_name/info.php

Build and Install PHP 87 on Ubuntu 18.04 Bionic Beaver 3

Verdict

That is it! We have successfully walked through how to build and install PHP 8,7 on Ubuntu 18.04 (Bionic Beaver). This should help you run the latest PHP versions on Ubuntu 18.04. I hope this was of help to you.

See more:

2 COMMENTS

  1. Hi many thanks for the tutorial and really helpful. Carefully followed every step however, when trying to install ajenti-v-fpm, still got error

    The following packages have unmet dependencies:
    ajenti-v-php8.1-fpm : Depends: php8.1-fpm but it is not installable

    can you please help? Many thanks.

  2. Thanks for the writeup!

    @L
    In order to install packages that depend on php8 with apt, instead of `make install` you have to package up this build as “your own” php8.1-fpm and install it via the package manager. Then it will meet the dependency.

LEAVE A REPLY

Please enter your comment!
Please enter your name here