搭建LNAMP环境(七)- PHP7源码安装Memcached和Memcache拓展

上一篇:搭建LNAMP环境(六)- PHP7源码安装MongoDB和MongoDB拓展

一、安装Memcached

1.yum安装libevent事件触发管理器

yum -y install libevent-devel

2.创建memcached用户组和用户

groupadd memcached
useradd -r -g memcached -s /sbin/nologin -M memcached

3.下载memcached源码包,并将源码包放到/usr/local/src/目录下
下载页面:http://memcached.org/downloads
这里用的是 memcached-1.4.33.tar.gz
下载地址:http://memcached.org/files/memcached-1.4.33.tar.gz
4.进入src/目录

cd /usr/local/src/

5.解压源码包

tar -zxf memcached-1.4.33.tar.gz

6.进入memcached源码目录,编译安装

cd memcached-1.4.33/

./configure --prefix=/usr/local/memcached  --with-libevent

make && make install

7.修改memcached目录权限

chown -R memcached:memcached /usr/local/memcached

8.将memcached命令加入环境变量,修改profile文件

vim /etc/profile

9.修改为下面内容,保存退出

PATH=/usr/local/mysql/bin:/usr/local/php/bin:/usr/local/redis/bin:/usr/local/mongodb/bin:/usr/local/memcached/bin:$PATH

10.使/etc/profile里的配置立即生效

source /etc/profile

11.将memcached服务脚本加入到init.d/目录,创建memcached文件

vim /etc/init.d/memcached

12.加入下面内容,保存退出

#!/bin/sh
#
# memcached:    MemCached memcached
#
# chkconfig:    - 90 25 
# description:  MemCached memcached
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
 
start()
{
    echo -n $"Starting memcached: "
    /usr/local/memcached/bin/memcached -d -u memcached -m 1024 -l 127.0.0.1 -p 11211
    echo
}
 
stop()
{
    echo -n $"Shutting down memcached: "
    killproc memcached
    echo
}
 
[ -f /usr/local/memcached/bin/memcached ] || exit 0
 
# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|reload)
        stop
        start
        ;;
    condrestart)
        stop
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|condrestart}"
        exit 1
esac
exit 0
View Code

参数解释:
/usr/local/bin/memcached -d -u memcached -m 1024 -l 127.0.0.1 -p 11211 -c 1024 -P /var/run/memcached/memcached.pid
-d 启动一个守护进程。可以启动多个守护进程,但是端口不能重复
-u 运行Memcache的用户
-m 分配的内存数量,单位是MB
-l 监听的服务器IP地址,默认是本机
-p 监听的端口
-c 最大运行的并发连接数,默认是1024
-P pid文件位置

13.为memcached添加可执行权限

chmod +x /etc/init.d/memcached

14.将memcached加入系统服务

chkconfig --add memcached

15.修改服务的默认启动等级

chkconfig memcached on

16.启动memcached

service memcached start

 

二、PHP7安装Memcache拓展

1.yum安装zip

yum install -y zip unzip

2.下载php7 memcache拓展包,并将源码包放到/usr/local/src/目录下
下载地址:https://codeload.github.com/websupport-sk/pecl-memcache/zip/php7/pecl-memcache-php7.zip
3.进入src/目录

cd /usr/local/src/

4.解压拓展包

unzip pecl-memcache-php7.zip

5.进入memcache拓展目录,编译安装拓展

cd pecl-memcache-php7

phpize

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

make && make install

6.修改php.ini文件

vim /usr/local/php/etc/php.ini

7.添加memcache.so扩展配置,保存退出

extension=memcache.so

8.重启Apache或php-fpm

service httpd restart
service php-fpm restart

9.在web目录下添加php文件,如/usr/local/apache/htdocs/memcache.php 或 /usr/local/nginx/html/memcache.php

<?php
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
$memcache->set('class','toefl');
print_r($memcache->get('class'));

访问URL,如:http://192.168.8.9/memcache.php
页面显示正常,则配置成功
Memcached安装完毕

posted @ 2016-12-29 11:40  成九  阅读(2206)  评论(0编辑  收藏  举报