WebFaction
Community site: login faq

I need to set some environment variables for my PHP application, but when I use SetEnv in .htaccess, the variables are ignored.

I've also tried using putenv in PHP, but by the time that runs, PHP is already invoked. I need the variables set before PHP actually runs.

Why isn't SetEnv working, and how do I work around this problem?

This question is marked "community wiki".

asked 31 Dec '15, 20:13

seanf's gravatar image

seanf ♦♦
11.4k21132
accept rate: 37%

wikified 28 Feb, 18:43


For security, we use suexec to ensure that your PHP and CGI scripts run as your user, and not as the system apache user. As an additional security measure, suexec wipes out environment variables before your script is executed.

Since SetEnv sets the environment prior to the invocation of suexec, the result is that suexec is wiping out whatever has been set via SetEnv.

To work around this, you need to wrap your scripts in a handler that sets its own environment prior to calling the actual script. suexec has already cleaned the environment when it runs the wrapper, so any environment variables set by the wrapper will be present when it calls the actual script being run.

Here's a real world example:

You've got a PHP 7 application "app_name" that needs a newer version of libcurl. You've built curl from source in your home directory, and now you need to set the LD_LIBRARY_PATH environment variable so that PHP will pick up your curl libraries

First, create a script ~/webapps/app_name/my_php.cgi with the following contents:

#!/bin/sh

# set the env variables
export LD_LIBRARY_PATH=/home/username/lib

# run the original script via PHP CGI
exec /home/php-cgi/php70.cgi /home/username/webapps/app_name$SCRIPT_URL

Next, make that script executable:

chmod 711 ~/webapps/app_name/my_php.cgi

Finally, add the following to the very top of ~/webapps/app_name/.htaccess:

Action php_custom /my_php.cgi
<FilesMatch .php$>
    SetHandler php_custom
</FilesMatch>

Once all of that is done, requests for your app will be handled by ~/webapps/app_name/my_php.cgi, which sets the environment variables and then runs PHP with the requested URL.

permanent link
This answer is marked "community wiki".

answered 31 Dec '15, 20:14

seanf's gravatar image

seanf ♦♦
11.4k21132
accept rate: 37%

wikified 28 Feb, 18:42

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×247
×11

question asked: 31 Dec '15, 20:13

question was seen: 1,491 times

last updated: 28 Feb, 18:43

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM