How to set up a virtual host on Ubuntu

Ubuntu is considered one of the most powerful Linux operating systems, known for its robust security features through the command-line interface. However, in this blog post, I will guide you through the process of manually setting up a virtual host or localhost with the Apache server without using the terminal or command-line interface.

Ubuntu virtual host setup

In this blog, I will demonstrate how to create a localhost domain named myproject.com You can follow these steps and replace it with your custom domain.

1. Go to file manager as super user

Open the terminal and run the command 'sudo nautilus' to access the file manager with superuser privileges.
system@username: $ sudo nautilus 
[sudo] password for user:

2. Create new folder inside html directory 

Establish your new project folder within the 'html' directory Ex: /var/www/html/myproject

Create new folder inside html directory

3. Create index.html file

<html>
  <head>
    <title>my project</title>
  </head>
  <body>
    <h1>Hello Bugger</h1>
  </body>
</html>
Copy the provided HTML code and save the file as 'index.html' in the directory 'var/www/html/myproject/' 👇

Create index.html file inside project folder

4. Create configuration

Navigate to the /etc/apache2/sites-available/ folder to create a configuration file for the virtual host. You'll find default .conf files there. Similarly, you can create a new 'myproject.conf' file using a text editor.
<Directory /var/www/html/myproject>
    #Require all granted
    #Options Indexes FollowSymLinks MultiViews
    Options +FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>
<VirtualHost *:80>
    ServerName myproject.com
    ServerAlias www.myproject.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/myproject
    #Protocols h2 http/1.1
</VirtualHost>
You should paste this code into myproject.conf file.

5. Set up a host with a specified domain name

Navigate to the /etc/hosts file to configure a custom domain. Ensure that the domain name matches the one specified in the corresponding .conf file.
127.0.0.1	localhost
127.0.0.1 www.myproject.com myproject.com
Create your custom domain name for your ip you can refer localhost domain then save it.

6. Enable configuration and restart apache

All is set for launch. Finally, you need to enable your configuration file. After making changes, restart the Apache web server by simply pasting this command in the terminal.
$ sudo a2ensite myproject.conf
$ sudo systemctl restart apache2
a2ensite is a script that enables the specified site (which contains a <VirtualHost> block) within the apache2 configuration (myproject.conf)

7. Check your virtual host

Search for 'myproject.com' (your domain) in your browser.

Localhost on ubuntu

NOTE : Ensure you grant all permissions to access your www folder and change permissions for enclosed files as well. Specifically, grant permissions only to the contents within the www folders; avoid granting any permissions to other folders, as this could potentially impact your operating system
Navigate to the folder properties and set the access level to 'Create and delete files' for full permissions.

Change permissions for www directory

or Execute the following command in your terminal to grant access to the www folder 👇
$ sudo chmod -R 755 /var/www

Thank You !