magento2magento2.1

Magento 2 moving Apache docroot to pub directory


I've recently performed a Magento security scan and it recommended I move the root of my Magento 2 installation to the pub directory.

I've followed the instructions here but I've had no success. The site fails to find the images and merged CSS files. I have double checked that the CSS files and images exist in their respective directories in pub/static.

My Apache configuration:

DocumentRoot /var/www/html/magento2/pub
ServerName www.somedomainorother.co.uk
ServerAlias somedomainorother.co.uk

<Directory "/var/www/html/magento2/">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>

I am using Apache 2.4, Centos 7.3.

Has anybody run across a problem like this before and how did you fix it?


Solution

  • I had exactly the same issue and the root cause I found that Magento was still looking for the static resources under [site_url]/pub/static or (if static versioning is enabled):

    [site_url]/version[NNNNNN]/pub/static

    While they should have been requested according to the new root

    [site_url]/version[NNNNNN]/static

    I had to check the static and media urls, in core_config_data I found [site_url]/pub/media and [site_url]/pub/static as values for base_media_url, base_static_url for secure and unsecure. I simply removed these config records from core_config_data and left the application to retrieve them correctly by default.