公司的一个项目要从php5.6升级到php7版本,项目的mongo类库用不了,看了php手册,发现要对于php7要安装新的类库。php7需要使用的library是 mongodb而不是旧的mongo,所以要在项目中下载这个mongodb(library)。
对于一个要安装mongodb library的项目,直接用composer下载,使用composer require mongodb/mongodb,但是报错
localhost:libraries taiwu$ composer require mongodb/mongodb
Using version ^1.3 for mongodb/mongodb
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- mongodb/mongodb 1.3.1 requires ext-mongodb ^1.4.0 -> the requested PHP extension mongodb is missing from your system.
- mongodb/mongodb 1.3.0 requires ext-mongodb ^1.4.0 -> the requested PHP extension mongodb is missing from your system.
- Installation request for mongodb/mongodb ^1.3 -> satisfiable by mongodb/mongodb[1.3.0, 1.3.1].
To enable extensions, verify that they are enabled in your .ini files:
- /usr/local/etc/php/5.6/php.ini
- /usr/local/etc/php/5.6/conf.d/ext-mcrypt.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
Installation failed, reverting ./composer.json to its original content.
上面错误说明两个问题:
a. 我的系统使用php -v,肯定得到的是5.6版本,因为我安装了php7版本,但是没有php7放到PATH中更靠前的设置,所以系统直接使用php时,用了5.6。既然主要用php7就,就要设置下PATH。(用命令export PATH=/usr/local/opt/php70/bin/:$PATH)
b.我的php(这里报错说的是5.6)并没有按照mongodb扩展,这个通过php -v能看出来的。
那么我们先让php7版本成为默认的php指令,修改PATH即可,这样在执行composer,它使用的php就是php7了。这时报错信息,就是php7.0要加上mongodb扩展才行
localhost:libraries taiwu$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/opt/php56/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin
localhost:libraries taiwu$ export PATH=/usr/local/opt/php70/bin/:$PATH
localhost:libraries taiwu$ echo $PATH
/usr/local/opt/php70/bin/:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/opt/php56/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin
localhost:libraries taiwu$ composer require mongodb/mongodb
Using version ^1.3 for mongodb/mongodb
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- mongodb/mongodb 1.3.1 requires ext-mongodb ^1.4.0 -> the requested PHP extension mongodb is missing from your system.
- mongodb/mongodb 1.3.0 requires ext-mongodb ^1.4.0 -> the requested PHP extension mongodb is missing from your system.
- Installation request for mongodb/mongodb ^1.3 -> satisfiable by mongodb/mongodb[1.3.0, 1.3.1].
To enable extensions, verify that they are enabled in your .ini files:
- /usr/local/etc/php/7.0/php.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
Installation failed, reverting ./composer.json to its original content.
解决上面的问题,最终让基于php7的项目能安装mongodb library很简单,只要保证系统安装的php7,且安装了mongodb扩展。
下面说一下具体步骤:
1、因为是mac pro,通过homebrew安装软件非常方便, 用它安装php7
使用brew search php7,来查看下载php7的具体参数,如下,homebrew/php/php70就是我要下载的版本
localhost:fanghuwang_global taiwu$ brew search php7
==> Searching local taps...
homebrew/php/php70 ✔ homebrew/php/php70-pspell homebrew/php/php71-imagick homebrew/php/php72-ast
homebrew/php/php70-amqp homebrew/php/php70-pthreads homebrew/php/php71-ioncubeloader homebrew/php/php72-blitz
homebrew/php/php70-apcu homebrew/php/php70-raphf homebrew/php/[email protected] homebrew/php/php72-couchbase
homebrew/php/php70-apcu-bc homebrew/php/php70-rdkafka homebrew/php/php71-lua homebrew/php/php72-ds
homebrew/php/php70-ast homebrew/php/php70-redis homebrew/php/php71-lz4 homebrew/php/php72-ev
homebrew/php/php70-blitz homebrew/php/php70-ref homebrew/php/php71-lzf homebrew/php/php72-event
homebrew/php/php70-couchbase homebrew/php/php70-scrypt homebrew/php/php71-mailparse homebrew/php/php72-gearman
homebrew/php/php70-ds homebrew/php/php70-snmp homebrew/php/php71-maxminddb homebrew/php/php72-geoip
homebrew/php/php70-ev homebrew/php/php70-sodium homebrew/php/php71-mecab homebrew/php/php72-geos
使用brew install homebrew/php/php70,但是报错了,提示冲突的公式(具体含义参考brew官网),按照下面提示操作,使用brew unlink php53即可。
localhost:~ taiwu$ brew install homebrew/php/php70
==> Installing php70 from homebrew/homebrew-php
Error: Cannot install homebrew/php/php70 because conflicting formulae are installed.
php53: because different php versions install the same binaries.
Please `brew unlink php53` before continuing.
Unlinking removes a formula's symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side-effects in the
resulting software.
localhost:~ taiwu$ brew unlink php53
Unlinking /usr/local/Cellar/php53/5.3.29_3... 16 symlinks removed
继续安装,安装成功。可以看到php的路径等信息如下,
然后执行 export PATH=/usr/local/opt/php70/bin/:$PATH, 这样shell中使用php命令,就是php7.0的了
localhost:~ taiwu$ brew install homebrew/php/php70
==> Installing php70 from homebrew/homebrew-php
==> Downloading https://homebrew.bintray.com/bottles-php/php70-7.0.0-rc.3.yosemite.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring php70-7.0.0-rc.3.yosemite.bottle.2.tar.gz
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so
SetHandler application/x-httpd-php
Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html
The php.ini file can be found in:
/usr/local/etc/php/7.0/php.ini
✩✩✩✩ Extensions ✩✩✩✩
If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:
PATH="/usr/local/bin:$PATH"
PHP70 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.
✩✩✩✩ PHP CLI ✩✩✩✩
If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file:
export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"
✩✩✩✩ FPM ✩✩✩✩
To launch php-fpm on startup:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
The control script is located at /usr/local/opt/php70/sbin/php70-fpm
OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:
PATH="/usr/local/sbin:$PATH"
You may also need to edit the plist to use the correct "UserName".
Please note that the plist was called 'homebrew-php.josegonzalez.php70.plist' in old versions
of this formula.
To have launchd start homebrew/php/php70 at login:
ln -sfv /usr/local/opt/php70/*.plist ~/Library/LaunchAgents
Then to load homebrew/php/php70 now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
==> Summary
2、然后基于这个php7安装mongodb扩展了,
查看php手册,说mac下通过homebrew直接安装即可,http://php.net/manual/zh/mongodb.installation.homebrew.php
taiwudeMacBook-Pro:~ taiwu$ brew install php70-mongodb
==> Installing php70-mongodb from homebrew/php
==> Downloading https://pecl.php.net/get/mongodb-1.4.0.tgz
但是上面页面等了半天没反应,果断手动安装,看手册另一个安装说明,http://php.net/manual/zh/mongodb.installation.manual.php,具体步骤如下:
$ git clone https://github.com/mongodb/mongo-php-driver.git
$ cd mongo-php-driver
$ git submodule update --init
$ phpize
$ ./configure
$ make all
$ sudo make install
要注意的是,因为我是用的新安装的php7来进行安装扩展,所以使用phpize的时候,指明具体路径,用命令/usr/local/opt/php70/bin/phpize 。
最后会提示扩展mongodb.so安装成功。
然后需要在php.ini中进行配置,vi /usr/local/etc/php/7.0/php.ini ,然后加上一句话
extension=mongodb.so
这样通过php -m能查看到mongodb的,这说明php7安装mongodb扩展完成了!!!
3、在基于php7的项目中如果要使用mongodb,需要安装mongodb的library,也即是用户业务层面的代码(这里不是php7的mongodb扩展,而且给用户写代码用的library)
继续查看php手册http://php.net/manual/zh/mongodb.tutorial.library.php,
直接用composer来给一个项目安装mongodb library即可,
$ composer require mongodb/mongodb