系统环境
Centos 7 x86_64
PHP 编译与安装
$ cd /home/www/tools
# 下载 PHP 源码
# https://www.php.net/downloads.php
# https://github.com/php/php-src/releases/tag/php-7.4.28
$ wget https://www.php.net/distributions/php-7.4.28.tar.gz
$ tar -xzvf ./php-7.4.28.tar.gz
$ cd php-7.4.28
$ pwd
/home/www/tools/php-7.4.28
# 安装依赖
# https://blog.youkuaiyun.com/terence_csdn/article/details/105870279
$ yum install -y gcc libxml2 libxml2-devel sqlite-devel
# 编译
$ ./configure --prefix=/home/www/install/php7.4.28--enable-fpm --enable-debug
$ make
$ make install
# 查看编译后的目录结构
$ yum install -y tree
$ tree -L 3 /home/www/install/
/home/www/install/
├── bin
│ ├── phar -> phar.phar
│ ├── phar.phar
│ ├── php
│ ├── php-cgi
│ ├── php-config
│ ├── phpdbg
│ └── phpize
├── etc
│ ├── php-fpm.conf.default
│ └── php-fpm.d
│ └── www.conf.default
├── include
│ └── php
│ ├── ext
│ ├── include
│ ├── main
│ ├── sapi
│ ├── TSRM
│ └── Zend
├── lib
│ └── php
│ ├── build
│ └── extensions
├── php
│ ├── man
│ │ ├── man1
│ │ └── man8
│ └── php
│ └── fpm
├── sbin
│ └── php-fpm
└── var
├── log
└── run
25 directories, 10 files
# 添加环境变量
$ echo 'PATH=$PATH:/home/www/install/php7.4.28/bin' >> /etc/profile
$ source /etc/profile
$ php -v
PHP 7.4.28 (cli) (built: Feb 20 2022 08:26:53) ( NTS DEBUG )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
# 复制配置文件
$ cp /home/www/tools/php-7.4.28/php.ini-development /home/www/install/php7.4.28/lib/php.ini
$ php --ini
...
Loaded Configuration File: /home/www/install/php7.4.28/lib/php.ini
...
扩展开发
$ cd /home/www/tools/php-7.4.28/ext
$ php ./ext_skel.php --ext mydemoext
$ cd mydemoext
$ tree
.
├── config.m4
├── config.w32
├── mydemoext.c
├── php_mydemoext.h
└── tests
├── 001.phpt
├── 002.phpt
└── 003.phpt
1 directory, 7 files
$ vim mydemoext.c
/* mydemoext extension for PHP */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_mydemoext.h"
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
/* {{{ string hello( [ string $var ] )
* */
PHP_FUNCTION(hello)
{
char *var;
size_t var_len;
zend_string *retval;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(var, var_len)
ZEND_PARSE_PARAMETERS_END();
retval = strpprintf(0, "Hello %s", var);
RETURN_STR(retval);
}
/* }}}*/
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(mydemoext)
{
#if defined(ZTS) && defined(COMPILE_DL_MYDEMOEXT)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(mydemoext)
{
php_info_print_table_start();
php_info_print_table_header(2, "mydemoext support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ arginfo
*/
ZEND_BEGIN_ARG_INFO(arginfo_hello, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ mydemoext_functions[]
*/
static const zend_function_entry mydemoext_functions[] = {
PHP_FE(hello, arginfo_hello)
PHP_FE_END
};
/* }}} */
/* {{{ mydemoext_module_entry
*/
zend_module_entry mydemoext_module_entry = {
STANDARD_MODULE_HEADER,
"mydemoext", /* Extension name */
mydemoext_functions, /* zend_function_entry */
NULL, /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(mydemoext), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(mydemoext), /* PHP_MINFO - Module info */
PHP_MYDEMOEXT_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_MYDEMOEXT
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(mydemoext)
#endif
$ yum install -y autoconf
# yum install -y m4
$ /home/www/install/php7.4.28/bin/phpize
$ ./configure --with-php-config=/home/www/install/php7.4.28/bin/php-config
$ make
$ ls modules/
mydemoext.la mydemoext.so
$ make install
Installing shared extensions: /home/www/install/php7.4.28/lib/php/extensions/debug-non-zts-20190902/
$ vim /home/www/install/php7.4.28/lib/php.ini
# 追加
extension=/home/www/install/php7.4.28/lib/php/extensions/debug-non-zts-20190902/mydemoext.so
$ php -m
[PHP Modules]
...
mydemoext
...
测试
$ vim test.php
<?php
echo hello('xchenhao') . "\n";
$ php test.php
Hello xchenhao
参考
- https://www.zend.com/resources/writing-php-extensions
Writing PHP Extensions