1) To declare Apache2 site configuration for Magento 2 store, you have to create a new configuration file magento2.conf:
sudo nano /etc/apache2/sites-available/magento2.conf
2)
Copy and paste the following content to the above file. Remember, you should change domain.com to your domain.
<VirtualHost *:80>
ServerAdmin admin@domain.com
DocumentRoot /var/www/html/magento2/
ServerName domain.com
ServerAlias www.domain.com
<Directory /var/www/html/magento2/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
If you are installing Magento locally, you can change domain.com to localhost.com, dev.com or m2.com.
3)
Then you have to update hosts file at /etc/hosts with
127.0.0.1 localhost.com
127.0.0.1 dev.com
127.0.0.1 m2.com
4)
Enable your configuration using below command
sudo a2ensite magento2.conf