Close [x]

Deploy Magento to production

Edit this page on GitHub

Contents

Overview of deployment

This topic provides guidelines and best practices for deploying a Magento site to production. We don鈥檛 recommend a specific set of instructions; these are intended as general guidelines. You should be working with an experienced system integrator who can help you make decisions for your particular deployment.

Assumptions:

  • Your development environment is behind a firewall with the Magento application in developer mode.
  • All custom code is in source control.
  • Your development environment is as similar as possible to production (same number of webnodes, same caching architecture, and so on)
  • Your production environment is a separate set of servers in a secure environment.
  • Your production environment runs the Magento application in production mode with desired security options enabled.
  • Your production system has a working, live Magento installation.

Deploying to production involves the following essential steps. Each set of steps is discussed in more detail in the sections that follow.

In your development system

  1. Test your system in your development environment.
  2. Put all custom code in source control.
  3. Compile code and generate static view files.
  4. Archive file system assets and export the database.
  5. Transfer file system and database assets to your production system.

In your production system

  1. Create the Magento deployment configuration.
  2. Extract file system and database assets from your archives.
  3. Set file system permissions.
  4. Test everything.
  5. When everything is working, 鈥渇lip鈥 your DNS server to point to your production system.

Prepare your development system

Your development system is a dedicated developer or build system where you periodically:

  1. Deploy Magento 2 code (including custom code).
  2. Clean static files.

    rm -rf <your Magento install dir>/pub/static/*
    
  3. Remove the Magento database and reinstall it with database schema and data from your implementation.

    One way to do this is to run the command-line installer with the optional --cleanup-database command.

  4. Use the magento setup:static-content:deploy command to deploy static view files.
  5. Compile code.

  6. Clean temporary files that shouldn鈥檛 be in production.

    cd <your Magento install dir>/var
    rm -rf cache/* composer_home/* log/* page_cache/* session/*
    
  7. Archive the file system to a suitable format (.tar, .tar.gz, .zip, and so on).
  8. Export the database using a tool like mysqldump

Best practices in development:

Production server checklist

Make sure you do as many as possible of the following before continuing:

  • Set up Redis caching
  • Set up Varnish web acceleration

    We strongly recommend Varnish for HTTP acceleration, including page caching.

  • Set up a Content Delivery Network (CDN)
  • Set up multiple webnodes with load balancing
  • Configure Magento payment and shipping methods
  • Set your web server virtual host to point to the Magento pub directory because doing so prevents running the Setup Wizard, Component Manager, or System Upgrade in production

Guidelines for deployment to production

This section provides guidelines for deploying a Magento installation to production. Depending on your requirements, additional tasks might be required. Consult a system administrator for advice.

  1. Copy your development file system and database archives to the production system.
  2. Extract the Magento 2 file system archive to an empty directory.
  3. Set up persistent storage (for example, media, logs, cache, session, and so on).
  4. Take your existing system offline by enabling maintenance mode using magento maintenance:enable. (DOWNTIME STARTS)
  5. Install the Magento software in a clean database (optionally using the --cleanup-database option).
  6. Import database data from your development system.
  7. Move your existing file system to an archive location.
  8. Move the file system you copied from your production system to the Magento docroot.
  9. Change to production mode and set file system permissions and ownership for production.
  10. Test everything thoroughly and carefully.
  11. Switch DNS to the new server. (DOWNTIME ENDS)

Best practices in production:

  • Use Varnish to accelerate HTTP traffic, including providing page caching
  • Use the magento deploy:mode:set command to enable developer mode
  • Use the pub directory as the Magento docroot to prevent running the Setup Wizard, Component Manager, or System Upgrade in production. (Set up virtual hosts to point to <your Magento install dir>/pub.)
  • Set up your robots.txt to allow search engines to index your production site.