Loading

openwrt nginx 配置PHP

简介:

如果经常看我的文章,应该知道我这里的一个主路由是X86软路由。

配置还行:

Intel(R) Celeron(R) CPU 3215U @ 1.70GHz

1.80 GiB  24G+500G

只做路由器太浪费了,所以就有了一些跑在路由器上的东西。

只是docker和ipv6貌似不和,或者说我没配置好。有心情再搞它。

最大的作用其实就是WEB服务,nginx为主,基本上什么都行。

openwrt,关于nginx的说明。

https://openwrt.org/docs/guide-user/services/webserver/nginx

一:PHP

  • PHP 是 "PHP Hypertext Preprocessor" 的首字母缩略词
  • PHP 是一种被广泛使用的开源脚本语言
  • PHP 脚本在服务器上执行
  • PHP 没有成本,可供免费下载和使用

二:安装

opkg update && opkg install nginx-all-module luci-ssl-nginx php8 php8-cgi php8-fastcgi php8-fpm

三:PHP配置

增加php配置:/etc/nginx/conf.d/php.locations

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    # Mitigate https://httpoxy.org/ vulnerabilities
    fastcgi_param HTTP_PROXY "";
    #error_log /dev/null;
    fastcgi_connect_timeout 300s;
    fastcgi_read_timeout 300s;
    fastcgi_send_timeout 300s;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 4 32k;
    fastcgi_busy_buffers_size 32k;
    fastcgi_temp_file_write_size 32k;
    client_body_timeout 10s;
    send_timeout 60s; # default, increase if experiencing a lot of timeouts.
    output_buffers 1 32k;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:1026;
    # include the fastcgi_param setting
    include fastcgi_params;

    # SCRIPT_FILENAME parameter is used for PHP FPM determining
    #  the script name. If it is not set in fastcgi_params file,
    # i.e. /etc/nginx/fastcgi_params or in the parent contexts,
    # please comment off following line:
    # fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
}

NGINX网站有一句If you’re using unix socket change fastcgi_pass to:fastcgi_pass unix:/var/run/php-fpm.sock;

根据我们的具体情况,可以改

fastcgi_pass 127.0.0.1:1026;为fastcgi_pass unix:/var/run/php8-fpm.sock;

四:FASTCGI配置

/etc/nginx/fastcgi_params

默认配置似乎有问题,因为我看nginx关于fastcgi的配置和openwrt中fastcgi配置有不同

openwrt推荐的有,nginx推荐的没有:

fastcgi_param  HTTPS              $https if_not_empty;
fastcgi_param  REQUEST_SCHEME     $scheme;                                  

nginx推荐的有,openwrt推荐的没有:

fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https;
fastcgi_param  PATH_INFO          $fastcgi_path_info;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

nginx有一句Please note if you’re using Ubuntu Precise (12.04), I change SCRIPT_FILENAME and add PATH_INFO params.

似乎是这个问题导致默认配置无法运行。

 我结合两个配置文件合并为:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  PATH_INFO          $fastcgi_path_info;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

五:测试

在/www目录准备两个文件:

test.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>测试页</title>
</head>
<body>
    <span>HTML测试</span>
</body>
</html>
View Code

test.php

<?php
    phpinfo();
?>
View Code

此时访问http://openwrt的IP/test.html

此时访问http://openwrt的IP/test.php

均正常显示不报错即可

 

六:注意事项

/etc/config/php8-fastcgi

配置了fastcgi的端口,openwrt的nginx,默认是1026

检查你的1026端口是否监听

netstat -antup | grep 1026

 

如果没有,重启一下php8-fastcgi

posted @ 2022-05-14 22:32  上官飞鸿  阅读(3224)  评论(0编辑  收藏  举报