linux下php+memcached+mongodb+redis安装配置

时间:2021-12-30 08:28:41

本篇接linux下nginx+mysql+php安装配置

memcached安装

memcached是基于libevent

libevent安装

tar zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr
make&&make install

输入一下,查看是否有目录列出(检查是否安装成功)

ls -al  /usr/lib | grep libevent

memcached安装

tar zxvf memcached-1.4.17.tar.gz
cd memcached-1.4.17
./configure --with-libevent=/usr
make && make install

配置启动脚本

vim /etc/init.d/memcached

#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached

# Source function library.
. /etc/rc.d/init.d/functions

PORT=11211
USER=root
MAXCONN=1024
CACHESIZE=64
OPTIONS=""
PID="/tmp/memcached.pid"
HOST="127.0.0.1"
PREFIX=/usr/local/bin/memcached

if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
exit 0
fi

RETVAL=0

start () {
echo "Starting memcached ..."
# insure that /var/run/memcached has proper permissions
chown $USER $PREFIX
$PREFIX -d -l $HOST -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P $PID $OPTIONS
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
echo "Stopping memcached ..."
kill `cat /tmp/memcached.pid`
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
rm -f /var/lock/subsys/memcached
rm -f $PID
fi
}
restart () {
stop
start
}


# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status memcached
;;
restart|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/memcached ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
exit 1
esac

增加执行权限

chmod +x /etc/init.d/memcached

添加开机启动

/sbin/chkconfig --add memcached
/sbin/chkconfig memcached on

启动服务


service memcached start

php扩展

memcached扩展基于libmemcached

libmemcached扩展安装

tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
/configure –prefix=/usr/local/libmemcached –with-memcached
make && make install

memcached扩展安装

tar zxvf memcached-2.1.0.tgz
cd memcached-2.1.0
/usr/local/bin/phpize
./configure –with-php-config=/usr/local/bin/php-config –with-libmemcached-dir=/usr/local/libmemcached/
make && make install

编辑php.ini

vim /usr/local/webserver/php/php.ini

[memcached]
extension=memcached.so

memcache扩展安装

tar vxzf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/bin/php-config
make&&make install

编辑php.ini

vim /usr/local/webserver/php/php.ini

[memcache]
extension=memcache.so


mongodb安装

tar zxvf mongodb-linux-x86_64-2.4.9.tgz 
mkdir /usr/local/webserver/mongodata
mkdir /usr/local/webserver/mongodata/db
mkdir /usr/local/webserver/mongodata/logs
cd mongodb-linux-x86_64-2.4.9
cd bin/
./mongod --dbpath=/usr/local/webserver/mongodata/db --logpath=/usr/local/webserver/mongodata/logs/db.log --logappend&

执行完会出现“ all output going to: /mongodbdata/mongodb_logs/mongodb.log”
检查端口是否启动,端口为:27017

netstat -lanp  | grep 27017

tcp        0      0 0.0.0.0:27017               0.0.0.0:*                   LISTEN      560/mongod         
unix  2      [ ACC ]     STREAM     LISTENING     128435 560/mongod          /tmp/mongodb-27017.sock

启动成功。
进入到bin目录,执行mongo

./mongo

MongoDB shell version: 2.0.4
connecting to: test

>use mongo_test 创建库
>db.createCollection("test") 创建表

mongod.conf配置文件

vim /etc/mongod.conf

# mongo.conf  

#where to log
logpath=/usr/local/webserver/mongodata/logs/db.log

logappend=true

# fork and run in background
fork = true

#port = 27017

dbpath=/usr/local/webserver/mongodata/db

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security. Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
#quota = true

# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#oplog = 0

# Diagnostic/debugging option
#nocursors = true

# Ignore query hints
#nohints = true

# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true

# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize = <size>

# Accout token for Mongo monitoring server.
#mms-token = <token>

# Server name for Mongo monitoring server.
#mms-name = <server-name>

# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com

启动mongodb

./webserver/mongodb-linux-x86_64-2.4.9/bin/mongod -f /etc/mongod.conf 

赶紧不爽

mongod脚本

vi /etc/init.d/mongod

#!/bin/sh  
#
#mongod - Startup script for mongod
#
# chkconfig: - 85 15
# description: Mongodb database.
# processname: mongod

# Source function library
. /etc/rc.d/init.d/functions


# things from mongod.conf get there by mongod reading it
# OPTIONS
OPTIONS=" -f /etc/mongod.conf"

#mongod
mongod="/root/webserver/mongodb-linux-x86_64-2.4.9/bin/mongod"

lockfile=/var/lock/subsys/mongod

start()
{
echo -n $"Starting mongod: "
daemon $mongod $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
}

stop()
{
echo -n $"Stopping mongod: "
killproc $mongod -QUIT
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
}

restart () {
stop
start
}

ulimit -n 12000
RETVAL=0

case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f $lockfile ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac

添加文件执行权限

chmod +x /etc/init.d/mongod

启动mongodb

service mongod start

mongodb扩展安装

tar zxvf mongo-1.4.5.tgz 
cd mongo-1.4.5
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/bin/php-config
make && make install

配置php.ini

vi /usr/local/webserver/php/php.ini

[mongodb]
extension=mongo.so


redis安装

tar zxvf redis-2.8.7.tar.gz
cd redis-2.8.7
make

redis服务端启动

src/redis-server

redis客户端读写

src/redis-cli
redis>set foo bar
OK
redis>get foo
"bar"

redis脚本

cp utils/redis_init_script /etc/init.d/redis

chkconfig --add redis

报redis 服务不支持 chkconfig

解决方案

修改/etc/init.d/redis文件

vi /etc/init.d/redis

在#!/bin/sh后新增一行

#chkconfig: 2345 80 90

修改EXEC CLIEXEC参数

EXEC=/usr/local/webserver/redis-2.8.7/src/redis-server
CLIEXEC=/usr/local/webserver/redis-2.8.7/src/redis-cli

开启redis命令,以后台运行的方式运行

$EXEC $CONF &

注意!&即将服务转到后面运行的意思,否则启动服务时,redis服务奖占据在前台,占用主用户界面,造成其他命令执行不了

redis配置文件拷贝到/etc/redis/${REDISPORT}.conf

mkdir /etc/redis
cp /usr/local/webserver/redis-2.8.7/redis.conf /etc/redis/6379.conf

chkconfig --add redis

service redis start
service redis stop

添加到环境变量
vi ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/webserver/mysql/bin:/usr/local/webserver/redis-2.8.7/src:

直接调用redis-cli命令

redis-cli

redis扩展
tar zxvf redis-2.2.4.tgz 
cd redis-2.2.4
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/bin/php-config
make && make install

php配置文件

vi /usr/local/webserver/php/php.ini

[redis]
extension=redis.so