Centos5.2 use memcache for PHP session. save_handler

Source: Internet
Author: User
Tags php session

Recently, my website access has been on the rise, because dynamic member game websites have been trying to optimize some methods. Today I think that if I put the PHP session in the memory, it may increase the Website access speed. So I began searching for materials. After a day of exploration, we finally got it done. The main problem is that PHP has two memcache modules: PHP-memcache, PHP-memcached, and PHP-memcached, the information on the internet is about PHP-memcache, but does not mention PHP-memcached. The configuration of PHP-memcached is different from that of PHP-memcache. The entire installation process is roughly as follows:

1. Install libevent 1.4.10

Wget http://monkey.org /~ Provos/libevent-1.4.10-stable.tar.gz

Tar xzvf libevent-1.4.9-stable.tar.gz
CD libevent-1.4.10-stable

./Configure -- prefix =/usr
Make & make install

 

2. Install memcached1.2.8

Wget http://memcached.googlecode.com/files/memcached-1.2.8.tar.gz

Tar-zxvf memcached-1.2.8.tar.gz

CD memcached-1.2.8
./Configure -- With-libevent =/usr

Make & make install

 

3. Install zlib

Wget http://www.zlib.net/zlib-1.2.3.tar.gz

Tar-zxvf zlib-1.2.3.tar.gz
CD zlib-1.2.3
./Configure -- prefix =/usr -- shared
Make & make install

4. Install libmemcached

Wget http://download.tangent.org/libmemcached-0.26.tar.gz

Tar zxvf libmemcached-0.26.tar.gz

CD libmemcached-0.26
./Configure -- prefix =/usr/local/libmemcached/

Make & make install

 

5. Install the PHP-memcached Module

Wget http://pecl.php.net/get/memcached

Tar-zxvf memcached-0.1.5.tgz

CD memcached-0.1.5

Phpize
./Configure -- With-PHP-Config =/usr/bin/PHP-config -- enable-memcache -- With-libmemcached-Dir =/usr/local/libmemcached

Make & make install

6. Start memcached.

/Usr/local/bin/memcached-P 11211-l 127.0.0.1-d

After execution, you can view the execution status in the following ways:

Telnet fig 11211

Stats

Stat PID 11568
Statuptime 5910
Stat time 1241876672
Stat version 1.2.8
Stat pointer_size 32
Stat rusage_user 0.751885
Stat rusage_system 1.584759
Stat curr_items 2615
Stat total_items 36753
Stat bytes 244835
Stat curr_connections 3
Stattotal_connections 18427
Stat connection_structures 36
Stat 1__flush 0
Stat performance_get 18388
Stat performance_set 38334
Stat get_hits 15717
Stat get_misses 2671
Stat evictions 0
Stat bytes_read 4924864
Stat bytes_written 1992526
Stat limit_maxbytes 134217728
Stat threads 2
Stat accepting_conns 1
Stat listen_disabled_num 0
End
If the above information is displayed, it indicates that the startup is successful!

 

7. Add to boot

Nano/etc/rc. d/local. d
Add a row

/Usr/local/bin/memcached-P 11211-l 127.0.0.1-d

8. Edit PHP. ini.

Nano/etc/PHP. ini

Add at the end of the file:

Extension = "memcached. So"
Modify

Session. save_handler = files-> session. save_handler = memcached
Session. save_path = "/var/lib/PHP/session"-> session. save_path = "Fig: 11211"

9. Restart Apache

Service httpd restart

 

Success!

 

Note the following points,

1. To install libmemcached, you need to use zlib. I don't have this on the machine. I spent a lot of time looking for the cause of the error when compiling libmemcached.

2. For PHP. ini file configuration, set the PHP-memcached module to memcached, which is different from PHP-memcache. You can see this on the registered save handlers page of phpinfo. If the settings are incorrect, an error message "cannot find save handler memcache" is displayed.

3. Session. almost all information in save_path is session. save_path = "TCP: // 127.0.0.1: 11211", but this may be used in memcaceh. I didn't verify it. I set it in the first place, however, the error "failed to write session data (memcached)" has always occurred. If I find it, I cannot find the cause. Later I found it on php.net.

Session. save_pathString

Defines a comma separated of hostname: port entries to use for session server pool, for example& Quot; sess1: 11211, sess2: 11211 & quot".

In this section, we removed TCP: // and tried again to finally succeed!

 

Finally, we recommend that you do not directly change PHP. ini because the website is always running,

Use a PHP file for testing.

 

<?
Ini_set ("session. save_handler", "memcached ");
Ini_set ("session. save_path", "Fig: 11211 ");

Session_start ();
If (! Isset ($ _ session ['test']) {
$ _ Session ['test'] = Time ();
}
$ _ Session ['test3'] = Time ();
Print $ _ session ['test'];
Print "<br> ";
Print $ _ session ['test3'];
Print "<br> ";
Print session_id ();
?>

 

As long as there is no error message in this file, you can directly modify the corresponding content of PHP. ini, and then restart Apache, it will not affect Member access to the website.

 

 

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.