Linux企业化运维--(3)PHP配置、nginx结合php-fpm、memcache模块、openresty模块以及高速缓存、tomcat结合memcache

Linux企业化运维

实验所用系统为Redhat-rhel7.6。


)

一PHP源码编译

1 下载软件:

方式1: 官网下载http://www.php.net
方式2: lftp 172.25.254.250

ls
cd pub/docs/lamp/
get php-7.4.12.tar.bz2
exit
tar jxf php-7.4.12.tar.bz2

[root@server1 ~]# ls
php-7.4.12
php-7.4.12.tar.bz2

2、软件编译

还是configure--makefile--make 三步曲,首先configure,但此时是不会成功,因为缺少依赖。

cd php-7.4.12
./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

下载依赖,提示缺什么就安装什么。

yum install -y systemd-devel
yum install -y libxml2-devel
yum install -y sqlite-devel
yum install -y libcurl-devel
yum install libpng-devel -y
yum install oniguruma-devel -y
make
make install

二、拷贝php-fpm配置文件

1、php-fpm.conf

cd /usr/local/php/etc/
ls
cp php-fpm.conf.default php-fpm.conf	
ls

2、www.conf

cd php-fpm.d/
ls
cp www.conf.default www.conf		
ls
vim /usr/local/php/etc/php-fpm.conf

对此配置文件进行更改。
在这里插入图片描述

3 php.ini

cd
cd php-7.4.12/
cp php.ini-production /usr/local/lnmp/php/etc/php.ini	
cd /usr/local/lnmp/php/etc/
vim php.ini

在这里插入图片描述

4、php-fpm.service,读取并开启服务

cd
cd php-7.4.12/
cd sapi/
ls
cd fpm/
cp php-fpm.service /usr/lib/systemd/system	
cd /usr/lib/systemd/system
vim php-fpm.service 		##php-fpm启动文件

在这里插入图片描述

systemctl daemon-reload 
systemctl start php-fpm.service

查看端口
在这里插入图片描述

三、nginx结合php-fpm

1、修改nginx配置文件

切入配置目录,编辑配置文件,注释之前的设定,取消php的注释。

cd /usr/local/nginx/conf/
vim nginx.conf

在这里插入图片描述

nginx -s reload
cd ..
cd html
ls
vim index.php

在这里插入图片描述即可在浏览器输入: 172.25.72.1/index.php 搜索到php
在这里插入图片描述

四、php添加memcache功能模块

1、软件下载

方式1: 官网:http://pecl.php.net/package/memcache
方式2: lftp 172.25.254.250

ls
cd pub/docs/lamp/
get memcache-4.0.5.2.tgz
exit

tar zxf memcache-4.0.5.2.tgz

2、添加环境变量

在这里插入图片描述

在这里插入图片描述

source .bash_profile

在这里插入图片描述安装依赖,重新执行phpize

在这里插入图片描述

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

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

./configure --enable-debug
make
make install

编辑php.ini ,然后重启服务,执行php -m可以看到memcache。
在这里插入图片描述连接phpmemcache服务
在这里插入图片描述出现memcache表示成功

systemctl reload php-fpm

在这里插入图片描述即可在浏览器输入: 172.25.72.1/index.php 搜索到memcache
在这里插入图片描述

4、添加memcache功能模块

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

yum install -y memcached
systemctl start memcached.service
netstat -antlp					##11211端口
cat /etc/sysconfig/memcached	##11211端口

在这里插入图片描述
切入memcache目录,拷贝文件并编译,最后重启服务。

cd 
cd memcache-4.0.5.2/
ls
cp example.php /usr/local/nginx/html/
cp memcache.php /usr/local/nginx/html/
cd /usr/local/nginx/html/
ls
vim memcache.php

在这里插入图片描述

nginx -s reload							##重启nginx
systemctl start php-fpm.service			##开启服务
systemctl start memcached.service		##开启服务

在真机浏览器中试访问,172.25.72.1/example.php
将在缓存中读取数据。
在这里插入图片描述
在这里插入图片描述但此时我们可以观察到,当前的信息处理率不能达到百分百,需要进行优化。在真机中执行压力测试命令,对其进行优化。

###真机
ab -c20 -n 1000 http://172.25.24.1/example.php  ##若显示没有找到ab,则下载http

在这里插入图片描述
刷新页面,可以看到信息处理率达到百分百。
在这里插入图片描述

五、配置php加载模块openresty,构建nginx高速缓存

基于openresty(构建高效透明的缓存机制) 访问,能将缓存放在nginx中,速度更快。
使用memc-nginxsrcache-nginx模块构建高效透明的缓存机制。
如果需要做到高速缓存,nginx可以跳过php直接向memcache存储,但是只能做静态存储 ,如果需要动态存储,还是要调用php,因此两种缓存策略时同时在进行的。
在这里插入图片描述

nginx -s stop		##先停止nginx服务

1、下载软件

方式1: 官网: https://openresty.org/cn/
方式2:lftp 172.25.254.250

ls
cd pub/docs/lamp
get openresty-1.19.3.1.tar.gz
exit

2、安装软件

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

3、软件配置

编辑配置文件,拷贝配置文件,重启服务。

cd /usr/local/openresty/nginx.conf
vim nginx.conf
cd ..
cd html/
cp /usr/local/nginx/html/example.php .		
cp /usr/local/nginx/html/index.php .
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload

进一步配置来提升性能。

cd /usr/local/openresty/nginx/conf
vim nginx.conf

在这里插入图片描述在这里插入图片描述
当所请求的uri以“.php”结尾时,首先到memcache中查询有没有以 u r i uri uriargs为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前以 u r i uri uriargs为key,将输入结果存入memcache。
在这里插入图片描述

/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload

真机进行测试

ab -c10 -n 50000 http://172.25.24.1/example.php
##可以看到压测速度很快,且没有报错,速度很快。
ab -c10 -n 50000 http://172.25.24.1/index.php
##传输量大幅度提升

在这里插入图片描述

六、tomcat结合memcache

1、下载软件

方式1:
方式2: lftp 172.25.254.250

cd /pub/docs/lamp
get apache-tomcat-7.0.37.tar.gz
get jdk-8u121-linux-x64.rpm
exit

rpm -ivh  jdk-8u121-linux-x64.rpm
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local
cd  /usr/local
ls
ln -s apache-tomcat-7.0.37/ tomcat
ll
cd tomcat
bin /startup.sh

在真机浏览器中试访问,172.25.72.1:8080 即可访问到tomcat
在这里插入图片描述

二、负载均衡与反向代理

在server1中设定反向代理,则可以通过server1访问到server2主机的tomcat。

cd /usr/local/nginx/conf
vim nginx.conf

在这里插入图片描述
在这里插入图片描述

nginx -t
nginx -s reload

在真机浏览器中试访问,172.25.72.1/index.jsp 即可访问到
在这里插入图片描述下载一个jsp测试页,在网页上对tomcat进行操作时,可以在日志中查看到tomcat的记录。
先下载测试页,在浏览器中进行访问并测试。

cd usr/local/tomcat/webapps/ROOT
lftp 172.25.254.250
cd /pub/docs/lamp
get test.jsp
quit

在真机浏览器中试访问,172.25.72.1/test.jsp 即可访问到
在这里插入图片描述如果将server1主机中的tomcat服务停掉,则不能通过server1访问。

cd /usr/local/tomcat
bin/shutdown.sh		##关闭服务

在这里插入图片描述将tomcat复制到server2主机中,并解压,开启服务

server1
cd /usr/local/
scp -r tomcat/ server3:/usr/local/		##复制到server2主机
scp jdk-8u121-linux-x64.rpm server3:

server2
rpm -ivh jdk-8u121-linux-x64.rpm
cd /usr/local/tomcat
bin/startup.sh							##开启服务

在server1机的nginx配置文件中设定负载均衡与反向代理,重启服务。

在这里插入图片描述

nginx -s reload

在浏览器中新建一个用户user3,然后关闭server1主机的tomcat。

##真机浏览器
172.25.24.1/test.jsp
user3=333

###server1
bin/shutdown.sh	

但是用户并不知道server1关闭,nginx将自动调度server2主机进行信息存储。此时服务器为server2,因为server1与server2信息不共享,因此信息消失。
在这里插入图片描述

三、tomcat结合memcache交互存储

tomcat本身存储所有的信息,memcache只是为了session共享,是tomcat的额外存储。

nginx调用其中一台主机的tomcat,并将信息交叉存储在另一台主机的memcache中,即就是,tomcat1将信息存在memcache2中。

如果tomcat1不能使用,nginx则自动调度tomcat2,此时将信息存储在本机memcache2中,信息此时是完整的。如果tomcat1恢复,则重新调度tomcat1,将信息继续存储在memcache2中,信息依旧完整。

如果memcache2不能使用,tomcat1将把信息存放在memcache1中,此时信息仍然完整,因为,tomcat1本身将所有的信息存储在本机memory中,memcache只是为了额外存储。

这样,无论什么情况,都最大限度的保证了数据的完整性,且信息同步共享。
在这里插入图片描述分别在server1和server2中安装并启用memcache。

server1
yum install -y memcached
systemctl start memcached

server2
yum install -y memcached
systemctl start memcached

在server1主机中对tomcat的配置文件进行编辑,设定交叉存储,将自身memcache设为n1,server2主机的memcache设为n2,正常运行时将信息存放在n2,不能正常运行时,将信息存储在n1,即自身memcache。

server1:
cd /usr/local/tomcat/conf/
ls--->context.xml
vim context.xml

在这里插入图片描述

lftp 172.25.254.250
> cd pub/docs/lamp/jar
> mget *
> exit
ls
rm -rf memcached-session-manager-tc6-1.6.3.jar	##版本需对应,这个不对应
cd ..
bin/startup.sh

在真机浏览器里进行测试,此时tomcat2将信息存放在memcache2,即n1。

###server2
tail -f logs/catalina.out ##INFO:[n1][n2]
telnet localhost 11211–>get xxx-n1 ##可以看到用户建立记录

server2:
cd /usr/local/tomcat/conf/
ls—>context.xml
vim context.xml

在这里插入图片描述server1:
完全关闭server1 再次建立用户
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值