Lnmp架构之PHP配置,nginx结合php-fpm,添加memcache功能模块

一.PHP源码编译

软件下载:https://www.php.net/

	 # tar jxf php-7.4.6.tar.bz2   #解压软件包

在这里插入图片描述

软件编译:

 # cd php-7.4.6
 # ./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  -y libpng-devel 
yum install  -y   oniguruma-6.8.2-1.el7.x86_64.rpm  oniguruma-devel-6.8.2-1.el7.x86_64.rpm

安装完依赖性重新编译./configure,再执行make`:

./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
make 
make install

在这里插入图片描述

二.拷贝php-fpm配置文件
1、php-fpm.conf
cd /usr/local/
cd php
cd etc/
ls
cp php-fpm.conf.default php-fpm.conf	
ls
vim php-fpm.conf       #更改配置文件

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

2. www.conf
 cd php-fpm.d/
 ls
 cp www.conf.default www.conf
 ls

在这里插入图片描述

3.php.ini
cd
cd php-7.4.12/
ll php.ini-production 
cp php.ini-production /usr/local/php/etc/php.ini
cd /usr/local/lamp/php/etc/
ls
vim php.ini    #更改时区

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

4.php-fpm.service
cd
cd php-7.4.12/
ls
cd sapi/
ls

在这里插入图片描述

cd fpm/
cp php-fpm.service /usr/lib/systemd/system
systemctl daemon-reload   #重载
systemctl start php-fpm   #开启服务

在这里插入图片描述
启动服务报错,则需要注释掉php-fpm.service中的#ProtectSystem=full
在这里插入图片描述

三.nginx结合php-fpm

修改nginx配置文件:

vim /usr/local/nginx/conf/nginx.conf
///
location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi.conf;
    }
///              ###取消注释这几行

在这里插入图片描述

编写php发布文件内容,在网页上能够显示。

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

在这里插入图片描述
在真机中打开浏览器访问:http://172.25.101.1/index.php
在这里插入图片描述

四.添加memcache功能模块
1、安装memcache模块

在server1中:

cd
lftp 172.25.254.250
>ls
>cd pub/docs/lamp
>get memcache-4.0.5.2.tgz
>exit
ls
tar zxf memcache-4.0.5.2.tgz 
cd memcache-4.0.5.2/	
ls
phpize		#扩展模块,如果执行失败则需要添加环境变量

cd
vim .bash_profile 		#添加环境变量
source .bash_profile    #引用环境变量
which phpize

在这里插入图片描述
编译之前需要安装以下两个依赖软件:

	yum install autoconf -y
    yum install automake.noarch -y
    phpize    #重新执行扩展

编译:

 ./configure --enable-debug


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

在这里插入图片描述
编辑php.ini ,添加memcache模块,然后重启服务,执行php -m可以看到memcache。

vim php.ini
systemctl reload php-fpm  #刷新服务
php -m | grep memcache

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

2 .添加memcache模块,构建nginx高速缓存

在这里插入图片描述
1.添加memcache模块,需要先安装memcached,并开启服务,查看端口:

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

在这里插入图片描述

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      #修改密码和server地址

在这里插入图片描述

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

在真机中打开浏览器访问:http://http://172.25.101.1/example.php,会从缓存中取数据
在这里插入图片描述接下来访问172.25.101.1/memcache.php。用户名为上面配置文件中的memcache,密码westos。
在这里插入图片描述
在这里插入图片描述
传统模式的访问太慢,在真机中执行压力测试命令,对其进行优化:
在这里插入图片描述

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

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

安装openresty:

https://openresty.org/download/openresty-1.19.3.1.tar.gz
tar zxf openresty-1.19.3.1.tar.gz 

在这里插入图片描述

ls
cd openresty-1.19.3.1/
ls

./configure #编译

在这里插入图片描述
make && make install
在这里插入图片描述
关闭nginx

systemctl stop nginx
netstat  -antlp

在这里插入图片描述
参考之前配置的nginx.conf文件

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

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

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

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

在这里插入图片描述
注意: 出现如图报错时,说明该路径下找不到nginx服务,通过nginx -c 指定 nginx.conf的位置

/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf//nginx.conf
/usr/local/openresty/nginx/sbin/nginx -s reload

在这里插入图片描述
在真机进行压力测试:

 ab -c10 -n 5000 http://172.25.101.1/example.php

传统模式:
在这里插入图片描述
新模式:
在这里插入图片描述
相比于传统模式,新模式的传输速率快了很多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值