ubuntu12.04 Use c to write php extension module tutorial share

  • 2020-12-09 00:46:51
  • OfStack

The system is ubuntu 12.04. apache and php have been installed. The version of php is 5.3.10.

The following actions are recommended to switch to root if you encounter permission issues.

1, download the source code

Check out the source code for php 5.3.10


$ svn checkout https://svn.php.net/repository/php/php-src/branches/PHP_5_3_10/

Tips: If you do not have this command, you need to install svn first. Use ES20en-ES21en under ubuntu to install OK:


$ sudo apt-get install subversion

About php source, such as the need to know more information, you can view php wiki about svn description: https: / / wiki php. net/vcs/svnfaq

2. Create modules

After checking out the source code, enter the source code ext directory, first use ext_skel to create a module named my:


$ ./ext_skel --extname=my

b, enter my module:


$ cd my

Modify the file ES59en.es60EN4 to find the following contents:


dnl PHP_ARG_WITH(my, for my support,
dnl Make sure that the comment is aligned:
dnl [  --with-my             Include my support])

Remove the previous dnl and conclude as follows:


PHP_ARG_WITH(my, for my support,
Make sure that the comment is aligned:
[  --with-my             Include my support])

Then save the file.

Tips: If you need to test whether the modification is correct, you can use php ES76en.php to test:


$ php my.php
confirm_my_compiled
Congratulations! You have successfully modified ext/my/config.m4. Module my is now compiled into PHP.

confirm_my_compiled above is the default function of the module, which can be called after the module is loaded.

3. Compile module

Again, in the directory of the my module, execute the following command:


$ phpize
$ ./configure
$ make
$ make install

After successful compilation, this message will be prompted:


Installing shared extensions: /usr/lib/php5/20090626+lfs/

Indicates that the module has been compiled and copied to the php module directory.

Tips: If the phpize command is not found, you need to install php5-ES106en. Use ES108en-ES109en to install OK.


$ apt-get install php5-dev

4. Load the module

Edit file:


$ vim /etc/php5/conf.d/my.ini

Add the following to indicate that the ES122en. so module is loaded and then saved.


extension=my.so

Restart apache


$ /etc/init.d/apache2 restart

In the web environment, you can use phpinfo to see if the my module is loaded. The terminal can be viewed using php-i | less.

5. Write test programs

If all of the above works well, then the my module is loaded, and the default method of the my module, confirm_my_compiled, is called. Prepare php test file:


echo confirm_my_compiled("Hello");

Execute the test file, and if you see the output below, it is successful.


Congratulations! You have successfully modified ext/my/config.m4. Module Hello is now compiled into PHP.


Related articles: