如何为每个虚拟主机创建自定义 php.ini 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22309620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:49:29  来源:igfitidea点击:

How do I create custom php.ini files for each virtual host?

phpapacheeasyphp

提问by Joao

I've installed EasyPHP WAMP for local development only (I'm not hosting any websites).

我只为本地开发安装了 EasyPHP WAMP(我没有托管任何网站)。

Is there a way to set custom php settings for separate virtual hosts?

有没有办法为单独的虚拟主机设置自定义 php 设置?

Currently and out-of-the-box, the php.inifile is loaded from: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion\php.iniIt would be nice if, say, I could drop in a custom php.inifile into the virtual host directory to override settings in the original php.iniThis way, I could better emulate a production server's environment on a per-site basis.

目前和开箱即用,php.ini文件是从以下位置加载的:C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion\php.ini如果我可以将自定义php.ini文件放入虚拟主机目录以覆盖原始文件中的设置,那就太好了。php.ini这样,我可以更好地模拟基于每个站点的生产服务器环境。

I've seen this work with online hosting accounts. But I can't figure out how to make this work on my machine.

我已经在在线托管帐户中看到了这项工作。但我不知道如何在我的机器上进行这项工作。

回答by álvaro González

Using custom php.inifiles is pretty straighforward for CGI/FastCGI based PHP installations but it isn't feasible when running PHP as Apache module (mod_php) because the whole server runs a single instance of the PHP interpreter.

php.ini对于基于 CGI/FastCGI 的 PHP 安装,使用自定义文件非常简单,但在将 PHP 作为 Apache 模块 (mod_php) 运行时这是不可行的,因为整个服务器运行 PHP 解释器的单个实例。

My advice:

我的建议:

  • Set from PHP itself as many settings as you can:

    ini_set('memory_limit', '16M');
    date_default_timezone_set('Europe/Madrid')
    ...
    

    In other words, directives that can be changed at runtime.

  • Set the rest of stuff from per-directory Apache setting files (aka .htaccess):

    php_flag short_open_tag off
    php_value post_max_size 50M
    php_value upload_max_filesize 50M
    

    i.e., settings that need to be defined before the script starts running

  • 从 PHP 本身设置尽可能多的设置:

    ini_set('memory_limit', '16M');
    date_default_timezone_set('Europe/Madrid')
    ...
    

    换句话说,可以在运行时更改的指令。

  • 从每个目录的 Apache 设置文件(又名.htaccess)中设置其余内容:

    php_flag short_open_tag off
    php_value post_max_size 50M
    php_value upload_max_filesize 50M
    

    即,需要在脚本开始运行之前定义的设置

Please have a look at the Runtime Configurationfor further details.

请查看运行时配置以获取更多详细信息。

Sometimes, you'll actually need differentsettings in development and production. There're endless ways to solve that with PHP code (from creating a boolean constant from the $_SERVER['HTTP_HOST']variable to just having a config.phpfile with different values) but it's trickier with .htaccess. I normally use the <IfDefine>directive:

有时,您实际上需要在开发和生产中进行不同的设置。用 PHP 代码有无数种方法可以解决这个问题(从从$_SERVER['HTTP_HOST']变量创建一个布尔常量到仅仅拥有一个config.php具有不同值的文件),但使用.htaccess. 我通常使用<IfDefine>指令

<IfDefine DEV-BOX>
    #
    # Local server directives
    #
    SetEnv DEVELOPMENT "1"

    php_flag display_startup_errors on
    php_flag display_errors on
    php_flag log_errors off
    #php_value error_log ...
</IfDefine>
<IfDefine !DEV-BOX>
    #
    # Internet server directives
    #
    php_flag display_startup_errors off
    php_flag display_errors off
    php_flag log_errors on
    php_value error_log "/home/foo/log/php-error.log"
</IfDefine>

... where DEV-BOXis a string I pass to the local Apache command-line:

...DEV-BOX我传递给本地 Apache 命令行的字符串在哪里:

C:\Apache24\bin\httpd.exe -D DEV-BOX

If you run Apache as service, the -D DEV-BOXbit can be added in the Windows registry, e.g.:

如果您将 Apache 作为服务运行,则-D DEV-BOX可以在 Windows 注册表中添加该位,例如:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Apache2.4\Parameters\ConfigArgs


Related: Find out how PHP is running on server (CGI OR fastCGI OR mod_php)

相关:了解 PHP 如何在服务器上运行(CGI 或 fastCGI 或 mod_php)

回答by Sontae

Simple way to use custom php.ini file for vhost using Fast CGI is to copy the php.ini into a folder in the host like "customini".

使用 Fast CGI 为 vhost 使用自定义 php.ini 文件的简单方法是将 php.ini 复制到主机中的文件夹中,如“customini”。

After that to your vhost directive and add this simple line :

之后到您的 vhost 指令并添加以下简单行:

FcgidInitialEnv PHPRC "/path_to_your_custom_ini_dir_for_this_vhost/"

BE SURE TO HAVE/to your path no \, (it won't work with \)

一定要有/你的路径不\,(它不会与\

Restart Apache.

重新启动阿帕奇。

That's all!

就这样!

Full sample (on Windows Server here) :

完整示例(在此处的 Windows Server 上):

 <VirtualHost *:80>
    DocumentRoot "C:/APACHE/htdocs/vhost/myvhost"
    ServerName www.vhost.com
    FcgidInitialEnv PHPRC "C:/APACHE/customini/myvhost/"
 </VirtualHost>

回答by Anthony Rutledge

Developing Multiple Domains on One Machine?

在一台机器上开发多个域?

Embedding php.ini settings in the httpd-vhost.conf, typically found in your server root under conf/extra/, is a great way to solve this common problem. If you never knew you could do this, see the PHP.net Manual under How To Change Configuration Settings. This will solve the pesky include_pathproblem, without adding configuration code to your bootstrapping code or anything else.

在 中嵌入 php.ini 设置httpd-vhost.conf(通常在您的服务器根目录下conf/extra/)是解决此常见问题的好方法。如果您从不知道您可以这样做,请参阅 PHP.net 手册下的如何更改配置设置。这将解决讨厌的include_path问题,而无需向引导代码或其他任何内容添加配置代码。

Of course, to use this effectively as localhost, you would need to make copies of a <VirualHost>block and configure each accordingly. Then, comment out all virtual host blocks exceptthe one that you want to use!

当然,要有效地将其用作localhost,您需要制作一个<VirualHost>块的副本并相应地配置每个。然后,注释掉所有虚拟主机块,除了要使用的块!

Alternatively, one could start Apache with the -foption to point the server daemon to a different httpd.confupon starting. Each httpd.confwould require an "if module block," such as and <IfModule phpx_module>block. Be sure to remember to account for Apache logging!

或者,可以-f选择httpd.conf在启动时将服务器守护程序指向不同的选项来启动 Apache 。每个httpd.conf都需要一个“if 模块块”,例如和<IfModule phpx_module>块。一定要记住考虑 Apache 日志记录!

httpd -f /usr/local/apache2/conf/domains/fooDomain.conf

httpd -f /usr/local/apache2/conf/domains/fooDomain.conf

httpd -f /usr/local/apache2/conf/domains/barDomain.conf

httpd -f /usr/local/apache2/conf/domains/barDomain.conf

Virtual Host Block With php.inistatements.

带有php.ini语句的虚拟主机块。

<VirtualHost 127.0.0.1:80>
    UseCanonicalName On
    ServerName localhost:80
    ServerAdmin you@localhost        
    CustomLog "/var/www/someDomain.com/data/logs/httpd_access_log" common
    ErrorLog "/var/www/someDomain.com/data/logs/httpd_error_log"
    LogLevel warn
    DocumentRoot "/var/www/someDomain.com/public"

    <Directory "/var/www/someDomain.com/public">
        # disable directory listing
        Options -Indexes +FollowSymLinks
        AllowOverride FileInfo
        Require all granted
    </Directory>

    <IfModule alias_module>
        # Alias /webpath /full/filesystem/path
        ScriptAlias /cgi-bin/ "/var/www/someDomain.com/scripts/cgi-bin/"
    </IfModule>

    <IfModule php5_module>
        # Use php7_module in opening statement above if on PHP 7+

        # Domain specific PHP configuration options.
        # The Apache process user must own any of the following directories.

        php_admin_value include_path "/var/www/someDomain.com/application/controllers:/var/www/someDomain.com/application/models:/var/www/someDomain.com/application/views"

        # Errors and Logging
        php_admin_flag display_startup_errors off
        php_admin_flag display_errors off
        php_admin_flag html_errors off
        php_admin_flag log_errors on
        php_admin_value error_log "/var/www/someDomain.com/data/logs/php_error_log"

        # File Related
        php_admin_flag file_uploads on
        php_admin_value upload_tmp_dir "/var/www/someDomain.com/data/uploads"
        php_admin_value upload_max_filesize 10M
        php_admin_value max_file_uploads 5

        # Sessions
        php_value session.save_handler "files"
        php_value session.save_path "/var/www/someDomain.com/data/sessions"

        # Caching
        php_value soap.wsdl_cache_dir "/var/www/someDomain.com/data/cache/sopa.wsdl"
    </IfModule>
</VirtualHost>

If you could use the %{SERVER_NAME} variable in conjunction with the <IfModule phpx_module> blocks to form a compound conditional, you could have just onehttpd.conf`, or include a extra/php.conf with all the domain specific PHP.ini settings (in blocks, also). However, as long as "localhost" is the domain target, it will not do what you want. Thus, my answer in the virtual host block above.

如果您可以将 %{SERVER_NAME} 变量与<IfModule phpx_module> blocks to form a compound conditional, you could have just onehttpd.conf`结合使用,或者包含一个 extra/php.conf 以及所有域特定的 PHP.ini 设置(也以块为单位)。但是,只要“localhost”是域目标,它就不会执行您想要的操作。因此,我在上面虚拟主机块中的回答。

回答by Sean Quinn

Several commentors have mention Vagrantwhich is an excellent solution.

一些评论者提到了Vagrant,这是一个很好的解决方案。

If you're not interested in using Vagrant, you can investigate using FastCGI as your interface to Apache and using the SetEnv PHPRC...suggestion proposed on this blog:

如果您对使用 Vagrant 不感兴趣,您可以调查使用 FastCGI 作为 Apache 的接口并使用SetEnv PHPRC...博客上提出的建议:

Apache & PHP: Multiple PHP.ini Configuration Files (Sunday, February 8, 2009)

Source:http://hyponiq.blogspot.com/2009/02/apache-php-multiple-phpini.html

The second work-around is to use the PHPRC environment variable configurable in Apache using the SetEnv directive. This directive allows you to set an environment variable that is then passed to any CGI script and/or Server Side Include (SSI) set in any static (x)HTML page (or other document type). In this instance, you'd be telling each PHP-CGI instance where to find its configuration settings. The example would be (coinciding with the previous one):

# vhosts.conf
NameVirtualHost *:81
<VirtualHost *:81>
  ServerAdmin [email protected]
  ServerName vhost.example.com
  DocumentRoot "C:/path/to/doc/root"
  ErrorLog "logs/vhost.example.com-errors.log"

  # Set the PHPRC environment variable
  SetEnv PHPRC "C:/path/to/doc/root/php.ini"

  <Directory "C:/path/to/doc/root">
    # ... yadda, yadda, yadda ...
    # you get the point!
  </Directory>
</VirtualHost>

However, the PHP documentation changing the runtime configurationsuggests that you can use certain values like php_valueto try loaalong with, possibly the SetEnvdirective.

Apache 和 PHP:多个 PHP.ini 配置文件(2009 年 2 月 8 日,星期日)

来源:http : //hyponiq.blogspot.com/2009/02/apache-php-multiple-phpini.html

第二种解决方法是使用可在 Apache 中使用 SetEnv 指令配置的 PHPRC 环境变量。此指令允许您设置一个环境变量,然后将其传递给任何静态 (x)HTML 页面(或其他文档类型)中设置的任何 CGI 脚本和/或服务器端包含 (SSI)。在这种情况下,您将告诉每个 PHP-CGI 实例在哪里可以找到其配置设置。这个例子是(与上一个一致):

# vhosts.conf
NameVirtualHost *:81
<VirtualHost *:81>
  ServerAdmin [email protected]
  ServerName vhost.example.com
  DocumentRoot "C:/path/to/doc/root"
  ErrorLog "logs/vhost.example.com-errors.log"

  # Set the PHPRC environment variable
  SetEnv PHPRC "C:/path/to/doc/root/php.ini"

  <Directory "C:/path/to/doc/root">
    # ... yadda, yadda, yadda ...
    # you get the point!
  </Directory>
</VirtualHost>

但是,更改运行时配置的 PHP 文档 建议您可以使用某些值,例如php_valuetry loaalong with,可能是SetEnv指令。

He suggests another option not using FastCGI, instead leveraging the php_value, php_flagetc. configuration options.

他建议不使用另一种选择的FastCGI,而是借力php_valuephp_flag等配置选项。

Other's seem to have had some success with executing scripts through FastCGI though, see: Separate php.ini for different virtual hosts

不过,其他人似乎在通过 FastCGI 执行脚本方面取得了一些成功,请参阅:针对不同虚拟主机的单独 php.ini

Other suggestions involve the PHPINIdirective, which may be pertinent for your needs, see: How do I limit PHP apps to their own directories and their own php.ini?

其他建议涉及该PHPINI指令,这可能与您的需求有关,请参阅:如何将 PHP 应用程序限制为它们自己的目录和它们自己的 php.ini?