1. Install memcached. apt-get is preferred for Ubuntu lazy mode. I will not talk about the installation of apachephp5. Apt-getinstallmemcachedphp5-memcache after installing the Memcache server, we need to start the service: memcached-d-m128-p11211
1. Install memcached. apt-get is preferred for Ubuntu lazy mode. I will not talk much about installing apache php5.
Apt-get install memcached php5-memcache
After installing the Memcache server, we need to start the service:
Memcached-d-m 128-p 11211-u root
Change 127.0.0.1 in etc/memcached. conf to the server address. Here is 192.168.1.105. You can create a user for memcache to run the service. The default value is nobody, so I will not create it.
Comment out extension = memcache. so in/etc/php5/apache2/conf. d/memcache. ini. Restart apache2 and check that "Registered save handlers" in phpinfo will have "files user memcache" available.
2. Then modify/etc/php5/apache2/php. ini
Session. save_handler = memcache
Session. save_path = "udp: // 192.168.0.100: 11211, tcp: // 192.168.0.101: 11211" // share multiple servers
Use commas (,) to separate multiple memcached servers.
You can also go to. htaccess:
Php_value session. save_handler "memcache"
Php_value session. save_path "tcp: // 192.168.1.100: 11211"
Or in an application:
Ini_set ("session. save_handler", "memcache ");
Ini_set ("session. save_path", "tcp: // 192.168.1.100: 11211 ");
We need to perform simple configuration in php. ini to open the/etc/php5/apache2/php. ini file and add the following content at the end:
[Memcache]
A high-performance distributed memory object cache system maintains a unified and huge hash table in the memory,
It can be used to store data in various formats, including images, videos, files, and database retrieval results.
Whether to transparently transfer data to other servers in case of errors.
Memcache. allow_failover = On
The maximum number of servers www.linuxidc.com that can be used to receive and send data is valid only when memcache. allow_failover is enabled. Memcache. max_failover_attempts = 20
Data will be transferred according to the block size set by this value. The smaller the value, the more extra network transmission is required.
If you find that the speed cannot be explained is reduced, you can try to increase this value to 32768.
Memcache. chunk_size = 8192
The default TCP port used to connect to the memcached server.
Memcache. default_port = 11211
Control the policy to map keys to the server. The default value "standard" indicates that the old hash policy of the previous version is used.
Set as "consistent" to allow you to add/delete servers in the connection pool without re-computing the ing between keys and servers.
; Memcache. hash_strategy = "standard"; controls the hash function that maps keys to the server. The default value "crc32" uses the CRC32 algorithm, while "fnv" indicates that the FNV-1a algorithm is used.
; FNV-1a than CRC32 speed is slightly lower, but the hash effect is better.
; Memcache. hash_function = "crc32 ″
Modify php. in ini, the value of extension_dir is/usr/lib/php5/20090626 + lfs/. Re-Execute php-m and you can see that the warning does not exist. At the same time, the memcache module has been installed.
3. start memcached/etc/init. d/memcached start
4. restart apache/etc/init. d/apache2 restart
After deploying apache on multiple distributed servers, place the following test code on each server:
Login. php // login page
Session_start ();
$ _ SESSION ['name'] = 'mym ';
$ _ SESSION ['pwd'] = '000000 ';
$ Session_id = session_id ();
Echo "sid:". $ session_id ."
";
Echo'
Read session ';
?>
Read. php
Session_start ();
$ Session_id = session_id ();
Echo "sid:". $ session_id ."
";
Echo 'sever: 192.168.1.100
'; // Change the ip address of another server to 192.168.1.101.
Echo 'name: '. $ _ SESSION ['name']."
";
Echo 'pwd: '. $ _ SESSION ['pwd']."
";
?>
After the front-end server is accessed by the ngnix reverse proxy: login. php, click the read. php page. We can see that the ip address of the backend server is switched cyclically and the corresponding session value is read.