Nginx 服务器建立与php语言的解析_nginx php
warning:
这篇文章距离上次修改已过202天,其中的内容可能已经有所变动。
要让Nginx解析PHP请求,你需要使用PHP-FPM(FastCGI Process Manager),它是一个PHP FastCGI实现,用以提高Web应用在PHP环境下的执行效率。以下是配置Nginx与PHP-FPM的基本步骤:
- 确保PHP-FPM已安装并运行。
- 配置Nginx服务器块(server block)来处理PHP请求。
以下是一个简单的Nginx配置示例,用于将PHP请求代理给PHP-FPM:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在这个配置中:
listen
指定监听的端口。server_name
指定域名。root
指定网站根目录。index
指定默认页面。location /
处理正常的静态文件请求。location ~ \.php$
是一个正则表达式,匹配所有以.php
结尾的请求。fastcgi_pass
指定PHP-FPM监听的地址,这里使用了Unix socket文件。SCRIPT_FILENAME
指定要执行的PHP脚本文件路径。include
指令包含了一些默认的参数配置。
确保你的PHP-FPM服务已启动,并监听正确的socket或端口。如果你的PHP-FPM服务的配置不是默认的,请相应地修改fastcgi_pass
指令中的路径。
这个配置假设你的PHP-FPM配置文件位于/etc/php/7.4/fpm/pool.d/www.conf
,并且你使用的是PHP 7.4版本。根据你的PHP版本和安装路径,可能需要调整fastcgi_pass
和include snippets/fastcgi-php.conf
中的路径。
评论已关闭