搭建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安裝完畢


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM