how to debug Symfony 4 project on production environment? I mean in a way that only I from particular IP can do it. In Symfony 2/3 I have app_dev.php controller. How to do this with Symfony 4 and Apache? If I change in .env or via VirtualHost APP_ENV to dev then all app users will see debug toolbar, exception logs etc.
4 Answers
Symfony environment configured by system environment variables. So just add in apache app config (.htaccess by default) something like
SetEnvIf Remote_Addr "127.0.0.1" APP_ENV=dev APP_DEBUG=1
(needs https://httpd.apache.org/docs/trunk/mod/mod_setenvif.html)
I don't recommend using the profiler in a production environment although with Apache you could "emulate" Symfony 2 - 3 behaviour if you configure like this:
<VirtualHost *:80>
...
DocumentRoot "/yourRootDirectory"
<Directory "/yourRootDirectory/">
DirectoryIndex index.php
Require all granted
</Directory>
...
SetEnvIf Request_URI ^/app_dev.php APP_ENV=dev
Alias /app_dev.php "/yourRootDirectory/"
<Location "/app_dev.php">
Require local
</Location>
...
</VirtualHost>
Requesting http://localhost/app_dev.php
from localhost machine you get your app in Symfony 4, 5 in dev mode with profiler like in Symfony 2, 3.
Special attention to Require local
for your alias, also you could to use Require ip 192.168.1.1 127.0.0.1
or the IPs you want (see Apache docs). But be careful to expose the dev environment to an ip or computer that is not under your control.
This is a little bit of a broad question.
The best way is to use a logging tool like monolog, default output directory is project/var/log/env.log
.
As why you are seeing all app users will see debug toolbar, exception logs etc.
has to do with your environment settings.
Symfony 4 environment settings work as follows, load config/packages/(env)/package.yaml
then load default/production configurations config/packages/package.yaml
(if exists).
When you set your env dev you are loading config/packages/dev/web_profiler.yaml
and in production, there is no definition for that plugin which defaults to not showing up.
Here is more information about environment setup in symfony 4 .https://symfony.com/doc/current/configuration/environments.html
public/index.php
for hardcode setting up$_SERVER['APP_ENV']
,$_SERVER['APP_DEBUG']
and$_ENV['APP_DEBUG']
like in config/bootstrap.php ? – Vladimir ChIP
, not for all ) without changing anything in the code - impossible! you must change in your apache *.conf on index.php or start another input script, that will loads as index.php but with your preinstalled config(maybe with login/password, only by 1 IP) – Vladimir Ch