安裝
yum install memcached
安裝完phpinfo測試, 看php是否已經支持. 如果沒有 ,則需要
php.ini文件中增加
extension = "memcache.so"
啓動
/bin/memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid
-d選項是啓動一個守護進程,
-m是分配給Memcache使用的內存數量,單位是MB,我這裏是1024MB,
-u是運行Memcache的用戶,我這裏是root,
-l是監聽的服務器IP地址,如果有多個地址的話,我這裏指定了本地服務器的IP地址
-p是設置Memcache監聽的端口,我這裏設置了11211,最好是1024以上的端口,
-c選項是最大運行的併發連接數,默認是1024,按照你服務器的負載量來設定,
-P是設置保存Memcache的pid文件,我這裏是保存在 /tmp/memcached.pid,
檢查是否啓動成功
ps aux | grep memcached
Yii中使用
web.php配置文件中, cache配置
'components' => [
'cache' => [
'class' => 'yii\caching\MemCache',
'servers' => [
[
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 60,
]
,
[
'host' => 'server2',
'port' => 11211,
'weight' => 40,
],
],
],...
php測試
任何一個控制器中,測試
Yii::$app->cache->set('name', $arr);
echo Yii::$app->cache->get('name');
安裝好後設置開機自啓動
打開這個文件 /etc/rc.d/rc.local
/bin/memcached -d -m 512 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid
以下內容不用.. 這個我個人的項目需要.
設置任務啓動
45 6 * * * /sbin/reboot
* * * * * /bin/sh /home/crontab.sh
crontab 文件如下
#!/bin/bash
step=30
for (( i = 0; i < 60; i=(i+step) )); do
curl 'http://www.123.com/index.php?r=public%2Ffix'
sleep $step
done
exit 0