1. 程式人生 > >基於php7的專案要安裝mongodb類庫

基於php7的專案要安裝mongodb類庫

公司的一個專案要從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
    
    <FilesMatch .php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

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