0

Maybe a silly question, but how do I pass environment variables in apache. I have enabled the LoadModule env_module modules/mod_env.so statement in apache's httpd.conf file and I know from documentation that I should use the statement PassEnv env-variable [env-variable]. But where exactly do I need to add this line (in which file and on what position)?

I.e. I want my system variable

PassEnv PYTHONPATH C:\Python\DLLs;C:\Python\lib;C:\Python;C:\Python\lib\site-packages

to be propagated to my web-server. Where do I put it?

Sorry, I'm an absolute beginner with apache and can't seem to find any step-by-step instructions for this problem.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
N. Maks
  • 539
  • 3
  • 15

1 Answers1

1

You can set the environment variable in a .htaccess file, which needs to be placed in the root of your website:

PassEnv PYTHONPATH C:/Python/DLLs;C:/Python/lib;C:/Python;C:/Python/lib/site-packages

Notice that I use forwards slashes in the path so that apache won't interpret them as escape characters.

To enable the use of .htaccess files you also need to make sure so the webroot (in other words the DocumentRoot) has AllowOverride All in the configuration:

<Directory "C:/WEBROOT">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • Thank you for the quick reply! I have created a EnvVar.htaccess file with the comand line from above and placed it to the website root (C:\Apache24\htdocs\Test). However it seems to have no effect...when I call my pythonscript python.py (C:\Apache24\htdocs\Test\cgi-bin) which simply executes: `print("PYTHONPATH" in os.environ)` I get `False` as an output. Am I missing something? – N. Maks Aug 14 '17 at 08:35
  • 1
    The filename should be `.htaccess` not `EnvVar.htaccess`. To be sure that the path will get correctly parsed by apache I should also use forward slashes i.e `PassEnv PYTHONPATH C:/Python\DLLs;C:/Python/lib;C:/Python;C:/Python/lib/site-packages` – Cyclonecode Aug 14 '17 at 08:45
  • Did the changes, restarted apache and even physically restarted the pc but still no effect. Could it be a permission problem? – N. Maks Aug 14 '17 at 09:29
  • You can try to check the log files - I don't know where it might be located on windows, but you should be able to check using something like `apache2ctl -S` or `httpd -S` I think – Cyclonecode Aug 14 '17 at 09:32
  • @N.Maks - I updated my answer. If you need more help I could guide you through it in the chat instead – Cyclonecode Aug 14 '17 at 09:37
  • I created a chat for this topic: https://chat.stackoverflow.com/rooms/151874/windows-apache24-setenv – Cyclonecode Aug 14 '17 at 09:42
  • Thank you Cyclonecode for your support! I acctually managed to find a workaround, by setting `os.environ['PYTHONPATH']='C:/Python/lib/site-pa‌​ckages'` within the python script itself and it seems to propagate it when being called via a web request. – N. Maks Aug 14 '17 at 10:14