How to fix Magento 2: CSS, JS, font, icon not working at frontend and backend

If you are using Apache or Litespeed web server below quick tutorial: How to fix Magento 2 CSS, JS, font, icon not working at frontend and backend.

First go to Magento 2 installation directory, for example in my case is /var/www/html, so

cd /var/www/html

1. Remove all static content, cache and generated folder

rm -rf pub/static var/cache var/composer_home var/generation var/page_cache var/view_preprocessed

2. Edit di.xml

File location: app/etc/di.xml

Find this Line (Word: Symlink)

<item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>

Change to Copy

<item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item>

3. Run setup upgrade

php bin/magento setup:upgrade

4. Run setup compile

php bin/magento setup:di:compile

5. Run Deploy static content command

php bin/magento setup:static-content:deploy -f

6. Flush cache

php bin/magento cache:flush

7. Reindex static block

php bin/magento indexer:reindex

8. Change permission

chmod -R 0777 var tmp pub generated

9. Replace .htaccess

Remove all .htaccess for root directory, pub directory and download .htaccess files from official Magento 2 repository (Github). Use below command:

rm -f /var/www/html/.htaccess /var/www/html/pub/.htaccess /var/www/html/pub/media/.htaccess /var/www/html/pub/static/.htaccess

Download .htaccess

For root directory

wget -O /var/www/html/.htaccess https://raw.githubusercontent.com/magento/magento2/2.4-develop/.htaccess

For pub/

wget -O /var/www/html/pub/.htaccess https://github.com/magento/magento2/raw/2.4-develop/pub/.htaccess

For pub/media

wget -O /var/www/html/pub/media/.htaccess https://github.com/magento/magento2/raw/2.4-develop/pub/media/.htaccess

For pub/static

wget -O /var/www/html/pub/static/.htaccess https://github.com/magento/magento2/raw/2.4-develop/pub/static/.htaccess

10. Fix Ownership

For example my web server is Apache – Ubuntu, the default ownership user:group is www-data:www-data, so

chown -R www-data:www-data /var/www/html/*

Solved.

Add a comment

Exit mobile version