Search code examples
node.jsgitubuntupm2api-key

Adding environment variables to Ubuntu srever when .env file is in .gitignore


I have a Github repository with my project that is being hosted on an Ubuntu server. When I include my .env file in my repository and "git pull" onto the Ubunut server, the website loads with no problem. But when I put my .env file in .gitignore, then I "git pull" my project onto the Ubuntu server, the website is down and I get this error in my "pm2 logs":

0|app      | OpenAIError: The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'My API Key' }).

My question is: how do I add environment variables to my Ubuntu server, while keeping all my API keys hidden?


Solution

  • You must avoid pushing your env variables to the github repo. Instead, in your hosting server, take the pull of your repo and create a .env file with your environment variables.

    In case you don't have GUI for your hosting server, you can make use of your terminal to create it.

    Type the following command in your ssh terminal after connecting to your server:

    nano .env

    After that, you can type or copy paste your environment variables along with its value into the terminal and press CTRL + X and then Y to save and exit from nano editor.

    Done.