Ubuntu12.04 use c to compile the php extension module tutorial

Source: Internet
Author: User

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

If you encounter permission problems, we recommend that you switch to root.

1. Download source code

Check out the source code of php 5.3.10.
Copy codeThe Code is as follows:
$ Svn checkout https://svn.php.net/repository/php/php-src/branches/PHP_5_3_10/

Tips: if you do not have this command, install svn first and use apt-get to install it in ubuntu:

Copy codeThe Code is as follows:
$ Sudo apt-get install subversion

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

2. Create a module

A. After the source code is checked out, go to the ext directory of the source code and use ext_skel to create a module named my:

Copy codeThe Code is as follows:
$./Ext_skel -- extname = my

B. Enter the my module:

Copy codeThe Code is as follows:
$ Cd my

C. Modify the config. m4 file and find the following content:

Copy codeThe Code is as follows:
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 the final result is as follows:

Copy codeThe Code is as follows:
PHP_ARG_WITH (my, for my support,
Make sure that the comment is aligned:
[-- With-my Include my support])

Save the file.

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

Copy codeThe Code is as follows:
$ Php my. php
Confirm_my_compiled

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

The above confirm_my_compiled is the default function of the module. After loading the module, you can call this function.

3. Compilation Module

Run the following command in the my module directory:

Copy codeThe Code is as follows:
$ Phpize
$./Configure
$ Make
$ Make install

After compilation is successful, the following message is displayed:
Copy codeThe Code is as follows:
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 the php5-dev, In ubuntu directly use apt-get installation on OK.

Copy codeThe Code is as follows:
$ Apt-get install php5-dev

4. Load modules

Edit file:
Copy codeThe Code is as follows:
$ Vim/etc/php5/conf. d/my. ini

Add the following content to load the my. so module and save it.

Copy codeThe Code is as follows:
Extension = my. so

Restart apache

Copy codeThe Code is as follows:
$/Etc/init. d/apache2 restart

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

5. Compile the test program

If the above execution is successful, the my module is loaded and the default confirm_my_compiled method of my module can be called. Compile a php test file:
Copy codeThe Code is as follows:
Echo confirm_my_compiled ("Hello ");

Run the test file. If the following output is displayed, the test is successful.
Copy codeThe Code is as follows:
Congratulations! You have successfully modified ext/my/config. m4. Module Hello is now compiled into PHP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.