众所周知,常用的跟php搭配的web server 有两个,apache 和nginx,编译完server之后需要配置下才可以正常解析php文件。下面我们来看下两种服务器是如何解析php文件。
Nginx
一、主流的nginx+php的运行原理如下:
1、nginx的worker进程直接管理每一个请求到nginx的网络请求。
2、对于php而言,由于在整个网络请求的过程中php是一个cgi程序的角色,所以采用名为php-fpm的进程管理程序来对这些被请求的php程序进行管理。php-fpm程序也如同nginx一样,需要监听端口,并且有master和worker进程。worker进程直接管理每一个php进程。
3、关于fastcgi:fastcgi是一种进程管理器,管理cgi进程。市面上有多种实现了fastcgi功能的进程管理器,php-fpm就是其中的一种。再提一点,php-fpm作为一种fast-cgi进程管理服务,会监听端口,一般默认监听9000端口,并且是监听本机,也就是只接收来自本机的端口请求,所以我们通常输入命令netstat -nlpt|grep php-fpm 会得到:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1078/php-fpm
4、关于fastcgi的配置文件,目前fastcgi的配置文件一般放在nginx.conf同级目录下,配置文件形式,一般有两种:fastcgi.conf 和 fastcgi_params。不同的nginx版本会有不同的配置文件,这两个配置文件有一个非常重要的区别:fastcgi_parames文件中缺少下列配置:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
我们可以打开fastcgi_parames文件加上上述行,也可以在要使用配置的地方动态添加。使得该配置生效。
5、当需要处理php请求时,nginx的worker进程会将请求移交给php-fpm的worker进程进行处理,也就是最开头所说的nginx调用了php,其实严格得讲是nginx间接调用php。
二、nginx 配置
来看一个host的简单配置:
server { listen 80; server_name example.com; location ~ \.php?.*$ { root /home/mark/www; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
1、第一个大括号 server{ }不必多说,代表一个独立的server
2、listen 80代表该server监听8011端口
3、location ~ \.php?.*${ }代表一个能匹配对应uri的location,用于匹配一类uri,并对所匹配的uri请求做自定义的逻辑、配置。这里的location,匹配了所有带.php的uri请到该location内的uri请求看做是cgi程序,并将请求发送到9000端口,交由php-fpm处理。
6、fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 动态添加了一行fastcgi配置,配置内容为SCRIPT_FILENAME,告知管理进程,cgi脚本名称。由于我的nginx中只有fastcgi_params文件,没有fastcgi.conf文件,所以要使php-fpm知道SCRIPT_FILENAME的具体值,就必须要动态的添加这行配置。
7、include fastcgi_params 引入fastcgi配置文件
以上就是最简洁版的nginx启动php脚本的最简配置,当重启nginx之后,在/home/mark/www目录下创建一个hello.php文件,输入7d068493c50c96539e6b2bf200c6ea16保存,然后在浏览器中访问localhost/hello.php就可以在网页上显示hello world了。
Apache
相比nginx ,apache 配置fastcgi稍微麻烦些,SetHandler/ProxyPassMatch/ProxyPass/Mod_Rewrite 都可以做到,这里我们只说官方推荐的ProxyPassMatch方法。
加载代理模块
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
配置虚拟主机支持
<VirtualHost *:80> DocumentRoot "/home/mark/www" ServerName test.com DirectoryIndex /index.php index.php ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/home/mark/www/$1 <Directory "/home/mark/www"> Options none AllowOverride All Require all granted </Directory> </VirtualHost>
重启apache,测试下,ok~
推荐教程:《PHP教程》
Atas ialah kandungan terperinci 使用 FastCGI 模式运行 PHP7 教程. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Pelayar Peperiksaan Selamat
Pelayar Peperiksaan Selamat ialah persekitaran pelayar selamat untuk mengambil peperiksaan dalam talian dengan selamat. Perisian ini menukar mana-mana komputer menjadi stesen kerja yang selamat. Ia mengawal akses kepada mana-mana utiliti dan menghalang pelajar daripada menggunakan sumber yang tidak dibenarkan.

DVWA
Damn Vulnerable Web App (DVWA) ialah aplikasi web PHP/MySQL yang sangat terdedah. Matlamat utamanya adalah untuk menjadi bantuan bagi profesional keselamatan untuk menguji kemahiran dan alatan mereka dalam persekitaran undang-undang, untuk membantu pembangun web lebih memahami proses mengamankan aplikasi web, dan untuk membantu guru/pelajar mengajar/belajar dalam persekitaran bilik darjah Aplikasi web keselamatan. Matlamat DVWA adalah untuk mempraktikkan beberapa kelemahan web yang paling biasa melalui antara muka yang mudah dan mudah, dengan pelbagai tahap kesukaran. Sila ambil perhatian bahawa perisian ini

SublimeText3 versi Inggeris
Disyorkan: Versi Win, menyokong gesaan kod!

EditPlus versi Cina retak
Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

SublimeText3 Linux versi baharu
SublimeText3 Linux versi terkini
