본문 바로가기
운영체제/리눅스

Memcached for CentOS

by o테리o 2014. 2. 26.


Memcached 란 무엇입니까?

Memcached는 오픈 소스 분산 메모리 객체 캐싱 프로그램으로, 메모리에 데이터와 객체를 캐싱하여 동적 웹 응용 프로그램의 성능을 향상시키고 속도를 높일 수 있습니다. Memcached는 또한 데이터베이스 성능을 향상시키기 위해 전체 데이터베이스 테이블과 쿼리를 캐시하는 데 사용됩니다. YouTube, Facebook, Twitter, Reddit, Drupal, Zynga 등과 같은 많은 큰 사이트에서 자유롭게 사용할 수있는 유일한 캐싱 시스템입니다.


(What is Memcached?)

(Memcached is an open source distributed memory object caching program that allows us to improve and speeding up performance of dynamic web applications by caching data and objects in Memory. Memcached is also used to cache entire database tables and queries to improve performance of database. It is the only one caching system available freely and used by many big sites likeYouTubeFacebookTwitterRedditDrupalZynga etc.)

RHEL / CentOS 6.3 / 5.8에서 EPEL 리포지토리 활성화

YUM을 사용하여 EPEL 리포지토리를 설치하고 활성화하는 가장 빠르고 쉬운 방법입니다. 먼저 제공된 링크에서 Linux OS 아키텍처와 일치하는 RPM을 선택하고 아래에 표시된 방법을 사용하여 설치하십시오. EPEL 저장소는 memcached에 필요한 모든 종속성 패키지를 설치합니다. (참고 : Fedora는 페도라 프로젝트의 일부이므로 EPEL 저장소가 필요하지 않습니다).

(Enable EPEL repository under RHEL/CentOS 6.3/5.8)

(The fastest and easiest way to install and enable EPEL repository using YUM. First, select the RPM that matches your Linux OSarchitecture from the provided links and install it using method shown in below. The EPEL repo will install all the required dependency packages for memcached. (Note : Fedora doesn’t required EPEL repo, because it is part of fedora project).)

For RHEL/CentOS 6 ( 32-Bit )

# wget http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-7.noarch.rpm
# rpm -Uvh epel-release-6-7.noarch.rpm

For RHEL/CentOS 6 ( 64-Bit )

# wget http://mirrors.kernel.org/fedora-epel/6/x86_64/epel-release-6-7.noarch.rpm
# rpm -Uvh epel-release-6-7.noarch.rpm

For RHEL/CentOS 5 ( 32-Bit )

# wget http://mirrors.kernel.org/fedora-epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -Uvh epel-release-5-4.noarch.rpm

For RHEL/CentOS 5 ( 64-Bit )

# wget http://mirrors.kernel.org/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm
# rpm -Uvh epel-release-5-4.noarch.rpm

Install Memcached

Install Memcached program by using following command with YUM tool.

# yum install memcached
Sample Output
Loaded plugins: fastestmirror
Determining fastest mirrors
epel: kartolo.sby.datautama.net.id
Dependencies Resolved

=====================================================================================================
 Package			Arch				Version					Repository					Size
=====================================================================================================
Installing:			
memcached			i386				1.4.5-1.el5				epel						71 k

Transaction Summary
=====================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 71 k
Is this ok [y/N]: y
Downloading Packages:
memcached-1.4.5-1.el5.i386.rpm													|  71 kB     00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : memcached														          1/1

Installed:
  memcached.i386 0:1.4.5-1.el5

Complete!

Configure Memcached

Open the file called /etc/sysconfig/memcached with VI editor.

# vi /etc/sysconfig/memcached

Set or update parameters as follows, save the file and exit.

# Running on Port 11211
PORT="11211"

# Start as memcached daemon
USER="memcached"

# Set max simultaneous connections to 1024
MAXCONN="1024"

# Set Memory size to 2048 - 4GB(4096)
CACHESIZE="2048"

#Set server IP address
OPTIONS="-l 127.0.0.1"

위의 각 매개 변수에 대해 자세히 설명하겠습니다. (Let’s discuss each of the above parameters in details.)

  1. PORT : memcached가 실행하는 데 사용하는 포트입니다. (The port used by memcached to run.)
  2. USER : memcached 서비스의 시작 데몬입니다. (The start-up daemon for memcached service.)
  3. MAXCONN : 최대 동시 연결 수를 1024로 설정하는 데 사용되는 값. 사용량이 많은 웹 서버의 경우 요구 사항에 따라 임의의 수로 늘릴 수 있습니다. (The value used to set max simultaneous connections to 1024. For busy web servers you can increase to any number based on your requirements.)
  4. CACHESIZE : 캐시 크기 메모리를 2048로 설정하십시오. 사용량이 많은 서버의 경우 최대 4GB를 늘릴 수 있습니다. (Set cache size memory to 2048. For busy servers you can increase upto 4GB.)
  5. OPTIONS : Apache 또는 Nginx 웹 서버가 연결할 수 있도록 서버의 IP 주소를 설정하십시오. (Set IP address of server, so that Apache or Nginx web servers can connect to it.

Start Memcached

다음 명령을 입력하여 Memcached 데몬을 시작하고 다시 시작하십시오. 

(Type the following commands to start and restart the Memcached daemon.)

# chkconfig --levels 235 memcached on
# /etc/init.d/memcached start
# /etc/init.d/memcached restart

중지하고 상태를 확인하려면 다음 명령을 사용하십시오. 

(To stop and check status, use the following commands.)

# /etc/init.d/memcached stop
# /etc/init.d/memcached status

Verify Memcached

netstat 명령을 사용하여 Memcached가 실행 중인지 확인하십시오. 

(Use netstat command to verify Memcached is running.)

# netstat -tulpn | grep :11211

tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      20775/memcached
udp        0      0 127.0.0.1:11211             0.0.0.0:*                               20775/memcached

memcached-tool을 사용하여 서버의 통계를 확인하십시오. 

(Check the stats of the server using memcached-tool.)

# memcached-tool 127.0.0.1 stats

Install Memcached PHP extension

이제 Memcached 데몬과 작동하도록 PHP 확장을 설치하십시오. 

(Now, install PHP extension to work with Memcached daemon.)

# yum install php-pecl-memcache

Install Memcached Perl Library

Memcached 용 perl 라이브러리를 설치하십시오. 

(Install perl library for Memcached.)

# yum install perl-Cache-Memcached

Install Memcached Python Library

Memcached 용 Python 라이브러리를 설치하십시오. 

(Install python library for Memcached.)

# yum install python-memcached

Restart Apache

Apache 서비스를 다시 시작하여 변경 사항을 반영하십시오. 

(Restart the Apache service to reflect changes.)

# /etc/init.d/httpd restart
OR
# service httpd restart

Configure Firewall to Secure Memcached Server

자신의 서버에 액세스 할 수 있도록 memcached 서버에만 액세스 할 수 있는지 확인하십시오. /etc/sysconfig/iptables라는 파일을여십시오. 

(Make sure you only have access to memcached server, to enable access to your own servers open file called/etc/sysconfig/iptables.

# vi /etc/sysconfig/iptables

다음 iptables 규칙을 추가하여 자신의 서버에 액세스 할 수 있습니다. 

(Append the following iptables rules to allow access to your own servers.)

## Enable access on IP ranges from 172.16.1.1 to 172.16.1.10 for Port 11211 ##
# iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW  -m iprange --src-range 172.16.1.1-172.16.1.10 -j ACCEPT
# iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW  -m iprange --src-range 172.16.1.1-172.16.1.10 -j ACCEPT

iptables 서비스를 다시 시작하여 변경 사항을 반영하십시오. 

(Restart the iptables service to reflect changes.)

# service iptables restart
OR
# /etc/init.d/iptables restart

Cache MySQL Queries with Memcached

쉬운 일은 아닙니다. MySQL 캐싱을 활성화하려면 API를 사용하여 PHP 코드를 수정해야합니다. Memcache에서 MySQL 및 PHP의 예제 코드를 찾을 수 있습니다. 

(It isn’t an easy task for all, you need to use API’s to modify your PHP codes to enable MySQL caching. You can find the examples codes at Memcache with MySQL and PHP.)

Enable Memcached on WordPress Sites

WordPress 기반 사이트의 경우 WordPress CMS에 설치해야하는 Memcached Object Cache라는 플러그인이 있습니다. 

(For WordPress based sites, there is a plugin called Memcached Object Cache that you need to install it on your WordPress CMS.)


출처: http://www.zosel.net/entry/Memcached-for-CentOS

번역: Google 번역