Вопрос

I would like to add a linux environment variable for my differents applications written in PHP and Ruby.

Its goal is to differntiate between 'production' and 'development' linux environment.

How to have an linux environment variable (ex : APPLICATION_ENV='production') that can be accessed with PHP and Ruby?

thanks

Edit 1 :

My first solution was :

for Apache/PHP in vhost :

SetEnv APPLICATION_ENV 'production'

for Ruby :

export APPLICATION_ENV='production'
puts ENV['APPLICATION_ENV']

However, this is two places to the same value... There are no solution to merge it in one place ? par exemple to use /etc/environment

Edit :

The answer of my question is detailed here : Inserting Variable Headers in Apache

Это было полезно?

Решение

A simple solution to have a central location for your variable would be to put it in /etc/environment and include it in /etc/init.d/httpd.

$ vim /etc/environment
APPLICATION_ENV=production

$ vim /etc/init.d/httpd
if [ -f /etc/environment ]; then
  . /etc/environment
fi

Then restart apache with /etc/init.d/httpd restart and it should be OK.

If you are using ubuntu 16.04 or similar, replace /etc/init.d/httpd with /etc/init.d/apache2

If you don't want to include environment file to apache, you can directly put your code in /etc/apache2/envvars for example at the end of the file: export HELLO='dear'

Другие советы

For the commandline, do a

export APPLICATION_ENV='production'

prior to calling your code, like Domon suggested. You can write a short wrapper bash script to do all of that in one line, like this

#!/bin/bash
export APPLICATION_ENV='production'
ruby /path/to/your/script.rb

For apache, make sure mod_env is loaded, and include the line

SetEnv APPLICATION_ENV production

in the site's vhost config or .htaccess.

Finally, you can set the APPLICATION_ENV globally on your system (using whatever your distri supports for that) and then simply pass the value to your web app by using

PassEnv APPLICATION_ENV

The most simple way is specifying environment variables directly in shell commands. Then you can access them through the ENV object in Ruby.

For example, create a file called env.rb:

puts ENV['APPLICATION_ENV']

And run it in the shell like this:

APPLICATION_ENV='production' ruby env.rb

It should print out the word "production".

Or, use the export command:

export APPLICATION_ENV='production'
ruby env.rb

You might want to add the export line to some config file. For example, if you use bash, add it to ~/.bashrc and it will get executed every time when you starts a new shell.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top