DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

How To Create Virtual Host In Ubuntu 22.04

In my journey through the world of web development, I've come to understand that a well-organized hosting environment is the bedrock of a successful online presence. With websites becoming increasingly intricate and diverse, the ability to efficiently manage multiple sites on a single server becomes indispensable.

This is where the magic of virtual hosts enters the stage – an approach that empowers me to run numerous websites on a single server, each with its own domain and configuration.

Ubuntu 22.04, the latest iteration of the renowned Linux distribution, continues to hold its place as a preferred choice for hosting environments due to its reliability and adaptability.

In this comprehensive guide, I dive headfirst into the world of creating virtual hosts in Ubuntu 22.04, unlocking the potential to seamlessly host and manage various websites.

Whether I'm a seasoned system administrator honing my hosting infrastructure or a budding developer taking my first strides into server management, this guide equips me with the vital know-how to confidently configure and sustain virtual hosts.

Join me as I navigate the intricate landscape of web server configuration, demystify the setup of virtual hosts, and empower myself to unleash the full capabilities of my Ubuntu 22.04 server.

Let's embark on this journey to transform my server into a multitasking powerhouse, taking my web hosting proficiency to soaring new heights.

Step 1: Access Your Ubuntu Server

Log in to your Ubuntu 22.04 server using SSH or any preferred remote connection method. Make sure you have administrative privileges (sudo access) to perform the necessary configurations.

Step 2: Install Apache Web Server

If you haven't already, install the Apache web server on your Ubuntu server:

sudo apt update
sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Directory Structure

For each virtual host, create a directory structure to hold the website's files. Replace example.com with your domain.

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chmod -R 755 /var/www
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure Virtual Host

Create a new virtual host configuration file for your domain using a text editor (like Nano or Vim).

sudo nano /etc/apache2/sites-available/example.com.conf
Enter fullscreen mode Exit fullscreen mode

Add the following content, replacing example.com with your domain.

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    DocumentRoot /var/www/example.com/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Save and exit the text editor.

Step 5: Enable the Virtual Host

Enable the virtual host configuration and restart Apache.

sudo a2ensite example.com.conf
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Then disable the default configuration file.

sudo a2dissite 000-default.conf
Enter fullscreen mode Exit fullscreen mode

Step 6: Configure Hosts File

Edit the local hosts file to associate your domain with the server's IP address.

sudo nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Add a line like this, replacing your_server_ip with the actual IP address and example.com with your domain.

your_server_ip   example.com
Enter fullscreen mode Exit fullscreen mode

Save and exit the text editor.

Step 7: Test the Configuration

Upload your website files to the /var/www/example.com/public_html directory. Ensure that an index.html or index.php file is present.

Step 8: Verify the Virtual Host

Open a web browser and enter your domain (e.g., http://example.com). You should see your website served by the newly configured virtual host.

Conclusion:

As we conclude this journey through the intricacies of creating virtual hosts in Ubuntu 22.04, we find ourselves equipped with a powerful tool to manage and host multiple websites seamlessly on a single server.

The ability to configure distinct domains, directories, and settings for each site opens the door to a more organized and efficient web hosting environment.

Throughout this guide, we've navigated through essential steps, from installing the Apache web server to crafting individual virtual host configurations.

Top comments (0)