一、下載php源碼php
wget http://cn2.php.net/distributions/php-5.6.10.tar.gz tar -zxvf php-5.6.10.tar.gz
二、創建擴展開發框架 ./ext_skel --extname=helloworldlinux
cd php-5.6.10/ext/ ./ext_skel --extname=helloworld Creating directory helloworld Creating basic files: config.m4 config.w32 .gitignore helloworld.c php_helloworld.h CREDITS EXPERIMENTAL tests/001.phpt helloworld.php [done]. To use your new extension, you will have to execute the following steps: 1. $ cd .. 2. $ vi ext/helloworld/config.m4 3. $ ./buildconf 4. $ ./configure --[with|enable]-helloworld 5. $ make 6. $ ./sapi/cli/php -f ext/helloworld/helloworld.php 7. $ vi ext/helloworld/helloworld.c 8. $ make Repeat steps 3-6 until you are satisfied with ext/helloworld/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.
三、進入php源碼的根目錄, 編輯文件 vim ext/helloworld/config.m4git
去掉這幾行代碼前面的dnl
PHP_ARG_ENABLE(helloworld, whether to enable helloworld support, Make sure that the comment is aligned: [ --enable-helloworld Enable helloworld support])
四、在php源碼根目錄執行命令 ./buildconf --forceweb
Forcing buildconf Removing configure caches buildconf: checking installation... buildconf: autoconf version 2.69 (ok) rebuilding configure rebuilding main/php_config.h.in
五、在php源碼的根目錄編譯php程序,注意命令爲 ./configure --with-helloworldshell
configure: WARNING: unrecognized options: --with-helloworld checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu checking for cc... cc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether cc accepts -g... yes checking for cc option to accept ISO C89... none needed checking how to run the C preprocessor... cc -E checking for icc... no checking for suncc... no ...... 最後出現的error,沒有處理 configure: error: xml2-config not found. Please check your libxml2 installation.
六、. 進入咱們的擴展目錄helloworld,執行命令 phpize(經過 sudo apt-get install php5-dev 安裝 phpize),此時你的擴展目錄會生成不少文件,能夠用於後期編譯。vim
cd php-5.6.10/ext/helloworld phpize Configuring for: PHP Api Version: 20121113 Zend Module Api No: 20121212 Zend Extension Api No: 220121212
七、在helloworld目錄編譯咱們的擴展 ./configure --with-php-config=/usr/bin/php-config(使用你本身環境的php-config) --enable-helloworldapi
你能夠經過一條命令查找你的php-config文件的位置(find / -name php-config)以下:個人地址爲/usr/bin/php-configbash
./configure --with-php-config=/usr/bin/php-config checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking for cc... cc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether cc accepts -g... yes checking for cc option to accept ISO C89... none needed checking how to run the C preprocessor... cc -E checking for icc... no checking for suncc... no checking whether cc understands -c and -o together... yes checking for system library directory... lib checking if compiler supports -R... no ......
八、進入擴展helloworld目錄,編輯文件php_helloworld.h,在最後一行添加函數PHP_FUNCTION(helloworldTest);app
PHP_FUNCTION(helloworldTest);
九、用vim 打開helloword.c,在helloworld.c中實現咱們的函數,以後 將helloworldTest函數加入到helloworld_functions[]中,保存退出框架
PHP_FUNCTION(helloworldTest) { RETURN_STRING("Hello World !", 1); } const zend_function_entry helloworld_functions[] = { PHP_FE(confirm_helloworld_compiled, NULL) /* For testing, remove later. */ PHP_FE(helloworldTest, NULL) PHP_FE_END /* Must be the last line in helloworld_functions[] */ };
十、執行make命令 make 編譯擴展,我在運行的過程當中仍是比較順利的。若是出現錯誤,請認真看前面步驟是否有錯,我在第一遍作的時候也有錯通常都是前面步驟有問題。
/bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=compile cc -I. -I/php/php-5.6.10/ext/helloworld -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /php/php-5.6.10/ext/helloworld/helloworld.c -o helloworld.lo libtool: compile: cc -I. -I/php/php-5.6.10/ext/helloworld -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /php/php-5.6.10/ext/helloworld/helloworld.c -fPIC -DPIC -o .libs/helloworld.o /bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=link cc -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -DHAVE_CONFIG_H -g -O2 -o helloworld.la -export-dynamic -avoid-version -prefer-pic -module -rpath /php/php-5.6.10/ext/helloworld/modules helloworld.lo libtool: link: cc -shared -fPIC -DPIC .libs/helloworld.o -O2 -Wl,-soname -Wl,helloworld.so -o .libs/helloworld.so libtool: link: ( cd ".libs" && rm -f "helloworld.la" && ln -s "../helloworld.la" "helloworld.la" ) /bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=install cp ./helloworld.la /php/php-5.6.10/ext/helloworld/modules libtool: install: cp ./.libs/helloworld.so /php/php-5.6.10/ext/helloworld/modules/helloworld.so libtool: install: cp ./.libs/helloworld.lai /php/php-5.6.10/ext/helloworld/modules/helloworld.la libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/sbin" ldconfig -n /php/php-5.6.10/ext/helloworld/modules ---------------------------------------------------------------------- Libraries have been installed in: /php/php-5.6.10/ext/helloworld/modules If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf' See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- Build complete. Don't forget to run 'make test'.
十一、安裝php
apt-get install php5 php5-gd php5-cli
十一、將編譯生成的helloworld.so 文件複製到你本機的php擴展目錄
建立:info.php 內容:phpinfo(); php info.php | grep exten extension_dir => /usr/lib/php5/20121212 => /usr/lib/php5/20121212 mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1. root@4ccdc77255ea:/php/php-5.6.10/ext/helloworld# cp modules/helloworld.so /usr/lib/php5/20121212/
十二、配置php.ini 開啓 helloworld.so 擴展
php info.php | grep php.ini Loaded Configuration File => /etc/php5/cli/php.ini root@4ccdc77255ea:/php# vim /etc/php5/cli/ conf.d/ php.ini
1三、建立echo.php
<?php print_r(helloworldTest()); 結果:Hello World !
1四、Docker中遇到的問題
1. apt-get install autoconf 2. gcc make等依賴包安裝