如何使用Apache / FastCGI在PHP中设置环境变量? [英] How set an environment variable in PHP with Apache/FastCGI?

查看:252
本文介绍了如何使用Apache / FastCGI在PHP中设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一个名为 SQLANY17 的环境变量,此变量应该在PHP中可用(即在标准 phpinfo( )页面)。 PHP通过FastCGI执行,我正在运行CentOS 7 x64,Apache 2.4.6和PHP 5.5.30。



我已经编辑了 / etc / httpd / conf.d / fcgid.conf 已经存在于我的发行版中。



在我的设置中,Nginx代理请求Apache这是 nginx.conf for host example.com:

  server {
listen 192.168.1.131:80;

server_name example.com;
server_name www.example.com;
server_name ipv4.example.com;

client_max_body_size 128m;

root/var/www/vhosts/example.com/httpdocs;
access_log/var/www/vhosts/system/example.com/logs/proxy_access_log;
error_log/var/www/vhosts/system/example.com/logs/proxy_error_log;

if($ host〜* ^ www.example.com $){
重写^(。*)$ http://example.com$1永久;
}

位置/ {
proxy_pass http://192.168.1.131:7080;
proxy_set_header Host $ host;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal / internal-nginx-static-location;
access_log关闭;
}
}

这是 httpd。 conf 对于同一台主机:

 < VirtualHost 192.168.1.131:7080> 
ServerNameexample.com:80
ServerAliaswww.example.com
ServerAliasipv4.example.com
ServerAdminadministrator@example.com
UseCanonicalName Off

DocumentRoot/var/www/vhosts/example.com/httpdocs
CustomLog /var/www/vhosts/system/example.com/logs/access_log
ErrorLog/var/www/vhosts/system/example.com/logs/error_log

< IfModule mod_suexec.c>
SuexecUserGroupexamplepsacln
< / IfModule>

< IfModule mod_fcgid.c>
FcgidInitialEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/example.com/etc/php.ini
FcgidInitialEnv PP_CUSTOM_PHP_CGI_INDEX plesk-php55-fastcgi
FcgidMaxRequestLen 134217728
< / IfModule>

< Directory /var/www/vhosts/example.com/httpdocs>
< IfModule mod_fcgid.c>
< Files〜(\.php $)>
SetHandler fcgid-script
FCGIWrapper / var / www / cgi-bin / cgi_wrapper / cgi_wrapper .php
选项+ ExecCGI
< / Files>
< / IfModule>

选项-Includes -ExecCGI
< / Directory>

< IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond%{HTTP_HOST} ^ www.example.com $ [NC]
RewriteRule ^(。*)$ http://example.com$1 [L,R = 301 ]
< / IfModule>
< / VirtualHost>


解决方案

首先,您需要确保模块已加载。你确定是吗?



PHP应用程序通常使用FcgidWrapper指令和相应的包装器脚本进行配置。包装器脚本可以是定义应用程序所需的任何环境变量的适当位置,例如PHP_FCGI_MAX_REQUESTS或其他任何内容。 (环境变量也可以用FcgidInitialEnv设置,但它们可以应用于所有应用程序。)



下面是一个使用包装器脚本来调用PHP的例子:



PHP应用程序 - /usr/local/phpapp/phpinfo.php

  ;?php 
phpinfo();
?>

配置指令

 code>#FcgidMaxRequestsPerProcess应为< = PHP_FCGI_MAX_REQUESTS 
#示例PHP包装器脚本覆盖默认的PHP设置。
FcgidMaxRequestsPerProcess 10000

#如果
#php.ini中的cgi.fix_pathinfo设置为1,则取消注释以下行:
#FcgidFixPathinfo 1

别名/ phpapp / / usr / local / phpapp /
< Location / phpapp />
AddHandler fcgid-script .php
选项+ ExecCGI
FcgidWrapper / usr / local / bin / php-wrapper .php

#自定义下面的两个指令要求。
订单允许,拒绝
允许从所有
< / Location>

PHP包装器脚本 - / usr / local / bin / php-wrapper

 #!/ bin / sh 
#设置所需的PHP_FCGI_ *环境变量。
#示例:
#PHP默认情况下,FastCGI进程退出500请求后退出。
PHP_FCGI_MAX_REQUESTS = 10000
导出PHP_FCGI_MAX_REQUESTS

#替换为启用FastCGI的PHP可执行文件的路径
exec / usr / local / bin / php-cgi $ b参考:
http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html


I need to define an environment variable named SQLANY17 and this variable should be available in PHP (i.e. under "Environment" in the standard phpinfo() page). PHP is executed via FastCGI and I'm running CentOS 7 x64, Apache 2.4.6 and PHP 5.5.30.

I've edited /etc/httpd/conf.d/fcgid.conf which already exists in my distribution. According to the documentation an environment can be defined using FcgidInitialEnv.

<IfModule mod_fcgid.c>
  # ...
  FcgidInitialEnv SQLANY17 /opt/sqlanywhere17
</IfModule>

However this doesn't work, even after a full machine reboot. Any ideas? I'm sure the fcgid.conf is correcly parsed because typing some random chars prevent the Apache server restart.

In my setup Nginx proxies requests to Apache This is nginx.conf for host example.com:

server {
    listen 192.168.1.131:80;

    server_name example.com;
    server_name www.example.com;
    server_name ipv4.example.com;

    client_max_body_size 128m;

    root "/var/www/vhosts/example.com/httpdocs";
    access_log "/var/www/vhosts/system/example.com/logs/proxy_access_log";
    error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";

    if ($host ~* ^www.example.com$) {
        rewrite ^(.*)$ http://example.com$1 permanent;
    }

    location / {
        proxy_pass http://192.168.1.131:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
        access_log off;
    }
}

And this is httpd.conf for the same host:

<VirtualHost 192.168.1.131:7080 >
    ServerName "example.com:80"
    ServerAlias "www.example.com"
    ServerAlias "ipv4.example.com"
    ServerAdmin "administrator@example.com"
    UseCanonicalName Off

    DocumentRoot "/var/www/vhosts/example.com/httpdocs"
    CustomLog /var/www/vhosts/system/example.com/logs/access_log
    ErrorLog "/var/www/vhosts/system/example.com/logs/error_log"

    <IfModule mod_suexec.c>
        SuexecUserGroup "example" "psacln"
    </IfModule>

    <IfModule mod_fcgid.c>
        FcgidInitialEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/example.com/etc/php.ini
        FcgidInitialEnv PP_CUSTOM_PHP_CGI_INDEX plesk-php55-fastcgi
        FcgidMaxRequestLen 134217728
    </IfModule>

    <Directory /var/www/vhosts/example.com/httpdocs>
        <IfModule mod_fcgid.c>
            <Files ~ (\.php$)>
                SetHandler fcgid-script
                FCGIWrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper .php
                Options +ExecCGI
            </Files>
        </IfModule>

        Options -Includes -ExecCGI
    </Directory>

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
        RewriteRule ^(.*)$ http://example.com$1 [L,R=301]
    </IfModule>
</VirtualHost>

解决方案

First, you need to make sure the module is loaded. Are you sure it is?

PHP applications are usually configured using the FcgidWrapper directive and a corresponding wrapper script. The wrapper script can be an appropriate place to define any environment variables required by the application, such as PHP_FCGI_MAX_REQUESTS or anything else. (Environment variables can also be set with FcgidInitialEnv, but they then apply to all applications.)

Here is an example that uses a wrapper script to invoke PHP:

PHP application - /usr/local/phpapp/phpinfo.php

<?php
phpinfo();
?>

Configuration directives

# FcgidMaxRequestsPerProcess should be <= PHP_FCGI_MAX_REQUESTS
# The example PHP wrapper script overrides the default PHP setting.
FcgidMaxRequestsPerProcess 10000

# Uncomment the following line if cgi.fix_pathinfo is set to 1 in
# php.ini:
# FcgidFixPathinfo 1

Alias /phpapp/ /usr/local/phpapp/
<Location /phpapp/>
AddHandler fcgid-script .php
Options +ExecCGI
FcgidWrapper /usr/local/bin/php-wrapper .php

# Customize the next two directives for your requirements.
Order allow,deny
Allow from all
</Location>

PHP wrapper script - /usr/local/bin/php-wrapper

#!/bin/sh
# Set desired PHP_FCGI_* environment variables.
# Example:
# PHP FastCGI processes exit after 500 requests by default.
PHP_FCGI_MAX_REQUESTS=10000
export PHP_FCGI_MAX_REQUESTS

# Replace with the path to your FastCGI-enabled PHP executable
exec /usr/local/bin/php-cgi

Referenced: http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html

这篇关于如何使用Apache / FastCGI在PHP中设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆