current position:Home>Lamp Architecture - extend the Memcache module of PHP and build nginx cache

Lamp Architecture - extend the Memcache module of PHP and build nginx cache

2022-01-26 21:57:38 Love Linux

expand php modular memcache

[[email protected] lamp]# scp memcache-4.0.5.2.tgz server1:/mnt/   Copy memcache-4.0.5.2.tgz To server1 Of mnt in 
[[email protected] ~]# cd /mnt/   Get into 
[[email protected] mnt]# tar zxf memcache-4.0.5.2.tgz    Unzip the file 
[[email protected] mnt]# cd memcache-4.0.5.2/   Go to unzip directory 
[[email protected] memcache-4.0.5.2]# ls    Found no configure Execute the script 
cloudbuild.yaml  config.m4   CREDITS  Dockerfile   LICENSE       php7    tests
config9.m4       config.w32  docker   example.php  memcache.php  README
[[email protected] memcache-4.0.5.2]# phpize   So run phpzie, Generate execution script 

 Insert picture description here

[[email protected] memcache-4.0.5.2]# yum install autoconf -y   install 
[[email protected] memcache-4.0.5.2]# phpize   Re execution phpize  Complete precompiled environment ,  Pay attention to the definition when executing this command php Starting route /usr/local/php/bin
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
[[email protected] memcache-4.0.5.2]# ls   It can be found that configure

 Insert picture description here

[[email protected] memcache-4.0.5.2]# ./configure --help  You can view help 
[[email protected] memcache-4.0.5.2]# ./configure --enable-memcache   perform , The following parameters are OK without adding 
[[email protected] memcache-4.0.5.2]# make   compile 
[[email protected] memcache-4.0.5.2]# make install   install 
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/    

 Insert picture description here

[[email protected] etc]#  cd /usr/local/php/etc/   Get into php Main configuration directory 
[[email protected] etc]# vim php.ini    Edit the main configuration file 

 Insert picture description here

[[email protected] etc]# systemctl reload php-fpm.service    Overload service 
[[email protected] etc]# php -m | grep memcache  -m To list php All modules 
memcache    The module already exists 
[[email protected] etc]# yum install memcached -y   install memcached
[[email protected] ~]# systemctl start memcached   start-up 
[[email protected] ~]# netstat -antlp | grep memcache   Check the port , The port has been opened as 11211
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      7047/memcached      
tcp6       0      0 :::11211                :::*                    LISTEN  
[[email protected] ~]# cd /mnt/memcache-4.0.5.2/  Enter the original code directory 
[[email protected] memcache-4.0.5.2]# cp example.php  /usr/local/nginx/html/   take example.php Copy to the default release directory 

visit 172.25.50.1/example.php , Access to
 Insert picture description here

[[email protected] memcache-4.0.5.2]# cp memcache.php /usr/local/nginx/html/    The page will be monitored memcache.php Put it in nginx Default publishing directory 
[[email protected] memcache-4.0.5.2]# cd /usr/local/nginx/html/
[[email protected] html]# vim memcache.php     edit 

 Insert picture description here
visit 172.25.50.1/memcacahe.php
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
The above is how to expand php Methods , Download other modules Online , Then do the same

structure nginx Cache

Traditional cache strategy
nginx Can only handle static ---- Dynamic handover fastcgi ----> Submit to backend php-fpm 9000 When processing, from memcache take hand nginx ---- Return to client
But there are disadvantages :nginx High concurrency cannot be reflected , because nginx Processing dynamic pages should be handed over to the back end php,php After processing , To feed back to nginx
 Insert picture description here
test : Pressure measurement under traditional cache mode

[[email protected] ~]# ab -c10 -n5000 http://172.25.50.1/example.php

 Insert picture description here

Efficient caching :
So we can take memcache In front of , Give it directly to nginx , In this way, the efficiency will be greatly improved , Here's the picture :
 Insert picture description here

How to achieve efficient caching , need nginx There are two modules memc+srcache, Recommended now nginx Another distribution openresty

[[email protected] html]# systemctl stop nginx.service    Stop first nginx , because nginx and openresty Ports are all 80, There will be conflicts 
[[email protected] lamp]# scp openresty-1.19.3.1.tar.gz server1:/mnt   Will be downloaded openresty Package copy to server1 Of mnt in 
[[email protected] mnt]# tar zxf openresty-1.19.3.1.tar.gz    decompression 
[[email protected] mnt]# cd openresty-1.19.3.1/   Enter the unzipped directory 
[[email protected] openresty-1.19.3.1]# ls
bundle  configure  COPYRIGHT  patches  README.markdown  README-windows.txt  util
[[email protected] openresty-1.19.3.1]# ./configure --prefix=/usr/local/openresty --with-http_ssl_module --with-http_stub_status_module  perform 
[[email protected] openresty-1.19.3.1]# make   compile 
[[email protected] openresty-1.19.3.1]# make  install   install 
[[email protected] ~]# cd /usr/local/openresty/nginx/conf/   Go to configuration directory 
[[email protected] conf]# vim nginx.conf   Edit profile 

 Insert picture description here
 Insert picture description here

[[email protected] conf]# /usr/local/openresty/nginx/sbin/nginx  -t   Check grammar    
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful     success 
[[email protected] conf]# /usr/local/openresty/nginx/sbin/nginx   function nginx
[[email protected] conf]# cd ..
[[email protected] nginx]# cd html/   Get into nginx Default publishing directory 
[[email protected] html]# vim index.php   establish php home page 
<?php
phpinfo()
?>

visit 172.25.50.1/index.php, Access to success
 Insert picture description here

[[email protected] nginx]# cd conf/   Get into nginx The configuration directory 
[[email protected] conf]# vim nginx.conf   Edit profile 

 Insert picture description here
 Insert picture description here

          }

 Insert picture description here
principle : First call srcache_fetch In their own mecache Locate key, See if there is any data required by users , If there are some, it's directly from memcache take , If not, go through fastcgi_cgi Call back-end php selection , Call after retrieving srcache_store Upload to memcache, Directly in the next call memcache selection

[[email protected] conf]# /usr/local/openresty/nginx/sbin/nginx -t   Check grammar , success 
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[[email protected] conf]# /usr/local/openresty/nginx/sbin/nginx -s reload   Overload service 
[[email protected] conf]# cd ..
[[email protected] nginx]# cd html/   Enter the default publishing directory 
[[email protected] html]# ls
50x.html  index.html  index.php
[[email protected] html]# cp /usr/local/nginx/html/example.php .   Copy 

visit 172.25.50.1/example.php, success
 Insert picture description here

[[email protected] Desktop]# ab -c10 -n5000 http://172.25.50.1/example.php   Pressure test 

 Insert picture description here
As you can see, efficient cache accesses per second 12994.24, Traditional cache accesses per second 1865 individual , Improved 10 times

copyright notice
author[Love Linux],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262157201560.html

Random recommended