Ubuntu Linux 12.04/14.04 LTS为Python和PHP应用程序安装Memcached服务器

时间:2020-01-09 10:40:20  来源:igfitidea点击:

如何在Ubuntu Linux 14.04 LTS服务器上安装和使用memcached?
我如何用PHP或Python编写的应用程序可以在Ubuntu LTS服务器上使用memcached?

Memcached是通用分布式内存缓存系统。

它通常用于通过在RAM中缓存对象来加速动态数据库驱动的webapp或网站。
通常会导致数据库负载减少。
您需要安装以下软件包:

  • memcached高性能内存对象缓存服务器
  • php5-memcachePHP5的Memcache扩展模块
  • php5-memcached用于PHP5的Memcached扩展模块,使用libmemcached
  • python-memcache纯python memcached客户端

如何在Ubuntu 14.04上安装memcached

输入以下apt-get命令:

sudo apt-get update
sudo apt-get install memcached

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libevent-2.0-5 libsasl2-2 libsasl2-modules libsasl2-modules-db
Suggested packages:
  libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql
  libsasl2-modules-gssapi-mit libsasl2-modules-gssapi-heimdal
  libcache-memcached-perl libmemcached
The following NEW packages will be installed:
  libevent-2.0-5 libsasl2-2 libsasl2-modules libsasl2-modules-db memcached
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 328 kB of archives.
After this operation, 1,036 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libsasl2-modules-db amd64 2.1.25.dfsg1-17build1 [14.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libsasl2-2 amd64 2.1.25.dfsg1-17build1 [56.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libevent-2.0-5 amd64 2.0.21-stable-1ubuntu1.14.04.1 [126 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libsasl2-modules amd64 2.1.25.dfsg1-17build1 [64.3 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main memcached amd64 1.4.14-0ubuntu9 [66.7 kB]
Fetched 328 kB in 1s (220 kB/s)      
Selecting previously unselected package libsasl2-modules-db:amd64.
(Reading database ... 14575 files and directories currently installed.)
Preparing to unpack .../libsasl2-modules-db_2.1.25.dfsg1-17build1_amd64.deb ...
Unpacking libsasl2-modules-db:amd64 (2.1.25.dfsg1-17build1) ...
Selecting previously unselected package libsasl2-2:amd64.
Preparing to unpack .../libsasl2-2_2.1.25.dfsg1-17build1_amd64.deb ...
Unpacking libsasl2-2:amd64 (2.1.25.dfsg1-17build1) ...
Selecting previously unselected package libevent-2.0-5:amd64.
Preparing to unpack .../libevent-2.0-5_2.0.21-stable-1ubuntu1.14.04.1_amd64.deb ...
Unpacking libevent-2.0-5:amd64 (2.0.21-stable-1ubuntu1.14.04.1) ...
Selecting previously unselected package libsasl2-modules:amd64.
Preparing to unpack .../libsasl2-modules_2.1.25.dfsg1-17build1_amd64.deb ...
Unpacking libsasl2-modules:amd64 (2.1.25.dfsg1-17build1) ...
Selecting previously unselected package memcached.
Preparing to unpack .../memcached_1.4.14-0ubuntu9_amd64.deb ...
Unpacking memcached (1.4.14-0ubuntu9) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libsasl2-modules-db:amd64 (2.1.25.dfsg1-17build1) ...
Setting up libsasl2-2:amd64 (2.1.25.dfsg1-17build1) ...
Setting up libevent-2.0-5:amd64 (2.0.21-stable-1ubuntu1.14.04.1) ...
Setting up libsasl2-modules:amd64 (2.1.25.dfsg1-17build1) ...
Setting up memcached (1.4.14-0ubuntu9) ...
Starting memcached: memcached.
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
Processing triggers for ureadahead (0.100.0-16) ...

我如何启动/停止/重启memcached服务器?

语法如下:

/etc/init.d/memcached start
/etc/init.d/memcached stop
/etc/init.d/memcached restart
/etc/init.d/memcached force-reload
/etc/init.d/memcached status

验证memcached是否已在服务器上安装并正常运行

使用pgrep命令或ps命令,如下所示来确定memcached是否正在运行:

pgrep memcached
## 或者
ps aux | grep '[m]emcached' 
## 或者
sudo /etc/init.d/memcached status

Memcached服务器配置

您需要编辑名为/etc/memcached.conf的文件,执行:

sudo vi /etc/memcached.conf

设置内存限制

默认情况下,memcached将以64 MB的内存上限开始。
守护程序将增长到此大小,但不会开始保留这么多的内存。
在此示例中,Im设置为2048 MB(2GB):

-m 2048

设置一个IP地址以监听

默认情况下,memcached仅侦听127.0.0.1。
您可以指定要监听的IP地址。
此参数是memcached拥有的仅有的安全措施之一,因此请确保在防火墙接口上进行监听。
我将仅使用127.0.0.1:

-l 127.0.0.1

限制同时传入的连接数

守护程序的默认值为1024,对于高流量的网站,可以增加该守护程序:

-c 1024

保存并关闭文件。
确保重新启动服务器:

sudo /etc/init.d/memcached restart

输出示例:

Restarting memcached: memcached.

测试内存缓存服务器

执行以下命令:

echo stats | nc 127.0.0.1 11211
echo "stats settings" | nc localhost 11211

输出示例:

STAT maxbytes 1073741824
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT num_threads_per_udp 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
STAT maxconns_fast no
STAT hashpower_init 0
STAT slab_reassign no
STAT slab_automove 0
END

您可以在bash shell中使用memcached storage/caching命令,如下所示。
语法为:

command    [key] [flags] [exptime] [bytes] [noreply]\r\n[value]\r
|
|
\./
set
add
get
gets
replace
append
prepend
cas
delete

让我们添加一个名为foo的键,其值为BAR,执行:

echo -e 'add foo 0 300 3\r\nBAR\r' | nc localhost 11211

输出示例:

STORED

其中:

  • add:将数据添加/存储到memcached服务器
  • foo:键名
  • 0:32位无符号整数
  • 300:有效时间(以秒为单位)
  • 3:数据块中的字节数(例如BAR == 3,theitroad == 8依此类推)
  • bar:值

要查看密钥(即获取foo值),请执行:

echo -e 'get foo\r' | nc localhost 11211

输出示例:

VALUE foo 0 3
BAR
END

要删除密钥,请执行:

echo -e 'delete foo\r' | nc localhost 11211

输出示例:

DELETED

安装memcached PHP支持

输入以下apt-get命令:

sudo apt-get install php5-memcached php5-fpm php5

您也可以安装mysql服务器和php支持:

sudo apt-get install mysql-server php5-mysql php5

最后,确保重新启动web-server/php-fam:

## restart php5-fpm if installed ##
sudo service php5-fpm restart
## restart Nginx if installed ##
sudo service nginx restart
## restart Apache 2 if installed ##
sudo service apache2 restart

最后,通过执行以下命令来验证是否启用了php5内存缓存支持:

php5 -i | grep memcache

输出示例:

/etc/php5/cli/conf.d/20-memcache.ini,
memcache
memcache support -  enabled
memcache.allow_failover -  1 -  1
memcache.chunk_size -  32768 -  32768
memcache.compress_threshold -  20000 -  20000
memcache.default_port -  11211 -  11211
memcache.hash_function -  crc32 -  crc32
memcache.hash_strategy -  consistent -  consistent
memcache.lock_timeout -  15 -  15
memcache.max_failover_attempts -  20 -  20
memcache.protocol -  ascii -  ascii
memcache.redundancy -  1 -  1
memcache.session_redundancy -  2 -  2
Registered save handlers -  files user memcache

您还可以使用以下代码创建PHP测试页,以验证memcached + php5是否在服务器上正常工作:

sudo vi /var/www/html/test.php

追加以下代码:

<?php
  phpinfo(INFO_MODULES);
?>

触发浏览器并输入以下网址:

http://your-server-name-or-ip-here/test.php

输出示例:
PHP Memcached检查安装测试脚本

安装memcached Python支持

输入以下apt-get命令:

sudo apt-get install python-memcache

我如何从CLI查看memcached的内存数据和统计信息?

语法如下:

/usr/share/memcached/scripts/memcached-tool  <host[:port] | /path/to/socket> [mode]

查看内存缓存的平板

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 display

输出示例:

#  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM
  1      96B        25s       1       5     yes        0        0    0
  2     120B       344s       1       2     yes        0        0    0
  3     152B    774542s       3   13945     yes        0        0    0
  4     192B    774003s      26  140491     yes        0        0    0
  5     240B    500480s       1     102     yes        0        0    0
  6     304B    483401s       1      24     yes        0        0    0
  7     384B    596752s       2    3261     yes        0        0    0
  8     480B    761228s      11   22058     yes        0        0    0
  9     600B    488482s       1     391     yes        0        0    0
10     752B    492207s       1     720     yes        0        0    0
11     944B    488482s       2    1135     yes        0        0    0
12     1.2K    774542s       3    2352     yes        0        0    0
13     1.4K    560606s       2    1277     yes        0        0    0
14     1.8K    487974s       5    2672     yes        0        0    0
15     2.3K    487974s      10    4384     yes        0        0    0
16     2.8K    407204s      15    5048     yes        0        0    0
17     3.5K    347351s      12    3175     yes        0        0    0
18     4.4K    633802s      36    8104     yes        0        0    0
19     5.5K    351573s      39    7101     yes        0        0    0
20     6.9K    293697s      46    6695     yes        0        0    0
21     8.7K    667676s      56    6597     yes        0        0    0
22    10.8K    653196s      29    2714     yes        0        0    0
23    13.6K     75620s      19    1138     yes        0        0    0
24    16.9K      2580s      39     385     yes        0        0    0
25    21.2K      3824s      92     415     yes        0        0    0
26    26.5K       763s       2      39     yes        0        0    0
27    33.1K       653s       1      17     yes        0        0    0
28    41.4K       501s       1      11     yes        0        0    0
29    51.7K       962s       1       5     yes        0        0    0
30    64.7K       226s       1       5     yes        0        0    0
31    80.9K       199s       1       4     yes        0        0    0
32   101.1K         6s       1       1     yes        0        0    0
33   126.3K       522s       1       1     yes        0        0    0
35   197.4K       339s       1       1     yes        0        0    0

查看memcached一般统计

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 stats

输出示例:

#127.0.0.1:11211   Field       Value
         accepting_conns           1
               auth_cmds           0
             auth_errors           0
                   bytes   302290901
              bytes_read 34590998647
           bytes_written 348047507143
              cas_badval           0
                cas_hits           0
              cas_misses           0
               cmd_flush           0
                 cmd_get   167116484
                 cmd_set     5551928
               cmd_touch           0
             conn_yields           0
   connection_structures          72
        curr_connections          56
              curr_items      234265
               decr_hits           0
             decr_misses           0
             delete_hits      299319
           delete_misses     1582752
       evicted_unfetched           0
               evictions           0
       expired_unfetched      167287
                get_hits   159501058
              get_misses     7615426
              hash_bytes     2097152
       hash_is_expanding           0
        hash_power_level          18
               incr_hits     2087299
             incr_misses           0
                libevent 2.0.21-stable
          limit_maxbytes  2147483648
     listen_disabled_num           0
                     pid         801
            pointer_size          64
               reclaimed      194549
            reserved_fds          20
           rusage_system 5567.638938
             rusage_user 1467.841043
                 threads           4
                    time  1425157331
       total_connections       31733
             total_items     4255143
              touch_hits           0
            touch_misses           0
                  uptime      774819
                 version 1.4.14 (Ubuntu)

转储内存缓存的键和值(不推荐)

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 dump

如何使用CLI刷新Memcached服务器的内容?

执行以下命令:

nc 192.168.1.10 11211<<<"flush_all"

有关更多信息,请参见常见问题解答"如何使用命令行刷新Memcached服务器的内容"。

可选:用于memcached的图形独立管理,以监视和调试目的

phpMemcachedAdmin允许实时(类似顶部)或从服务器开始查看获取,设置,删除,递增,递减,逐出,收回,cas命令的统计信息以及服务器统计信息(网络,项目,服务器版本)以及googlecharts和服务器内部配置。

安装phpMemcachedAdmin

执行以下命令:

cd /var/www/html/
sudo mkdir pmadm
sudo wget http://phpmemcacheadmin.googlecode.com/files/phpMemcachedAdmin-1.2.2-r262.tar.gz 
sudo tar xvf phpMemcachedAdmin* -C pmadm
cd pmadm
chmod 0777 Config/Memcache.php
chmod 0777 Temp/

最后,在Nginx服务器上使用密码保护/var/www/html/pmadm /:

sudo mkdir /etc/nginx/.htpasswd/
sudo htpasswd -c /etc/nginx/.htpasswd/passwd Hyman

编辑nginx.conf文件,并更新服务器{.....}块/上下文,如下所示:

location /pmadm/ {
  auth_basic  "Restricted";
  auth_basic_user_file   /etc/nginx/.htpasswd/passwd;
}

重新启动/重新加载nginx服务器,执行:

sudo service nginx reload

触发浏览器并输入以下网址:

http://your-server-name-or-ip-here/pmadm/