Home  >  Article  >  Backend Development  >  Installation of PHP extension developed under Linux

Installation of PHP extension developed under Linux

WBOY
WBOYOriginal
2016-07-29 09:15:10884browse

Introduction to the Preface

The words of a friend from Maimai make me more and more aware of the advantages of having a background in computer science. The longer I program, the more I feel the importance of basic knowledge. It is like a mountain that cannot be bypassed. There are twists and turns. The future may be bright, but there will always be encounters. This mountain, only by facing up to the difficulties and climbing over it in one go can we continue on the road we are on.
I strongly agree. The longer you program, the more you have to study the underlying layers to improve yourself, so the next goal is to systematically study PHP source code. It just so happened that I was studying for C in the Adult Education Entrance Examination, and the teacher was an NB academic figure. The stack of books that claim to have read C is higher than the desk.

Preparation

I found from the Internet that there are not many and complete information on PHP extension development, and there is no established forum. It seems that there is not much demand in this area.
First you have to download the php source code, which can be downloaded from git/svn or the official website. I downloaded it from the official website, because it is said on the Internet that some source codes under git do not have the phpize tool (I have not tried it) phpize is used to extend PHP extensions Modular.

Start

Enter the downloaded php source code
There is an ext_skel file in the ext directory of the php source code. You can use it to easily build a PHP extension framework. Let's create a myext extension

Enter the ext directory cd ext/

./ext_skel –extname=myext
Creating directory myext
Creating basic files: config.m4 config.w32 .gitignore myext.c php_myext.h CREDITS EXPERIMENTAL tests/001.phpt myext.php [done].

To use your new extension, you will have to execute the following steps:

  1. $ cd ..
  2. $ vi ext/myext/config.m4
  3. $ ./buildconf
  4. $ ./configure –[with|enable]-myext
  5. $ make
  6. $ ./sapi/cli/ php -f ext/myext/myext.php
  7. $ vi ext/myext/myext.c
  8. $ make

Repeat steps 3-6 until you are satisfied with ext/myext/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.`

At this time, there should be a myext directory in your directory. This is the extension framework generated by ext_skel for you.
Then you need to modify config.m4
Remove the dnl in front of the following code:

<code>dnl PHP_ARG_ENABLE(myext, whether <span>to</span> enable myext support,
dnl [  <span>--enable-myext           Enable myext support])</span></code>

Save and exit!
Start compiling
In the myext directory. Execute the following code:

<code>phpize
<span>.</span>/configure <span>--</span><span>with</span><span>-php</span><span>-config</span><span>=</span>/usr/<span>local</span>/php/bin/php<span>-config</span>
make
make test
make install</code>

After make install, you will be prompted to install the extension directory /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/. After entering, you can see myext.so

Then add extended information in php.ini
extension = myext.so
Use the php -m command to see whether the extension is installed successfully.
Restart fpm and you can see the installed extension in phpinfo

Installation of PHP extension developed under Linux

Note

It is done to install an extension here.
When making test, you will be warned that some functions are disabled in php.ini. Find the disable_function of php.ini and remove the warning function.
In addition, let me explain the installation steps of the programs generated by GNU's AUTOCONF and AUTOMAKE in Linux

config/configure/Configure
This is used to detect the target characteristics of your installation platform. For example, it will detect whether you have CC or GCC. It does not require CC or GCC. It is a shell script
This step is generally used to generate Makefile to prepare for the next step of compilation. You can control the installation by adding parameters after configure, such as:
./configure –prefix=/usr
The above means to install the software under /usr
The executable file will be installed in /usr/bin (instead of the default /usr/local/bin)
The resource files will be installed in /usr/share (instead of the default /usr/local/share)
At the same time, you can specify the configuration files of some software by specifying –sys-c
There are also parameters such as –with, –enable, –without, –disable, etc. to control compilation. You can view detailed instructions and help through ./configure –help
make
This step is used for compilation. It reads instructions from the Makefile and then compiles
This step is compilation. Most source code packages are compiled through this step
Of course, some software written in perl or python needs to call perl or python to compile
If an error occurs during the make process, you need to write down the error code (note not just the last line), and then you can submit a bugreport to the developer (usually there is a submission address in INSTALL), or your system has fewer dependencies. Libraries, etc., you need to study the error codes carefully
make test / make check
As the name suggests, this step is to check the previous step of make. It is necessary to ensure that there are no errors in make. That is, the test and check of this step must all be OK, and the error is 0
sudo make install
This step is for installation. It also reads instructions from the Makefile and installs it to the specified location
To install this command, you generally need to have root permissions (because you need to write files to the system), so sudo

AUTOMAKE and AUTOCONF are very useful for publishing C programs. If you also write programs and want to use AUTOMAKE and AUTOCONF, you can refer to the relevant articles on CNGNU.ORG

The above introduces the installation of PHP extensions developed under Linux, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn