How to create Virtual Hosts on Ubuntu 22.04 with Apache

Avatar
D

Daniel O'Connor

Staff Systems Engineer••
8390Views
5mRead
0Reactions

Creating a virtual host in Ubuntu 22.04 involves several steps, and I'll guide you through the process using Apache as the web server. Make sure you have Apache installed on your system before proceeding. If not, you can install it by running:

sudo apt update
sudo apt install apache2

Once Apache is installed, follow these steps to create a virtual host:

Step 1: Create a Directory for Your Website

sudo mkdir /var/www/example.com

Replace "example.com" with your domain or the name of your project.

Step 2: Set Permissions

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

Step 3: Create an HTML File

nano /var/www/example.com/index.html

Add some content to the file (e.g., a simple "Hello, World!" message).

Step 4: Create a Virtual Host Configuration File

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

Add the following configuration:

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

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

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

Make sure to replace "example.com" with your domain or project name.

Read More E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)

Step 5: Enable the Virtual Host

sudo a2ensite example.com.conf

Step 6: Disable the Default Site (Optional)

sudo a2dissite 000-default.conf

Step 7: Restart Apache

sudo systemctl restart apache2

Step 8: Update Hosts File (Optional)

If you are working on a local machine, you may want to update your hosts file to map the domain to your server's IP address. Open the host's file:

sudo nano /etc/hosts

Add the following line:

127.0.0.1   example.com

Replace "example.com" with your domain.

Now, you should be able to access your virtual host by entering the domain in your web browser. I hope, it will help you generate a virtual host step by step. Thank you for your time.

Connect with KoderSolution 🚀

Master modern software engineering, scalable architectures, and advanced backend systems.

What's your setup?What's your production setup for this scenario? Have you encountered similar architectural bottlenecks or edge-case gotchas?
Daniel O'Connor
D
Written by•Staff Systems Engineer

Daniel O'Connor

Broke production enough times to respect database transactions and idempotent jobs. Specializes in Redis queues, Horizon workers, and webhook resilience.

React to this article

Recommended Articles

View all articles