Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ionut's avatar

How to pass ENV vars from NGINX to Laravel?

Hi every one,

I googled a lot this issue, but with no luck.

Basically I have this nginx site configuration:

server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/api/production/current/public;
    index index.php index.html index.htm;

    server_name api.example.com www.api.example.com;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV "production";
        fastcgi_param DATABASE_NAME "database";
        fastcgi_param DATABASE_USER "user";
        fastcgi_param DATABASE_PASSWORD "password";
        include fastcgi_params;
    }
}

And in database.php

'mysql'   => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => getenv('DATABASE_NAME'),
        'username'  => getenv('DATABASE_USER'),
        'password'  => getenv('DATABASE_PASSWORD'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ],
    

Obviously I'm getting the environment in start.php

$env = $app->detectEnvironment(function () {
    return getenv('APPLICATION_ENV');
});

It seems that NGINX doesn't set the environment variables.

I'm using the latest nginx from the nginx/stable ppa with php5-fpm.

What am I doing wrong here?

Thanks in advance to all your (hopefully) replies :D

0 likes
8 replies
bashy's avatar

Pretty sure that should work, did you reload php5-fpm? Can't remember if setting the params in fastcgi actually passes it to $_ENV or getenv() but $_SERVER

Ionut's avatar

Thanks for your reply, @bashy Poking around I found, as you said, that fastcgi passes it to $_SERVER. Any way to pass everything to $_ENV?

I shoved phpinfo() real quick in the index.php and the _SERVER["APP_ENV"] production pops out....

But if I run php artisan env I get:

PHP Notice: Undefined index: APP_ENV in /var/www/example.com/api/production/releases/20150604120734/bootstrap/start.php on line 28

Obviously in the environment "part" in start.php I have:

$env = $app->detectEnvironment(function () {
        return $_SERVER["APP_ENV"];
});
bashy's avatar

I think you'll need to add them into the pool for PHP5-FPM. Each site should have a pool file in /etc/php5/fpm/pool.d/?

Ionut's avatar

See my update above please, @bashy

Anyway the result that I want to achieve is to be able to configure env variables in nginx site configurations and get the right env by doing getenv() or $_ENV. I have three environments on the same droplet and I can't distinguish between stages without env vars.

Could you point me in some direction or give me a hand with this? I'm going crazy :/

bashy's avatar

Might be that PHP isn't passing them to $_ENV, try set the E in EGPCS in your php.ini file. If that doesn't work you can try use getenv() since that searches both.

Not sure why $_SERVER['APP_ENV'] isn't working though...

Ionut's avatar

Thanks for all your replies.

Same result, the variable doesn't get read....

nyce's avatar

Agree - this is causing me a bit of a problem at the moment. It seems that OCI8 PHP extension reads the LD_LIBRARY_PATH and ORACLE_HOME variables from the $ENV array, which means I don't have the option of adding them to the $_SERVER array instead. I've been looking at this for several hours now, and I'm coming to the conclusion that I need to use Apache instead of Nginx.

Please or to participate in this conversation.