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:STEP-BY-STEP PROCESS 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 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.conf3. Now, you need to add the path to
/pub/
folder to the document root directive.
<VirtualHost *:80>4. Finally, you need to reload Apache
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>
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 examplehttp://192.168.33.10/magento2)
you’ll need to remove it.1. First, log in to your Magento 2 store database:
mysql -u2. Then, you need to specify the Magento database during the Magento installation-p
use <database-name>3. 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: 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:flush3. Refresh your browser and make sure the storefront displays correctly.
4. Enable developer mode:
bin/magento deploy:mode:set developer
bin/magento cache:flush5. 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!