配置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倍。
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsswm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值