Ubuntu Server 11.04 installation memcache and PHP use Memcache to store session methods, 11.04memcache_php tutorial

Source: Internet
Author: User

Ubuntu Server 11.04 installation memcache and PHP use Memcache to store session methods, 11.04memcache


This example describes the Ubuntu server 11.04 installation memcache and the way PHP uses Memcache to store the session. Share to everyone for your reference, as follows:

1, first install Memcache server:

sudo apt-get install memcached

The system automatically starts after the installation completes the Memcached service occupies 11211 ports

If you need to reconfigure the 11211-port service, turn off the memcached service

To start manually:

Memcached-d-M 128-p 11211-u memcache

Here you need to explain the startup parameters of the Memcached service:

-P Listening Port
-L connected IP address, default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown Close the running memcached service
-D Install memcached service
-d Uninstall Uninstall memcached service
-U Run as (only valid when running as root)
-m maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-c Maximum number of simultaneous connections, default is 1024
-F Block size growth factor, default is 1.25-n minimum allocation space, key+value+flags default is 48
-H Display Help

2. Install PHP Memecache Client

$ sudo apt-get install Php5-memcache

Restarting the Web server

To test the Memcache code:

<?php$mem = new Memcache; Create Memcache Object $mem->connect ("127.0.0.1", 11211); Connect memcache Server $val = "This is a memcache test."; $key = MD5 ($val), if ($k = $mem->get ($key))) {//Determines whether to get to the specified key  echo ' from cache: '. $k;} else {  echo ' normal ';//This We need to replace the query database and create the cache in actual use.  $mem->set ($key, $val, 0, 120); Add a cache with a cache time of 120s}

Storing session with Memcache

In general, the Session is stored as a text file on the server side. If you use Seesion, or if the PHP file calls the session variable, you must start it before calling the session and use the Session_Start () function. Others do not need you to set up, PHP automatically complete the Session file creation. The storage path of its default Session is the system Temp folder of the server.

However, if the sesstion of large data is encountered, the use of file-based session access bottlenecks may be in the disk IO operation, now using memcached to save session data, directly through the memory of the way, the efficiency naturally can improve a lot. The read and write speed is much faster than the files, and it is convenient to have multiple servers sharing the session, which is configured to use the same set of memcached servers, which reduces the additional effort.

The disadvantage is that the session data is stored in memory, once the outage, the data will be lost. However, the session data is not a serious problem.

How to use memcached to store a session? The following are the basic configuration steps:

1. Install memcached (skip, not clear the package can see the previous article: http://www.bkjia.com/article/85510.htm)
The "Registered save handlers" in the Phpinfo output will have "files User SQLite".

2. Modify the configuration file,

①. Global settings in php.ini (* Requires server restart)

Session.save_handler = Memcachesession.save_path = "tcp://127.0.0.1:11211"

②. Or. htaccess in a directory:

Php_value Session.save_handler "memcache" Php_value Session.save_path "tcp://127.0.0.1:11211"

③. can also be in one application:

Ini_set ("Session.save_handler", "memcache");
Ini_set ("Session.save_path", "tcp://127.0.0.1:11211");

Note: The use of multiple memcached servers is separated by commas "," and, as described in the memcache::addserver () documentation, can take additional parameters "persistent", "weight", "timeout", "Retry _interval "And so on, like this:

"Tcp://host:port?persistent=1&weight=2,tcp://host2:port2".

3. Start memcached
Copy the Code code as follows: Memcached-d-M 10-u root-l 127.0.0.1-p 11211-c 256-p/tmp/memcached.pid

4. Test to create a session

<?php//set_session.phpsession_start (); if (!isset ($_session[' admin ')) {  $_session[' admin '] = ' wan ';} Print $_session[' admin '];p rint "/n";p rint session_id (); >

5. Use SessionID to check the memcached.

<?php//get_session.php$mem = new Memcache; $mem->connect ("127.0.0.1", 11211); Var_dump ($mem->get (' 0935216dbc0d721d629f89efb89affa6 '));? >

Copy the Code code as follows: [Root@localhost html]#/usr/local/webserver/php/bin/php-f get_session.php
Output Result:

String (+) "Admin|s:3:" Wan ";"

Prove that the session is working properly.

Re-dive into multi-domain sites to share session data using memcache methods

By understanding how the session works, we can find that, by default, each server generates session IDs for the same client individually, such as the session ID generated by a server for the same user browser. 30DE1E9DE3192BA6CE2992D27A1B6A0A, while the B server generates C72665AF28A8B14C0FE11AFE3B59B51B. In addition, the SESSION data of PHP is stored separately in the file system of this server.

Once you have identified the problem, you can proceed with the solution. To share session data, two goals must be achieved: one is that each server must have the same session ID as the same client, and it can be passed through the same COOKIE, which means that each server must be able to read the same name as PHPSESSID. A COOKIE; the other is how the SESSION data is stored/placed to ensure that each server has access to it. In short, the session ID of the multi-server shared client, and the server-side session data must also be shared.

The first goal of the implementation is very simple, only need to set the domain of the cookie is special, by default, the domain of the cookie is the domain name/IP address of the current server, and the domain is different, the individual servers set cookies are not accessible to each other, such as Www.aaa.com server is not able to read and write www.bbb.com server settings cookies. Here we say that the server of the same site has its particularity, that is, they belong to the same domain, such as: Tieba.xiaoyuan.com and www.xiaoyuan.com belong to the domain. xiaoyuan.com, then we can set the COOKIE domain as. Xiaoyuan.com, so tieba.xiaoyuan.com, www.xiaoyuan.com, and so on can access this COOKIE. The Setup method in the PHP code is as follows:

<?phpini_set (' Session.cookie_domain ', ' xiaoyuan.com ');? >

The purpose of each server sharing the same client SESSION ID is achieved.

So you can a.domain.com under the

session.php

<?phpini_set (' Session.cookie_domain ', ' domain.com '); session_start (); if (!isset ($_session[' admin ')) {  $_ session[' admin ' = ' wan ';} Print $_session[' admin '];p rint "\ n";p rint session_id ();

Under the b.domain.com

session2.php

<?phpini_set (' Session.cookie_domain ', ' domain.com '); session_start (); Echo $_session[' admin '];

I hope that this article on the Ubuntu platform for everyone on the PHP program has helped.

http://www.bkjia.com/PHPjc/1133039.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133039.html techarticle Ubuntu Server 11.04 installs Memcache and PHP uses Memcache to store session methods, 11.04memcache This example describes the Ubuntu server 11.04 installing memcache and PHP using memcache to store se ...

  • 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.