Mistakes to Avoid While Deploying Symfony App

Lubna Altungi
3 min read3 days ago

--

Updating your app in production, especially on a Friday, can feel like a nightmare. If you don’t want to ruin your weekend, here are the most common mistakes to avoid:

.gitignore File:

Don’t forget to configure your .gitignore file properly. List all the files and folders that don’t need to be pushed to GitLab or GitHub. Most are configured by default, but you need to add the rest manually, such as the folder for uploaded files.

.env File:

I assume that you use separate .env files for local, test, and production environments. Even so, double-check that the correct environment variables are set for production to avoid any issues.

APP_ENV=dev to APP_ENV=prod

Update your database information accordingly:

DATABASE_URL="....."

The activated bundles:

Verify that all bundles are configured correctly for their respective environments. Some bundles should not be activated in production:

    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],

Folder Permissions:

If images or other assets aren’t appearing after deployment, check your folder permissions. For example:

Cd public/media/cache
chmod -R 775 public/media/cache
chown -R www-data:www-data public/media/cache

Ensure that mod_rewrite is enabled in the .htaccess file if necessary.

Database Configuration:

If you face a problem with the database, check it with this command:

php app/console doctrine:schema:validate

If the output shows the following, it means that the mapping is correct, but the database schema is not in sync:

[Mapping]  OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.

If your database is correctly mapped, you should check the privileges of your current user:

grant all privileges on database dbname to dbuser;

Composer Installation:

In production, always use:

symfony composer install --no-dev --optimize-autoloader

Using composer install directly will install all bundles and components, including those for development and testing.

Update Configuration Files:

If you use Apache2 or Nginx, ensure your .env file and server configuration are in sync:

Cd /var/www/your-app/.env

And:

Cd /etc/apache2/sites-available/000-default.conf

Conflicting configurations will prioritize the server configuration file over the 000-default.conf file. That means .env file will be ignored.

Redirect URLs:

href:

Use route names in path links:

href="{{ path('app_contact') }}"

instead of:

href="/contact"

redirect_uri:

If you use OAuth or SSO authentication, you need to replace the redirect URLs in production. Before each push to GitHub or GitLab, search for:

http://127.0.0.1:8000

and replace it with your production domain.

Error Controller:

Don’t forget to uncomment your error controller in production:

cd your-app/config/packages/framework.yaml
error_controller: App\Controller\ErrorController::index

Access Control:

Enhance security by configuring access control properly. Ensure you use HTTPS and assign correct roles to each path, especially for admin:

cd your-app/config/packages/security.yaml
- { path: ^/admin, roles: ROLE_USER, requires_channel: https }
- { path: ^/admin, roles: ROLE_SUPER_ADMIN }

Database migration:

It’s literally like a nightmare for developers. I keep my fingers crossed and just pray not to make any mistakes here.

First step, never ever touch the SQL directly. Stay safe and only use Doctrine migration. I always prefer to clean the migrations folder and keep only the latest file that I need to migrate.

If needed, you can migrate it in production directly:

php bin/console make:migration

Then check if it is correct and suits your requirements.

To complete the migration use:

php bin/console doctrine:migrations:migrate

Asset Mapper :

If you use Asset Mapper instead of Webpack Encore, run this command after each deployment:

php bin/console asset-map:compile

Clear cache and restart server:

After each deployment, clean the cache:

php bin/console cache:clear

If needed, restart the server:

sudo service apache2 restart

Happy coding :)

--

--