3

Environment Variables are used to set up the PHP application and the application runs smoothly. The issue that I'm facing is when I'm trying to reference the application's database connection file and run it from CLI.

Apache Set Up:

<VirtualHost *:80>
 DocumentRoot /var/www/html
 ServerName server.hostname.com
 SetEnv HOSTNAME 'hostname'
 SetEnv USERNAME 'username'
 SetEnv DB_PASSWORD 'password'
 SetEnv DB_NAME 'databaseName'
</VirtualHost>

The environment variables store all the connection-related data and the connection is referencing that.

Connection:

<?php

$username = $_SERVER['USERNAME'];
$password = $_SERVER['DB_PASSWORD'];
$hostname = $_SERVER['HOSTNAME'];
$database = $_SERVER['DB_NAME'];
error_reporting(0);
$mysqli = new mysqli($hostname, $username, $password, $database);

But, when I run it from CLI, it doesn't pick up the environment variables and it also doesn't let me run the cronjobs that are set up on the server because of the same reason.

sotirov
  • 4,379

1 Answers1

2

If you are not on a shared hosting environment, you can put your vars on /etc/environment and they will be accessible via $_SERVER when running from the CLI.

Format would be:

HOSTNAME="hostname"
USERNAME="username"
DB_PASSWORD="password"
DB_NAME="databaseName"

No SetEnv, One per line.

This would allow to keep this potentially sensitive configuration values outside of your code (and therefore outside of version control) for added safety.