1

On FreeBSD, I need NODE_ENV=production and other systemwide environment variables to be set on startup, before nginx fires up.

Which is the right place i.e. file I do that?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
LongHike
  • 4,016
  • 4
  • 37
  • 76
  • How do you run nginx? Manually, or as a service (`/etc/rc.conf`)? – arrowd Feb 04 '20 at 07:14
  • Thanks for asking, sorry I wasn't specific. I run it as a service. – LongHike Feb 04 '20 at 07:25
  • Then, I think, the correct solution would be to set env vars by Nginx. Something like https://stackoverflow.com/a/8331663/637669 – arrowd Feb 04 '20 at 08:12
  • That's what I am doing at the moment. But I want need to have some of the variables at other places too. Therefore I'd like them to be available globally. – LongHike Feb 04 '20 at 09:55

2 Answers2

4

Also, if you'd like to set some environment variables for an rc(8) service then you might also take a look at the ${name}_env and ${name}_env_file variables described in rc.subr(8). They allow you to set environment variables for services on FreeBSD in rc.conf(5), e.g.:

nginx_enable="YES"
nginx_env="NODE_ENV=production"
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
3

One option could be to add your environment variables to /etc/login.conf in the setenv capability, for example:

default:\
    :passwd_format=sha512:\
    :copyright=/etc/COPYRIGHT:\
    :welcome=/etc/motd:\
    :setenv=MAIL=/var/mail/$,BLOCKSIZE=K,NODE_ENV=production:\
    ...

From the login.conf man:

setenv          list           A comma-separated list of
                               environment variables and
                               values to which they are to
                               be set.

If you modity the /etc/login.conf file, don't forget to run:

cap_mkdb /etc/login.conf
nbari
  • 25,603
  • 10
  • 76
  • 131