How to run Magento 2 with pub as document root?

I tried to access magento home page like https://127.0.0.1/magento2/pub/index.php but all css and js files are not loaded.is there anything i need to change in .htaccess?

What is the purprose of it?

Magento 2 have two root directory and /. All static files are stored in pub folder. we can use pub folder as root to secure our server and files.when i get media path programmatically (sending data to other third party api) it is with pub but when we see image in fronted it is without pub. it is difficult to decide whether i should take image path with pub or without pub.I want to test my code with pub root directory.Magneto2 doc didn’t mentioned anything about that.

Dhara,

You will need to set the Apache VirtualHost docroot to your …/magento2/pub directory and then flush all the cache including storage. Now, open a new browser to https://127.0.0.1 and it will work as you are expecting.

I’m not completely sure why it doesn’t work to browse /pub but I think it has to do with caching and the index.php files in those directories.

Hope that helps!

Doug