前置作業:必須有apache及php
1. 檢查更新
[bash]
sudo aptitude update
sudo aptitude upgrade
[/bash]
2. 安裝所需套件,php5-memcache, php5-memcached兩個都必須裝
[bash]
sudo aptitude install php5 php5-memcache php5-memcached
sudo aptitude install memcached
sudo aptitude install php-pear
sudo aptitude install build-essential
[/bash]
3. 使用PECL安裝memcache
[bash]
sudo pecl install memcache
#安裝過程中,如果出現:Enable memcache session handler support? [yes] :的字樣,請輸入yes
[/bash]
裝完後必須restart apache
[bash]
sudo service apache2 restart
[/bash]
4. 把memcache加入memcache.ini
[bash]
echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini
[/bash]
5. 確認memcache是否有起來,可從phpinfo()看
[bash]
ps aux | grep memcache
[/bash]
可以用php試試是否有裝好:
[php]
extension_loaded(‘memcached’)
extension_loaded(‘memcache’)
[/php]
[php]
if ( ! extension_loaded(‘memcached’)) {
echo "memcached not found";
}
if ( ! extension_loaded(‘memcache’)){
echo "memcache not found";
}
$mem = new Memcached();
//Connect
$mem->addServer(‘localhost’, 11211) or die ("Could not connect");
//設定name(name, value, time(s))
$mem->set(‘name’, ‘febr’, 30);
//取得name value
echo $mem->get(‘name’);
//memcache status
print_r( $mem->getStats() );
[/php]
參考資料: https://www.digitalocean.com/community/articles/how-to-install-and-use-memcache-on-ubuntu-12-04