Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Setting Custom Environment Variables via SSH

DZone's Guide to

Setting Custom Environment Variables via SSH

Setting your own environment variables in a shell is often very necessary. This article reviews the basics of how to do it.

· DevOps Zone
Free Resource

Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.

Environment variables represent a kind of placeholder where you can store the frequently used parameters’ values or strings in order so as not to specify them manually in the code each time they are needed. There are a number of default environment variables that are preconfigured at Jelastic application servers and can be easily integrated into your app and 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 of 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.

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

1. 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-profile2. 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

3. 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

4. After the variables are set, you must 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 okay, 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.


Download the ‘Practical Blueprint to Continuous Delivery’ to learn how Automic Release Automation can help you begin or continue your company’s digital transformation.

Topics:
variables ,configuration ,custom ,ssh ,jelastic ,environment ,parameters

Published at DZone with permission of Tetiana Markova, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}