lmnp架构(1)-php和缓存部署

一、什么是lnmp架构。

LNMP是指LNMP==Linux+Nginx+Mysql+PHP的结构体系。

-用户通过http协议发起请求,请求会先抵达LNMP架构中的nginx

-nginx会根据用户的请求进行判断,这个判断是由Location完成的

-判断用户请求的是静态页面,nginx直接进行处理

-判断用户请求的是动态页面,nginx会将该请求交给fastcgi协议下发

-fastcgi会将请求交给php-fpm管理进程,php-fpm管理进程收到后调用具体的工作进程wrapper

-wrapper线程会调用php进行解析,如果只是解析php代码,那么直接返回结果给客户端

-如果有查询数据库的操作,则由php连接数据库(用户、密码、IP)然后发起查询的操作

-最终数据由mysql-----php-----php-fpm----fastcgi------nginx---http---user

二、lnmp架构部署。

PHP是一种在服务器端执行的嵌入HTML文档的脚本语言

LNMP中php是以一个服务的方式存在的,服务名为php-fpm

接受web访问请求时和apache处理方式不同的是,apache通过编译的模块来解析php页面,而nginx则是把php页面交给php-fpm这个服务来进行解析。nginx优于apache的一点在于处理静态请求,nginx会直接处理静态请求,所有的动态请求都会转发给php-fpm处理。

1.php编译安装

根据提示安装软件依赖,提前解决报错问题

[root@server1 ~]# yum install -y systemd-devel libxml2-devel sqlite-devel libcurl-devel libpng-devel
[root@server1 ~]# yum install -y oniguruma-devel-6.8.2-1.el7.x86_64.rpm oniguruma-6.8.2-1.el7.x86_64.rpm

进入php目录,三部曲:configure-make-make install

执行configure文件

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

[root@server1 ~]# cd php-7.4.12/

[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --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-mbstring --enable-bcmath --with-fpm-systemd

[root@server1 php-7.4.12]# make

[root@server1 php-7.4.12]# make install

拷贝php配置文件,复制服务脚本到/etc/init.d目录下

[root@server1 php-7.4.12]# cp php.ini-production /usr/local/php/etc/php.ini
[root@server1 php-7.4.12]# cp sapi/fpm/php-fpm.service /lib/systemd/system
[root@server1 php-7.4.12]# cd /usr/local/php/etc/
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf

修改php-fpm.conf文件,指定pid的存放目录

[root@server1 etc]# vim php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/php/var
; Default Value: none
pid = run/php-fpm.pid #打开注释
修改时区

修改php.ini文件

[root@server1 etc]# vim php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai

添加环境变量

[root@server1 etc]# cd php-fpm.d/
[root@server1 php-fpm.d]# cp www.conf.default www.conf
[root@server1 ~]# vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/php/bin
[root@server1 ~]# source .bash_profile

编辑php-fpm启动文件,启动php服务,查看端口

[root@server1 ~]# vim /lib/systemd/system/php-fpm.service
#ProtectSystem=full #注释
[root@server1 ~]# systemctl daemon-reload
[root@server1 ~]# systemctl start php-fpm.service
[root@server1 ~]# netstat -antlp|grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 24342/php-fpm: mast

nginx整合php

PHP的sapi有fpm的运行模式,或者说接口,就是一种访问PHP这个软件的方式,nginx配合fpm这个接口,访问PHP程序,实现和PHP的配合。启动php-fpm后,监听一个端口,默认监听9000(见配置文件listen = 127.0.0.1:9000)

nginx处理静态文件和反向代理请求,PHP 处理动态请求并生成网页内容。通过这种方式,可以提高网站的性能和安全性。

表示默认访问index.php页面,如果没有再访问html页面,由于fastcgi.conf已经包含了fastcgi_params,因此只要include fastcgi.conf文件即可

这里直接展示如何设置systemd启动方式

[root@server1 ~]# 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; #修改这行
}

nginx与php结合测试,进入到nginx的默认发布目录,编写php测试页面

[root@server1 ~]# systemctl restart nginx.service
[root@server1 ~]# cd /usr/local/nginx/html/
[root@server1 html]# vim index.php
<?php
phpinfo()
?>

访问nginx服务器ip,看到php info页面:

2.传统缓存策略

memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著。

一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。

客户端访问时整个流程:

client > nginx > fastcgi_pass > php-fpm:9000 > php-memcache > memcached:11211

memcache模块编译

三部曲,执行phpize,安装依赖。phpize是一个运行脚本,主要作用检测php的环境,并在特定的目录生成相应的configure文件,makeinstall之后生成的.so文件自动加载到php扩展目录下面。进入php.ini

添加memcache扩展模块

[root@server1 ~]# tar zxf memcache-4.0.5.2.tgz
[root@server1 ~]# cd memcache-4.0.5.2/
[root@server1 memcache-4.0.5.2]# yum install -y autoconf
[root@server1 memcache-4.0.5.2]# phpize
[root@server1 memcache-4.0.5.2]# ./configure --enable-memcache
[root@server1 memcache-4.0.5.2]# make
[root@server1 memcache-4.0.5.2]# make install
[root@server1 no-debug-non-zts-20190902]# cd /usr/local/php/etc/
[root@server1 etc]# ls
php-fpm.conf php-fpm.conf.default php-fpm.d php.ini
[root@server1 etc]# vim php.ini
extension=memcache #连接php与memcache服务

查看是否添加成功

[root@server1 etc]# systemctl reload php-fpm.service
[root@server1 etc]# php -m |grep memcache
memcache #出现memcache表示添加成功

安装memcached服务

复制memcache的示例页面到nginx的默认发布目录下,添加用户名及密码

[root@server1 html]# yum install -y memcached
[root@server1 html]# systemctl enable --now memcached
[root@server1 html]# netstat -antlp|grep :11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 27633/memcached
tcp6 0 0 :::11211 :::* LISTEN 27633/memcached
[root@server1 memcache-4.0.5.2]# cp memcache.php example.php /usr/local/nginx/html/
[root@server1 conf]# cd /usr/local/nginx/html/
[root@server1 html]# vim memcache.php
$MEMCACHE_SERVERS[] = 'localhost:11211';

测试,将在缓存中读取数据。

http://192.168.56.170/memcache.php

站点压测:ab -c10 -n500 http://172.25.56.170/example.php

没有memcache缓存加速

memcache缓存加速

3.高效缓存系统

nginx的memcached_module模块可以直接从memcached服务器中读取内容后输出,后续的请求不再经过应用程序处理,如php-fpm、django,大大的提升动态页面的速度。nginx只负责从memcached服务器中读取数据,要往memcached写入数据还得需要后台的应用程序来完成,主动的将要缓存的页面缓存到memcached中,可以通过404重定向到后端去处理的。

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

使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制。

如果需要做到高速缓存,nginx可以跳过php直接向memcache存储,但是只能做静态存储 ,如果需要动态存储,还是要调用php,因此两种缓存策略时同时在进行的。

暂停nginx服务,安装openresty软件

[root@server1 ~]# systemctl stop nginx.service
[root@server1 ~]# tar zxf openresty-1.21.4.1.tar.gz
[root@server1 ~]# cd openresty-1.21.4.1/
[root@server1 openresty-1.21.4.1]# ./configure --prefix=/usr/local/openresty --with-http_stub_status_module --with-http_ssl_module --with-threads --with-file-aio
[root@server1 openresty-1.21.4.1]# make
[root@server1 openresty-1.21.4.1]# make install

拷贝配置文件

[root@server1 html]# cp /usr/local/nginx/html/example.php .
[root@server1 html]# cp /usr/local/nginx/html/index.php .

编辑配置文件,重启服务

[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# vim nginx.conf
user nginx;
worker_processes 2;
events {
worker_connections 65535;
}
http {
upstream memcache {
server 127.0.0.1:11211;
keepalive 512; #保持512个不立即关闭的连接用于提升性能
}
...
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /memc {
internal; #表示只接受内部访问
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string; #使用内置的$query_string来作为key
set $memc_exptime 300; #表示缓存失效时间
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key; # 先在memcache中找,如果有则直接返回,如果没有再通过fastcgi去后端找
srcache_store PUT /memc $key; # 将在后端找到的值存储到memcache中,下次再请求的时候可以直接返回
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
}
}
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t #检测是否有语法错误
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx
[root@server1 conf]# netstat -antlp|grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 37163/nginx: master

再次进行压力测试

ab -c10 -n10000 http://172.25.56.170/example.php

请求速度明显加快

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值