Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 how to enable prod mode?

Tags:

symfony

In Symfony 4, the index.php sets dev mode by default

$env = $_SERVER['APP_ENV'] ?? 'dev';

Symfony official doc says : Symfony Dotenv should only be used in development/testing/staging environments. For production environments, use "real" environment variables.

So I understand : APP_ENV=prod as environment variable is mandatory to enable prod mode.

Am I right?

like image 883
guns17 Avatar asked Feb 19 '18 15:02

guns17


1 Answers

You have to ensure that the environment variable APP_ENV is set correctly in your production environment. The documentation tells you how to do this for various web servers. For example, in your Apache-configuration you have to use SetEnv. This could look something like this:

DocumentRoot /var/www/project/public
<Directory /var/www/project/public>
    AllowOverride None
    Order Allow,Deny
    Allow from All

    <IfModule mod_rewrite.c>
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
</Directory>

SetEnv APP_ENV prod
SetEnv APP_DEBUG 0
...

A similar configuration for nginx would use fastcgi_param and would look somewhat like this:

location ~ ^/index\.php(/|$) {
    fastcgi_pass unix:/var/run/php7.1-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;

    fastcgi_param APP_ENV prod;
    fastcgi_param APP_DEBUG 0;
}

The doc page is a bit mean, because the respective bits are commented out.

If you want to simulate the production environment with the built-in webserver you can do it like this:

APP_ENV=prod APP_DEBUG=0 bin/console server:run

This requires the server-pack to be installed using composer and of course you can also set the command option --env as well, for the same effect.

And also make sure that you have checked .env and .env.local file too.

# prod config of .env or/and .env.local
APP_ENV=prod
APP_DEBUG=0
like image 194
dbrumann Avatar answered Oct 21 '22 11:10

dbrumann