nginx、php-fpm安装mongodb及驱动扩展
1.安装mongodb
linux下安装mongodb很简单,执行如下命令完成安装
curl http://downloads.mongodb.org/linux/mongodb-linux-i686-2.2.2.tgz > mongo.tgz
tar -zxvf mongo.tgz
mkdir /db
cp -R -n mongodb-linux-i686-2.2.2/ /db/mongodb
cd /db/mongodb/
2.启动mongodb命令
cd /db/mongodb/bin && ./mongod --port=12345 --dbpath=/db/mongodb_data/ --logpath=/db/mongodb_log/mongodb.log --logappend&
这样mongodb就在端口12345运行了
3.安装mongodb的php驱动扩展
yum install php-pear
cd /usr/local/lnmp/php/bin
sudo ./pecl install mongo
其中标红的地方很重要,该目录是php-fpm的可执行目录,里面同时也会有php可执行文件
安装完成后会出现如下提示
Build process completed successfully
Installing '/usr/lib/php/modules/mongo.so'
install ok: channel://pecl.php.net/mongo-1.3.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini
然后编辑php.ini添加
添加extension=mongo.so
4.测试脚本
[php]
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
header("Content-type: text/plain");
$connection = new Mongo(127.0.0.1:12345);
$db = $connection->mydb;
$collection = $db->mycollection;
$myobj = array("first_name" => "John", "last_name" => "Doe", "age" => 30);
$myobj2 = array("first_name" => "Jane", "last_name" => "Doe", "age" => 27);
$collection->insert($myobj);
$collection->insert($myobj2);
$cursor = $collection->find();
foreach($cursor as $result)
{
echo "Name: " . $result["last_name"] . ", " . $result["first_name"] . "n";
echo "Age: " . $result["age"] . "nn";
}
$db->drop();
$connection->close();
?>
[/php]
ps:
如果安装过程中出现error while loading shared libraries: libmcrypt.so.4: cannot open shared object file: No such file or directory
安装对应的库即可,如果搞不定可以留言
第4步的测试脚本是用来做什么的?怎样测试啊?
php的。就是安装完可不可以用
LinuxToday祝您新年快乐!合家团圆!