Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

发布时间:2022-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

目录

PHP介绍

1.php源码编译

php下载

 软件编译

 2.php-FPM文件的配置

3.nginx+php-fpm 

 4.在Php中添加memcache模块

软件下载

构建nginx高速缓存,添加memcache功能模块 

5.配置php加载模块oPEnresty

下载模块

 配置模块文件信息

6.tomcat结合memcache实现Session交叉存放 

7.goaccess日志可视化 


PHP介绍

PHP,全称 Hypertext PReprocessor ,中文翻译“超文本预处理器”。被广泛应用的开通用脚本语言,尤其适用于 Web 开发。

1、php开源免费,没有任何收费的项目。你可以随便修改他的内核然后用于自己的商业项目。 2.、PHP拥有丰富的开发组件 3、社区成熟,85%以上的问题能在社区找到相应的解决思路。 4、PHP学习成本低、见效快 5、开源库丰富,基本所有类型均有开源产品 6、PHP7.0基本不输任何一门语言——这是重点!!! 7、与数据库MySQL近乎完美的支持

1.PHP源码编译@H_126_43@

php下载

http://www.php.net/ ##官方下载地址

拿到软件包并解压,如果因为缺少插件解压失败,安装bz2压缩插件即可

tar jxf php-7.4.12.tar.bz2

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 软件编译

./configure 配置 make 编译 make install 安装 编译三部曲

./configure --prefix=/usr/local/lnmp/php --wITh-config-file-path=/usr/local/lnmp/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mySQLnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-SOAp --enable-inline-optimization --enable-XMl --enable-ftp --enable-gd --enable-exif --enable-mbstring  --enable-bcmath --with-fpm-systemd

编译过程中会提示缺少相关包

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

根据提示安装即可,因为rh-7.6源中缺少下面两个包,所以从外部下载

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

yum install Systemd-devel libxML2-devel sqlite-devel libcurl-devel libpng-devel oniguruma-devel -y
大概缺少这几个,每个人都不尽相同

如果不确定配置是否成功, 可以输入F1a; echo $? 进行查看,输出为0则成功

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 配置成功,编译安装 make 编译成功

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

make install 安装完成 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 2.php-fpm文件的配置

拷贝配置php-fpm.conf和www.conf两个文件

注释: php-fpm php-fpm是一个实现了Fastcgi的程序,他的管理对象是PHP-CGI php-cgi php-cgi只是个CGI程序,他自己本身只能解析请求,返回结果,不会进程管理 CGI CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者 Fastcgi Fastcgi是用来提高CGI程序性能的(标准的CGI对每个请求都会执行这些步骤,Fastcgi会先启一个master,解析配置文件,初始化执行环境,然后再启动多个worker。当请求过来时,;master会传递给一个worker,然后立即可以接受下一个请求。这样就避免了重复的劳动,效率自然是高)

#fastcgi是一个协议,php-fpm实现了这个协议

cd /usr/local/lnmp/php/etc/  
cp php-fpm.conf.default php-fpm.conf  #拷贝文件
vim php.fpm.conf   #编辑配置文件
cd php-fpm.d/
cp www.conf.default www.conf

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

拷贝php.ini配置文件 

cd /root/php-7.4.12/
cp php.ini-production /usr/local/lnmp/php/etc/php.ini  #拷贝到指定路径
cd /usr/local/lnmp/php/etc
vim php.ini   #编辑文件,更改时区为亚洲上海

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 配置php-fpm.service 添加service服务

cd /root/php-7.4.12/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system  #将php服务文件放置到指定目录下
cd /usr/lib/systemd/system
ls
vim php-fpm.service  
\
#ProtectSystem=full    #注释这一行
\
systemctl daemon-reload  #重载服务
systemctl start php-fpm.service  #开启

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

3.nginx+php-fpm 

修改nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf 取消php服务的这几行注释,并且注释其中fastcgi_param这一行,最后的fastcgi文件改为fastcgi.conf

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 编写一个php发布文件,重启nginx

cd /usr/local/nginx/html/
vim index.php
///
<?php
phpinfo()
?>
///
nginx -s reload

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

真机浏览器访问server1可以看到php页面 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

添加环境变量 

vim .bash_profile	##在.bash_profile文件添加文件路径
source .bash_profile 		##使其生效
echo $PATH

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 4.在php中添加memcache模块

软件下载

下载、解压 tar zxf memcache-4.0.5.2.tgz

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 切换环境到memcache-4.0.5.2目录中,执行phpize

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

下载依赖性软件,再次执行phpize 

yum install autoconf automake.noarch -y
phpize			##扩展成功

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

对memcache进行源码编译,configure–make–make install 三步曲

./configure --enable-debug
make
make install

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 编辑php.ini ,然后重启服务,执行php -m可以看到memcache

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

构建nginx高速缓存,添加memcache功能模块 

使用Memc-nginx模块构建高效透明的缓存机制。 如果需要做到高速缓存,nginx可以跳过php直接向memcache存储,但是只能做静态存储 ,如果需要动态存储,还是要调用php,通过php调用到数据后下一次nginx就可以直接从memcache那里取,效率大大提高。因此高速缓存时两种策略在同时进行。 

安装memcached,并开启服务,查看端口

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

切换环境到memcache目录下,拷贝文件并编译,最后重启服务 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 在真机浏览器测试,访问172.25.35.1/example.php

用户名、密码为刚设置的root、root

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

当前的信息处理率不能达到百分百,需要进行优化 

真机中执行压力测试命令 

ab -c20 -n 1000 http://172.25.35.1/example.php

刷新页面,可以看到经真机的压力测试,信息处理率达到百分百

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

5.配置php加载模块openresty

基于openresty(构建高效透明的缓存机制) 访问,能将缓存放在nginx中,速度更快

注意: nginx -s stop ## 先停止nginx

下载模块

官方下载地址:https://openresty.org/cn/,下好后解压编译安装

tar zxf openresty-1.19.3.1.tar.gz  
cd openresty-1.19.3.1/
./configure --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make
make install

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 配置模块文件信息

编辑openresty中nginx的配置文件,拷贝配置文件,重启模块

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 在真机中压力测试

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

速度变快且传输量大幅度提升 

6.tomcat结合memcache实现session交叉存放 

 session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session对象存储特定用户会话所需的属性及配置信息,当用户在应用程序的 Web 页之间跳转时,存储在 Session对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。

为了对数据的保存更加牢靠,我们选择交叉存放session的方法,即将访问tomcat1上的数据存放在memcache2中,将访问tomcat2上的数据存放在memcache1中,这样存放,当某个tomcat服务断了之后,访问它的客户session并不会消失,而是存放在了对立的memcache,如果存放session的memcache坏了,那么它的数据会立即切换到另一个memcached中…

 tomcat下载地址:https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src/master/

在server2和server3主机中操作: 下载apache-tomcat-7.0.37.tar.gz和 jdk-8u121-linux-x64.rpm安装包 

在server2中:///
tar zxf apache-tomcat-7.0.37.tar.gz  #解压
rpm -ivh  jdk-8u121-linux-x64.rpm  #安装rpm包
mv apache-tomcat-7.0.37 /usr/local/ #移动目录到指定路径下
cd /usr/local/
ln -s apache-tomcat-7.0.37/ tomcat  #方便起见建立tomcat软连接
cd /usr/local/tomcat/webapps/ROOT/
lftp 172.25.254.250  #连接服务器下载文件 
cd /pub/docs/lamp
get test.jsp    #下载一个.jsp文件,用于后面测试
cd /usr/local/tomcat/bin
ls    #bin目录中有启动和停止tomcat的脚本
./startup.sh  #启动tomcat
\
在server3中可以不必下载,直接从server2中scp传输即可
相同的操作,最终启动tomcat 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

通言的步骤在server3上操作一遍

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 在真机上访问server1

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

配置server1主机中的nginx.conf配置文件 

vim /usr/local/nginx/conf/nginx.conf 利用sticky模块,将server2,server3的8080端口写入

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

此时可以在浏览器中访问172.25.7.1/test.jsp 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

在server2中,配置tomcat 交叉存储,所以我们的两台tomcat(server2、server3)分别对应的是memcache (n2、n1

cd /usr/local/tomcat/conf
vim context.xml

 正常运行的情况下server2中的tomcat往n2中储存,server3中的tomcat往n1中储存,如下图,在server2中的储存策略 failoverNodes=“n1” 当交叉存储失败时server2与n1连接

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

cd /usr/local/tomcat/lib下
lftp 172.25.254.250
cd /pub/docs/lamp/jar
mget *      #下载所有tomcat所匹配的jar包
exit
ls
rm -fr memcached-session-manager-tc6-1.6.3.jar    #删除6版本的memcache
cd /usr/local/tomcat/bin
./startup.sh    #启动

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 重新启动tomcat,再在浏览器中访问(U盘损坏数据丢失,以下实验结果用另一个ip)

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

浏览器显示的是server3中的tomcat2,由于我们用的是sticky模块,即当server3中的tomcat2宕掉时,server2中的tomcat1会接手 这里我们可以停掉serve3中的tomcat 

/usr/local/tomcat/bin/shutdown.sh   #关闭tomcat2

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

在server3中 配置tomcat,与server2一样配置 

在server2、server3中都下载telnet.x86_64         yum install telnet.x86_64 -y

在浏览器中输入几组名字密码 用tenet指令在对应的tomcat上查看 此处访问到的是server3的tomcat,默认储存在了n1中,n1的memcache是server2中的设备,故应该在server2中才能查看到我们输入的两组数据 

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

#这里我们分别在servre2 server3中输入指令进行查看
telnet localhost 11211
get 76227198A7AD4E5BB850EE59580fcC5E-n1    #此处的id号是浏览器中显示的

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

只有在server2中才可以查看到结果 

将server2中的memcache停掉         systemctl stop memcache.service

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

 浏览器中server3的设备自动变换为了n2,此时我们可以看到,当server3交叉存储失败的时候将会存储在自己主机中的n2,memcache中 此时的信息我们应该在server3中的memcache访问

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

成功查到新输入的数据 这就是nginx+tomcat+memcache实现session交叉存放的原理

7.goaccess日志可视化 

下载地址: http://tar.goaccess.io/goaccess-1.4.tar.gz

lftp 172.25.254.250
cd pub/docs/lamp
get goaccess-1.4.tar.gz
tar zxf goaccess-1.4.tar.gz   #解压
./configure --enable-utf8 --enable-geoip=legacy   #安装测试
测试缺少两个插件,需要安装
lftp 172.25.254.250
cd pub/docs/lamp
get GeoIP-devel-1.5.0-13.el7.x86_64.rpm  
exit
rpm -ivh  GeoIP-devel-1.5.0-13.el7.x86_64.rpm 
yum install ncurses-devel.x86_64    ##安装这两个缺少的插件
重新configure
make 
make install

cd /usr/local/nginx/LOGs
ls
goaccess access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html   ##监控access.log日志

真机中压力测试,增加请求量
ab -c10 -n 1000 http://172.25.7.1/index.html    #增加1000访问
刷新

浏览器访问172.25.35.1/report.html 可以看到可视化日志

Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放

脚本宝典总结

以上是脚本宝典为你收集整理的Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放全部内容,希望文章能够帮你解决Linux企业运维 6.4 -- nginx+php、tomcat+memcache交叉存放所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。