Set an environment variable in linux for lucee apps

Hi Gang - tis me yet again!

I have Lucee running on an AWS Linux 2, instance.
And I need to set an OS environment variable that I can observe via
#server.system.environment.<MYVAR>#

On my local, dev, machine - which is Windows - I can set a “system” environment variable and it is available to use in my CFML code.

I have tried adding it to /etc/environment
MYVAR=myVal

And also tried with quotes
MYVAR="myVal"

With a server restart after each change - but the variable is still missing, when I dump the server scope - and of course my code doesn’t work as expected because it relies on #server.system.environment.<MYVAR>#

Once I bring up the console (via AWSs “Management Console”);
echo $MYVAR
returns nothing.

If I sudo -i
then echo $MYVAR
it correctly retuns myVal, as expected.

As always - thanks very much!
Gavin.

In NIX unless your app user has system permissions, you may not get what you are looking for.

Instead run a bashscript via cfexecute to run a uname -a or cat /etc/issue and pipe the results to a file in with the read permission for the user or group in the lucee path.

Or you could play with Catalina logs, and determine what is running
Or you could just play with cgi scope, which is easiest
cgi.server_software
that should return if its windows, linux, freebsd, ectra…

On Linux you can also set these variables with tomcats setenv.sh file as shown in the docs here

Sorry - I forgot to mention - that I tried that first, actually.

dumping the server scope, show;
CATALINA_BASE string /usr/share/tomcat/current
CATALINA_HOME string /usr/share/tomcat/current
CATALINA_OPTS string -Xms256m -Xmx512m -server -XX:+UseParallelGC
CATALINA_PID string /usr/share/tomcat/current/temp/tomcat.pid
HOME string /usr/share/tomcat

So I added setenv.sh to /usr/share/tomcat/current/bin/
changes the owner and group to tomcat - so ity matched the other files in the directory
and also added R and X attributes for all.

The variable doesn’t appear anywhere.

How did you install Lucee/Tomcat? I think the location you are using is not the default installation location if you use Lucee installer. Usually (with Ubuntu) its in /opt/lucee/tomcat/bin

Outside of docker (where env vars are completely extracted from the container and managed by the orchestration layer), env vars a bit of a pain because they either end up in

  • operating system config
  • CF engine installation config

and neither of those locations are easy to manage, portable, or consistent across environments. Since I was just bothering you on another thread about how CommandBox would simplify your rewrites… handling env vars across environments is also many times easier. You just put this in your server.json

{
  "env" : {
    "MYVAR" : "myVal"
  }
}

or drop an .env file in the web root by convention with

MYVAR=myVal

and now you’ve got a nice portable way to use those env vars in dev, staging, or prod, and it’s baked into your repo if you want and part of your deploy to the web root, not spread all across the hard drive and OS-dependant. :slight_smile:

** .env file requires commandbox-dotenv module to be installed.

Create a simple bash script, call it env-get.sh

if you are not sure
sudo vim /etc/env-get.sh

#!/bin/bash
#this gets the env and places it in webroot for lucee 
#show all variable
#printenv
#show all and create or overwrite a file in /var/www/html
printenv >> /var/www/html/myenv.rocks

touch /var/www/html/myenv.rocks
chown www-data:www-data /var/www/html/myenv.rocks
now chmod +x env-get.sh
now you can either add that to the /etc/rc.local/ or anacron (daily, hourly, whatever interval you like) or crontab

Now you have a solution that works in your environment, and if you get bored you can write it as part of your setup file in CFM why using cfcontentsave

1 Like

@Gavin_Baumanis Sorry, way tired last night when I responded.

There is also setenv in linux.

use is
setenv The_VAR_Label TheVarProperty

so if you want MyVarLuceeRocks LuceeOnLinux
setenv MyVarLuceeRocks LuceeOnLinux