How to Create a Virtual Host in Ubuntu: A Step-by-Step Guide

19 Oct 2024 09:59 PM

Setting up a virtual host in Ubuntu is an essential skill for web developers and system administrators. Virtual hosts allow you to host multiple websites on a single server by mapping domain names to specific directories. In this guide, we will walk through the steps to create a virtual host using Apache on Ubuntu.

Prerequisites

  1. Ubuntu Server: Ensure you have a running instance of Ubuntu (preferably 20.04 or later).
  2. Apache Installed: Apache web server should be installed. You can install it using:
    sudo apt update
    sudo apt install apache2
    
  3. Domain Name: You should have a domain name pointing to your server's IP address. For local development, you can use example.local.

Step 1: Create the Directory Structure

First, we need to create a directory for your website. Replace example.com with your actual domain name.

sudo mkdir -p /var/www/example.com/public_html

Next, set the appropriate permissions for the directory:

sudo chown -R $USER:$USER /var/www/example.com/public_html

You can also set the permissions to allow access:

sudo chmod -R 755 /var/www

Step 2: Create a Sample HTML File

Create an index.html file in your new directory to test the virtual host:

echo "<html>
<head>
    <title>Welcome to Example.com!</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>" | sudo tee /var/www/example.com/public_html/index.html

Step 3: Create a Virtual Host Configuration File

Now, we need to create a configuration file for the virtual host. This file will tell Apache how to serve your domain.

Create a new file in the sites-available directory:

sudo nano /etc/apache2/sites-available/example.com.conf

Add the following configuration to the file, replacing example.com with your domain name:

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

    <Directory /var/www/example.com/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Enable the Virtual Host

Enable the new virtual host configuration using the a2ensite command:

sudo a2ensite example.com.conf

Next, disable the default site (optional):

sudo a2dissite 000-default.conf

Step 5: Test Apache Configuration

Before restarting Apache, it’s a good idea to test the configuration for any syntax errors:

sudo apache2ctl configtest

If everything is okay, you should see Syntax OK.

Step 6: Restart Apache

Now, restart Apache to apply the changes:

sudo systemctl restart apache2

Step 7: Update Your Hosts File (for Local Testing)

If you’re testing locally, you may need to update your /etc/hosts file to map the domain to your local server:

sudo nano /etc/hosts

Add the following line:

127.0.0.1 example.com

Step 8: Access Your Website

Open a web browser and visit http://example.com. You should see the welcome message you set in the index.html file.

Conclusion

You’ve successfully created a virtual host on Ubuntu using Apache! This setup allows you to host multiple websites on a single server. You can repeat these steps for additional domains by creating new directories and configuration files.

If you have any questions or run into issues, feel free to ask. Happy hosting!

 

 

 

1
10