运维之道 | LNMP 环境部署 Memcache 缓存(编译安装)

一、部署LNMP环境

运维之道 | CentOS7.6 + Nginx1.8 + MySQL5.6 + PHP7.3 环境部署(编译安装)
使用firewall-cmd命令开放80及443端口:
firewall-cmd -permanent -zone=public -add-service=http
firewall-cmd -permanent -zone=public -add-service=https

二、安装 libevent、memcache

  • 安装 libevent
1、获取libevent源码包
[root@localhost ~]# wget https://github.com/libevent/libevent/releases/download/release-2.1.11-stable/libevent-2.1.11-stable.tar.gz
2、解压libevent源码包
[root@localhost ~]# tar -xzvf libevent-2.1.11-stable.tar.gz
3、编译,安装
[root@localhost ~]# cd libevent-2.1.11-stable
[root@localhost libevent-2.1.11-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.1.11-stable]# make && make install

  • 安装 memcached
1、获取memcached源码包
[root@localhost ~]# wget http://www.memcached.org/files/memcached-1.6.1.tar.gz
2、解压memcached源码包
[root@localhost ~]# tar -xzvf memcached-1.6.1.tar.gz
3、编译,安装
[root@localhost ~]# cd memcached-1.6.1
[root@localhost memcached-1.6.1]#  ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@localhost memcached-1.6.1]# make && make install

启动memcached
[root@localhost ~]# /usr/local/memcached/bin/memcached -m 32m -p 11211 -d -u root -P /usr/local/memcached/memcached.pid -c 256
[root@localhost ~]# netstat -lntup |grep 11211
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      125476/memcached    
tcp6       0      0 :::11211                :::*                    LISTEN      125476/memcached
  • -d启动守护进程(后台进行)
  • -m设置memcache使用的内存大小
  • -p 端口默认11211(增加实例,只需要修改端口号即可11212…)
  • -u以root用户启动进程
  • -c指定连接数
  • -P指定pid地址

三、安装php的扩展模块

1、安装 libmemcached 扩展
[root@localhost ~]# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
[root@localhost ~]# tar -xzvf libmemcached-1.0.18.tar.gz
[root@localhost ~]# cd libmemcached-1.0.18
[root@localhost libmemcached-1.0.18]# ./configure --prefix=/usr/local/libmemcached --with-memcached
[root@localhost libmemcached-1.0.18]# make && make install
2、安装 memcached 扩展
  • 先检查phpize和php-config工具有没安装,如果没有直接yum install php-devel
[root@localhost ~]# which phpize
/usr/bin/phpize
[root@localhost ~]# which php-config
/usr/bin/php-config
  • 编译安装
[root@localhost ~]# wget http://pecl.php.net/get/memcached-3.1.5.tgz
[root@localhost ~]# tar -xzvf memcached-3.1.5.tgz
[root@localhost ~]# cd memcached-3.1.5

[root@localhost memcached-3.1.5]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
[root@localhost memcached-3.1.5]# ./configure --prefix=/usr/local/phpmemcached --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
[root@localhost memcached-3.1.5]# make 
[root@localhost memcached-3.1.5]# make test
[root@localhost memcached-3.1.5]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/
3、修改php的配置文件php.ini

加上模块extension=memcached.so,看看extension_dir指定的模块路径下是否有memcached.so文件
extension_dir一般是/usr/lib/php/modules/

[root@localhost ~]# mkdir -p /usr/lib/php/modules
[root@localhost ~]# ln -s /usr/lib64/php/modules /usr/lib/php/modules
4、在php.ini配置文件中添加memcached.so的记录
[root@localhost ~]# vim /usr/local/php/lib/php.ini
extension=memcached.so
5、编辑info.php信息界面
[root@localhost ~]# vim /usr/local/nginx/html/test.php
<?php
        phpinfo();
?>
6、重启php服务
[root@localhost ~]# /usr/local/php/sbin/php-fpm
7、访问info.php信息界面

运维之道 | LNMP 环境部署 Memcache 缓存(编译安装)_第1张图片
运维之道 | LNMP 环境部署 Memcache 缓存(编译安装)_第2张图片


四、使用telnet连接memcached查看信息

1、安装telnet命令
yum install -y telnet
2、连接memcached
[root@localhost ~]# telnet 192.168.182.11 11211
Trying 192.168.182.11...
Connected to 192.168.182.11.
Escape character is '^]'.
stats
STAT pid 125476
STAT uptime 3514
STAT time 1584515555
STAT version 1.6.1
STAT libevent 2.1.11-stable
STAT pointer_size 64
STAT rusage_user 0.823110
STAT rusage_system 1.728532
STAT max_connections 256
STAT curr_connections 2
STAT total_connections 352
STAT rejected_connections 0
STAT connection_structures 4
STAT response_obj_bytes 2382720
STAT response_obj_total 2040
STAT response_obj_free 2039
STAT response_obj_oom 0
STAT read_buf_bytes 65536
STAT read_buf_bytes_free 49152
STAT read_buf_oom 0
STAT reserved_fds 20
STAT cmd_get 4626
STAT cmd_set 2574
......
3、memcached使用教程

Memcached菜鸟教程


拓展:启动memcached报错

PS:此时会报错:/usr/local/bin/memcached: error while loading shared libraries: libevent-2.1.so.7: cannot open shared object file: No such file or directory

解决方法: 创建libevent软连接

1、查找libvent命令所在目录
[root@localhost ~]# sudo find / -name libevent-2.1.so.7
[root@localhost ~]# /usr/local/lib/libevent-2.1.so.7
[root@localhost ~]# /root/libevent-2.1.11-stable/.libs/libevent-2.1.so.7
2、查看memcached 从哪里启动libevent
[root@localhost ~]# LD_DEBUG=libs /usr/local/bin/memcached -v
      4607:     find library=libevent-2.1.so.7 [0]; searching
      4607:      search cache=/etc/ld.so.cache
      4607:      search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64         (system search path)
      4607:       trying file=/lib64/tls/x86_64/libevent-2.1.so.7
      4607:       trying file=/lib64/tls/libevent-2.1.so.7
      4607:       trying file=/lib64/x86_64/libevent-2.1.so.7
      4607:       trying file=/lib64/libevent-2.1.so.7
      4607:       trying file=/usr/lib64/tls/x86_64/libevent-2.1.so.7
      4607:       trying file=/usr/lib64/tls/libevent-2.1.so.7
      4607:       trying file=/usr/lib64/x86_64/libevent-2.1.so.7
      4607:       trying file=/usr/lib64/libevent-2.1.so.7
      4607:
/usr/local/bin/memcached: error while loading shared libraries: libevent-2.1.so.7: cannot open shared object file: No such file or directory

从Debug信息中就知道程序去哪里找链接库了。我这边程序去 trying file=/usr/lib64/libevent-2.1.so.7而我的链接库的实际存储位置是 /usr/local/lib/libevent-2.1.so.7

3、制作软连接
[root@localhost ~]# ln -s /usr/local/lib/libevent-2.1.so.7 /usr/lib64/libevent-2.1.so.7

你可能感兴趣的:(memcached)