Setting Custom Environment Variables via SSH

By September 13, 2016 HowTo No Comments
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Environment variables represent a kind of placeholders, where you can store the frequently used parameters’ values or strings in order not to specify them manually in the code each time they are needed. There is a number of default environment variables which are preconfigured at Jelastic application servers and can be easily integrated into your app, hosted within these servers, to make your work with Jelastic Cloud even more convenient.

In order to set your own custom environment variables, you’ll need to establish the SSH connection to the desired server and declare them in the corresponding configuration file via your SSH console. The following guide will provide you with the details on how this can be done using the example of the Apache Ruby application server. Nevertheless, this guide is also fully suitable for the rest of the application servers, regardless whether they are intended to serve Java, PHP, Ruby, Python or Node.js applications. Furthermore, it is also applicable for other container types, so you can also declare your custom variables for a DB server, NGINX-balancer, Memcached node etc if needed.

We have a separate instruction on setting custom environment variables and JVM options through the Jelastic dashboard for Java application servers. For the rest of the nodes (except .NET and Dockers) you will have to follow the workflow below.

Setting Custom Variables

The first thing you need to do for setting any custom variable is to connect to the desired server through the Jelastic SSH Gateway, as the necessary configuration file can only be accessed via the console. All further operations will also be performed through it.

1. In case you haven’t worked with SSH at Jelastic before, perform the following steps with the help of the linked guides:

2. Just after the connection to the necessary container is successfully established, you enter its home directory. There you can find the non-editable .bash_profile file, which stores the default environment variables. To see its content, enter the following command:

cat .bash_profile

jelastic-ssh-bash-profile3. In order to be able to set your own custom variables, you need to have another file, named .bashrc, in the same folder. It doesn’t exist in the container by default, so you should use the vi command, which either opens an existing file for editing or otherwise creates a new empty file.

vi .bashrc

jelastic-ssh-bashrc

Note that both files, mentioned above, are to some extent “hidden” due to UNIX systems’ peculiarities, so you need to specify the a option for the default ls command (which is used for showing files and folders inside the selected directory) to list them inside the container:jelastic-ssh-ls-a

4. Now you can start adding your variables (click on the Insert keyboard button to start editing) according to the following format:

export {var_name}={var_value}

where

  • {var_name} – name of the variable you would like to specify
  • {var_value} – value of your variable

To save the changes made and to exit the editing shell, press the Esc button, then type :wq and hit Enter.jelastic-ssh-export

5. After the variables are set, it is required to refresh the source files. It will be done automatically while re-connecting (i.e. running the exit command and entering your server again) or by forcing the source file’s addition as it’s shown below:

source ~/.bashrc

jelastic-ssh-source6. Finally, you can check if the specified variables were applied correctly by executing the next simple command:

echo ${var_name}

where {var_name} value should be similar to the one you’ve defined for the particular variable inside the configuration file. If everything is OK, you’ll see the value of the needed variable in the output.jelastic-ssh-var-name

That’s all! For now, the stated custom environment variables can be used throughout your application. Try to set your own custom variables for free – just choose a cloud hosting provider within Jelastic Cloud Union.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Leave a Reply

Subscribe to get the latest updates