登录  /  注册

php 怎么安装扩展

藏色散人
发布: 2021-03-05 09:41:01
原创
7871人浏览过

php安装扩展的方法:1、使用apt或者yum命令安装扩展;2、使用pecl install命令安装扩展;3、通过源码编译安装;4、通过phpize安装。

php 怎么安装扩展

本文操作环境:windows7系统、php5.3.29版,DELL G3电脑

php安装扩展的几种方法

安装扩展

注意

与Apache+PHP或者Nginx+PHP的运行模式不同,WorkerMan是基于PHP命令行 PHP CLI 运行的,使用的是不同的PHP可执行程序,使用的php.ini文件也可能不同。所以在网页中打印phpinfo()看到安装了某个扩展,不代表命令行的PHP CLI也安装了对应的扩展。

如何确定PHP CLI安装了哪些扩展

运行 php -m 会列出命令行 PHP CLI 已经安装的扩展,结果类似如下:

~# php -m
[PHP Modules]
libevent
posix
pcntl
...
登录后复制

如何确定PHP CLI 的php.ini文件的位置

当我们安装扩展时,可能需要手动配置php.ini文件,把扩展加进去,所以要确认PHP CLI的php.ini文件的位置。可以运行php --ini查找PHP CLI的ini文件位置,结果类似如下(各个系统显示结果会有差异):

~# php --ini
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      /etc/php5/cli/conf.d/apc.ini,
/etc/php5/cli/conf.d/libevent.ini,
/etc/php5/cli/conf.d/memcached.ini,
/etc/php5/cli/conf.d/mysql.ini,
/etc/php5/cli/conf.d/pdo.ini,
/etc/php5/cli/conf.d/pdo_mysql.ini
...
登录后复制

给PHP CLI安装扩展(安装memcached扩展为例)

方法一、使用apt或者yum命令安装

如果PHP是通过 apt 或者 yum 命令安装的,则扩展也可以通过 apt 或者 yum 安装

debian/ubuntu等系统apt安装PHP扩展方法(非root用户需要加sudo命令)

1、利用apt-cache search查找扩展包

~# apt-cache search memcached php
php-apc - APC (Alternative PHP Cache) module for PHP 5
php5-memcached - memcached module for php5
登录后复制

2、使用apt-get install安装扩展包

~# apt-get install -y php5-memcached
Reading package lists... Done
Reading state information... Done
...
登录后复制

centos等系统yum安装PHP扩展方法

1、利用yum search查找扩展包

~# yum search memcached php
php-pecl-memcached - memcached module for php5
登录后复制

2、使用yum install安装扩展包

~# yum install -y php-pecl-memcached
Reading package lists... Done
Reading state information... Done
...
登录后复制

说明:

使用apt或者yum安装PHP扩展会自动配置php.ini文件,安装完直接可用,十分方便。缺点是有些扩展在apt或者yum中没有对应的扩展安装包。

【推荐学习:《PHP视频教程》】

方法二、使用pecl安装

使用pecl install命令安装扩展

1、pecl install安装

~# pecl install memcached
downloading memcached-2.2.0.tgz ...
Starting to download memcached-2.2.0.tgz (70,449 bytes)
....
登录后复制

2、配置php.ini

通过运行 php --ini查找php.ini文件位置,然后在文件中添加extension=memcached.so

方法三、源码编译安装(一般是安装PHP自带的扩展,以安装pcntl扩展为例)

1、利用php -v命令查看当前的PHP CLI的版本

~# php -v
PHP 5.3.29-1~dotdeb.0 with Suhosin-Patch (cli) (built: Aug 14 2014 19:55:20)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies
登录后复制

2、根据版本下载PHP源代码

PHP历史版本下载页面:http://php.net/releases/

3、解压源码压缩包

例如下载的压缩包名称是php-5.3.29.tar.gz

~# tar -zxvf php-5.3.29.tar.gz
php-5.3.29/
php-5.3.29/README.WIN32-BUILD-SYSTEM
php-5.3.29/netware/
...
登录后复制

4、进入源码中的ext/pcntl目录

~# cd php-5.3.29/ext/pcntl/
登录后复制

5、运行 phpize 命令

~# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
登录后复制
登录后复制

6、运行 configure命令

~# ./configure
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
...
登录后复制

7、运行 make 命令

~# make
/bin/bash /tmp/php-5.3.29/ext/pcntl/libtool --mode=compile cc ...
-I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend...
...
登录后复制

8、运 行make install 命令

~# make install
Installing shared extensions:     /usr/lib/php5/20090626/
登录后复制
登录后复制

9、配置ini文件

通过运行 php --ini查找php.ini文件位置,然后在文件中添加extension=pcntl.so

说明: 此方法一般用来安装PHP自带的扩展,例如posix扩展和pcntl扩展。除了用phpize编译某个扩展,也可以重新编译整个PHP,在编译时用参数添加扩展,例如在源码根目录运行

~# ./configure --enable-pcntl --enable-posix ...
~# make && make install
登录后复制

方法四、phpize安装

如果要安装的扩展在php源码ext目录中没有,那么这个扩展需要到http://pecl.php.net 搜索下载

以安装libevent扩展为例(假设系统安装了libevent-dev库)

1、下载libevent扩展文件压缩包(在当前系统哪个目录下载随意)

~# wget http://pecl.php.net/get/libevent-0.1.0.tgz
--2015-05-26 21:43:40--  http://pecl.php.net/get/libevent-0.1.0.tgz
Resolving pecl.php.net... 104.236.228.160
Connecting to pecl.php.net|104.236.228.160|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9806 (9.6K) [application/octet-stream]
Saving to: “libevent-0.1.0.tgz”

100%[=======================================================>] 9,806       41.4K/s   in 0.2s
登录后复制

2、解压扩展文件压缩包

~# tar -zxvf libevent-0.1.0.tgz
package.xml
libevent-0.1.0/config.m4
libevent-0.1.0/CREDITS
libevent-0.1.0/libevent.c
....
登录后复制

3、进入到源码目录

~# cd libevent-0.1.0/
登录后复制

4、运行phpize命令

~# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
登录后复制
登录后复制

5、运行configure命令

~# ./configure
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
...
登录后复制

6、运行make命令

~# /bin/bash /data/test/libevent-0.1.0/libtool --mode=compile cc  -I. -I/data/test/libevent-0.1.0 -DPHP_ATOM_INC -I/data/test/libevent-0.1.0/include
...
登录后复制

7、运行make install命令

~# make install
Installing shared extensions:     /usr/lib/php5/20090626/
登录后复制
登录后复制

8、配置ini文件

通过运行 php --ini查找php.ini文件位置,然后在文件中添加extension=libevent.so

以上就是php 怎么安装扩展的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号