Backend Development 3 min read

How to Build and Install a PHP Extension from Scratch

This guide walks you through creating a PHP extension, covering environment setup, using phpize, configuring, compiling with make, installing the shared test.so module, adding it to php.ini, and verifying its functionality with command‑line tests, preparing you for production deployment.

360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
How to Build and Install a PHP Extension from Scratch

Today we share a series of articles on writing PHP extensions, covering environment setup to extension development, introducing relevant PHP core data structures and APIs.

The extension framework can be compiled without any modifications. The first phpize command is part of the PHP build created in step one (it should already be in your PATH).

<code>$ phpize
$ ./configure
$ make
$ make install</code>

These commands build the shared extension test.so and copy it to the PHP installation directory. To load it, add a line to a custom php.ini file.

<code>$ vi ~/php-bin/DEBUG/etc/php.ini</code>

Add the following line:

<code>extension=test.so</code>

Verify that the extension is loaded and working; the php -m command lists loaded extensions:

<code>$ php -m | grep test
test</code>

You can also run functions defined in the test extension:

<code>$ php -r 'test_test1();'
The extension test is loaded and working!
$ php -r 'echo test_test2("world\n");'
Hello world</code>

This completes the build and installation of the extension; the next step will prepare it for a production environment.

BackendCompilationlinuxPHPExtension Development
360 Zhihui Cloud Developer
Written by

360 Zhihui Cloud Developer

360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.