How to modify the Magento root directory?

In a standard installation with Apache, Magento 2 is installed in the default /var/www/html/magento2 web root and the Magento2 folder contains:

  • /pub/
  • /setup/
  • /var/
  • other folders

Setting the root to the pub/ directory prevents site visitors from accessing the confidential locations of the Magento 2 file system from the browser.

So how to change the Apache root folder in Magento 2 to serve files from the Magento pub/ directory?

Step 1. Edit your server config:

1. Log in to your Magento server.

2. Edit virtual host file:

vim /etc/apache2/sites-available/000-default.conf

3. Get the path added to your Magento pub/ directory to the Document Root directive:

<VirtualHost *:80>
         ServerAdmin webmaster@localhost
         DocumentRoot /var/www/html/magento2ce/pub
         ErrorLog ${APACHE_LOG_DIR}/error.log
         CustomLog ${APACHE_LOG_DIR}/access.log combined
         <Directory "/var/www/html">
                     AllowOverride all

4. Reload Apache:

systemctl restart apache2

Step 2. Update base URL:

1. Log in to the Magento database:

mysql -u <user> -p

2. Specify the Magento database created during Magento installation:

use <database-name> 

3. Update the base URL:

UPDATE core_config_data SET value='' WHERE path='web/unsecure/base_url';

Step 3. Update the env.php file:

Add the following info to the env.php file:

'directories' => [
    'document_root_is_pub' => true

Step 4. Switch modes using the Magento CLI tool:

1. Navigate to the Magento installation directory.

2. Switch to production mode:

bin/magento deploy:mode:set production
bin/magento cache:flush

3. Refresh the browser and make sure the storefront displays correctly.

4. Switch to developer mode:

bin/magento deploy:mode:set developer
bin/magento cache:flush

5. Refresh the browser and make sure the storefront displays correctly.

Step 5. Check the storefront:

1. Enter the hostname or IP address of your server into the address bar of your browser.

2. Try to get access to the Magento root directory from a browser by entering the directory name to the server’s hostname or IP address in the address bar path. If you see a 404 or an access denied message, then everything is done correctly.

If you have problems with server configuration, you can use Magento Server Configuration and Migration Service. Our experts will consult you, help you install all the necessary elements, and provide a guarantee for the work performed.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.