Table of contents
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:
- Use the below command to manually clear all the files under pub/static directory except .htaccess file:
find . -depth -name .htaccess -prune -o -delete
- 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:
cd <your Magento 2 install dir>/bin and run them as ./magento <command name>
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:
- Login or Switch to your Magento 2 file system owner
- Remove the content under <your Magento install dir>/pub/static.
- 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:
Following the next steps to deal with the issue:
- Install Magento 2 using command line or setup wizard Using Command Line or Setup Wizard for Magento 2 installation
- Login or Switch to your Magento 2 system owner
- Remove contents under <your Magento 2 install dir>/pub/static
- And run the Static View Files deployment tool <your Magento install dir>/bin/magento setup:static-content:deploy.