LNMP memcached front end data cache and session session retention
Keywords:
PHP
Nginx
Session
MySQL
1, memcached front end data cache
1. Test the lnmp environment.
[root@lnmp nginx]# netstat -lntup|egrep "nginx|php|mysql"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1401/nginx
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1503/php-fpm
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1232/mysqld
[root@lnmp nginx]# cat /application/nginx/html/www/index.php
<?php
$link_id=mysql_connect('localhost','root','000000') or mysql_error();
if($link_id){
echo "mysql successful by https://blog.csdn.net/liang_operations!";
}else{
echo mysql_error();
}
?>
[root@Memcached ~]# curl http://10.0.0.130
mysql successful by https://blog.csdn.net/liang_operations!
2. Download and install memcache
[root@lnmp ~]# wget http://pecl.php.net/get/memcache-3.0.8.tgz
[root@lnmp ~]# tar xf memcache-3.0.8.tgz
[root@lnmp ~]# cd memcache-3.0.8
[root@lnmp memcache-3.0.8]# /application/php/bin/phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@lnmp memcache-3.0.8]# ./configure --enable-memcache --with-php-config=/application/php/bin/php-config --with-zlib-dir
[root@lnmp memcache-3.0.8]# make && make install
[root@lnmp memcache-3.0.8]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
-rwxr-xr-x. 1 root root 449093 Oct 7 10:54 /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
3. Modify php.ini configuration file
[root@lnmp memcache-3.0.8]# vi /application/php/lib/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
extensio = memcache.so###increase
4. Restart nginx and php
[root@lnmp memcache-3.0.8]# pkill php-fpm
[root@lnmp memcache-3.0.8]# php-fpm
[root@lnmp memcache-3.0.8]# nginx -s reload
5. Write php connection memcached test
[root@Memcached ~]# netstat -lntup|grep mem
tcp 0 0 0.0.0.0:11212 0.0.0.0:* LISTEN 10035/memcached
tcp 0 0 :::11212 :::* LISTEN 10035/memcached
[root@lnmp memcache-3.0.8]# cat /application/nginx/html/www/index.php
<?php
$memcache = new Memcache; //Create a memcache object
$memcache->connect('10.0.0.139', 11211) or die ("Could not connect"); //Connect to Memcached server
$memcache->set('key', 'test'); //Set a variable to memory. The name is key and the value is test
$get_value = $memcache->get('key'); //Get the value of key from memory
echo $get_value;
?>
[root@lnmp ~]# curl http://10.0.0.130/index.php
test
2, memcached for web session retention
1. Modify the configuration file (all web environments)
Default / application/php/lib/php.ini configuration
Session. Save? Handler = files? In what form? The default is file.
; session.save#path = "/ tmp" ාාාාstorage location
Modify / application/php/lib/php.ini
session.save_handler = memcache
session.save_path = "tcp://10.0.0.139:11212"
Tips:
10.0.0.139:11212 is the IP and port of memcached database cache
2. restart
[root@lnmp ~]# pkill php-fpm
[root@lnmp ~]# php-fpm
Posted by mostwantedunm on Thu, 19 Dec 2019 13:51:19 -0800