Learn How to Deploy Magento 2 Static Content

The term Static View files comprise of two different ideas, static and view.

‘Static’ means that you can cache the data for a site, for which files are not dynamically created. (Examples include images and CSS generated from LESS). Meanwhile, ‘View’ means the presentation layer of Model-View-Controller (MVC).

According to the definition, mentioned above, static view files’ deployment command enables you to write static content files for the Magento file system, when you set your Magento 2 store into the production mode.

In this guide, you will learn about the procedure to deploy Magento 2 static content files.

You can find the static view files in the static directory of your Magento 2 store here: your Magento install dir>/pub/static. However, some are also cached in your Magento install dir>/var/view_preprocessed directory.

Modes Affecting Magento 2 Static Content Deployment

Static view files’ deployment tends to get affected by following Magento modes.

Production mode

Default mode

Developer mode

If you want to know your store’s current mode, just run the following CLI command in the root directory:

php bin/magento deploy:mode:show

You have to write static view files manually to the file system of your Magento 2 store by using the CLI command. After running the CLI command, you can restrict permissions in order to limit vulnerabilities and for the prevention of malicious overwriting of files.

Note: In the developer mode, it is necessary to clean the old files of CSS, JavaScript, Layouts and etc. in order to ensure that static files run well and smooth on enabling a new module.

To clean the generated static view follow these steps:

Manual Way

Clear all the files under pub/static directory except .htaccess file. To do that, run this command,

find.-depth-name.htaccess-prune-o-delete

Automated Way

Open the admin panel of your Magento 2 store and then navigate to SYSTEM → Tools → Cache Management. Click on the Flush Static Files Cache.

Magento 2 Deploy Static Content

Login to your store’s server with that user who has permissions to edit the Magento 2 file system. However, if you are using the bash shell, then use the following syntax to change the files system owner:

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

In order to run a command from any directory, add /bin after the install directory path of your Magento 2 store.