Skip to content

Instantly share code, notes, and snippets.

@m1st0
Last active November 25, 2023 07:33
Show Gist options
  • Save m1st0/1c41b8d0eb42169ce71a to your computer and use it in GitHub Desktop.
Save m1st0/1c41b8d0eb42169ce71a to your computer and use it in GitHub Desktop.
Compiling PHP 8 on Ubuntu 22.10 with Various Supported Modules
#!/bin/bash
# PHP 8 Compile #
# Author: Maulik Mistry
# Please share support: https://www.paypal.com/paypalme/m1st0
# References:
# http://www.zimuel.it/install-php-7/
# http://www.hashbangcode.com/blog/compiling-and-installing-php7-ubuntu
# root-talis https://gist.github.com/root-talis/40c4936bf0287237839ccd3fdfdaec28
#
# License: BSD License 2.0
# Copyright (c) 2015-2023, Maulik Mistry
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Desired PHP branch if not using git tag autodetection
PHP_VERSION="8.3.0"
# Recommended setup location
PHP_DIR="/usr/local"
# Stop execution if things fail to move forward.
set -e
## DEPENDENCIES
# Setup dependencies for PHP 8
# Add any missing needs from configuration area
#sudo apt-get update
sudo apt install libldap2-dev \
libldap-common \
libtool-bin \
libzip-dev \
lbzip2 \
libxml2-dev \
bzip2 \
re2c \
libbz2-dev \
apache2-dev \
libjpeg-dev \
libxpm-dev \
libxpm-dev \
libgmp-dev \
libgmp3-dev \
libmcrypt-dev \
libmysqlclient-dev \
mysql-server \
mysql-common \
libpspell-dev \
librecode-dev \
libcurl4-openssl-dev \
libxft-dev \
libonig-dev \
libsqlite3-dev
# LDAP does not recognize these without additional symlinks
if [[ ! -e /usr/lib/libldap.so ]]; then
sudo ln -sf /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so
fi
if [[ ! -e /usr/lib/liblber.so ]]; then
sudo ln -sf /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so
fi
if [[ ! -e /usr/include/gmp.h ]]; then
sudo ln -sf /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h
fi
## CONFIGURE
if [ ! -d ./php-src ]; then
git clone https://github.com/php/php-src
fi
if [ -d ./php-src ]; then
cd ./php-src
# Obtain latest tag
git reset --hard HEAD
git fetch --tags
# Sometimes the latest tag is not properly found using either of the following
#tag=$(git describe --tags `git rev-list --tags --max-count=1`)
#tag=$(git describe --tags `git rev-list --branches --max-count=1`)
#git checkout tags/$tag -b $tag
git checkout tags/php-${PHP_VERSION} -b PHP-${PHP_VERSION}
else
git clone https://github.com/php/php-src
cd ./php-src
git checkout tags/php-${PHP_VERSION} -b PHP-${PHP_VERSION}
fi
# Helped fix configure issues and ignored files needing an update
./buildconf --force
# Compile options
./configure --prefix="$PHP_DIR/php8" \
CPPFLAGS="-I/usr/include/mysql" \
--with-config-file-path="$PHP_DIR/php8/etc/" \
--with-config-file-scan-dir="$PHP_DIR/php8/etc/conf.d/" \
--enable-mbstring \
--with-zip \
--enable-bcmath \
--enable-pcntl \
--enable-ftp \
--enable-exif \
--enable-calendar \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-intl \
--enable-zts \
--with-curl \
--with-iconv \
--with-gmp \
--with-pspell \
--with-zlib-dir=/usr \
--enable-gd-jis-conv \
--with-openssl \
--with-gettext=/usr \
--with-zlib=/usr \
--with-bz2 \
--with-apxs2=/usr/bin/apxs \
--with-ldap \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-xdebug
# --with-mcrypt \
# --enable-wddx \
# --with-gd \
# --with-jpeg-dir=/usr \
# --with-png-dir=/usr \
# --with-xpm-dir=/usr \
# --with-freetype-dir=/usr \
# --enable-gd-native-ttf \
# --with-recode=/usr \
# --without-xml
## COMPILE
# Cleanup for previous failures
make clean
# Build
cpunum=`nproc`
make -j ${cpunum}
# Install
sudo make install
# Update paths for easy switching
sudo update-alternatives --install /usr/bin/php php $PHP_DIR/php8/bin/php 50 \
--slave /usr/share/man/man1/php.1.gz php.1.gz \
$PHP_DIR/php8/php/man/man1/php.1
# Provide PHP version choice
printf "Select the version of PHP you want active in subsequent shells and the \
system:\n"
sudo update-alternatives --config php
## APACHE SETUP
# Provide Apache2 libphp.so for later configuration
printf "Running 'libtool --finish ./libs' as recommended.\n"
libtool --finish ./libs
sudo mkdir -p "$PHP_DIR/php8/lib/apache2/modules"
sudo cp "./libs/libphp.so" "$PHP_DIR/php8/lib/apache2/modules/"
# Left unconfigured between Apache2 vs CLI
sudo mkdir -p "$PHP_DIR/php8/etc/"
sudo cp "php.ini-development" "$PHP_DIR/php8/etc/"
sudo cp "php.ini-production" "$PHP_DIR/php8/etc/"
# Work on non-threaded version from configure
#sudo a2dismod mpm_worker
#sudo a2enmod mpm_prefork
# Work on threaded --enable-zts version from configure
sudo a2dismod mpm_prefork
sudo a2enmod mpm_worker
PHP_MODULE_CONF="/etc/apache2/mods-available/php8.conf"
if [[ ! -e "$PHP_MODULE_CONF" ]]; then
sudo tee "$PHP_MODULE_CONF" > /dev/null <<EOF
<IfModule php_module>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
SetHandler application/x-httpd-php-source
# Deny access to raw php sources by default
# To re-enable it's recommended to enable access to the files
# only in specific virtual host or directory
Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(ar|p|ps|tml)$">
Require all denied
</FilesMatch>
</IfModule>
EOF
fi
PHP_MODULE_LOAD="/etc/apache2/mods-available/php8.load"
if [[ ! -e "$PHP_MODULE_LOAD" ]]; then
sudo tee "$PHP_MODULE_LOAD" > /dev/null <<EOF
LoadModule php_module $PHP_DIR/php8/lib/apache2/modules/libphp.so
EOF
fi
sudo a2dismod php
sudo a2enmod php8
# Restart Apache if all went well.
sudo systemctl restart apache2
sudo systemctl status apache2
# View any errors for Apache startup.
printf "Any errors starting Apache2 with PHP can be seen with 'sudo journalctl -xe' .\n"
# An example:
#
# Running PHP scripts in user directories is disabled by default
#
# You may commnt this out as needed in conf files at
# /etc/apache2/mods-available/
# to re-enable PHP in user directories
# from <IfModule> to </IfModule>
#
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
#</IfModule>"
# Return to reprevious location
cd -
@vanilla-thunder
Copy link

you can save a lot of hdd space and time by shadow cloning a particular branch instead the whole repository:
$git clone https://github.com/php/php-src = 320MB
$git clone -b PHP-7.0.6 https://github.com/php/php-src --depth 1 = 19.7 MB
(my revision: https://gist.github.com/vanilla-thunder/83149000fcc7974b58a157f3f3020a9d/revisions )

@nileshadiyecha
Copy link

I am not seeing any folder "/etc/php7/". so anyone guide me about this issue?

@7snovic
Copy link

7snovic commented Aug 4, 2016

when you run :

./configure
.....
--with-apxs2=/usr/bin/apxs

i got that error :

sorry i cannot run apxs. possible reasons follow

to resolve this you need to configure with :
--with-apxs2=/usr/bin/apxs2

and if you got that error :

configure: error: freetype-config not found.

you will need to install libfreetype6-dev

@7snovic
Copy link

7snovic commented Aug 15, 2016

in some machines you will need to do some manual search for those files
libldap.so , liblber.so , gmp.h

on AMD64 system you will not be able to locate that folder
/usr/lib/x86_64-linux-gnu/
and i found this folder under i386-linux-gnu name

so that part in some machines may be as like that

sudo ln -sf /usr/lib/i386-linux-gnu/libldap.so /usr/lib/libldap.so
sudo ln -sf /usr/lib/i386-linux-gnu/liblber.so /usr/lib/liblber.so
sudo ln -sf /usr/include/i386-linux-gnu/gmp.h /usr/include/gmp.h

@7snovic
Copy link

7snovic commented Aug 15, 2016

you will face a problem with --with-t1lib
from php.net
T1lib To enable support for T1lib (Postscript Type 1 fonts) add --with-t1lib[=DIR] (Removed as of PHP 7.0.0).

@Llbe
Copy link

Llbe commented Oct 12, 2016

I recommend --with-readline as well, to make sure you don't get stuck with libedit. Eg you can cut and paste text with ^U/^W and ^Y in interactive mode.

@gustawdaniel
Copy link

gustawdaniel commented Dec 18, 2016

Line with t1lib is unnecesarry because of depreciation. (75)

http://php.net/manual/en/image.installation.php

T1lib To enable support for T1lib (Postscript Type 1 fonts) add --with-t1lib[=DIR] (Removed as of PHP 7.0.0).

Generally great script. I am impressed!

@Miladbr
Copy link

Miladbr commented Jan 11, 2017

On Ubuntu server 16.04 , before executing configure you should install bison package.

Configure: error: bison is required to build PHP/Zend when building a GIT checkout!

@amdrade
Copy link

amdrade commented Jan 21, 2017

Ubuntu 15.04 ERROR: Module php7 does not exist!

@dericlima
Copy link

I used your script to install php 5.6.
Works like a charm. Thanks

@Pintu79
Copy link

Pintu79 commented May 30, 2017

I am installing php-7.1.5 and able to build it smoothly using source code. I have one doubt, which url should I check if php server is running fine

@root-talis
Copy link

I have updated the gist here: https://gist.github.com/root-talis/40c4936bf0287237839ccd3fdfdaec28

@m1st0 please consider pulling the changes.

How do you create a pull request for a gist?..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment