nginx-PHP安装及高速缓存策略

php源码编译

下载安装包解压安装php

[root@server1 ~]# yum install -y bzip2
[root@server1 ~]# tar jxf php-7.4.12.tar.bz2 

安装依赖项,编译后安装

[root@server1 ~]# yum install -y systemd-devel libxml2-devel.x86_64 sqlite-devel libcurl-devel libpng-devel oniguruma-devel-6.8.2-1.el7.x86_64.rpm make 
[root@server1 php-7.4.12]# ./configure --prefix=/usr/lnmp/php --with-config-file-path=/usr/local/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
[root@server1 php-7.4.12]# make
[root@server1 php-7.4.12]# make install

预编译成功
在这里插入图片描述

PHP-FPM

PHP-FPM是一个实现了Fastcgi的程序,PHP-FPM的管理对象是php-cgi,被PHP官方收购了,后来PHP内核集成了PHP-FPM之后就方便多了,使用enalbe-fpm这个编译参数即可
PHP-CGI
php-cgi是解释PHP脚本的程序,只是个CGI程序,他自己本身只能解析请求,返回结果,不会进程管理

拷贝php-fpm配置文件,修改php-fpm启动文件

[root@server1 /mnt/php-7.4.12]# cd /usr/local/php/etc/
[root@server1 /usr/local/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 /usr/local/php/etc]# vim php-fpm.conf

去掉注释
在这里插入图片描述

[root@server1 etc]# cd php-fpm.d/
[root@server1 php-fpm.d]# cp www.conf.default www.conf

编辑php核心配置更改时区

[root@server1 php-fpm.d]# cd 
[root@server1 ~]# cd php-7.4.12/
[root@server1 php-7.4.12]# cp php.ini-production /usr/local/php/etc/php.ini
[root@server1 php-7.4.12]# vim /usr/local/php/etc/php.ini

在这里插入图片描述拷贝解压源码编译包目录中php-fpm.service的到系统systemd下,通过systemctl来控制php

[root@server1 ~]#cp /root/php-7.4.12/sapi/fpm/php-fpm.service  /usr/lib/systemd/system/
[root@server1 php-7.4.12]# vim /usr/lib/systemd/system/php-fpm.service 

注释掉这一行内容
在这里插入图片描述
重载服务,启动

[root@server1 php-7.4.12]# systemctl daemon-reload
[root@server1 php-7.4.12]# systemctl enable --now php-fpm.service 

修改nginx的配置文件,打开php

[root@server1 php-7.4.12]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述在nginx中写入php测试页面

[root@server1 php-7.4.12]# vim /usr/local/nginx/html/index.php
[root@server1 php-7.4.12]# cat /usr/local/nginx/html/index.php
<?php
phpinfo()
?>
[root@server1 php-7.4.12]# 

重载nginx,用浏览器访问

在这里插入图片描述

构建传统缓存策略

用户访问流程: client -> nginx -> fastcgi_pass -> php-fpm:9000 -> memcached:11211

在这里插入图片描述下载并解压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]# ls
cloudbuild.yaml  config.w32  Dockerfile   memcache.php  tests
config9.m4       CREDITS     example.php  php7
config.m4        docker      LICENSE      README
[root@server1 memcache-4.0.5.2]# 

安装依赖性

[root@server1 memcache-4.0.5.2]#  yum install -y autoconf automake

phpize命令是准备php扩展安装的编译环境的。用于手动编译安装php扩展,在php的安装目录下有phpize,需要先加入全局变量

[root@server1 memcache-4.0.5.2]# cd /usr/local/lnmp/php/bin/
[root@server1 bin]# ls
phar  phar.phar  php  php-cgi  php-config  phpdbg  phpize
[root@server1 bin]# vim ~/.bash_profile 
[root@server1 bin]# source ~/.bash_profile

在这里插入图片描述cd

[root@server1 ~]# cd memcache-4.0.5.2/
[root@server1 memcache-4.0.5.2]# phpize
[root@server1 memcache-4.0.5.2]# ls

在这里插入图片描述预编译,及安装

[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

修改php.ini主配置文件,加入extension=memcache模块

[root@server1 memcache-4.0.5.2]# vim /usr/local/lnmp/php/etc/php.ini

在这里插入图片描述
查看php的cache模块,添加成功

在这里插入图片描述

拷贝php的发布样本目录到nginx的发布目录

[root@server1 memcache-4.0.5.2]# cp example.php memcache.php /usr/local/nginx/html/

安装memcached软件,启用memcached服务器,重载php-fpm

[root@server1 memcache-4.0.5.2]# yum install -y memcached
[root@server1 memcache-4.0.5.2]# systemctl start memcached
[root@server1 memcache-4.0.5.2]# systemctl reload php-fpm.service

访问测试页面
在这里插入图片描述
编辑memcache.php,修改信息

[root@server1 memcache-4.0.5.2]# cd /usr/local/nginx/html
[root@server1 html]# vim memcache.php

在这里插入图片描述
访问管理页面,输入自己修改的账号密码
在这里插入图片描述
在这里插入图片描述
对php测试页面做压测试,当多次访问此时页面,只有第一次是从后端拿的,之后都是从缓存里拿的,所以缓存命中率会越来越高
在这里插入图片描述

构建高速缓存ngixn ->memcache

在这里插入图片描述
构建新性的访问流程:client -> nginx -> memcache
传统缓存是php从缓存拿,响应时间由php决定,这样直接从nginx访问到缓存memcache,查看是否具有缓存,没有缓存再走php处理过程。减少了php的处理过程,加快了速率。

设置openresty里的nginx和memcahe连接
下载openresty安装包,解压编译和安装

[root@server1 ~]# tar zxf openresty-1.19.3.1.tar.gz
[root@server1 ~]# cd openresty-1.19.3.1
[root@server1 openresty-1.19.3.1]# ./configure
[root@server1 openresty-1.19.3.1]# make
[root@server1 openresty-1.19.3.1]# make install

拷贝文件
关闭以前的nginx,为方便设置php部分的文件,直接拷贝以前nginx的配置文件到openresty下的配置文件下

[root@server1 openresty-1.19.3.1]# nginx -s stop 
[root@server1  openresty-1.19.3.1] cp /usr/local/nginx/conf/nginx.conf   /usr/local/openresty/nginx/conf/
cp: overwrite '/usr/local/openresty/nginx/conf/nginx.conf'? y              
[root@ server1 conf] cp /usr/local/nginx/html/index.php /usr/local/openresty/nginx/html/  

修改配置文件,添加缓存模块;keepalive 512 保持512个不立即关闭的连接用于提升性能

[root@server1 openresty-1.19.3.1]# vim /usr/local/openresty/nginx/conf/nginx.conf

在这里插入图片描述
添加缓存具体配置
在这里插入图片描述

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;
        }

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

 location ~ \.php$ {
            set $key $uri$args;
            srcache_fetch GET /memc $key;
            srcache_store PUT /memc $key;
            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做压力测试
没有失败访问

[root@server1 html]# /usr/local/openresty/nginx/sbin/nginx
[root@erqian ~]# ab -c10 -n1000 http://172.25.1.1/example.php

在这里插入图片描述

nginx日志可视化

GoAccess是一个实时网络日志分析器和交互式查看器,可以在浏览器中运行,动态可视化服务器日志并提供统计信息

下载GoAccess源码/编译和安装:
安装依赖性

[root@server1 ~]# yum install GeoIP-devel-1.5.0-13.el7.x86_64.rpm -y
[root@server1 ~]#  yum install ncurses-devel.x86_64 -y 

j进入解压后的文件夹,预编译/编译/安装

[root@server1 ~]# cd goaccess-1.4/
[root@server1 goaccess-1.4]# ./configure --enable-utf8 --enable-geoip=legacy
[root@server1 goaccess-1.4]# make
[root@server1 goaccess-1.4]# make install

进入nginx日志目录中,执行goaccess监控语句,并打入后台持续运行,之前配置的高速缓存nginx会影响本实验,应先关闭。

[root@server1 ~]# cd /usr/local/nginx/logs/
[root@server1 logs]# goaccess access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html &

在这里插入图片描述
进入浏览器中访问,即可可视化日志文件,
http://172.25.1.1/report.html
在这里插入图片描述
进行压测,会发现页面随着访问量的增加实施更新

[root@erqian ~]# ab -c 10 -n 5000 http://172.25.1.1/index.php

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: nginx-rtmp-module是一个用于在Nginx服务器上实现流媒体传输的模块。安装nginx-rtmp-module需要先安装Nginx服务器,然后下载并编译安装nginx-rtmp-module。 具体步骤如下: 1. 安装Nginx服务器 可以使用包管理器安装Nginx,也可以从官网下载源码编译安装安装完成后,启动Nginx服务器。 2. 下载nginx-rtmp-module 从nginx-rtmp-module的官网下载最新版本的源码,解压缩到任意目录。 3. 编译安装nginx-rtmp-module 进入nginx-rtmp-module的源码目录,执行以下命令: ./configure --add-module=/path/to/nginx-rtmp-module make sudo make install 其中,/path/to/nginx-rtmp-module是nginx-rtmp-module的源码目录。 4. 配置Nginx服务器 在Nginx的配置文件中添加以下内容: rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; } } } 其中,listen指定了rtmp协议的端口号,application指定了应用名称,live on表示开启直播功能,record off表示不进行录制。 5. 重启Nginx服务器 执行以下命令重启Nginx服务器: sudo service nginx restart 安装完成后,可以使用rtmp协议进行流媒体传输。 ### 回答2: nginx-rtmp-module 是一个使用 Nginx 作为 RTMP 流媒体服务器的模块。它可以用于直播流媒体和 VOD (点播) 流媒体。下面是使用 nginx-rtmp-module 安装的步骤。 1. 安装依赖 在安装 nginx-rtmp-module 之前,需要在系统上安装几个依赖项。例如,在 Ubuntu 上,可以使用以下命令安装所需的依赖项: ```shell sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev ``` 2. 下载 Nginx 和 RTMP 模块 在安装 nginx-rtmp-module 之前,需要先下载 nginx 和 rtmp 模块的源代码。可以从官方网站上下载最新版本的 nginx 和 rtmp 模块。例如,在 Ubuntu 上可以使用以下命令下载 nginx 和 rtmp 模块: ```shell wget https://nginx.org/download/nginx-1.20.0.tar.gz wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.2.tar.gz ``` 3. 解压和编译 Nginx 和 RTMP 模块 在下载了 nginx 和 rtmp 模块的源代码之后,需要解压并编译它们。可以使用以下命令来解压 nginx 和 rtmp 模块的源代码: ```shell tar -xzvf nginx-1.20.0.tar.gz tar -xzvf v1.2.2.tar.gz ``` 解压之后,切换到 nginx 目录,使用以下命令编译 nginx: ```shell cd nginx-1.20.0/ ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-1.2.2 make sudo make install ``` 在编译过程中,使用了两个参数。第一个参数是编译 nginx 时需要开启 SSL 功能,第二个参数是添加 rtmp 模块。 4. 配置 Nginx 和 RTMP 模块 完成了编译和安装之后,需要配置 nginx 和 rtmp 模块的配置文件。可以使用以下命令来创建一个 nginx 配置文件: ```shell sudo vi /usr/local/nginx/conf/nginx.conf ``` 在配置文件中添加以下代码,以启用 RTMP 流媒体服务器: ``` rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; } } } ``` 这里的配置将 RTMP 流媒体服务器的监听端口设置为 1935,启用直播流媒体应用程序“live”。 5. 启动 Nginx 服务器 完成了配置之后,可以使用以下命令来启动 nginx 服务器: ```shell sudo /usr/local/nginx/sbin/nginx ``` 注意,必须使用 sudo 启动 nginx 服务器,因为它需要使用特权端口。 最后,通过 RTMP 推送流到 RTMP 流媒体服务器,您就可以开始直播了。 总结:nginx-rtmp-module 的安装过程较为简单,需要通过下载、编译和配置等步骤进行。并且需要保证系统的某些依赖项已经安装。最终,通过启动 nginx 服务器,并通过 RTMP 推送流到 RTMP 流媒体服务器,就可以开始直播了。 ### 回答3: Nginx-rtmp-module是一个基于Nginx的第三方扩展模块,用于提供RTMP(Real Time Messaging Protocol)流媒体服务。它使得Nginx服务器可以像常规Web服务器一样处理流媒体请求,包括直播、点播和视频会议等。本文将介绍如何在Linux系统上安装nginx-rtmp-module。 准备工作: 在安装nginx-rtmp-module之前,需要先安装一些必要的软件。在Ubuntu系统上,可以通过以下命令进行安装: sudo apt-get update sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev 安装Nginx: 首先,需要下载最新的Nginx源代码。可以在Nginx官网上下载: wget http://nginx.org/download/nginx-1.12.2.tar.gz 解压并进入目录: tar -zxvf nginx-1.12.2.tar.gz cd nginx-1.12.2 然后进行编译并安装Nginx: ./configure --add-module=/path/to/nginx-rtmp-module make sudo make install 其中,--add-module=/path/to/nginx-rtmp-module是指定nginx-rtmp-module的目录位置。 配置Nginx-rtmp-module: 在安装完成后,需要进行配置以启用Nginx-rtmp-module。首先,打开Nginx配置文件nginx.conf: sudo nano /usr/local/nginx/conf/nginx.conf 在http块中添加以下内容: rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; } } } 其中,application live是rtmp应用名,可以按需更改。live on表示启用流媒体服务,record off表示禁止录制。 重新启动Nginx服务并检查配置是否正确: sudo /usr/local/nginx/sbin/nginx -t sudo /usr/local/nginx/sbin/nginx -s reload 使用Nginx-rtmp-module: 启动rtmp流媒体服务并推送直播流: rtmp://localhost/live/stream_name 其中,stream_name为直播流的名称,可以随意设置。 接下来,可以使用RTMP协议的播放器进行观看: rtmp://localhost/live/stream_name 总结: 本文介绍了在Linux系统上安装nginx-rtmp-module的过程。Nginx-rtmp-module是一个强大的流媒体服务模块,可以方便地提供流媒体服务,同时也可以灵活地进行个性化配置。学习和使用Nginx-rtmp-module可以为流媒体服务的开发和应用带来不少的便利和收益。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值