Cài đặt và cấu hình Memcached trên VestaCP [ Phần 9 ]

5
3875

 Cài đặt và cấu hình Memcached trên VestaCP

 

memcached_flowchart

I – Memcached là gì ?

–    Memcached là một dịch vụ, người ta hay gọi là Memory Cache, Memcached lưu data theo dạng Key, Value lên Ram. Bạn có thể đưa mọi dữ liệu lên Ram thông qua Memcached. Ví dụ : Kết quả query của Database, hoặc là html pages. …

II – Cái lợi và hại khi sử dụng Memcached :

–     Như mình đã nói ở trên, việc đưa dữ liệu gì lên Memcached là phụ thuộc vào nhu cầu sử dụng. Ở đây mình chỉ tóm gọn dữ liệu sẽ đưa lên Memcached là html pages và Query Database.

–     Giả sử khi mà bạn request một trang như https://wikivps.net thì lúc đó trình duyệt sẽ yêu cầu Web Server cung cấp cho mình trang chủ của site fasthost.vn . Lúc đó Web Server phải thực thi nhiều công việc như : Thực thi các câu query để lấy dữ liệu lên hiển thị trên trang chủ, thực thi các đoạn mã PHP. sau đó mới trả kết quả về lại cho người dùng. Nếu cùng lúc đó có tầm 100 người cùng request tới yêu cầu trang chủ của fasthost.vn thì Web Server phải thực thi 100 lần như vậy. để đáp ứng nhu cầu cho Client, Như vậy các bạn củng đủ thấy là rất tốn tài nguyên (resource) của VPS mình đúng không. Do vậy nên nhu cầu cache lại kết quả trang chủ để đáp ứng cho 100 người request sau là cần thiết. Lúc này chúng ta có thể lấy kết quả trả về của trang chủ lưu vào Memcached, mỗi khi mà có request yêu cầu trang chủ thì Web Service sẽ vào Memcached lấy và trả về cho người dùng.

–     Như vậy tốc độ truy cập sẽ được cải thiện, giảm thời gian delay, làm tăng resource của VPS, Server.

–     Như mình đã nói ở trên, Memcached là Memory Cache, cho nên nó sẽ cần nhiều ram để hoạt động. Nên cân nhắc trước khi sử dụng Memcached. Với lại 1 điều nữa là nếu sử dụng Memcached cho nhiều site mà cùng sử dụng một opensource như WordPress chẳn hạn. Thì sẽ gặp phải tình trạng site A request lên mà Memcached lấy dữ liệu của Site B trả về cho người dùng. Bởi vì Memcached lưu dữ liệu theo dạng Key,Value cho nên nếu cùng là Open Source thì cái Key nó sẽ có lúc trùng nhau. ( Bữa sau mình sẽ viết một bài để Fix trường hợp này. Có nghĩa là chạy nhiều site WordPress chung VPS, Server mà vẫn sài Memcached được. )

II – Cài đặt và cấu hình Memcache cho VestaCP [ Hướng dẫn này các bạn củng có thể áp dụng cho bất kì mô hình nào, không nhất thiết phải là VestaCP ]

  • Cài đặt thư viện cần thiết.
# yum install vim* wget gcc gcc-c++ make libevent libevent-* -y
  • Download  và cài đặt Memcached.
# cd /usr/src
# wget -O memcached-1.4.20.tar.gz https://memcached.org/latest
# tar -zxvf memcached-1.4.20.tar.gz
# cd memcached-1.4.20
# ./configure
# make
# make install
  •  Download và cài đặt thư viện để PHP giao tiếp với Memcached.
# cd /usr/src
# wget https://pecl.php.net/get/memcache-2.2.7.tgz
# tar -zxvf memcache-2.2.7.tgz
# cd memcache-2.2.7
# phpize
# ./configure --with-php-config=/usr/local/vesta/php/bin/php-config
# make
# make install

–   Nếu không phải dùng VestaCP thì các bạn có thể dùng lệnh sau để tìm ra file php-config. sau đó lấy đường dẫn thay vào chuỗi config.

[root@vps ~]# find / -type f -name "php-config"
/usr/bin/php-config

– Cấu hình php.ini, Open file php.ini bằng vim rùi copy dòng extension bên dưới bỏ vào cuối file php.ini. Thoát và lưu file php.ini lại.

# vim /etc/php.ini
# extension="/usr/lib/php/extensions/no-debug-non-zts-20100525/memcache.so"
  • Tạo file cấu hình cho Memcached.

–  Dùng vim mở file này lên sau đó copy đoạn config phía dưới paste vào. lưu lại.

# vim /etc/memcached.conf

–  Config.

# Memory
-m 512
# default port
-p 11211
# user to run daemon nobody/apache/www-data
-u nobody
# only listen locally
-l 127.0.0.1
  • Tạo file startup.

–    Mở file này lên dùng vim hoặc trình soạn thảo mà bạn biết. ví dụ vi củng được.

# vim /etc/init.d/memcached

– Copy đoạn code dưới đây paste vào file mới mở lên, và lưu lại. Copy tất cả các dòng dưới đây, ngay cả dấu # củng copy luôn.

#!/bin/bash
#
# memcached This shell script takes care of starting and stopping
# standalone memcached.
#
# chkconfig: - 80 12
# description: memcached is a high-performance, distributed memory
# object caching system, generic in nature, but
# intended for use in speeding up dynamic web
# applications by alleviating database load.
# processname: memcached
# config: /etc/memcached.conf
# Source function library.
. /etc/rc.d/init.d/functions
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/memcached
DAEMONBOOTSTRAP=/usr/local/bin/start-memcached
DAEMONCONF=/etc/memcached.conf
NAME=memcached
DESC=memcached
PIDFILE=/var/run/$NAME.pid
[ -x $DAEMON ] || exit 0
[ -x $DAEMONBOOTSTRAP ] || exit 0
RETVAL=0
start() {
echo -n $"Starting $DESC: "
daemon $DAEMONBOOTSTRAP $DAEMONCONF
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $PIDFILE
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down $DESC: "
killproc $NAME
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $PIDFILE
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL

–  Sau đó chạy lệnh sau.

# chmod +x /etc/init.d/memcached

– Tiếp theo tạo tiếp file này.

# vim /usr/local/bin/start-memcached

– Sau đó copy đoạn mã dưới đây bỏ vào file mới mở. nhớ copy luôn cả dấu # nha các bạn.

#!/usr/bin/perl -w
# start-memcached
# 2003/2004 - Jay Bonci <jaybonci@debian.org>
# This script handles the parsing of the /etc/memcached.conf file
# and was originally created for the Debian distribution.
# Anyone may use this little script under the same terms as
# memcached itself.
use strict;
if ($> != 0 and $< != 0) {
print STDERR "Only root wants to run start-memcached.\n";
exit;
}
my $etcfile = shift || "/etc/memcached.conf";
my $params = [];
my $etchandle;
# This script assumes that memcached is located at /usr/bin/memcached, and
# that the pidfile is writable at /var/run/memcached.pid
my $memcached = "/usr/local/bin/memcached";
my $pidfile = "/var/run/memcached.pid";
# If we don't get a valid logfile parameter in the /etc/memcached.conf file,
# we'll just throw away all of our in-daemon output. We need to re-tie it so
# that non-bash shells will not hang on logout. Thanks to Michael Renner for
# the tip
my $fd_reopened = "/dev/null";
sub handle_logfile {
my ($logfile) = @_;
$fd_reopened = $logfile;
}
sub reopen_logfile {
my ($logfile) = @_;
open *STDERR, ">>$logfile";
open *STDOUT, ">>$logfile";
open *STDIN, ">>/dev/null";
$fd_reopened = $logfile;
}
# This is set up in place here to support other non -[a-z] directives
my $conf_directives = {
"logfile" => \&handle_logfile
};
if (open $etchandle, $etcfile) {
foreach my $line (<$etchandle>) {
$line =~ s/\#.*//go;
$line = join ' ', split ' ', $line;
next unless $line;
next if $line =~ /^\-[dh]/o;
if ($line =~ /^[^\-]/o) {
my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/;
$conf_directives->{$directive}->($arg);
next;
}
push @$params, $line;
}
}
unshift @$params, "-u root" unless (grep $_ eq '-u', @$params);
$params = join " ", @$params;
if (-e $pidfile) {
open PIDHANDLE, "$pidfile";
my $localpid = <PIDHANDLE>;
close PIDHANDLE;
chomp $localpid;
if (-d "/proc/$localpid") {
print STDERR "memcached is already running.\n";
exit;
} else {
<code>rm -f $localpid</code>;
}
}
my $pid = fork();
if ($pid == 0) {
reopen_logfile($fd_reopened);
exec &quot;$memcached $params&quot;;
exit(0);
} elsif (open PIDHANDLE,&quot;&gt;$pidfile&quot;) {
print PIDHANDLE $pid;
close PIDHANDLE;
} else {
print STDERR &quot;Can't write pidfile to $pidfile.\n&quot;;
}

– Sau đó chạy câu lệnh sau.

# chmod +x  /usr/local/bin/start-memcached

IV – Start Memcached.

–   Start Memcached.

# /etc/init.d/memcached restart
Shutting down memcached: [ OK ]
Starting memcached: [ OK ]

– Cấu hình onboot cho Memcached. ( Khi VPS, Server khởi động lại thì nó sẽ được auto start lên. )

# /sbin/chkconfig memcached on

:mrgreen:   Tới đây là cài đặt xong Memcached và thư viện memcache cho PHP rùi nhé. Bài tiếp theo mình sẽ hướng dẫn các bạn cấu hình sử dụng Memcached cho một số OpenSource phổ biến. 🙂

 

Bình luận qua Facebook

5 BÌNH LUẬN

  1. em bi loi nay anh oi
    [root@thethoi ~]# chmod +x /usr/local/bin/start-memcached
    [root@thethoi ~]# /etc/init.d/memcached restart
    /etc/init.d/memcached: line 1: ched: command not found
    Shutting down memcached: [FAILED]
    Starting memcached: /usr/local/bin/start-memcached: line 1: tart-memcached: command not found
    /usr/local/bin/start-memcached: line 7: use: command not found
    /usr/local/bin/start-memcached: line 8: syntax error near unexpected token {'
    /usr/local/bin/start-memcached: line 8:
    if ($> != 0 and $< != 0) {'
    [FAILED]

    • Hi Em,
      Em copy lại nội dung file init cẩn thận nhé, chắc trong quá trình copy bị dư hoặc thiếu kí tự thôi.
      Thân,
      Goo Nux

  2. Cho mình hỏi mình cài đến phần

    ./configure –with-php-config=/usr/local/vesta/php/bin/php-config

    thì không chạy được nữa.

    Quá trình cài đặt

    checking for grep that handles long lines and -e… /bin/grep
    checking for egrep… /bin/grep -E
    checking for a sed that does not truncate output… /bin/sed
    checking for cc… cc
    checking for C compiler default output file name… a.out
    checking whether the C compiler works… yes
    checking whether we are cross compiling… no
    checking for suffix of executables…
    checking for suffix of object files… o
    checking whether we are using the GNU C compiler… yes
    checking whether cc accepts -g… yes
    checking for cc option to accept ISO C89… none needed
    checking how to run the C preprocessor… cc -E
    checking for icc… no
    checking for suncc… no
    checking whether cc understands -c and -o together… yes
    checking for system library directory… lib
    checking if compiler supports -R… no
    checking if compiler supports -Wl,-rpath,… yes
    checking build system type… x86_64-unknown-linux-gnu
    checking host system type… x86_64-unknown-linux-gnu
    checking target system type… x86_64-unknown-linux-gnu
    checking for PHP prefix… /usr/local/vesta/php
    checking for PHP includes… -I/usr/local/vesta/php/include/php -I/usr/local/vesta/php/include/php/main -I/usr/local/vesta/php/include/php/TSRM -I/usr/local/vesta/php/include/php/Zend -I/usr/local/vesta/php/include/php/ext -I/usr/local/vesta/php/include/php/ext/date/lib
    checking for PHP extension directory… /usr/local/vesta/php/lib/php/extensions/no-debug-non-zts-20100525
    checking if debug is enabled… no
    checking if zts is enabled… no
    checking for re2c… no
    configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
    checking for gawk… gawk
    checking whether to enable memcache support… yes, shared
    checking whether to enable memcache session handler support… yes
    checking for the location of ZLIB… no
    checking for the location of zlib… configure: error: memcache support requires ZLIB. Use –with-zlib-dir= to specify prefix where ZLIB include and library are located

    như vậy là bị lỗi gì vậy bạn. Giúp mình với

Leave a Reply to Cấu hình Wordpress, Joomla, Xenforo, vBulletin sử dụng Memcached Hủy trả lời

Please enter your comment!
Please enter your name here