准备
1.php版本
https://www.php.net/distributions/php-5.5.8.tar.bz2
tar xvf php-5.5.8.tar
2.安装依赖类库
yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel
# 增加用户组和用户
groupadd www
useradd -g www -s /sbin/nologin -M www
3.安装php
./configure \--prefix=/usr/local/php --with-config-file-path=/etc --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --with-iconv --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvsem --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-readline --without-sqlite3 --without-pdo-sqlite --with-pear
make && make install
4.配置修改
# php.ini 配置拷贝
cp php-5.5.8/php.ini-development /etc/php.ini
# php-fpm 配置修改
cp /usr/local/php/etc/php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
;pid = run/php-fpm.pid #;去掉保存
# 启动项
cp php-5.5.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
# 启动命令
/etc/init.d/php-fpm start
/etc/init.d/php-fpm stop
/etc/init.d/php-fpm restart
# php -v
vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/php/bin
export PATH
. ~/.bash_profile
ext_skel
1.格式
php-5.5.8/ext/ext_skel --extname=扩展名
2.自定扩展
php-5.5.8/ext/ext_skel --extname=array_square_sum
Creating directory zzmeow
Creating basic files: config.m4 config.w32 .svnignore zzmeow.c php_zzmeow.h CREDITS EXPERIMENTAL tests/001.phpt zzmeow.php [done].
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/array_square_sum/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-array_square_sum
5. $ make
6. $ ./php -f ext/zzmeow/array_square_sum.php
7. $ vi ext/zzmeow/array_square_sum.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/array_square_sum/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.
3.编辑config.m4 去掉dnl
vim php-5.5.8/ext/array_square_sum/config.m4
dnl PHP_ARG_WITH(array_square_sum, for array_square_sum support,
dnl Make sure that the comment is aligned:
dnl [ --with-array_square_sum Include array_square_sum support])
4.编辑array_square_sum.c
1.array_square_sum_functions 调整
const zend_function_entry array_square_sum_functions[] = {
PHP_FE(confirm_array_square_sum_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in array_square_sum_functions[] */
};
# 修改成
const zend_function_entry array_square_sum_functions[] = {
PHP_FE(array_square_sum, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in array_square_sum_functions[] */
};
2.confirm_array_square_sumw_compiled 调整
PHP_FUNCTION(confirm_array_square_sumw_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "zzmeow", arg);
RETURN_STRINGL(strg, len, 0);
}
# 修改成
PHP_FUNCTION(array_square_sum)
{
zval *array_data;
HashTable *ht_data;
int ret;
char *key;
uint index;
zval **pdata;
double sum = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array_data) == FAILURE) {
return;
}
ht_data = Z_ARRVAL_P(array_data);
zend_hash_internal_pointer_reset(ht_data);
while (HASH_KEY_NON_EXISTANT != (ret = zend_hash_get_current_key(ht_data, &key, &index, 0))) {
ret = zend_hash_get_current_data(ht_data, &pdata);
if (Z_TYPE_P(*pdata) == IS_LONG) {
sum += Z_LVAL_P(*pdata) * Z_LVAL_P(*pdata);
} else {
RETURN_FALSE;
}
zend_hash_move_forward(ht_data);
}
zend_hash_internal_pointer_end(Z_ARRVAL_P(array_data));
RETVAL_DOUBLE(sum);
}
5.phpize 编译
find / -name php-config #查找本地php-config路径
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
6.php.ini 添加扩展
vim php/etc/php.ini
[array_square_sum]
extension=array_square_sum.so
引用
https://www.cnblogs.com/martinjinyu/articles/3663410.html
https://blog.csdn.net/2301_77162959/article/details/130928012
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。