Monday, August 27, 2018

Creating custom domain for localhost in Ubuntu

We browse websites using a link/url like http://localhost/examplesite while developing a website/web application in localhost. But we can set a custom domain like dev.example or example.dev to visit the website we are developing. I am covering here in this post how to set up custom domain in Ubuntu. But setting up custom domain in Windows and Mac platform are almost same.

Step 01: Open terminal by pressing the key binding below
ctrl+alt+t

Step 02: Change the directory to etc using the command line cd /etc cd /etc

Step 03: Now open the hosts file with gedit using the command line sudo gedit hosts
sudo gedit hosts

Step 04: Now add this line 127.0.0.1    dev.laraframe and save the hosts file
127.0.0.1    dev.laraframe

Step 05: Change directory to sites-enabled using this command line
cd /etc/apache2/sites-available/

Step 06: Use ls command to see the files inside sites-enabled folder, there you will see a file named 000-default.conf

Step 07: Now we will copy and rename this file as custom.conf using the command sudo cp 000-default.conf custom.conf

Step 08: We will open this custom.conf file in a text editor using the command sudo gedit custom.conf

Step 09: Then we will add these following lines below...
But you need to change some information against your localhost settings.
<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com 

    ServerAlias www.example.com

    DocumentRoot /var/www/html/projectfolder
    <Directory "/var/www/html/projectfolder">
                Options +FollowSymLinks
                AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> 

Step 10: We have to enable the custom.conf now using the command line sudo a2ensite custom.conf


Step 11: Need to reload apache2 using the command line
systemctl reload apache2

Step 12: Restart apache2 using sudo service apache2 restart 

Step 13: Now visit the site with your custom domain using your favorite browser