如何使用Apache / FastCGI在PHP中设置环境变量?

时间:2016-01-11 14:26:05

标签: php apache environment-variables cgi fastcgi

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

我已经编辑了/etc/httpd/conf.d/fcgid.conf已经存在于我的发行版中。 According to the documentation可以使用FcgidInitialEnv定义环境。

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

然而,即使在整机重启后,这也不起作用。有任何想法吗?我确定fcgid.conf被正确解析,因为键入一些随机字符会阻止Apache服务器重启。

enter image description here

在我的设置中,Nginx代理对Apache的请求这是主机example.com的nginx.conf

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;
    }
}

对于同一主机,这是httpd.conf

<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>

1 个答案:

答案 0 :(得分:0)

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

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

以下是使用包装器脚本调用PHP的示例:

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

<?php
phpinfo();
?>

配置指令

# 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包装器脚本 - / 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

引用: http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html