How to install PHP from Source on Ubuntu

Compiling PHP from source code can be essential when you require a specific PHP version that isn’t available through your Ubuntu distribution or a Personal Package Archive (PPA). This approach is particularly useful if you need to enable custom modules, such as pcntl, which is disabled by default on Ubuntu. By compiling PHP yourself, you gain the flexibility to customize your PHP environment to meet your exact requirements.

Install Requirements

Installing PHP from source code requires many packages and libraries installed. You can install it with the command

apt-get install -y build-essential autoconf automake pkgconf re2c libbz2-dev libcurl4-openssl-dev libxml2-dev libpng-dev libjpeg-dev libssl-dev libzip-dev libmcrypt-dev libreadline-dev libonig-dev libsodium-dev bison libsqlite3-dev libfreetype-dev libsystemd-dev libwebp-dev libxslt1-dev libkrb5-dev libldap2-dev

Download and extract PHP source code

cd /usr/local/src/
wget https://www.php.net/distributions/php-8.3.8.tar.gz
tar -xzvf php-8.3.8.tar.gz
cd php-8.3.8

Configure PHP

This step allows you to enable or disable PHP modules.

./configure \
	--with-pic \
	--enable-embed \
	--prefix=/usr/serverok/php \
	--with-config-file-scan-dir=/usr/serverok/php/lib/php.conf.d \
	--enable-pcntl \
	--enable-fpm \
	--with-fpm-systemd \
	--with-curl \
	--enable-gd \
	--with-gettext \
	--with-jpeg \
	--with-freetype \
	--with-kerberos \
	--with-openssl \
	--with-mhash \
	--with-mysql-sock=/var/lib/mysql/mysql.sock \
	--with-mysqli=mysqlnd \
	--with-pdo-mysql=mysqlnd \
	--with-pear \
	--with-sodium \
	--with-webp \
	--with-xsl \
	--with-zlib \
	--with-zip \
	--enable-bcmath \
	--enable-calendar \
	--enable-exif \
	--enable-ftp \
	--enable-sockets \
	--enable-soap \
	--enable-mbstring \
	--enable-intl \
	--with-ldap

Compile and Install PHP

make
make install
cp ./php.ini-production /usr/serverok/php/lib/php.ini
cp /usr/serverok/php/etc/php-fpm.conf.default  /usr/serverok/php/etc/php-fpm.conf

Create php-fpm pool file

Create file

vi /usr/serverok/php/etc/php-fpm.d/www.conf

Add following content

[www]
user = www-data
group = www-data
listen = /run/php/php-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
catch_workers_output = yes
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/fpm-php.log
php_admin_flag[log_errors] = on

Start php-fpm on boot

Create a service file

vi /etc/systemd/system/php-fpm.service

Add following content

[Unit]
Description=PHP FastCGI Process Manager (ServerOK)
After=network.target

[Service]
Type=forking
PIDFile=/run/php/php-fpm.pid
ExecStart=/usr/serverok/php/sbin/php-fpm --daemonize --pid /run/php/php-fpm.pid
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Start php-fpm service

systemctl daemon-reload
systemctl enable php-fpm
systemctl start php-fpm
systemctl status php-fpm

Install PHP modules from PECL

To install PHP modules from PECL, run

apt install -y libmagick++-dev
/usr/serverok/php/bin/pecl install APCu
/usr/serverok/php/bin/pecl install redis
/usr/serverok/php/bin/pecl install imagick

To enable the PHP modules, edit

vi /usr/serverok/php/lib/php.ini

add

extension=imagick.so
extension=apcu.so
extension=redis.so

Restart php-fpm service

systemctl resart php-fpm

Enable php-fpm for your website

To enable php-fpm for Apache, add the following code in the Apache VirtualHost entry.

<FilesMatch \.php$>
	SetHandler proxy:unix:/run/php/php-fpm.sock|fcgi://127.0.0.1
</FilesMatch>

For nginx, use socket

/run/php/php-fpm.sock

Back to PHP

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *