重庆南川网站制作公司哪家好,阳新网站建设,wordpress如何调用js,网站建设实训不足CGI的由来#xff1a; 最早的 Web 服务器只能简单地响应浏览器发来的 HTTP 请求#xff0c;并将存储在服务器上的 HTML 文件返回给浏 览器#xff0c;也就是静态 html 文件#xff0c;但是后期随着网站功能增多网站开发也越来越复杂#xff0c;以至于出现动态技 术 最早的 Web 服务器只能简单地响应浏览器发来的 HTTP 请求并将存储在服务器上的 HTML 文件返回给浏 览器也就是静态 html 文件但是后期随着网站功能增多网站开发也越来越复杂以至于出现动态技 术比如像 php(1995 年 ) 、 java(1995) 、 python(1991) 语言开发的网站但是 nginx/apache 服务器并不 能直接运行 php 、 java 这样的文件 apache 实现的方式是打补丁但是 nginx 缺通过与第三方基于协议实 现即通过某种特定协议将客户端请求转发给第三方服务处理第三方服务器会新建新的进程处理用户 的请求处理完成后返回数据给 Nginx 并回收进程最后 nginx 在返回给客户端那这个约定就是通用网 关接口 (common gateway interface 简称 CGI) CGI 协议 是 web 服务器和外部应用程序之间的接口 标准是 cgi 程序和 web 服务器之间传递信息的标准化接口。 为什么会有FastCGI CGI 协议虽然解决了语言解析器和 Web Server 之间通讯的问题但是它的效率很低因为 Web Server 每收到一个请求都会创建一个 CGI 进程 PHP 解析器都会解析 php.ini 文件初始化环境请求结束的时候 再关闭进程对于每一个创建的 CGI 进程都会执行这些操作所以效率很低而 FastCGI 是用来提高 CGI 性 能的 FastCGI 每次处理完请求之后不会关闭掉进程而是保留这个进程使这个进程可以处理多个请 求。这样的话每个请求都不用再重新创建一个进程了大大提升了处理效率。 什么是 PHP-FPM PHP-FPM(FastCGI Process Manager FastCGI 进程管理器 ) 是一个实现了 Fastcgi 的程序并且提供进程管理的功能。 进程包括 master 进程和 worker 进程。 master 进程只有一个负责监听端口接受来自 web server 的请求 worker 进程一般会有多个每个进程中会嵌入一个 PHP 解析器进行 PHP 代码的处理 FastCGI实战案例 : Nginx与php-fpm在同一服务器 编译安装更方便自定义参数或选项所以推荐大家使用源码编译 官方网站 www.php.net 源码编译php 首先保持一纯净的NGINX服务之前操作过的删掉重新建立一个NGINX服务。 [rootfastcgi nginx-1.26.1]# make install 建立完成后添加PHP模块 中途会报错有一个解释软件包中没有要在阿里云找个rpm包的下载地址 [rootnginx ~]# wget https://repo.almalinux.org/almalinux/9/CRB/x86_64/os/Packages/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm [rootnginx ~]# dnf install oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm [rootnginx php-8.3.9]# make install 原码安装成功。 #利用yum解决php依赖
[rootNginx ~]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel
libpng-devel libcurl-devel oniguruma-devel
#解压源码并安装
[rootNginx ~]# ./configure \
--prefix/usr/local/php \ #安装路径
--with-config-file-path/usr/local/php/etc \ #指定配置路径
--enable-fpm \ #用cgi方式启动程序
--with-fpm-usernginx \ #指定运行用户身份
--with-fpm-groupnginx \
--with-curl \ #打开curl浏览器支持
--with-iconv \ #启用iconv函数转换字符编码
--with-mhash \ #mhash加密方式扩展库
--with-zlib \ #支持zlib库用于压缩http压缩传输
--with-openssl \ #支持ssl加密
--enable-mysqlnd \ #mysql数据库
--with-mysqli \
--with-pdo-mysql \
--disable-debug \ #关闭debug功能
--enable-sockets \ #支持套接字访问
--enable-soap \ #支持soap扩展协议
--enable-xml \ #支持xml
--enable-ftp \ #支持ftp
--enable-gd \ #支持gd库
--enable-exif \ #支持图片元数据
--enable-mbstring \ #支持多字节字符串
--enable-bcmath \ #打开图片大小调整,用到zabbix监控的时候用到了这个模块
--with-fpm-systemd #支持systemctl 管理cgiPHP添加模块[rootNginx ~]#./configure --prefix/usr/local/php --with-config-file-path/usr/local/php/etc --enable-fpm --with-fpm-usernginx --with-fpm-groupnginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemdNGINX1.26 添加模块[rootNginx ~]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --add-module/root/echo-nginx-module-0.63 --add-module/root/memc-nginx-module-0.20 --add-module/root/srcache-nginx-module-0.33 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_modulephp相关配置优化 [rootNginx ~]# cd /usr/local/php/etc
[rootNginx etc]# cp php-fpm.conf.default php-fpm.conf
[rootNginx etc]# vim php-fpm.conf
去掉注释
pid run/php-fpm.pid #指定pid文件存放位置
[rootNginx etc]# cd php-fpm.d/
[rootNginx php-fpm.d]# cp www.conf.default www.conf
#生成主配置文件
[rootNginx php-fpm.d]# cd /root/php-8.3.9/
[rootNginx php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[rootNginx ~]# vim /usr/local/php/etc/php.ini
[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
date.timezone Asia/Shanghai #修改时区
#生成启动文件
[rootNginx ~]# cd /root/php-8.3.9/
[rootNginx php-8.3.9]# cp sapi/fpm/php-fpm.service /lib/systemd/system/
# Mounts the /usr, /boot, and /etc directories read-only for processes invoked by
this unit.
#ProtectSystemfull #注释该内容
[rootNginx php-8.3.9]# systemctl start php-fpm.service
[rootNginx php-8.3.9]# netstat -antlupe | grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 0
820758 176202/php-fpm: mas 准备php测试页面 [rootNginx ~]# mkdir /data/php -p
[rootcentos8 ~]# cat /data/php/index.php #php测试页面
?php
phpinfo();
? Nginx配置转发 Nginx 安装完成之后默认生成了与 fastcgi 的相关配置文件一般保存在 nginx 的安装路径的 conf 目录当中比如 /apps/nginx/conf/fastcgi.conf 、 /apps/nginx/conf/fastcgi_params 。 NGINX主配文件添加子配置文件发布路径。 添加php环境变量 [rootNginx ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programsPATH$PATH:$HOME/bin:/apps/nginx/sbin:/usr/local/php/bin
export PATH
[rootNginx ~]# source .bash_profile 3 php的动态扩展模块php的缓存模块 安装memcache模块 [rootNginx ~]# tar zxf memcache-8.2.tgz
[rootNginx ~]# cd memcache-8.2/
[rootNginx memcache-8.2]# yum install autoconf
[rootNginx memcache-8.2]# phpize
[rootNginx memcache-8.2]# ./configure make make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-nonzts-20230831/
[rootNginx memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-
20230831/
memcache.so opcache.so [rootnginx etc]# dnf install memcached -y 复制测试文件到nginx发布目录中 [rootNginx ~]# cd memcache-8.2/
[rootNginx memcache-8.2]# ls
autom4te.cache config.log configure.ac example.php Makefile.fragments
README
build config.m4 config.w32 include Makefile.objects runtests.php
config9.m4 config.nice CREDITS libtool memcache.la src
config.h config.status docker LICENSE memcache.php
tests
config.h.in configure Dockerfile Makefile modules
[rootNginx memcache-8.2]# cp example.php memcache.php /data/php/
[rootNginx ~]# vim /data/php/memcache.php
define(ADMIN_USERNAME,admin); // Admin Username
define(ADMIN_PASSWORD,lee); // Admin Password
define(DATE_FORMAT,Y/m/d H:i:s);
define(GRAPH_SIZE,200);
define(MAX_ITEM_DUMP,50);
$MEMCACHE_SERVERS[] localhost:11211; // add more as an array
#$MEMCACHE_SERVERS[] mymemcache-server2:11211; // add more as an array 测试 6.3.4 php高速缓存 部署方法 在我们安装的 nginx 中默认不支持 memc 和 srcache 功能需要借助第三方模块来让 nginx 支持此功能所以 nginx 需要重新编译 前面做过了检查版本。 [rootNginx ~]# tar zxf srcache-nginx-module-0.33.tar.gz
[rootNginx ~]# tar zxf memc-nginx-module-0.20.tar.gz
[rootNginx ~]# cd nginx-1.26.1/
[rootNginx nginx-1.26.1]# ./configure --prefix/apps/nginx --usernginx --
groupnginx --with-http_ssl_module --with-http_v2_module --withhttp_realip_module --with-http_stub_status_module --with-http_gzip_static_module
--with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --
add-module/root/memc-nginx-module-0.20 --add-module/root/srcache-nginx-module-
0.33
[rootNginx nginx-1.26.1]# make make install
[rootNginx ~]# vim /apps/nginx/conf.d/php.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
server {
listen 80;
server_name php.timinglee.org;
root /data/php;
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; #缓存失效时间300秒
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args; #设定key的值
srcache_fetch GET /memc $key; #检测mem中是否有要访问的php
srcache_store PUT /memc $key; #缓存为加载的php数据
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[rootNginx ~]# systemctl start nginx.service [rootnginx conf.d]# cat php.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}server {listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;location /memc {internal;memc_connect_timeout 100ms;memc_send_timeout 100ms;memc_read_timeout 100ms;set $memc_key $query_string;set $memc_exptime 300;memc_pass memcache;}location ~ \.php$ {set $key $uri$args;srcache_fetch GET /memc $key;srcache_store PUT /memc $key;root /data/web/php;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;}
}[rootnginx conf.d]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [rootnginx conf.d]# nginx -s reload 测试结果 [rootnginx conf.d]# ab -n500 -c10 http://php.timinglee.org/index.php
This is ApacheBench, Version 2.3 $Revision: 1903618 $
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking php.timinglee.org (be patient)
apr_sockaddr_info_get() for php.timinglee.org: Name or service not known (670002)
[rootnginx conf.d]# cd
[rootnginx ~]# cd php-8.3.9/
[rootnginx php-8.3.9]# ab -n500 -c10 http://www.timinglee.org/index.php
This is ApacheBench, Version 2.3 $Revision: 1903618 $
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking www.timinglee.org (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requestsServer Software: nginx/1.26.1
Server Hostname: www.timinglee.org
Server Port: 80Document Path: /index.php
Document Length: 74915 bytesConcurrency Level: 10
Time taken for tests: 0.042 seconds
Complete requests: 500
Failed requests: 0
Total transferred: 37549977 bytes
HTML transferred: 37457500 bytes
Requests per second: 11990.12 [#/sec] (mean)
Time per request: 0.834 [ms] (mean)
Time per request: 0.083 [ms] (mean, across all concurrent requests)
Transfer rate: 879353.00 [Kbytes/sec] receivedConnection Times (ms)min mean[/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 1 0.3 1 3
Waiting: 0 1 0.3 1 3
Total: 0 1 0.3 1 3Percentage of the requests served within a certain time (ms)50% 166% 175% 180% 190% 195% 198% 299% 2100% 3 (longest request)