Magento Tutorials

How to Deploy Static Content in Magento 2

This guide will walk you through how to deploy static content (or static view files) in Magento 2. As a result of the deployment, you will be able to write static files to the Magento 2 system when setting production mode for Magento 2.

You can find static view files in dir/pub/static and some in dir/var/view_preprocessed directory.

Those static view files need to be written to your Magento 2 file system manually by the CLI command.

Note: In developer mode, you must clean the old files of CSS, JavaScript, Layouts and etc. to keep static files running well when you enable a new module.

You can follow the steps below to clean generated static view files:

  1. Use the below command to manually clear all the files under pub/static directory except .htaccess file:
find . -depth -name .htaccess -prune -o -delete
  1. In your Magento 2 Admin Panel, you can do an automated clearing of generated static view files by navigating to System/ Tools/ Cache Management and clicking on the Flush Static Files Cache.

Deployment of static view files

Login to your Magento 2 server with the an user permitted to write to the Magento 2 file system. Use the syntax below to change to the Magento file system owner if bash shell are on use:

su <Magento file system owner> -s /bin/bash -c <command>

Add /bin after your Magento 2 install directory path to trigger Magento 2 command from any directory.

Below are the other ways to run the commands in Magento 2:

  1. cd <your Magento 2 install dir>/bin and run them as ./magento <command name>
  2. php <your Magento install dir>/bin/magento <command name>

Using CLI to deploy Static View Files

There are few steps that you need to follow:

  1. Login or Switch to your Magento 2 file system owner  
  2. Remove the content under <your Magento install dir>/pub/static.
  3. Run the static view files deployment tool by <your Magento install dir>/bin/magento setup:static-content:deploy.

Note: when you enable merging static view files in your Magento Admin, pub/static directory system must be writable.

Troubleshooting Deployment Tool of the Static View Files

You might be facing the below issue within the deployment of Static View Files:

magento 2 deploy static content

Following the next steps to deal with the issue:

  1. Install Magento 2 using command line or setup wizard Using Command Line or Setup Wizard for Magento 2 installation
  2. Login or Switch to your Magento 2 system owner
  3. Remove contents under <your Magento 2 install dir>/pub/static
  4. And run the Static View Files deployment tool <your Magento install dir>/bin/magento setup:static-content:deploy.
Dom

A knowledge craver who always strive to be wiser everyday.