I have a Ghost droplet that I recently noticed was not taking image uploads as indicated by the following code that appeared when I tried to upload from the markdown editor file explorer 
. As a result I decided to ssh into my droplet to check out my config file where I am using environment variables to store images to my s3 bucket and noticed when I ran printenv
that my environment variables were not there. I decided to set the env variables again like so: export AWS_ACCESS_KEY=*KEY ID*
and ran printenv
again and the key was there, but ssh’ing into the droplet a day later and the AWS_ACCESS_KEY
is gone. Am I setting the env variables correctly? Any reason why they would disappear? I tend to run service ghost restart
after setting them, but this didn’t reset the env variables.
Here is the storage configuration in my config.js
:
storage: {
active: 'ghost-s3',
'ghost-s3': {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
bucket: process.env.S3_BUCKET,
region: process.env.S3_REGION,
assetHost: process.env.S3_URL
}
},
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
When you use export
, whether it’s in .profile
, .bash_profile
, or another file that’s loaded when you login to SSH, the exported data will only persist for that session (i.e. until that SSH user logs out). For data to persist, it needs to be defined in a persistent way.
Method #01
With NodeJS, the first option is to use process.env
and define the environment variables in a file that isn’t accessible to the public. Yes, this is slightly frowned upon and most will say you’re doing it wrong but it’s the first obvious method.
process.env['VARIABLE_NAME'] = VALUE
Method #02
Alternatively, you can pass environment variables when running node
. For example:
VAR_01='value' VAR_02='value' node app.js
Method #03
Utilize and existing package to handle .env
and environment variables – particularly, dotenv
.
https://github.com/motdotla/dotenv
https://www.npmjs.com/package/dotenv
Using Git/GitHub/Version Control?
Make sure you setup a line to ignore your environment files in Method #01 and #03. You don’t want to commit those files to any public repository.
When you run export
, the export is only valid for the shell that you’re currently logged in to. Once you logout, whatever you’ve exported will disappear unless it’s explicity set in .bash_profile
for the user in question.
For example, if you’re username is connord
, you’d setup a .bash_profile
for this user and add to it:
export AWS_SECRET_ACCESS_KEY=YOUR_KEY
export AWS_ACCESS_KEY_ID=YOURID
Then when you login as connord
, the export
commands will automatically run and you’ll be able to access those variables.
That said, export
is still only valid to the logged in users’ shell, so you’d need to physically set them for persistence.
For details, you can read about process.env
here:
https://nodejs.org/api/process.html#process_process_env
You’ll need to define the process.env
variables for them to be persistent. For example:
process.env.AWS_SECRET_ACCESS_KEY = "YOURKEY";
process.env.AWS_ACCESS_KEY_ID = "YOURID"
You can drop the export
’s in .profile
, though keep in mind that this will only apply to the root
user and only when that user logs in. They won’t be globally set, available to other users, or available to your application unless you’re running it while logged in via SSH. Once you close the connection to SSH, whether it’s by hitting the X
or using exit
, they will disappear.
The same applies even if you’re running as a user other than root
(ideally, you should be – creating a sudo
user and escalating to run root
commands is better in terms of security).
If you need the exported data to persist, you would need to define them using process.env
as noted at the end of my previous reply.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.