Our Feature Items: SM Market // SM AutoStore // SP Revo
Shopping cart (0)

How to Modify the Magento 2 Root Directory?

Rate this item
(0 votes)
Written by  Hoant
Tuesday, 17 May 2022 17:28

Today, we're going to show you how to change the Magento 2 root directory.
Magento 2 is usually installed in the /var/www/html/magento2 directory, which comprises the following files::

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

  • The Magento app is served from /var/www/html/magento2/pub. The rest of the Magento file system is vulnerable because it is accessible visa a browser. Setting the webroot to the pub/ directory prevents visitors from using their browsers to access sensitive portions of the Magento file system.
    We will teach you exactly how to modify the Magento 2 root directory and serve files from the /pub/ folder in the sections below:


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

    STEP 1: First of all, you must to edit the server config.

    1. Go to your Magento 2 server and log in.
    2. Edit the virtual host file.
    vim /etc/apache2/sites-available/000-default.conf
    3. 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
    4. Finally, you need to reload Apache
     systemctl restart apache2

    STEP 2: The base URL must be updated:

    If you appended a directory name to your server’s hostname or IP address to create the base URL when you installed Magento (for example you’ll need to remove it.
    1. First, log in to your Magento 2 store database:
    mysql -u  -p
    2. Then, you need to specify the Magento database during the Magento installation
    use <database-name>
    3. Lastly, you need to update the base URL by using the following command:
    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. Go to the Magento installation directory.
    2. Switch to production mode:
    bin/magento deploy:mode:set production
    bin/magento cache:flush
    3. Refresh your browser and make sure the storefront displays correctly.
    4. Enable developer mode:
    bin/magento deploy:mode:set developer
    bin/magento cache:flush
    5. Refresh the browser and make sure the frontend displays correctly.

    STEP 5: Verify the storefront

    1. Enter the hostname or IP address of your server into the address bar of your browser.
    2. Enter the directory name to the server's hostname or IP address in the address bar path to gain access to the Magento root directory from a browser. If you get a 404 or an access denied notice, you've done everything right.

    Thank you for utilizing our theme!
    I wish you success in your business and a significant increase in sales!
    Joomla TemplatesFree Joomla TemplatesVirtuemart TemplatesK2 TemplatesJoomShopping TemplatesHikaShop TemplatesSobiPro TemplatesOpenCart Themes
    Quà Tết Cao Cấp Hộp quà tết Giỏ quà tết Túi quà tết