LNMP+memcached搭建流程

LNMP+memcached整合及高可用配置指南
本文档详细介绍了如何在Linux环境中搭建LNMP(Nginx、PHP、MariaDB)并结合memcached实现高可用和分布式缓存。步骤包括安装memcached、配置Nginx的负载均衡、在后端web服务器上搭建LNMP环境以及在PHP中设置Session共享。通过代理服务器和多台web服务器,以及memcached的使用,提高了动态web应用的响应速度。

#!/bin/bash
#LNMP+memcached
#memcached是高性能的分布式缓存服务器 用来集中缓存数据库查询结果,减少数据库访问次数,用以提高中动态web应用的响应速度
#LNMP是主流网站搭建架构 即 nginx php linux mariadb
#代理服务器上proxy上安装memcached 最少需要三台 一代理服务 两台后面web 来做高可用 可以试着用代理服务本机做客户端

yum -y install memcached
#安装包
systemctl start memcached
systemctl status memcached
ss -pluatn | grep memcached
#开启服务 查看端口
setenforce 0
firewall-cmd --set-default-zone=trusted

#关闭SELinux、防火墙
yum -y install telnet
#telnet连接服务器测试memcached服务器 可装可不装
vim /usr/local/nginx/conf/nginx.conf
upstream webs {
server 192.168.2.100:80;
server 192.168.2.200:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webs;
root html;
index index.php index.html index.htm;
}
}

#Nginx配置文件中,通过upstream定义后端服务器地址池,默认调度策略为轮询,使用proxy_pass调用upstream定义的服务器地址池:
/usr/local/nginx/sbin/nginx -s reload
重新加载服务
测试页面是否成功 curl http://192.168.4.5/index.html

#!/bin/bash
#在后端web服务上装基础包,搭建LNMP环境 详细的在LNMP环境一健安装有
yum -y install gcc openssl-devel pcre-devel zlib-devel
yum -y install mariadb mariadb-server mariadb-devel
yum -y install php php-mysql
yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm
systemctl start mariadb php-fpm
vim /usr/local/nginx/conf/nginx.con
location / {
root html;
index index.php index.html index.htm;
}
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME d o c u m e n t r o o t document_root documentrootfastcgi_script_name;
include fastcgi.conf;
}

#修改配置文件 几个web就要改几次
/usr/local/nginx/sbin/nginx -s reload
#重新加载nginx服务
**vim /usr/local/nginx/html/test.php

<?php $memcache=new Memcache; //创建memcache对象 $memcache->connect('192.168.2.5',11211) or die ('could not connect!!'); $memcache->set('key','test'); //定义变量 $get_values=$memcache->get('key'); //获取变量值 echo $get_values; ?>**

这个是测试脚本 测试服务是否成功 有内容显示 firefox http://192.168.2.100/test.php

yum -y install php-pecl-memcache
#后端LNMP服务器上部署Session共享 php-pecl-memcache 是PHP扩展模块
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://192.168.2.5:11211”
#Session信息存储在公共的memcached服务器上,主机参数中为memcache(没有d)
#过path参数定义公共的memcached服务器在哪(服务器的IP和端口)

systemctl restart php-fpm
重启服务

#我这里用的是LNMP 如果想用LAMP 可以把nginx服务停掉 然后安装HTTP软件包 然后开启服务 如果HPPD服务开启不了 可能是端口被抢占了 可以查看端口信息
systemctl stop nginx
yum -y install httpd
systemctl restart httpd
ss -ntplu | grep :80

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值