Heim  >  Fragen und Antworten  >  Hauptteil

Legen Sie php.ini-Anweisungen für jede Site fest

<p>Ich habe mehrere Websites, die unter Apache2 und PHP auf Ubuntu Server laufen. Ich weiß, dass PHP eine php.ini-Datei hat, mit der Sie die Werte für <code>upload_max_filesize</code>, <code>upload_tmp_dir</code> festlegen können. Dadurch wird jedoch ein Wert auf alle Websites angewendet. </p> <p>Wie lege ich Richtlinien für jede Site fest? Beispiel: Ich möchte <code>upload_max_filesize</code> von <code>sitea.com</code> auf 50 MB und <code>upload_max_filesize</ von <code>siteb.com< festlegen ;/code> code> ist auf 5M eingestellt. </p> <p>Ebenso möchte ich für jede Site einen eindeutigen <code>session.name</code> festlegen. Wie kann dies erreicht werden? Ich habe gelesen, dass es etwas gibt, das <code>PHP_INI_USER</code>, <code>PHP_INI_PERDIR</code>, <code>PHP_INI_SYSTEM</code>, <code>PHP_INI_ALL</code> heißt Ich tue es? </p>
P粉283559033P粉283559033391 Tage vor495

Antworte allen(2)Ich werde antworten

  • P粉320361201

    P粉3203612012023-08-28 22:34:33

    Mike 'Pomax' Kamermans 的明确评论说明应如何完成此操作。然而 PHP 手册文档可能有点令人反感。

    还有一些关于其他答案的澄清:

    • 使用 PHP 解析器文件(如 Lajos Arpad 中所述)会增加一些安全风险和一堆编码、语法和一些确实不需要的处理开销。

    • .htaccess 确实可以为某些 PHP 安装设置自定义 ini 指令,但如果您使用的是“FastCGI”或“CGI”(可能还有 suPHP) ) PHP 安装实际上会导致您的网站崩溃并出现 500 错误,因此请使用本地 .user.ini 文件,如此处所述。 如何找到我的 SAPI?

    • 如果您将 PHP 作为 Apache 模块(例如 mod_php)运行,请使用 .htaccess如何找到我的 SAPI?

    PHP suPHP/FastCGI/CGI SAPI

    那么,你的愿望应该如何完成呢?

    1) 查找 Core PHP.ini 详细信息。

    读取当前 PHP 版本的 PHP.ini 文件。

    在第 180 行(对于 PHP 8.1),应该是这样的:

    记下该值,我建议自定义该值(如本示例所示)。该值是将位于服务器上每个唯一帐户中的文件的文件名,保存全局 PHP.ini 文件的帐户特定“本地”设置。

    该文件通常位于 public_html 文件夹中,因此应以 . 开头,以便默认设置为隐藏(稍后会详细介绍)。如果该文件不存在,则不会更改帐户的核心 PHP 设置。

    2) 记住/设置文件名并创建新规则

    所以您现在知道/已经设置了自定义 PHP 文件的名称;现在在您的帐户中生成文件,然后您可以设置要单独为此帐户自定义的 PHP.ini 设置。

    例如;

    将包含以下示例信息:

    ;
    ; Some optional comment text here for human consumption
    ;
    session.cookie_secure=1
    session.save_path=/home/sitea.com_account/sessions
    session.name=session-sitea
    error_log=/home/sitea.com_account/errors/PHP_errors.log
    upload_max_filesize=50M

    此信息由 PHP 解析,并覆盖核心 PHP.ini 中的任何相应值,但仅限于该帐户。

    3) 根据需要为每个帐户进行自定义

    例如,对于您的 siteb.com php 用户 ini 文件,它看起来更像是这样:

    将包含以下示例信息:

    ;
    ; Some optional comment text here for human consumption
    ;
    session.cookie_secure=1
    session.save_path=/home/siteb.com_account/session_b
    session.name=site-b-UniqueSession
    error_log=/home/siteb.com_account/errors/PHP_errors.log
    upload_max_filesize=5M

    然后,您可以通过探索每个帐户上的 phpinfo() 并注意显示的本地和核心差异来检查这些帐户设置是否正确设置。

    4) 测试并确认

    /public_html/ 路径中的核心 user.ini 文件应该是该帐户上要处理的每个子文件/文件夹所需的全部文件(这有时取决于您的确切 LAMP 服务器设置和你的 PHP 处理程序)。

    正如我所说,一旦设置了测试 ini 文件,您应该运行 phpinfo() 来检查其值是否已实现。

    如果您不想使用 phpinfo() 那么您可以轻松使用 [ini_get](https://www.php.net/manual/en/function .ini-get) 将敏感数据转储到错误日志而不是浏览器。

    <?php
    error_log("ini data for this account: ".ini_get('upload_max_filesize'));
    exit;

    5) 安全性

    user.ini 文件通常以 . 字符开头,以将其隐藏在目录中,但这并不意味着浏览器代理无法访问它,因此最好添加几行添加到您的 .htaccess 以拒绝对此文件的访问。

    <Files .account-php81.ini>
        Require all denied
    </Files>

    一些最终想法:

    核心 PHP.ini 值在新版本的 PHP 中确实会发生变化,因此最好的做法是针对不同的 PHP 版本使用不同的本地 .ini 文件 (.account-php80.ini, <代码>.account-php81.ini等)。请注意,每个 php.ini 核心都需要显式调用其各自的本地 user.ini 文件,如上面步骤 1 中所述。

    上述原则概述于PHP手册,必须注意:

    Antwort
    0
  • P粉144705065

    P粉1447050652023-08-28 14:10:59

    您可以使用每个站点(甚至每个文件夹)的 .htaccess 文件来设置 PHP 配置值 - 至少对于大多数设置:如果您查看 在配置指令文档中,对于标记为 PHP_INI_PERDIR 或 < code>PHP_INI_ALL 您可以使用 PHP php_value 或 php_flag 命令在 .htaccess 文件中设置这些内容="https://www.php.net/manual/en/configuration.changes.php" rel="nofollow noreferrer">“如何更改配置设置”文档。

    例如,要在网站中设置 upload_max_filesize,请在该网站的文档根目录中创建一个 .htaccess 文件,并在其中放入文本:

    php_value upload_max_filesize 24M

    不幸的是,max_file_uploadsupload_tmp_dir 是标记为 PHP_INI_SYSTEM 的设置,您无法在 .htaccess 中更改它们文件...

    Antwort
    0
  • StornierenAntwort