Tools and Resources for all Your Ecommerce Pain Points
Explore Now

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.

Here are some other ways to run a command 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>

Magento 2 Deploy Static Content Using CLI

To configure and deploy Magento static content via CLI, follow these steps:

  • Login to Magento 2 server as file system owner or switch ownership
  • Remove the contents under <your Magento install dir>/pub/static., except for the .htaccess file.
  • Now run the static view files deployment tool by <your Magento install dir>/bin/magento setup:static-content:deploy -f.

Note: Do ensure that the pub/static directory system is writable when you enable merging static view files in your Magento Admin

Troubleshooting Deployment Tool of the Static View Files

After installation of Magento 2, when you run the static view flies deployment tool, you might face this error:

ERROR: You need to install the Magento application before running this utility.

To resolve this error, implement the following steps:

  • Install Magento 2 either by Command line or Setup Wizard.
  • Login to Magento 2 server as file system owner or switch ownership
  • Remove contents of <your Magento 2 install dir>/pub/static
  • Run the static view deployment tool: <your Magento install dir>/bin/magento setup:static-content:deploy -f.

Final Words

Deployment of static content is a great way to boost the speed of your Magento 2 store. After following this guide, I believe that you can easily deploy Magento 2 static content within your store.

If you’re having any confusion related to this guide, just drop your comment below. I will get back to you.

See: Setup Database Caching To Make Your Magento Ecommerce Store Faster

 

About Author

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.