Ubuntu server 11.04 installs memcache and php uses memcache to store sessions. 11.04 memcache

Source: Internet
Author: User

Ubuntu server 11.04 installs memcache and php uses memcache to store sessions. 11.04 memcache

This article describes how to install memcache on Ubuntu server 11.04 and use memcache in php to store sessions. We will share this with you for your reference. The details are as follows:

1. First install the memcache Server:

sudo apt-get install memcached

After the installation is complete, the system automatically starts the memcached service to occupy port 11211.

To reconfigure the service on port 11211, You need to disable the memcached service that has been enabled.

Manual start:

memcached -d -m 128 -p 11211 -u memcache

Here we need to describe the memcached service startup parameters:

-P listening port
-L connected IP address. The default value is local
-D start: start the memcached service.
-D restart: restart the memcached service.
-D stop | shutdown the running memcached Service
-D install the memcached Service
-D uninstall memcached Service
-U runs as the identity (only valid when running as root)
-MB maximum memory usage, in MB. The default value is 64 MB.
-An error is returned when M memory is used up, instead of deleting items.
-C: Maximum number of simultaneous connections. The default value is 1024.
-F block size growth factor. The default value is 1.25-n. The minimum allocation space is 48 for key + value + flags.
-H Show Help

2. Install the PHP Memecache Client

$ sudo apt-get install php5-memcache

Restart the web Server

Test the memcache code:

<? Php $ mem = new Memcache; // create a Memcache object $ mem-> connect ("127.0.0.1", 11211); // connect to the Memcache server $ val = "This Is A Memcache test. "; $ key = md5 ($ val); if ($ k = $ mem-> get ($ key ))) {// determine whether the specified key echo 'from cache: 'is obtained :'. $ k;} else {echo 'normal'; // here we need to replace it with query database and create cache in actual use. $ mem-> set ($ key, $ val, 0,120); // adds a cache inserted for 120 s}

Store sessions with memcache

Generally, sessions are stored on the server as text files. If you use Seesion or the PHP file needs to call the Session variable, you must start the Session before calling it and use the session_start () function. PHP automatically creates the Session file. The default Session storage path is the temporary system folder of the server.

However, if you encounter Sesstion with a large amount of data, the bottleneck of file-based Session access may be in disk I/O operations. Now, Memcached is used to save Session data and the memory is used directly, efficiency can naturally be improved a lot. The speed of reading and writing is much faster than that of files, and it is more convenient for multiple servers to share sessions. You can configure these servers to use the same group of memcached servers, this reduces the additional workload.

The disadvantage is that session data is stored in memory, and data will be lost once it goes down. However, session data is not a serious problem.

How to Use memcached to store sessions? The basic configuration steps are as follows:

1. Install memcached)
In the phpinfo output, "Registered save handlers" will have "files user sqlite ".

2. modify the configuration file,

①. Set global settings in php. ini (* restart the server)

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"

③. You can also:

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

Note: Multiple memcached servers are separated by commas (,), which are the same as those described in the Memcache: addServer () document, you can include additional parameters such as "persistent", "weight", "timeout", and "retry_interval:

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

3. Start memcached
Copy codeThe Code is as follows: memcached-d-m 10-u root-l 127.0.0.1-p 11211-c 256-P/tmp/memcached. pid

4. Test the creation of a session

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

5. Use sessionid to query memcached.

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

Copy codeThe Code is as follows: [root @ localhost html] #/usr/local/webserver/php/bin/php-f get_session.php
Output result:

String (16) "admin | s: 3:" wan ";"

This proves that the session works properly.

Go deep into the multi-domain website and share SESSION data using MEMCACHE.

By understanding the working principle of the SESSION, we can find that by default, each server generates a session id for the same client, for example, for the same user browser, the session id generated by server A is 30de1e9de3192ba6ce2992d27a1b6a0a, while that generated by server B is c000065af28a8b14c0fe11afe3b59b51b. In addition, the SESSION data of PHP is stored in the file system of the current server.

After confirming the problem, you can start to solve it. To share SESSION data, you must achieve two goals: one is that the SESSION IDs generated by each server on the same client must be the same and can be transmitted through the same COOKIE, that is to say, each server must be able to read the same COOKIE named PHPSESSID; the other is the SESSION data storage method/location, which must be accessible to each server. Simply put, multiple servers share the session id of the client, and must also share the SESSION data of the server.

The implementation of the first target is actually very simple. You only need to set the COOKIE domain. By default, the COOKIE domain is the domain name/IP address of the current server, if the domain is different, the cookies set by each server cannot access each other. For example, the server www.aaa.com cannot read or write the cookies set by the server www.bbb.com. The servers of the same website have their own particularity, that is, they belong to the same level-1 domain. For example, tieba.xiaoyuan.com and www.xiaoyuan.com both belong to the domain .xiaoyuan.com, then we can set the COOKIE domain to .xiaoyuan.com, so that tieba.xiaoyuan.com and www.xiaoyuan.com can all access this COOKIE. The setting method in PHP code is as follows:

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

In this way, each server shares the same client session id.

So you can go to a.domain.com

Session. php

<?phpini_set('session.cookie_domain', 'domain.com');session_start();if (!isset($_SESSION['admin'])) {  $_SESSION['admin'] = 'wan';}print $_SESSION['admin'];print "\n";print session_id();

Under B .domain.com

Session2.php

<?phpini_set('session.cookie_domain', 'domain.com');session_start();echo $_SESSION['admin'];

I hope this article will help you design php programs on the Ubuntu platform.

Related Article

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.