Home  >  Article  >  Backend Development  >  Ubuntu server 11.04 installs memcache and php uses memcache to store sessions, 11.04memcache_PHP tutorial

Ubuntu server 11.04 installs memcache and php uses memcache to store sessions, 11.04memcache_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:54742browse

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

This article describes the example of Ubuntu server 11.04 installing memcache and PHP using memcache to store sessions. . Share it with everyone for your reference, the details are as follows:

1. First install the memcache server:

sudo apt-get install memcached

After the installation is completed, the system automatically starts the memcached service occupying port 11211

If you need to reconfigure the service of port 11211, you need to close the enabled memcached service

Manual start:

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

Here we need to explain the startup parameters of the memcached service:

-p listening port
-l The IP address of the connection, the default is the local machine
-d start starts memcached service
-d restart restart memcached service
-d stop|shutdown shut down the running memcached service
-d install install memcached service
-d uninstall uninstall memcached service
-u Run as (only valid when running as root)
-m Maximum memory usage, in MB. Default 64MB
-M Return an error when memory is exhausted instead of deleting items
-c Maximum number of simultaneous connections, default is 1024
-f block size growth factor, default is 1.25 -n minimum allocated space, key value flags default is 48
-h show help

2. Install PHP Memecache client

$ sudo apt-get install php5-memcache

Restart the web server

Test memcache code:

<&#63;php
$mem = new Memcache; //创建Memcache对象
$mem->connect("127.0.0.1", 11211); //连接Memcache服务器
$val = "这是一个Memcache的测试.";
$key = md5($val);
if(($k = $mem->get($key))){ //判断是否获取到指定的key
  echo 'from cache:'.$k;
} else {
  echo 'normal'; //这里我们在实际使用中就需要替换成查询数据库并创建缓存.
  $mem->set($key, $val, 0, 120); //增加插入一条缓存,缓存时间为120s
}

Use memcache to store session

Generally, Session is stored on the server side in the form of a text file. If Seesion is used, or the PHP file wants to call the Session variable, it must be started before calling the Session, using the session_start() function. You don’t need to set anything else, PHP automatically completes the creation of the Session file. The default Session storage path is the system temporary folder of the server.

But if you encounter a session with a large amount of data, the bottleneck of using file-based session access may be disk IO operations. Now Memcached is used to save session data directly through memory, and the efficiency can naturally be improved. Quite a few. The read and write speed will be much faster than files, and it will be more convenient when multiple servers need to share sessions. Just configure these servers to use the same group of memcached servers, reducing additional workload.

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

How to use memcached to store sessions? Here are the basic configuration steps:

1. Install memcached (skip it, if you are not sure, you can check the previous article: http://www.bkjia.com/article/85510.htm)
There will be "files user sqlite" in the "Registered save handlers" in the phpinfo output.

2. Modify the configuration file,

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

session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"

②. Or .htaccess in a certain directory:

php_value session.save_handler "memcache"
php_value session.save_path "tcp://127.0.0.1:11211"

③. It can also be used in an application:

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

Note: When using multiple memcached servers, separate them with commas ",", and as explained in the Memcache::addServer() document, you can take additional parameters "persistent", "weight", "timeout", "retry_interval" etc., something like this:

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

3. Start memcached
Copy code The 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 Create a session

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

5. Use sessionid to query in memcached

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

Copy the code The 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";"

Prove that session is working properly.

Go deeper again and use MEMCACHE to share SESSION data with multiple domain name websites

By understanding the working principle of SESSION, we can find that by default, each server will generate a SESSION ID for the same client respectively. For example, for the same user browser, the SESSION ID generated by server A is 30de1e9de3192ba6ce2992d27a1b6a0a, The B server generates c72665af28a8b14c0fe11afe3b59b51b. In addition, PHP's SESSION data are stored separately in the file system of this server.

After identifying the problem, you can start to solve it. If you want to share SESSION data, you must achieve two goals: One is that the SESSION ID generated by each server for the same client must be the same and can be passed through the same COOKIE, which means that each server must be able to read the same SESSION ID. COOKIE named PHPSESSID; the other is that the storage method/location of SESSION data must ensure that each server can access it. Simply put, multiple servers share the client's SESSION ID and must also share the server's SESSION data.

第一个目标的实现其实很简单,只需要对 COOKIE 的域(domain)进行特殊地设置即可,默认情况下,COOKIE 的域是当前服务器的域名/IP 地址,而域不同的话,各个服务器所设置的 COOKIE 是不能相互访问的,如 www.aaa.com 的服务器是不能读写 www.bbb.com 服务器设置的 COOKIE 的。这里我们所说的同一网站的服务器有其特殊性,那就是他们同属于同一个一级域,如:tieba.xiaoyuan.com 和 www.xiaoyuan.com 都属于域 .xiaoyuan.com,那么我们就可以设置 COOKIE 的域为 .xiaoyuan.com,这样 tieba.xiaoyuan.com、www.xiaoyuan.com 等等都可以访问此 COOKIE。PHP 代码中的设置方法如下:

<&#63;php
ini_set('session.cookie_domain', 'xiaoyuan.com');
&#63;>

这样各个服务器共享同一客户端 SESSION ID 的目的就达到了。

于是 可以在 a.domain.com 下

session.php

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

在b.domain.com下

session2.php

<&#63;php
ini_set('session.cookie_domain', 'domain.com');
session_start();
echo $_SESSION['admin'];

希望本文所述对大家Ubuntu平台上的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133039.htmlTechArticleUbuntu server 11.04安装memcache及php使用memcache来存储session的方法,11.04memcache 本文实例讲述了Ubuntu server 11.04安装memcache及php使用memcache来存储se...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn