Lnmp +memcached

Source: Internet
Author: User
Tags fpm memcached nginx load balancing

Proxe Proxy Server:

Installing the Memcached Service

#yum-y Install gcc

#tar-xf libevent-2.0.21-stable.tar.gz (memcached service library file)

#cd libevent-2.0.21-stable

#./configure

#make && make Install


You can directly link to the library files can also be written in the library configuration file, do one of the lines.

1) #ln-S/usr/local/lib/libevent/*/usr/lib/

#ldconfig (//Refresh)


2) #vim/etc/ld.so.conf (configuration file for library files)

/usr/local/lib/

#ldconfig (//Refresh)


#tar-ZXF memcached-1.4.24.tar.gz

#cd memcached-1.4.24

#./configure

#make && make Install


#memcached-H (View Help)


[Email protected] ~]# memcached-u root-m 64-d

To see if the service started:

[Email protected] ~]# NETSTAT-ANPTU |grep:11211

TCP 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6813/memcached

TCP 0 0::: 11211:::* LISTEN 6813/memcached

UDP 0 0 0.0.0.0:11211 0.0.0.0:* 6813/memcached

UDP 0 0::: 11211:::* 6813/memcached



Test:

[[email protected] b]# yum-y install Telnet


[[Email protected] b]# telnet 192.168.4.5 11211

Trying 192.168.4.5 ...

Connected to 192.168.4.5.

Escape character is ' ^] '.


Set name 0 100 3

Tom

STORED

Get Name

VALUE Name 0 3

Tom

END

Replace name 0 200 3

Asc

STORED

Get Name

VALUE Name 0 3

Asc

END





Add set Replace Delete get



Customer---site---login [code]---database


Memcached temporary data, permanent data


Permanent data must be stored in SQL database

memcached purposes, cache these data



When will the memcached data be lost?

1. Service restart

2. Computer restart

3. Data expiration

4.delete or Fluash_all

5.LRU


——————————————————————————————————————————————————————————————————————————————————————————

——————————————————————————————————————————————————————————————————————————————————————————

Lnmp +memcache in the installation of PHP process, to press the Memcache module, in order to use PHP to parse memcached instructions. (Memcached is the service memcache is the module that PHP calls the service)



(Note: This is the function that PHP needs to install when using Database Memcache, because the installation columns are already added by default in the script)

[Email protected] memcache-2.2.5]#/configure \

>--with-php-config=/usr/local/php5/bin/php-config

>--enable-memcache)




LNMP +memcached Experiment: (Linux nginx mysql php) +memcached



[Email protected] ~]# vim/usr/local/nginx/conf/nginx.conf


Location ~ \.php$ {

root HTML;

Fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

# Fastcgi_param Script_filename/scripts$fastcgi_script_name;

Include fastcgi.conf;

}



[Email protected] ~]# vim/usr/local/nginx/html/test.php

<?php

$memcache =new memcache;

$memcache->connect (' localhost ', 11211) or die (' could not connect!! ‘);

$memcache->set (' key ', ' test ');

$get _values= $memcache->get (' key ');

echo $get _values;

?>

[Email protected] ~]# Nginx-s Reload

[Email protected] ~]# NETSTAT-ANPTU |grep:80

TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6875/nginx


[[Email protected] ~]# service php-fpm Restart (installation of the Memcache module, the default script is installed, no need to add modules)

[Email protected] ~]# NETSTAT-ANPTU |grep:9000

TCP 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9931/php-fpm

[Email protected] ~]# NETSTAT-ANPTU |grep:11211

TCP 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6813/memcached


Test:

Browser testing:

[email protected] ~]# Firefox http://192.168.4.5/test.php

Test


Memcache database View Test results:

[[Email protected] nginx]# telnet 192.168.4.5 11211

Trying 192.168.4.5 ...

Connected to 192.168.4.5.

Escape character is ' ^] '.

Get key

VALUE Key 0 4

Test

END

——————————————————————————————————————————————————————————————————————————————————————————————



++++++++++++++++++++++++++++++++

PHP--->memcached

++++++++++++++++++++++++++++

Note: The Memcache installation is to be installed on a host with PHP


Memcache is an extension library file for PHP



#tar-XF memcache-2.2.5.tgz

#cd memcache-2.2.5


#/usr/local/php5/bin/phpize.

#./configure

#make && make Install



#sed-I ' 728i extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/" '/usr/local/php5/etc/ Ini


#sed-i ' 856i extension=memcache.so '/usr/local/php5/etc/php.ini



PHP can be used to support connecting memcached server after installing memcache extension to PHP


#vim/usr/local/nginx/html/test.php

<?php

$memcache =new memcache;

Create a Memcache object

$memcache->connect (' localhost ', 11211) or die (' could not connect!! ‘);

$memcache->set (' key ', ' test ');

Defining variables

$get _values= $memcache->get (' key ');

Get variable Value

echo $get _values;

?>

#service php-fpm Restart



Troubleshooting:

1.nginx is started

2.nginx whether PHP is connected via Fastcgi_pass

3.PHP whether to start service php-fpm start

4.PHP installed the MEMCAC he extension, called the extension, and restarted PHP

6.test.php If there is a syntax error

7.memcached Start-up service

User->nginx->php->memcache->memcached

LOG: Nginx/logs/error.log



++++++++++++++++++++++++++++++++

client:192.168.4.100 eth0

nginx:192.168.4.5 eth0 Nginx

192.168.2.5 eth1

tomcat1:192.168.2.100 eth1 Tomcat

tomcat2:192.168.2.200 eth1 Tomcat


Load balancer, back to Tomcat from httpd


1.nginx Load Balancing

#vim nginx/conf/nginx.conf

Upstream Tom {

Server 192.168.2.100:8080;

Server 192.168.2.200:8080;

}

server {

Listen 80;

server_name localhost;

Location/{

Proxy_pass Http://tom;

...

}

}

#nginx-S Reload



2. Install boot tomcat

< using scripts or manual installation >

#/usr/local/tomcat/bin/catalina.sh start



++++++++++++++++++++++++++++++

Experiment: Save session information with memcached


The following operations need to be performed on both Tomcat


1. Create Java page, display SessionID

#cat/usr/local/tomcat/webapps/root/test.jsp

<body bgcolor= "Green" >

<center>

<%string s = Session.getid ();%>

<%=s%>

</center>

</body>



2. Make Tomcat connect memcached

Need to install an extension library file for Tomcat

#cd lnmp_soft/session/

#cp *.jar/usr/local/tomcat/lib/



3. Write the tomcat context.xml file, specifying the IP and port of the memcached


#cd lnmp_soft/session

#cp context.xml/usr/local/tomcat/conf/


#vim/usr/local/tomcat/conf/context.xml

... ...

Memcachednodes= "mem1:192.168.2.5:11211"

Modify IP to the IP address of the host that started the memcached service

#/usr/local/tomcat/bin/catalina.sh stop

#/usr/local/tomcat/bin/catalina.sh start


This article is from the "12336621" blog, please be sure to keep this source http://12346621.blog.51cto.com/12336621/1901435

Lnmp +memcached

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.