配置php-fpm服务

nginx(unix domain socket方式)

server {
  listen 80;
  #root /test/php/public

  location / {
    #URL重写 例如隐藏index.php
    if (!-f $request_filename) {
      rewrite  ^(.*)$  /index.php?s=/$1  last;
      break;
    }
  }

  location ~ [^/]\.php(/|$) {
    #try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_pass unix:/tmp/php/var/run/php-fpm.sock;
    #pathinfo给fastcgi权限 可支持?s=/module/controller/action的url访问模式
    fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    include fastcgi_params;
  }
}

apache(unix domain socket方式)

<VirtualHost *:80>
  DocumentRoot "/test/php/public"
  ServerName _
  #ServerAlias _
  #ServerAdmin _
  #errorDocument 404 /404.html
  ErrorLog "logs/htdocs-error.log"
  CustomLog "logs/htdoc-access.log" combined

  #DENY FILES
  <Files ~ (\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)$>
    Order allow,deny
    Deny from all
  </Files>

  <FilesMatch \.php$>
    SetHandler "proxy:unix:/tmp/php/var/run/php-fpm.sock|fcgi://localhost"
  </FilesMatch>

  <Directory "/test/php/public">
    SetOutputFilter DEFLATE
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    DirectoryIndex index.php index.html
  </Directory>
</VirtualHost>

防跨目录设置-open_basedir

使用open_basedir可以限制程序可操作的目录和文件,提高系统安全性。
但会影响I/O性能导致系统执行变慢,因此需要根据具体需求,在安全与性能上做平衡,必经任何事物对于每个人的态度不一样嘛。
使用open_basedir会将realpath_cache_size设置为0,从而禁用realpath缓存。

用open_basedir指定的限制实际上是前缀,不是目录名。
也就是说open_basedir=/test/php/public也会允许访问/test/php/public_zxc。
如果要将访问限制为目录,使用斜线结束路径名,例如:open_basedir=/test/php/public/。
如果要设置多个目录,window使用;分隔目录,Linux使用:分隔目录。例如:open_basedir=/test/php/public/:/tmp。
也可加上:/proc/。

php.ini中

open_basedir = /test/php/public/:/tmp/

在程序中

ini_set('open_basedir', '/test/php/public/');

nginx或apache中

fastcgi_param PHP_VALUE "open_basedir=/test/php/public/:/tmp/"

.user.ini文件(在php项目根目录添加)

  • chattr -i .user.ini 解锁(可编辑)
  • chattr +i .user.ini 加锁(不可编辑)
open_basedir = /test/php/public/:/tmp/

测试

<?php
declare (strict_types=1);

function getMicroTime(): float
{
    list($useC, $sec) = explode(' ', microtime());
    return (float)$useC + (float)$sec;
}

/** 记录开始时间 */
$startTime = getMicroTime();

/* 读取10000次文件 */
for ($i = 0; $i < 10000; $i++) {
    file_get_contents('test.txt');
}

/* 记录结束时间 */
$endTime = getMicroTime();

printf('run time %f ms' . PHP_EOL, (($endTime) - ($startTime)) * 1000);
关闭open_basedir测试
run time 150.619030 ms
打开open_basedir测试
run time 600.969076 ms
开启open_basedir后,执行时间接近关闭的4倍。

相关推荐

  1. 配置php-fpm服务

    2024-07-22 08:54:01       19 阅读
  2. PHP-FPM 的主要作用和功能:

    2024-07-22 08:54:01       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 08:54:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 08:54:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 08:54:01       45 阅读
  4. Python语言-面向对象

    2024-07-22 08:54:01       55 阅读

热门阅读

  1. 【机器学习框架TensorFlow和PyTorch】基本使用指南

    2024-07-22 08:54:01       16 阅读
  2. 华为eNSP模拟器安装

    2024-07-22 08:54:01       15 阅读
  3. HTTP协议的演进:从HTTP/1.0到HTTP/2.0

    2024-07-22 08:54:01       13 阅读
  4. 在Ubuntu 14.04上安装和使用Docker Compose的方法

    2024-07-22 08:54:01       16 阅读
  5. 【自动化机器学习AutoML】AutoML工具和平台的使用

    2024-07-22 08:54:01       16 阅读
  6. 【数据挖掘基础】数据挖掘技术概述和基本算法

    2024-07-22 08:54:01       16 阅读
  7. 常用传感器误差补偿方法介绍

    2024-07-22 08:54:01       16 阅读
  8. ARM/Linux嵌入式面经(十七):美团校招面经

    2024-07-22 08:54:01       15 阅读
  9. 深度学习简介(框架)

    2024-07-22 08:54:01       15 阅读
  10. ChatGPT的工作记忆容量:一项实证研究

    2024-07-22 08:54:01       14 阅读
  11. AI学习指南机器学习篇-SOM的拓扑结构与参数调优

    2024-07-22 08:54:01       16 阅读
  12. 如何调整图像的窗宽窗位

    2024-07-22 08:54:01       15 阅读
  13. linux字符设备驱动+fops应用测试程序

    2024-07-22 08:54:01       15 阅读
  14. opencv—常用函数学习_“干货“_14

    2024-07-22 08:54:01       16 阅读
  15. 网络安全防线:黑龙江等级保护测评标准详解

    2024-07-22 08:54:01       16 阅读
  16. thinkphp8结合layui2.9 图片上传验证

    2024-07-22 08:54:01       14 阅读