红联Linux门户
Linux帮助

CentOS编译安装MongoDB与mongoDB的php扩展

发布时间:2014-11-25 15:07:41来源:linux网站作者:linux人

系统:CentOS5.6

下载mongo源码包,pcre,epel,js

1 #wget http://downloads.mongodb.org/src/mongodb-src-r1.8.1.tar.gz
#wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz
#wget http://sourceforge.net/projects/pcre/files/pcre/8.12/pcre-8.12.tar.bz2

安装 python :

#yum install -y python-devel

安装scons: 下载scons

tar zxf scons-2.0.1.tar.gz

cd scons-2.0.1
python setup.py install

安装spidermonkey库,下载支持c的js api库 点此下载js-1.7.0.tar.gz

yum install -y boost boost-devel

tar zxvf js-1.7.0.tar.gz
cd js/src/
export CFLAGS="-DJS_C_STRINGS_ARE_UTF8"
make -f Makefile.ref
JS_DIST=/usr gmake -f Makefile.ref export
cd ../..

安装pcre

tar zxf pcre-8.12.tar.gz

cd pcre-8.12
./configure --enable-utf8 --enable-unicode-properties
make && make install
cd ..

安装MongoDB

tar zxf mongodb-src-r1.8.1.tar.gz

cd mongodb-src-r1.8.1
scons all   // scons可能出现找不到pcre库的现象(修改/etc/ld.so.conf也无用,是scons自身的问题),这时需要打开mongodb-src-r1.8.0下的SConstruct,查找【 linux2"== os.sys.platform:】,在LIBPATH后面添加上pcrecpp库的安装路径,在LIBS后添加上pcrecpp库名,再重新scons all即可(操作:vim SConstruct;原来:env.Append( LIBPATH=["/usr/lib64" , "/lib64" ] ) ;修改后env.Append( LIBPATH=["/usr/lib64" , "/lib64" ,"/usr/local/pcre/lib"]);  接下来在env.Append( LIBS=["pthread"] )后面添加 env.Append( LIBS=["libpcrecpp"] )  )

scons --prefix=/usr/local/mongo install

如果需要安装lib和head,使用如下方式安装

scons --prefix=/usr/local/mongo --full install

创建配置文件

mkdir -p /usr/local/mongo/etc /usr/local/mongo/data /usr/local/mongo/log/ /usr/local/mongo/repair

vim  /usr/local/mongo/etc/mongo.conf

在mongo.conf中添加下面的内容

dbpath = /usr/local/mongo/data

logpath = /usr/local/mongo/mongodb.log

repairpath = /usr/local/mongo/repair

pidfilepath = /usr/local/mongo/mongodb.pid

directoryperdb = true

logappend = true

noauth = true

port = 27017

maxConns = 1024

fork = true

rest = true

quota = true

quotaFiles = 1024

nssize = 16

启动mongodb

ln -s /usr/local/mongo/bin/mongod /usr/bin/mongod

mongod -f /usr/local/mongo/etc/mongo.conf

看看是不是启动起来了,但是使用这种方式管理mongodb服务器很不明智,我们完善一下:

mkdir -p /usr/local/mongo/srv

vim /usr/local/mongo/srv/mongodb-start

添加下面的内容

#!/bin/sh

mongod -f /usr/local/mongo/etc/mongo.conf

vim /usr/local/mongo/srv/mongodb-stop

添加下面的内容

#!/bin/bash

pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;

if [ "${pid}" != "" ]; then

kill -2 ${pid};

fi

添加执行权限

chmod a+x /usr/local/mongo/srv/mongodb-start

chmod a+x /usr/local/mongo/srv/mongodb-stop

vim /etc/rc.d/init.d/mongodb

添加下面的内容

#! /bin/sh

#

# mongodb – this script starts and stops the mongodb daemon

#

# chkconfig: - 85 15

# description: MongoDB is a non-relational database storage system.

# processname: mongodb

# config: /usr/local/mongo/etc/mongo.conf

# pidfile: /usr/local/mongo/mongodb.pid

PATH=/usr/local/mongo/bin:/sbin:/bin:/usr/sbin:/usr/bin

NAME=mongodb

test -x $DAEMON || exit 0

set -e

case "$1" in

start)

echo -n "Starting MongoDB... "

/usr/local/mongo/srv/mongodb-start

;;

stop)

echo -n "Stopping MongoDB... "

/usr/local/mongo/srv/mongodb-stop

;;

*)

N=/etc/init.d/$NAME

echo "Usage: $N {start|stop}" >&2

exit 1

;;

esac

exit 0

添加服务

chmod a+x /etc/rc.d/init.d/mongodb

chkconfig --add mongodb

chkconfig --level 345 mongodb on

/etc/rc.d/init.d/mongodb start

php mongo 扩展安装:

服务器环境Cent OS 5.6 64位, php版本 5.2.17编译安装,安装路径/usr/local/php

首先下载最新的php mongodb扩展源码,源码可以在http://pecl.php.net/package/mongo下载到

wget http://pecl.php.net/get/mongo-1.2.2.tgz

tar zxf mongo-1.2.2.tgz

cd mongo-1.2.2

进入文件夹后,首先运行phpize来准备编译扩展的环境,phpize这个程序的介绍在这里

/usr/local/php/bin/phpize

运行后执行结果如下:

Configuring for:

PHP Api Version:         20041225

Zend Module Api No:      20060613

Zend Extension Api No:   220060519

运行后,./configure 脚本就会生成了,这个时候我们运行./configure脚本来进行配置

./configure --with-php-config=/usr/local/php/bin/php-config

--with-php-config这个参数是告诉配置脚本php-config这个程序的路径,php-config的介绍在这里

上面命令在正确配置的环境下运行结果如下

creating libtool

appending configuration tag "CXX" to libtool

configure: creating ./config.status

config.status: creating config.h

这时用make来编译扩展
make && make install

正确编译执行结果如下:

Build complete.

Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

完成后,请编辑你php.ini文件增加一行
extension=mongo.so
一般默认的编译php的ini文件在
/usr/local/php/etc/php.ini
重启你的web服务器或者php-fpm,打印phpinfo,如果看到mongo项表,那么mongodb的扩展安装成功了。