Environment variables for PHP applications

A PHP application requires me to set several environment variables with things like database credentials, SMTP server, etc.

Examples of variables include:

FOO_DATABASE_URL=mysql://user:pass@host:3306/dbname
FOO_SMTP_SERVER=smtp://user:pass@host:25
FOO_FILE_PATH=/var/www/foo
FOO_URL_HOST=example.org
FOO_URL_PATH=/foo

Where is the appropriate place to set these variables?

Initially I tried setting them in ~/.profile but I realized that I'm not the user that Apache runs as so that wouldn't work. I've found /etc/environment but I'm not sure if that's the appropriate place to be setting them. Currently there's only PATH there. If I set them there do I need to prefix the lines with export?

The documentation states that ENV files should be avoided in production to maximize performance. I'd also like to avoid using Apache's SetEnv and PassEnv directives if possible.

My production server is CentOS, however I'd like to know how to do this on Linux Mint and Ubuntu as well even though I currently use an ENV file for development.


You can simply put that .env file in the root folder of your project. Just make sure it's not publicly accessible.

This is what Laravel does but if you have concerns about performance and/or security you might take a look at Vault.