Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Apache/FastCGI在PHP中设置环境变量?_Php_Apache_Environment Variables_Cgi_Fastcgi - Fatal编程技术网

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

如何使用Apache/FastCGI在PHP中设置环境变量?,php,apache,environment-variables,cgi,fastcgi,Php,Apache,Environment Variables,Cgi,Fastcgi,我需要定义一个名为SQLANY17的环境变量,该变量应该在PHP中可用(即在标准phpinfo()页面的“环境”下)。PHP是通过FastCGI执行的,我运行的是CentOS 7 x64、Apache 2.4.6和PHP5.5.30 我编辑了/etc/httpd/conf.d/fcgid.conf,它已经存在于我的发行版中。可以使用FcgidInitialEnv定义环境 <IfModule mod_fcgid.c> # ... FcgidInitialEnv SQLANY17

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

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

<IfModule mod_fcgid.c>
  # ...
  FcgidInitialEnv SQLANY17 /opt/sqlanywhere17
</IfModule>
这是同一主机的
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>

ServerName“example.com:80”
ServerAlias“www.example.com”
ServerAlias“ipv4.example.com”
服务器管理员“administrator@example.com"
UseCononicalName关闭
DocumentRoot“/var/www/vhosts/example.com/httpdocs”
CustomLog/var/www/vhosts/system/example.com/logs/access\u log
ErrorLog“/var/www/vhosts/system/example.com/logs/error\u log”
SuexecUserGroup“示例”“psacln”
fcgidsinitialenv PP_CUSTOM_PHP_INI/var/www/vhosts/system/example.com/etc/PHP.INI
FCGidInitialeEnv PP_自定义_PHP_CGI_索引PLEXK-php55-fastcgi
FcgidMaxRequestLen 134217728
SetHandler fcgid脚本
FCGIWrapper/var/www/cgi-bin/cgi\u-wrapper/cgi\u-wrapper.php
选项+执行CGI
选项-Includes-ExecCGI
重新启动发动机
RewriteCond%{HTTP_HOST}^www.example.com$[NC]
重写规则^(.*)$http://example.com$1[L,R=301]

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

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

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

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

<?php
phpinfo();
?>
参考:

你好,谢谢你的帮助。我想为所有应用程序定义该环境变量:服务器是一个使用Plesk管理的虚拟主机,如果没有该环境变量,PHP将无法加载扩展(即SQL Anywhere),从而导致其他Plesk进程崩溃。在我的系统中,php包装器已经由Plesk设置,它是一个二进制文件,而不是shell脚本(/var/www/cgi-bin/cgi\u-wrapper/cgi\u-wrapper)。我不能用你的解决方案。知道为什么我的坏了吗?你确定模块已经加载了吗?根据您发布的内容,它看起来好像没有实际加载。否则,您需要发布整个httpd配置,但请确保您实际上正在加载fastcgi模块,该模块在fastcgi工作的地方。我已经用完整的配置更新了我的问题,正如您所看到的,Nnginx在Apache前面充当代理,而Apache反过来使用FastCGI。
<?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>
#!/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