在Ubuntu上設定memcached
建立對象:
- 經驗豐富
- 管理員
- 開發人員
本節提供在Ubuntu上安裝memcached的說明。
因為PHP對memcache沒有原生支援,所以您必須安裝擴充功能以供PHP使用。 有兩個可用的PHP擴充功能,請務必解碼要使用哪個:
在Ubuntu上安裝並設定memcached
若要在Ubuntu上安裝並設定memcached:
-
以具有
root
許可權的使用者身分,輸入下列命令:apt-get -y update
apt-get -y install php5-memcached memcached
-
變更
CACHESIZE
和-l
的memcached組態設定:-
在文字編輯器中開啟
/etc/memcached.conf
。 -
找到
-m
引數。 -
將其值變更為至少
1GB
-
找到
-l
引數。 -
將其值變更為
127.0.0.1
或localhost
-
將變更儲存至
memcached.conf
並結束文字編輯器。 -
重新啟動memcached。
service memcached restart
-
-
重新啟動您的網頁伺服器。
針對Apache,
service apache2 restart
-
繼續下一節。
在安裝Magento之前驗證memcached的運作方式
Adobe建議先測試memcached以確保其可運作,然後再安裝Commerce。 只需幾分鐘即可完成這項工作,並可簡化後續的疑難排解。
驗證Web伺服器是否可辨識memcached
若要確認網頁伺服器可辨識memcached:
-
在網頁伺服器的docroot中建立
phpinfo.php
檔案:<?php // Show all information, defaults to INFO_ALL phpinfo();
-
前往網頁瀏覽器中的該頁面。 例如:
http://192.0.2.1/phpinfo.php
-
請確定memcached顯示如下:
確認您使用的是memcached 3.0.5版或更新版本。
如果memcached未顯示,請重新啟動網頁伺服器並重新整理瀏覽器頁面。 如果仍然未顯示,請確認您已安裝
php-pecl-memcached
擴充功能。
驗證memcached可以快取資料
此測試使用PHP指令碼來驗證memcached可以儲存和擷取快取資料。
如需此測試的詳細資訊,請參閱如何在Ubuntu教學課程中安裝及使用Memcache。
在網頁伺服器的docroot中建立cache-test.php
,其內容如下:
$meminstance = new Memcached();
$meminstance->addServer("<memcached hostname or ip>", <memcached port>);
$result = $meminstance->get("test");
if ($result) {
echo $result;
} else {
echo "No matching key found. Refresh the browser to add it!";
$meminstance->set("test", "Successfully retrieved the data!") or die("Could not save anything to memcached...");
}
其中<memcached hostname or ip>
是localhost
、127.0.0.1
或memcache主機名稱或IP位址。 <memcached port>
是接聽連線埠;預設為11211
。
在網頁瀏覽器中前往該頁面。 例如
http://192.0.2.1/cache-test.php
第一次前往頁面時,會顯示下列專案: No matching key found. Refresh the browser to add it!
重新整理瀏覽器。 訊息變更為Successfully retrieved the data!
最後,您可以使用Telnet檢視memcache金鑰:
telnet localhost <memcache port>
出現提示時,輸入
stats items
結果類似下列:
STAT items:2:number 1
STAT items:2:age 106
STAT items:2:evicted 0
STAT items:2:evicted_nonzero 0
STAT items:2:evicted_time 0
STAT items:2:outofmemory 0
STAT items:2:tailrepairs 0
STAT items:2:reclaimed 0
STAT items:2:expired_unfetched 0
STAT items:2:evicted_unfetched 0
排清成員快取儲存體並結束Telnet:
flush_all
quit