Skip to content
Deploying apps5 min read

How to set environment variables (.env) on hosting

Short answer

It depends on the project: Laravel and PHP read a .env file in the app folder outside public_html, Node.js and Python apps in cPanel take values from the Environment variables section of Setup Node.js App or Setup Python App, and front-end variables such as VITE_ are baked in at build time on your computer. After any change, restart the app or clear the config cache.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Where your project reads its variables
  2. Steps
  3. Common syntax mistakes
  4. Troubleshooting
  5. On ArabyHost

Environment variables are the values that differ between your computer and the server and must not live in the code: the database password, API keys, the site URL, the run mode. Locally you usually keep them in a .env file. On hosting there’s no single place for them: each kind of project reads them from somewhere different, which is behind most “works on my machine, not on the server” problems.

Where your project reads its variables

Project type Where values come from on hosting When they’re read
Laravel and PHP A .env file in the app folder, outside public_html On each request, or once at config:cache
Node.js (Setup Node.js App) The app’s Environment variables section, or .env with dotenv When the app starts
Python (Setup Python App) Environment variables, or .env with python-dotenv When the app starts
React, Vue, Vite front end .env.production on your computer At build time only
A script run by cron Nothing automatic; the script loads .env itself On every run

Steps

1. List the variables you need

Open .env.example in your project, or search the code for process.env., env( and os.environ, and write down the names. If an AI tool built the project, ask it: “List every environment variable this project needs.”

2. Prepare production values

Don’t copy your local values as they are. On the server these usually change: the database name and user (with the cPanel account prefix), APP_URL to https:// and your domain, the run mode to production, and secret keys, which are best generated fresh with the password generator.

3. Laravel and PHP: the .env file

  1. In File Manager, open Settings and enable Show Hidden Files (dotfiles).
  2. Go to the app folder (such as /home/USER/myapp, not public_html).
  3. Copy .env.example to .env, open it with Edit and change the values.
  4. Under Permissions, set the file to 600 so only your account can read it.

If a simple PHP project forces you to keep .env inside public_html, block browser access with this in .htaccess:

<Files ".env">
  Require all denied
</Files>

Then open https://example.com/.env and confirm it returns 403. The full Laravel walkthrough is in the Laravel deploy guide.

4. Node.js and Python: the Environment variables screen

In Setup Node.js App or Setup Python App, click the edit icon next to the app, then under Environment variables click Add Variable, enter the Name and Value, click Done, and when all are added click Save and then Restart. The code reads them directly:

const db = process.env.DATABASE_URL;
import os
db = os.environ["DATABASE_URL"]

A .env file with dotenv is fine too, but give its full path, because the working directory under Passenger may not be your project folder:

require('dotenv').config({ path: '/home/USER/api/.env' });
from dotenv import load_dotenv
load_dotenv("/home/USER/app/.env")

More in the Node.js app guide.

5. Front end: build-time variables

Vite variables starting with VITE_ and Next.js variables starting with NEXT_PUBLIC_ are written into the JavaScript files during npm run build. So they go in .env.production on your computer before the build, and any change needs a new build and upload. Everything in them is visible to every visitor, so never put a secret key there. Next.js variables read only on the server go in the Environment variables screen like any Node.js app.

6. Cron jobs and background scripts

A cron job sees neither Passenger’s variables nor .bashrc. Load the file inside the command itself:

cd /home/USER/app && set -a && . ./.env && set +a && venv/bin/python worker.py

7. Check without exposing secrets

Print which variables are set, never their values. In Node.js, add this line temporarily at the top of the startup file, restart, and open the Passenger log:

['DATABASE_URL', 'JWT_SECRET'].forEach(k => console.log(k, process.env[k] ? 'set' : 'MISSING'));

Check from inside the app rather than from an SSH prompt, because the shell doesn’t necessarily see the values Passenger passes to the app. And never leave a page showing phpinfo() or all variables on a live site.

Common syntax mistakes

  • Spaces around =: write DB_PASSWORD=abc, not DB_PASSWORD = abc.
  • A value with a space or #: wrap it in quotes: APP_NAME="My Shop".
  • A file saved with Windows line endings: the password becomes abc\r and the database login fails. Save the file as LF (the option is at the bottom of the VS Code window).
  • A different letter case: Db_Host is not DB_HOST; names are case-sensitive.

Troubleshooting

On ArabyHost

On every ArabyHost plan you create the .env file for PHP and Laravel projects in File Manager, and your account stays isolated from other accounts inside CloudLinux CageFS. On the AI/Developer plans you set variables for Node.js and Python apps in Setup Node.js App and Setup Python App, or over SSH. If the app doesn’t pick up a value, the Araby AI assistant in the panel reads the error log and explains why. To choose a plan, see the developers page.

FAQ

Should I push my .env file to GitHub?

No. Add it to .gitignore and commit a .env.example with the variable names and no values instead. Then create the .env file or the variables on the server by hand.

Why can't I see .env in File Manager?

Files whose names start with a dot are hidden by default. Open Settings at the top of File Manager and enable Show Hidden Files (dotfiles).

I edited .env and nothing changed. Why?

The app is still running with the old values. In Laravel clear the config cache (config:clear) or delete bootstrap/cache/config.php; in Node.js and Python click Restart on the app's page. Front-end variables need a new build.

Do cron jobs see the Setup Node.js App variables?

No. Variables on that screen are given only to the app Passenger runs. A script started by a cron job has to load the .env file itself.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.