How To Compile And Install PHP Extensions From Source
Mattias Geniar, September 14, 2008
Follow me on Twitter as @mattiasgeniar
It’s not as difficult as it might sound, so here’s the quick-n-dirty way of installing and compiling your very own PHP extensions/libraries, from the PHP source code. With a normal install, not every library is compiled and installed – so it might leave you with several functions that aren’t working.
This guide is building a module for PHP 5.2, but the steps are identical for PHP 5.3, 5.4, 5.5, or 5.6.
In this case, I’ll assume you’re trying to compile the dBase-extension. It’ll probably start with the following error, shown on screen:
It means the extension wasn’t loaded – and in most cases isn’t present on the server at all. So we’ll have to create it ourselves.
Install the PHP development package
On Ubuntu/debian, you can use apt-get, it’s a piece of cake.
On CentOS / Red Hat, use yum for your PHP packages.
Download & unzip the PHP5 source code
Go the the PHP-download page, and select the complete PHP5 source code.
Next, unzip the file you just downloaded (the “.gz”-file extensions means it has been gzipped).
Prepare the extension (phpize)
Go to the proper directory. You just untarred the files, so we’ll browse to that specific subdirectory (note; directory-names may vary, depending on the version you just downloaded). In this case, we go to the “dbase” subfolder, but it could be any of the extensions you want to configure.
And use phpize in that directory.
Configure & Make the extension
The make command, will render a lot of text (compilation-info), and most likely end with the following snippet.
Woohoo, we compiled our dbase-extension! Now all we need to do, is activate it.
Move the extension
If you already have an extensions-directory on your server, use that one – if not, you can create a new directory to place the extensions. To find out, check the extension_dir directive.
Copy the extension to that directory.
Edit your PHP.INI
The extension is ready to be included – now we need to change the php.ini file to load that particular extension. First, find out which php.ini your system is using (note: the CLI may load a different config than the php-fpm or the Apache module one, a phpinfo() in your application would tell you for certain).
In the example above, we use /etc/php.ini as our base php.ini file.
Add the module you want to load. I usually add these at the very bottom, where my changes are. This is relative to the extension_dir directive you found above.
Build php from source
Building PHP on Windows 7 with Visual Studio 2010 installed.
— this differs slightly from the stepbystepbuild info and it worked for me 🙂
When running «cscript /nologo configure.js» 5.x on Windows
2000 or XP, if you get the following error:
Microsoft JScript runtime error: Object doesn’t support this
property or method
You can download WSH 5.6 here:
http://www.microsoft.com/downloads/details.aspx?
FamilyID=c717d943-7e4b-4622-86eb-95a22b832caa&DisplayLang=en
Only if you compile with the wrong runtime library.
If trying to compile the code (Windows XP, SDK v6.1) and you get the following types of errors:
c:\phpdev\php-5.2.9\main\php_network.h(128) : warning C4005: ‘POLLIN’ : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1495) :
see previous definition of ‘POLLIN’
c:\phpdev\php-5.2.9\main\php_network.h(129) : warning C4005: ‘POLLPRI’ : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1496) :
see previous definition of ‘POLLPRI’
c:\phpdev\php-5.2.9\main\php_network.h(130) : warning C4005: ‘POLLOUT’ : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1499) :
see previous definition of ‘POLLOUT’
c:\phpdev\php-5.2.9\main\php_network.h(131) : warning C4005: ‘POLLERR’ : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1502) :
see previous definition of ‘POLLERR’
c:\phpdev\php-5.2.9\main\php_network.h(132) : warning C4005: ‘POLLHUP’ : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1503) :
see previous definition of ‘POLLHUP’
c:\phpdev\php-5.2.9\main\php_network.h(133) : warning C4005: ‘POLLNVAL’ : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1504) :
see previous definition of ‘POLLNVAL’
NMAKE : fatal error U1077: ‘»C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN
\cl.exe»‘ : return code ‘0x2’
Stop.
You can alter the Makefile and add the following switch to CFLAGS:
PHP extensions can be compiled statically or shared.
— Shared compilation creates a well-known php_*.dll file.
— Static compilation puts the extension directly into PHP (therefore it does not need to be loaded and cannot be unloaded)
You can switch whether to compile given extension statically or as library by adding =static or =shared to the extension in configure.js command during the compilation.
How To Install PHP 8 From Source On Ubuntu 20.04 LTS
It provides all the steps required to compile PHP 8 on Ubuntu 20.04 LTS from the source code with the JIT support.
Notes: The default version of PHP available for Ubuntu 20.04 LTS is PHP 7.4. You can also follow How To Install PHP 7 On Ubuntu 20.04 LTS. We can also install PHP 8 on Ubuntu 20.04 LTS using PPA as explained in How To Install PHP 8 On Ubuntu 20.04 LTS.
It might be possible that some of the PHP functions used by your projects might not work, since we are making a custom build. You may feel free to build your own version of PHP by selecting the appropriate dependencies and providing the required configurations. I have selected the commonly used dependencies and configurations for the most common uses.
In this step, we will install the dependencies required to build PHP from the source code. The below-mentioned commands can be executed to install the dependencies.
We can get the most recent version of PHP 8 source code by cloning it from the GIT repository as shown below.
The above command will clone the master directory of PHP 8 from GitHub.
In this step, we will apply some standard configurations before compiling the source code. There is no configuration file available in the source code cloned by us in the previous step, hence generate the one using the command as shown below.
Now configure the compilation using the below-mentioned command. You may change your configurations according to your requirements.
You can also use the below-mentioned command to view all the available options to further change the configuration.
In this step, we will build the PHP from the source code using the configurations generated in the previous step. Now start building the PHP 8 using the commands shown below.
You may also fasten the process by giving the number of cores as shown below.
You should see an output similar to the one shown below.
We can also test the PHP compiled by us using the test command as shown below.
Now complete the installation by executing the below-mentioned command.
It will install PHP to the destination directory configured by us while generating the configuration file. We have installed PHP 8 at /opt/php/php8. Now verify the installation by using the below-mentioned command.
While writing this tutorial, the steps mentioned in this tutorial installed PHP 8.1 on Ubuntu 20.04 LTS. The same steps can be used on Ubuntu 18.04 LTS.
Configure JIT Support for PHP 8
To test the support for JIT, we must enable the opcache extension as shown below.
It clearly shows that there is no php.ini file available at the desired location i.e. /opt/php/php8/lib. We can copy the php.ini-development or php.ini-production from the php-src directory and rename it to php.ini as shown below.
We can see that our copied ini file has been identified and loaded as the default php.ini file.
Now enable opcache extension by updating the file as shown below.
Now check whether opcache extension is enabled using the below-mentioned command.
With this, you must end with the latest PHP with JIT support. Now write a simple script to test with and without JIT.
The generated assembler code confirms that JIT support is enabled.
Enable PHP-FPM for PHP 8
We can configure the PHP 8 for the Apache Web Server and NGINX by updating the php.ini and php-fpm.conf is shown below.
The above command confirms the location of the php.ini file. Update the php.ini file as shown below.
Now create the configuration file of PHP-FPM as shown below.
Also, update the php-fpm.conf as shown below.
Now create the init script to start PHP-FPM on system start.
Also, make the script executable and start on system start as shown below.
This completes the configuration of PHP-FPM for the web servers. The web servers including Apache Web Server and NGINX can pass PHP specific requests to PHP-FPM for execution and generates the output of the PHP scripts.
Also, test PHP-FPM before system restart by executing it as shown below.
Configure Apache for PHP 8
We can configure the PHP 8 installed by us to use with Apache 2 on Ubuntu 20.04 LTS. In case it’s not installed yet, you can follow How To Install Apache 2 On Ubuntu 20.04 LTS. After installing Apache 2 on Ubuntu 20.04 LTS, configure the PHP 8 installed by us as shown below.
Also, update the default host to process the PHP script via PHP-FPM as shown below.
Now test the configuration changes as shown below.
Also, restart Apache as shown below.
Now create the info.php file at the root of the Apache Web Server. You may also create the default file index.php at the root of the Apache Web Server.
Now open the browser and request info.php. It should be similar to Fig 1.
Configure NGINX for PHP 8
In the previous step, we have configured the Apache Web Server to use PHP-FPM as reverse proxy to execute PHP scripts. In this step, we will configure NGINX to execute the PHP scripts using PHP-FPM as reverse proxy.
Update the Nginx user by updating the configuration as shown below.
Now we will configure Nginx to access PHP via FPM since Nginx does not support PHP by default. Update the default server block of NGINX as shown below.
Now, reload NGINX to apply the configuration changes.
Also, create the info.php file in the root directory of nginx as shown below.
Now, access the info.php from the browser as we did for Apache Web Server in the previous section. It should show the output similar to Fig 1.
Summary
This tutorial provided all the steps required to install PHP 8 on Ubuntu 20.04 LTS using the source code with JIT support. It also provided the steps to configure PHP-FPM to start on system restart. The last two sections of this tutorial provided the steps to configure Apache Web Server and NGINX to use PHP-FPM as a reverse proxy to process PHP requests.
Build php from source
The PHP Interpreter
PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world. PHP is distributed under the PHP License v3.01.
The PHP manual is available at php.net/docs.
Prebuilt packages and binaries
Prebuilt packages and binaries can be used to get up and running fast with PHP.
For Windows, the PHP binaries can be obtained from windows.php.net. After extracting the archive the *.exe files are ready to use.
For other systems, see the installation chapter.
Building PHP source code
For a minimal PHP build from Git, you will need autoconf, bison, and re2c. For a default build, you will additionally need libxml2 and libsqlite3.
On Ubuntu, you can install these using:
On Fedora, you can install these using:
Testing PHP source code
PHP ships with an extensive test suite, the command make test is used after successful compilation of the sources to run this test suite.
Shall run make test with a maximum of 4 concurrent jobs: Generally the maximum number of jobs should not exceed the number of cores available.
The qa.php.net site provides more detailed info about testing and quality assurance.
Installing PHP built from source
After a successful build (and test), PHP may be installed with:
Depending on your permissions and prefix, make install may need super user permissions.
Contributions are most welcome by forking the GitHub repository and sending a pull request.
Discussions are done on GitHub, but depending on the topic can also be relayed to the official PHP developer mailing list internals@lists.php.net.
See Git workflow for more details on the pull request workflow.
Guidelines for contributors
See further documents in the repository for more information on how to contribute:
For the list of people who’ve put work into PHP, please see the PHP credits page.
Documentation
Note: You are currently viewing documentation for Moodle 3.8. Up-to-date documentation for the latest stable version of Moodle may be available here: Compiling PHP from source.
Compiling PHP from source
This page shows you how to build PHP from source on Ubuntu or other Debian based distributions. It could no doubt be used as a basis for compiling PHP on other Unix or Linux systems.
This discussion was based on Ubuntu 10.04 which (at the time of writing) is the latest Ubuntu LTS. It does not carry a new enough PHP version for Moodle 2.1.
Contents
Assumptions
It is assumed that you will have a basic install of Ubuntu (10.04 in this case) without (specifically) PHP installed and possibly no Apache web server either. If you already have PHP installed (as a package) you will need to remove it first:
sudo apt-get remove php5
Apache web server
You can either install Apache from the Ubuntu packages (recommended) or compile it from source. Compiling from source is simple but you end up (using default settings) with a directory structure that is completely different from the Ubuntu packaged version. This is not covered further here.
To install the package version, it’s just
sudo apt-get install apache2 apache2-dev
Obtaining additional libraries
Moodle requires a significant number of optional PHP modules. Many of these require development libraries to be available on the system before PHP is compiled. This aspect is what makes building PHP from source tricky. If you would like a challenge, all of these can be downloaded as source packages and built from scratch but it is much easier to use the packaged versions. They are installed as follows.
You may also need to create the following symbolic links as root on Linux:
Note that the above includes the clients for both the MySQL and PostgreSQL databases. You may not want to install both. You may need to include other libraries if you add further options to your PHP build. A Google search involving your Ubuntu version, the function and the word ‘library’ or ‘development’ will often turn up the correct package name.
Build environment
Ubuntu does not have all the compilers, linkers and libraries you need in a standard installation. If you have not compiled anything from source before this can be fixed by a single command.
Obtaining and building PHP
The next step is to run the ‘configure’ program. This digs around your system and creates specific make files based on your particular configuration. It also specified all the optional modules that will be compiled in. The minimum for Moodle 2 (and 1.9) is as follows.
If you have special requirements you may need others. To get the full list of possibilities you can do.
This should complete without errors and finishes with an obvious copyright notice in a box. If you do get errors, it is most likely to be due to missing libraries. Make sure you have added all the libraries described above (with apt-get). Failing that, Google is your friend.
Once that bit is done, it’s time to do the actual compiling and linking. Simply do.
It’s then just a matter of installing the files.
Configuring Apache and PHP
The first step is to copy the PHP configuration file (php.ini) from the source distribution to where it will be read on Apache startup. There are two supplied versions, one for production systems and one for development systems. The latter display many more errors and warnings and should (for security reasons) only be used in a development environment, as its name suggests. To copy your chosen file, do the following (from the source directory)..
The second line is optional and makes a link to the ini file in /etc (you can find it at /etc/php.ini). At this point you might want to edit the ini file to customise it for your needs. A very likely change is the file upload limits and the php memory_limit setting.
Apache’s configuration file will now require some changes in order to correctly handle PHP files. Ubuntu’s APache configuration is a little strange and could be a whole discussion by itself. Essentially, you need to add the following lines:
The first two can be stuck at the end of /etc/apache2/apache2.conf and the DirectoryIndex by checking mods-available/dir.conf and ensuring that index.php is in the list. Strictly speaking, you would create a file in mods-available and enable it with the a2enmod command but this is left as an exercise!
Once this has been completed, you just need to restart Apache.
Testing
The easiest way to test, is to create a small test file in the root of your web served directory (/var/www), calling it something like test.php.
Then access the page from a web browser using http://hostname.of.your.server/test.php. You should see a lengthy list of the PHP setup (which you might want to check). If you just see the php code, the three settings in the Apache config have not worked for some reason and will require checked. If you get some other error, read on.
Troubleshooting
The best advice I can give is to look at the logs. The error log for Apache is in /var/log/apache/error.log with the latest errors at the end. Always look here first. Most problems are caused by incorrect file or directory permissions or incorrect settings in the Apache configuration.
Updating PHP
There are really two possibilities. Firstly, you may want to rebuild php with extra configuration settings, or you may want to build a newer version of PHP. You can follow the installation instructions exactly as above, right up to and including sudo make install. After that, simply restart the Apache web server and you are done.
