How to Modify the Magento 2 Root Directory?

 

 

Today, we’re going to teach you guys how to modify the Magento 2 root directory.

Magento 2 is generally installed in the default /var/www/html/magento2 where it contains

  • /pub/
  • /setup/
  • /var/
  • Other folders

Here, we need to set the root to the /pub/ folder so that it restricts store visitors from accessing the confidential files of Magento 2 from the browser.

And for that, we need to learn is how to change the apache root folder in Magento 2 to serve files from the /pub/ folder.

Below, we will show you exactly how to modify the Magento 2 root directory and serve files from the /pub/ folder.

Steps to Modify the Magento 2 Root Directory

Please follow the below steps to learn how to modify the Magento 2 root directory.

Step 1.

First of all, you need to edit the server config.

  • Log in to your Magento 2 server.
  • Edit the virtual host file.

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

  • Now, you need to add the path to /pub/ folder 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
    
             </Directory>
    
     </VirtualHost>
  • Then, you need to reload Apache
    systemctl restart apache2

Step 2.

After that, you need to update the base URL.

  • First, log in to your Magento 2 store database:
    mysql -u <user> -p
  • Then, you need to specify the Magento database during the Magento installation
    use <database-name>
  • Lastly, you need to update the base URL by using the following command:

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

Step 3.

Next, you need to update the env.php file by adding the following information.

'directories' => [

    'document_root_is_pub' => true

]

Step 4.

Now, you need to switch the modes using the Magento CLI tool as shown below:

  • First, you need to navigate to the Magento 2 installation folder.
  • Then, you need to switch to the production mode by executing the following command:

bin/magento deploy:mode:set production

bin/magento cache:flush

  • Now, you need to refresh the browser and switch back to the developer mode by executing the following command:
    bin/magento deploy:mode:set developer

bin/magento cache:flush

  • Finally, refresh the browser again and ensure that the storefront is displayed as expected.

Step 5.

Lastly, you need to enter the hostname of your Magento 2 server in the browser’s address bar.

Then, access the Magento 2 root folder by directing entering the hostname of your Magento 2 server. If you see an access denied message or 404 error, then everything is working fine.

Also read: How to Get Module Directory Path in Magento 2?

Final Note

And there you go!

This is the simplest way to modify the Magento 2 root directory.

And if you need our professional assistance, feel free to contact us at any time.