This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

✨ Vultr and Linode are back on Cloudways! VIEW PLANS→

How to Deploy Static View Files in Magento 2 Using CLI

Updated on March 1, 2023

3 Min Read

Static View is made with two different words, “static” and “view”. Here, static means that file data can be cached for a site for which files are not dynamically created. And the word view refers to the presentation layer from Model-View-Controller (MVC). In simple words, the deployment commands of static view files enable us to write static files to the Magento 2 file system when we set Magento 2 for production mode.

These static view files are located in Magento’s static directory (your Magento 2 install dir/pub/static), and some files are cached in your Magento 2 install dir/var/view_preprocessed directory. Deployment of static view files is affected by these Magento Modes: Production mode, Default mode, and Developer mode.

We need to write static view files to the Magento 2 file system manually using the CLI command. We can restrict permissions to limit our vulnerabilities and to overwrite files after using the commands I show in this article.

Scalable, Super Fast, & Secure Magento Hosting

Our Clients Love us because we never compromise on these features

Note: In only developer mode: When we install or enable a new module, it may load new CSS, JavaScript, Layouts, etc. To stay away from issues with static files, we must clean the old files to make sure everything runs great on the new module.

In this way, you can easily clean generated static view files:

  1. By clearing pub/static and var/view_preprocessed directories and subdirectories except for pub/static/.htaccess. To clear all the files under the pub/static directory except .htaccess file, use this command:
find . -depth -name .htaccess -prune -o -delete

This is the manual way to clean generated static view files.

  1. In the Magento 2 admin panel, go to System > Tools > Cache Management and click on the Flush Static Files Cache.

This is the automated way to clean generated static view files.

Let’s start with the deployment of static view files in Magento 2.

Log in to your Magento 2 server as a user with permission to write to the Magento 2 file system. If you are using the bash shell, you can use the syntax mentioned below to switch to the Magneto file system owner.

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

To run Magento 2 command from any directory, add /bin after your Magento 2 install directory path.

In Magento 2, we can also run the commands in the following ways:

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

Deploy Static View Files Using CLI

For deployment of static view files, follow these steps:

  • Log in to your Magento 2 server as, or switch to, the Magento 2 file system owner.
  • Delete the content under <your Magento install dir>/pub/static.
  • Run the static view files deployment tool <your Magento install dir>/bin/magento setup:static-content:deploy.

Note: pub/static directory system must be writable when we enable merging static view files in the Magento admin.

For example, to deploy static view files for the pt_DE language, the command must be like this:

magento --ansi setup:static-content:deploy pt_DE

After using this command, the following are some sample messages that display to indicate successful deployment:

messages

Troubleshooting Deployment Tool of the Static View Files

After installation of Magento 2, if you try to run the static view files deployment tool, and the following error is displayed:

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

To solve this error, follow these steps:

  1. Install Magento 2 using command line or setup wizard
  2. Login to Magento 2 system as file owner or switch ownership
  3. Delete contents of <your Magento 2 install dir>/pub/static
  4. And then, run the static view files deployment tool I’ve shown above.

Conclusion

This is a short but useful tutorial through which you may easily deploy static view files. If you face some issues in this process, feel free to leave your comment below, and I’ll get back to you.

Cloudways provides Magento store owners complete peace of mind by handling their server-level security issues. The Managed Magento Hosting Platform is built for launching servers on high-performance reliability. You can easily launch Google Cloud, DigitalOcean, Vultr, and even Magento AWS hosting servers within minutes. Start your free trial today.

Q. What is static content deploy in Magento 2?

A. The deployment command of static view files enables the user to write static files to the Magento file system when it is set to production mode.

Q. What 3 are the static deployment strategies in Magento 2?

A. These are the 3 static deployment strategies in Magento 2

  • Standard: It’s a regular deployment process
  • Quick: Is a default and requires minimum deployment time when more than one locale are deployed.
  • Compact: Reduces the space taken by published view files.
Share your opinion in the comment section. COMMENT NOW

Share This Article

Abdur Rahman

Abdur Rahman is the Magento whizz at Cloudways. He is growth ambitious, and aims to learn & share information about Ecommerce & Magento Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now