当前位置:首页 > Web开发 > 正文

主机参数中为memcache(没有d) //通过path参数定义公共的memcached服务器在哪(服务器的IP和端口)

2024-03-31 Web开发

proxy   10.10.11.10
client  10.10.11.11
web1    10.10.11.12
web2    10.10.11.13

proxy:
1.构建memcached处事
]# yum -y install memcached
]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
]# systemctl start memcached
]# systemctl enable memcached
]# netstat -anptu | grep memcached
 
2.使用telnet访谒memcached处事器
]# yum -y install telnet
]# telnet 10.10.11.10 11211
##提示:0暗示不压缩,180为数据缓存时间,3为需要存储的数据字节数量。
set name 0 180 3 # 界说变量,变量名称为name
aaa              # 输入变量的值,值为aaa               
STORED
get name         # 获取变量的值
VALUE name 0 3   # 输出功效
aaa
END
 
add myname 0 180 10      # 新建,myname不存在则添加,存在则报错
wwwwwwwwww
STORED

set myname 0 180 5       # 添加或替换变量
wwwww
STORED

replace myname 0 180 3   # 替换,如果myname不存在则报错
ttt
STORED

get myname               # 读取变量
ttt
END

append myname 0 180 5   # 向变量中追加数据
ooooo
STORED
get myname
VALUE myname 0 8
tttooooo

delete myname  # 删除变量
DELETED

stats          # 检察状态
flush_all      # 清空所有
OK

quit           # 退出登录                                  
 
3.LNMP+memcached(web1 web2)
3.1 部署nginx(前面有)
3.2 部署mariadb
]# yum -y install mariadb mariadb-server mariadb-devel
]# systemctl start  mariadb
]# systemctl enable mariadb
]# mysqladmin -uroot -p password "123456"
]# mysql -uroot -p123456

3.3 部署PHP
]# yum -y install php php-mysql php-fpm php-pecl-memcache
]# systemctl start php-fpm
]# systemctl enable php-fpm

nginx开启php
]# vim /etc/nginx/conf.d/default.conf
...
location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
    }
...
location ~ \.php$ {
        root           /usr/share/nginx/html;   #绝对路径
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
]# nginx -s reload

连接memcache数据库
]# vim /usr/share/nginx/html/test.php
<?php
$memcache=new Memcache;             # 创建memcache东西
$memcache->connect(‘10.10.11.10‘,11211) or die (‘could not connect!!‘);
$memcache->set(‘key‘,‘test‘);       # 界说变量
$get_values=$memcache->get(‘key‘);  # 获取变量值
echo $get_values;
?>

client检测:
]# firefox
 
4.PHP的本地Session信息
通过Nginx调理器负载后端两台Web处事器
部署Nginx为前台调理处事器
调理算法设置为轮询
后端为两台LNMP处事器
部署测试页面,检察PHP本地的Session信息
 
4.1 proxy搭建nginx(前面有)
web1
]# echo "web1" > /usr/share/nginx/html/index.html
web2
]# echo "web2" > /usr/share/nginx/html/index.html
4.2 7层调理
]# vim /etc/nginx/nginx.conf
.. ..
http {
.. ..
#使用upstream界说后端处事器集群,集群名称任意(如webserver)
#使用server界说集群中的具体处事器和端口
upstream webserver {
       server 10.10.11.12:80;
       server 10.10.11.13:80;
        }
.. ..

]# vim /etc/nginx/conf.d/default.conf
server {
        listen     80;
        server_name  ;
#通过proxy_pass将用户的请求转发给webserver集群
        location / {
            proxy_pass ;
        }
...
]# nginx -s reload

client测试:(默认轮询)
]# curl
web1
]# curl
web2
]# curl
web1
]# curl
web2
 
4.3 部署测试页面(session)
web1 web2
]# cd php-memcached-demo
]# cp -a * /usr/share/nginx/html/
]# ls
50x.html  images      index.php  README.md  
home.php  index.html  login.php  style.css
web1:(proxy轮询,client便利检察是哪台处事器)
]# vim index.php  --> <body bgcolor="red">
]# vim home.php   --> <body bgcolor="red">
web2:
]# vim index.php  --> <body bgcolor="blue">
]# vim home.php   --> <body bgcolor="blue">
真机goole chrome检察:
(F5刷新检察登陆页面轮询)
输入账户、暗码(2次,以便调理器在两台web处事器都存储session信息)
登陆后:(F5刷新检察登陆页面轮询)
 
5. PHP实现session共享
沿用4,通过改削PHP-FPM配置文件,实现session会话共享.
配置PHP使用memcached处事器共享Session信息.
客户端访谒两台差此外后端Web处事器时,Session 信息一致.
在操练三拓扑的根本上,Nginx处事器除了承当调理器外,,还需要负担卖力memcached数据库的角色,并在两台后端LNMP处事器上实现PHP的session会话共享。
5.1 部署memcache
]# yum -y install memcached
]# systemctl start memcached
]# systemctl enable memcached
]# netstat -anptu | grep memcached
5.2 在后端LNMP处事器上部署Session共享
web1 web2
]# vim /etc/php-fpm.d/www.conf  //文件的最后2行
 
改削前效果如下:
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
//原始文件,默认界说Sessoin会话信息本地计算机(默认在/var/lib/php/session)
 
改削后效果如下:
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://10.10.11.10:11211"
//界说Session信息存储在大众的memcached处事器上,主机参数中为memcache(没有d)
//通过path参数界说大众的memcached处事器在哪(处事器的IP和端口)
]# systemctl restart php-fpm
 
真机goole chrome检察:
(F5刷新检察登陆页面轮询)

如果呈现错误,检察php日志:
]# ls /var/log/php-fpm/
error.log  www-error.log

16_构建memcached处事、LNMP+memcached、PHP的本地Session信息、PHP实现session共享

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32129.html