memcached principle deployment memcached, session sharing

Source: Internet
Author: User
Tags define session fpm php language memcached session id openssl nginx server

Case 1: Building memcached Services
Case 2:lnmp+memcached
Local session information for case 3:php
Case 4:php Implementation session sharing
1 Case 1: Building memcached Services
1.1 Questions

This case requires a quick set up a memcached server, and the memcached simple increase, delete, change, check operation:
Install the Memcached software and start the service
Testing the memcached service with Telnet
To memcached, delete, change, check and other operations
1.2 Solutions

Use 1 RHEL7 virtual machines as memcached servers (192.168.4.5).
The RHEL7 system CD contains memcached, so you need to configure the Yum source in advance to use Yum installation directly, and you need to install the Telnet remote tool in advance of the client test.
Authentication requires the client host to install Telnet, remote memcached to verify the functionality of the server:
Add name 0 180 10//variable not present adds
Set name 0 180 10//Add or Replace variable
Replace name 0 180 10//replacement
Get name//Read variable
Append name 0 180 10//Append data to Variable
Delete name//Remove variable
Stats//View status
Flush_all//Clear All
Tip: 0 means no compression, 180 is the data cache time, and 10 is the number of bytes of data that needs to be stored.
1.3 Steps

The implementation of this case needs to follow the steps below.
Step One: Build memcached Services

1) Install the package with Yum memcached
[Email protected] ~]# yum-y install memcached
[Email protected] ~]# RPM-QA memcached
Memcached-1.4.15-10.el7_3.1.x86_64
2) memcached configuration file (view, no modification required)
[Email protected] ~]# Vim/usr/lib/systemd/system/memcached.service
Execstart=/usr/bin/memcached-u $USER-P $PORT-M $CACHESIZE-C $MAXCONN $OPTIONS
[Email protected] ~]# vim/etc/sysconfig/memcached
Port= "11211"
User= "Memcached"
maxconn= "1024"
Cachesize= "64"
Options= ""
3) Start the service and see if network connection status verification is successful:
The netstat command allows you to view the port information that is started on the system, and the following common options are:
-a displays information for all ports
-N Display port numbers in numeric format
-T displays the port of the TCP connection
-U Displays the port of the UDP connection
-L Displays the port information that the service is listening on, such as when HTTPD is started, it listens on port 80
-P shows what the service name of the listening port is (that is, the program name)
Note: In the RHEL7 system, the SS command can be used instead of netstat, with the same functionality as the options.
[Email protected] ~]# systemctl start memcached
[Email protected] ~]# systemctl status memcached
[Email protected] ~]# NETSTAT-ANPTU | grep memcached
TCP 0 0 0.0.0.0:11211 0.0.0.0:LISTEN 2839/memcached
TCP 0 0::: 11211::
:
LISTEN 2839/memcached
UDP 0 0 0.0.0.0:11211 0.0.0.0:2839/memcached
UDP 0 0::: 11211:
::
2839/memcached
[Email protected] ~]# Setenforce 0
[Email protected] ~]# Firewall-cmd--set-default-zone=trusted
Step two: Use Telnet to access the memcached server

1) Install Telnet using yum
[[email protected] ~]# yum-y install Telnet
2) Use Telnet to connect to the server to test the memcached server function, including adding, deleting, changing, checking and other operations.
[[Email protected] ~]# telnet 192.168.4.5 11211
Trying 192.168.4.5 ...
......
# #提示: 0 means no compression, 180 is the data cache time, and 3 is the number of bytes of data that needs to be stored.
Set name 0 180 3//define variable with name of variable
PLJ//input variable value, value PLJ
STORED
Get name//Get the value of the variable
VALUE name 0 3//output result
Plj
END
# #提示: 0 means no compression, 180 is the data cache time, and 3 is the number of bytes of data that needs to be stored.
Add myname 0 180 10//new, MyName does not exist then add, presence Error
Set MyName 0 180 10//Add or Replace variable
Replace MyName 0 180 10//replacement, error if myname not present
Get myname//Read variable
Append myname 0 180 10//Append data to the variable
Delete myname//Remove variable
Stats//View status
Flush_all//Clear All
Quit//Sign Out
2 Case 2:lnmp+memcached
2.1 Questions

Using practice one, deploy the Lnmp+memcached website platform and implement data manipulation on the memcached server via PHP pages to achieve the following goals:
Deploy LNMP to implement PHP Dynamic Web site architecture
Installing Memcache Extensions for PHP
Create PHP pages and write PHP code to implement data manipulation on memcached
2.2 Solutions

Use 2 RHEL7 virtual machines, one as memcached and LNMP Server (192.168.4.5), and another as a test Linux client (192.168.4.100), 1 as shown.

Figure-1
In the RHEL7 system CD contains the mariadb we need, PHP, we need to use the source to install Nginx, using the RPM package to install FPM. In addition, if you want to use PHP to operate memcached, note that you must install Memcache extension (php-pecl-memcache) for PHP, or PHP will not be able to resolve the connection memcached instructions. You need to install the Telnet remote tool in advance of client testing.
2.3 Steps

The implementation of this case needs to follow the steps below.
Step one: Deploy the LNMP environment (if the environment already exists LNMP environment This step can be ignored)

1) Install the base dependency package using Yum
[Email protected] ~]# yum-y install gcc openssl-devel pcre-devel zlib-devel
.. ..
2) Source Installation Nginx
[Email protected] ~]# TAR-XF nginx-1.12.2.tar.gz
[Email protected] ~]# CD nginx-1.12.2
[Email protected] nginx-1.12.2]#/configure \

--with-http_ssl_module
[[email protected] nginx-1.12.2]# make && make install
3) Install the MARIADB database
[[email protected] ~]# yum-y install mariadb mariadb-server mariadb-devel
4) Install PHP
[[ Email protected] ~]# yum-y install php php-mysql
[[email protected] ~]# yum-y Install Php-fpm-5.4.16-42.el 7.X86_64.RPM
5) Modify Nginx configuration file
[[email protected] ~]# vim/usr/local/nginx/conf/nginx.conf
Location/{
Root html;
Index index.php index.html index.htm;
}
Location ~. php$ {
root html;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;

Fastcgi_param script_filename $document _root$fastcgi_script_name;

Include fastcgi.conf;
}
Step two: Start the service (you can ignore this step if all services are already started)

1) Start Nginx Service
It is important to note that if other service software that listens on 80 ports (such as HTTPD) is already started on the server, you need to shut down the service first, or there will be a conflict.
[[email protected] ~]# systemctl stop httpd//If the service is present, close the service
[[email protected] ~]#/usr/local/nginx /sbin/nginx
[[email protected] ~]# NETSTAT-UTNLP | grep:80
TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32428/nginx 2) Start the MySQL service
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# systemctl status MARIADB
3) Start php-fpm service
[[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# Systemctl status PHP-FPM
4) Turn off SELinux, firewall
[[email protected] ~]# setenforce 0
[[email protected] ~ ]# firewall-cmd--set-default-zone=trusted
Step three: Create a PHP page, test the memcached service using the PHP language

1) Deploy test page
to create a PHP home document/usr/local/nginx/html/index.php, the test page can refer to lnmp_soft/php_scripts/mem.php:
[email  protected] ~]# vim/usr/local/nginx/html/test.php
<?php
$memcache =new memcache;//Create Memcache object
$ Memcache->connect (' localhost ', 11211) or die (' could not connect!! ');
$memcache->set (' key ', ' test ');//define Variable
$get _values= $memcache->get (' key ');//Get variable value
Echo $get _values;
?
2) Client testing (results will fail)
client accesses the server PHP home page document using a browser to verify the success of the operation on memcached:
[[email protected] ~]# Firefox http:/ /192.168.4.5/test.php
Note: Because there is no extension package for PHP, the default PHP cannot connect to the Memcached database, you need to install the extension module to PHP to connect to the memcached database.
3) Add memcache extension to PHP
[[email protected] ~]# yum-y install Php-pecl-memcache
[[email protected] ~] # systemctl Restart PHP-FPM
4) Client tests again (results will successfully display data results)
[[email protected] ~]# Firefox http://192.168.4.5/ test.php
3 Case 3:php Local Session information
3.1 question

The Nginx scheduler loads back-end two Web servers to achieve the following goals:
Deploy Nginx to the foreground dispatch server
The scheduling algorithm is set to poll
Back end for two LNMP servers
Deploy test page to view PHP local session information
3.2 Solutions

Use 4 RHEL7 virtual machines, one of which is deployed as an nginx front-end Scheduler server (eth0:192.168.4.5,eth1:172.16.2.5) and two virtual machines as LNMP servers. The WEB1 server (192.168.2.100) and WEB2 Server (192.168.2.200), and the other as a test Linux client (172.16.4.100), are shown in topology 2.

Figure-2
3.3 Steps

The implementation of this case needs to follow the steps below.
Step one: Deploy backend LNMP server-related software

Note: The following deployment of LNMP server operation, need to do the same operation on two back-end servers, the following we take a WEB1 server (192.168.2.100) as an example, the WEB2 server to do the same operation.
1) Install the base dependency package using Yum
[Email protected] ~]# yum-y install gcc openssl-devel pcre-devel zlib-devel
.. ..
2) Source Installation Nginx
[Email protected] ~]# TAR-XF nginx-1.12.2.tar.gz
[Email protected] ~]# CD nginx-1.12.2
[Email protected] nginx-1.12.2]#/configure \

--with-http_ssl_module
[[email protected] nginx-1.12.2]# make && make install
3) Install the MARIADB database
[[email protected] ~]# yum-y install mariadb mariadb-server mariadb-devel
4) Install PHP (PHP-FPM package is available in lnmp_soft)
[[email protected] ~]# yum-y install PHP php-mysql
[[email  Protected] ~]# yum-y install php-fpm-5.4.16-42.el7.x86_64.rpm
5) Modify Nginx configuration file (modify default home page and static separation)
[[email  Protected] ~]# vim/usr/local/nginx/conf/nginx.conf
Location/{
root html;
Index index.php index.html index.htm;
}
Location ~. php$ {
root html;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;

Fastcgi_param script_filename $document _root$fastcgi_script_name;

Include fastcgi.conf;
}
Step two: Start LNMP server-related services

1) Start Nginx Service
It is important to note that if other service software that listens on 80 ports (such as HTTPD) is already started on the server, you need to shut down the service first, or there will be a conflict.
[[email protected] ~]# systemctl stop httpd//If the service is present, close the service
[[email protected] ~]#/usr/local/nginx /sbin/nginx
[[email protected] ~]# NETSTAT-UTNLP | grep:80
TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32428/nginx 2) Start the MySQL service
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# systemctl status MARIADB
3) Start php-fpm service
[[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# Systemctl status PHP-FPM
4) Turn off SELinux, firewall
[[email protected] ~]# setenforce 0
[[email protected] ~ ]# firewall-cmd--set-default-zone=trusted
Step Three: Deploy the front-end Nginx Dispatch server

1) Use source to install Nginx software (if Nginx package already exists can ignore this step)
[[email protected] ~]# yum-y install pcre-devel openssl-devel
[ [email protected] ~]# tar-xf nginx-1.12.2.tar.gz
[[email protected] ~]# cd nginx-1.12.2
[[Email  protected] nginx-1.12.2]#./configure
[[email protected] nginx-1.12.2]# make && make install
2) Modify Nginx configuration file
Nginx configuration file, the backend server address pool is defined through upstream, the default scheduling policy is polling, and the server address pool defined by the upstream is called with Proxy_pass:
[[Email  protected] ~]# vim/usr/local/nginx/conf/nginx.conf
....
Upstream Webs {
Server 192.168.2.100:80;
Server 192.168.2.200:80;
}
Server {
Listen;
server_name localhost;
Location/{
Proxy_pass http://webs;
root HTML;
Index index.php index.html index.htm;
}
}
3) Reload configuration file

[Email protected] ~]#/usr/local/nginx/sbin/nginx-s Reload
#请先确保nginx是启动状态才可以执行命令成功, otherwise error, error message is as follows:
#[error] Open () "/usr/local/nginx/logs/nginx.pid" failed (2:no such file or directory)
4) Turn off SELinux, firewall
[Email protected] ~]# Setenforce 0
[Email protected] ~]# Firewall-cmd--set-default-zone=trusted
Step four: Test environment is configured successfully

1) Browser Access test page validation.
[[email protected] ~]# Curl http://192.168.4.5/index.html//See if there is data
Step five: Deploy the test page

1) Deploy the test page (WEB1 server).
The test page can refer to lnmp_soft/php_scripts/php-memcached-demo.tar.gz.
[Email protected] ~]# CD lnmp_soft/php_scripts/
[Email protected] php_scripts]# TAR-XF php-memcached-demo.tar.gz
[Email protected] php_scripts]# CD Php-memcached-demo
[Email protected] php-memcached-demo]# cp-a/usr/local/nginx/html/
2) The browser directly accesses the test page of the backend server (WEB1 server).
[[email protected] ~]# Firefox http://192.168.2.100//fill in Account information
[[email protected] ~]# cd/var/lib/php/session///View server local session information
[[email protected] ~]# ls
sess_ahilcq9bguot0vqsjtd84k7244//Note that the ID here is random
[email protected] ~]# cat sess_ahilcq9bguot0vqsjtd84k7244
Note: You can modify the contents of the index.php and home.php two files by adding the page color properties to differentiate between two different servers on the backend: <body bgcolor=blue>.
3) Deploy the test page (WEB2 server).
The test page can refer to lnmp_soft/php_scripts/php-memcached-demo.tar.gz.
[Email protected] ~]# CD lnmp_soft/php_scripts/
[Email protected] php_scripts]# TAR-XF php-memcached-demo.tar.gz
[Email protected] php_scripts]# CD Php-memcached-demo
[Email protected] php-memcached-demo]# cp-a
/usr/local/nginx/html/
4) The browser directly accesses the test page of the backend server (WEB2 server).
[[email protected] ~]# Firefox http://192.168.2.100//fill in Account information
[[email protected] ~]# cd/var/lib/php/session///View server local session information
[[email protected] ~]# ls
sess_qqek1tmel07br8f63d6v9ch401//Note that the ID here is random
[email protected] ~]# cat sess_qqek1tmel07br8f63d6v9ch401
Note: You can modify the contents of the index.php and home.php two files by adding the page color properties to differentiate between two different servers on the backend: <body bgcolor=green>.
5) browser Access front-end scheduler test (different backend server session inconsistent).
It is recommended to use Google Browser testing.
[Email protected] ~]# Google-chrome http://192.168.4.5
After filling in the registration information, refresh, also need to register again, indicating that two computers are using the local session
The second host does not know that you are again the first host is logged in, the first host's login information is not passed to the second host
4 Case 4:php Implementation session sharing
4.1 Questions

Following exercise three, by modifying the PHP-FPM configuration file for session sharing, this case needs to be implemented on the basis of exercise three:
Configure PHP to share session information using Memcached server
The Session information is consistent when the client accesses two different back-end Web servers
4.2 Solutions

On the basis of practicing the three topologies, the Nginx server should assume the role of the memcached database in addition to the scheduler, and implement the session sharing of PHP on the two back-end LNMP servers. Topology-4 is shown.

Figure-4
4.3 steps

The implementation of this case needs to follow the steps below.
Step One: Build memcached Services

1) Install the Memcached service (this step can be ignored if the package is already available on 192.168.4.5)
[Email protected] ~]# yum-y install memcached
2) Start the service and see if network connection status verification is successful:
[Email protected] ~]# systemctl restart memcached
[Email protected] ~]# NETSTAT-ANPTU | grep memcached
TCP 0 0 0.0.0.0:11211 0.0.0.0: LISTEN 2839/memcached
TCP 0 0::: 11211::
: LISTEN 2839/memcached
UDP 0 0 0.0.0.0:11211 0.0.0.0: 2839/memcached
UDP 0 0::: 11211::
: 2839/memcached
3) Turn off SELinux, firewall
[Email protected] ~]# Setenforce 0
[Email protected] ~]# Firewall-cmd--set-default-zone=trusted
Step Two: Deploy session sharing on the back-end LNMP server

Note: These actions are required on both back-end Web servers, as an example of the WEB1 (192.168.2.100) server.
1) Add memcache extension to PHP
Note that because the backend two Web servers (WEB1,WEB2) need to connect to the Memcached database, both hosts will need to install the PHP extension module (also the web for example).
[Email protected] ~]# yum-y install Php-pecl-memcache
2) Modify the PHP-FPM configuration file and restart the service
Note that because the backend two Web servers (WEB1,WEB2) need to modify the configuration file (also the Web for example below).
[[email protected] ~]# vim/etc/php-fpm.d/www.conf//Modify two parameters of the configuration file
The last 2 lines of the file
The effect before modification is as follows:
Php_value[session.save_handler] = files
Php_value[session.save_path] =/var/lib/php/session
Original file, default definition sessoin session information Local computer (default in/var/lib/php/session)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The following effects are modified:
Php_value[session.save_handler] = Memcache
Php_value[session.save_path] = "tcp://192.168.2.5:11211"
Define session information stored on a public memcached server with Memcache (no d) in the host parameter
Define where the public memcached server is (IP and port of the server) through the path parameter
[Email protected] ~]# systemctl restart PHP-FPM
Step Three: Client testing

The client uses a browser to access two different Web servers.
The procedure is consistent with the practice, and finally the session ID information can be obtained.

memcached principle Deployment memcached, session sharing

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.