Configure vHost Apache in Ubuntu 24 Linux (Fast & Easy Setup)

Photo of author
By Jay
— 4 min read
Photo of author
Written by
Photo of author
Verified by
Published On
— 4 min read

Virtual Hosts (vHosts) in Apache allow you to run multiple websites on a single server by defining separate configurations for each domain. Whether you’re hosting multiple domains or subdomains, knowing how to configure vHost Apache efficiently is essential for Linux administrators. This beginner-friendly guide will walk you through the process step by step.

configure vHost Apache in Ubuntu

What is a Virtual Host in Apache?

A Virtual Host allows you to host multiple websites on a single server by mapping specific domain names to unique document roots. This helps manage website configurations efficiently and is particularly useful for shared hosting environments.



Prerequisites to configure vHost Apache

Before configuring vHosts in Apache, ensure that:


Step 1: Install Apache Web Server (If Not Already Installed)

To install Apache, run:

sudo apt update
sudo apt install apache2 -y

Enable and start Apache:

sudo systemctl start apache2
sudo systemctl enable apache2

Step 2: Create Directory for Each Website

Create separate directories to store website files:

sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html

Set permissions and ownership for these directories:

sudo chown -R $USER:$USER /var/www/example1.com/public_html
sudo chown -R $USER:$USER /var/www/example2.com/public_html
sudo chmod -R 755 /var/www

Add sample content for testing:

echo "<h1>Welcome to Example1.com</h1>" | sudo tee /var/www/example1.com/public_html/index.html
echo "<h1>Welcome to Example2.com</h1>" | sudo tee /var/www/example2.com/public_html/index.html

Step 3: Create Virtual Host Configuration Files

Navigate to the Apache configuration directory:

cd /etc/apache2/sites-available

Create configuration files for each domain:

sudo vi example1.com.conf

Press i to go into insert mode then Add the following configuration:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file (Press Esc to exit from insert mode and save and exit from editor by :wq! then Enter).

Repeat for the second domain:

sudo vi example2.com.conf

Add the corresponding configuration:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot /var/www/example2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Enable Virtual Host Files

Enable the configuration files using the a2ensite command:

sudo a2ensite example1.com.conf
sudo a2ensite example2.com.conf

Disable the default configuration (optional):

sudo a2dissite 000-default.conf

Step 5: Test Apache Configuration

Check for syntax errors:

sudo apache2ctl configtest

or

sudo apache2ctl -t

You should see:

Syntax OK

Step 6: Restart Apache

Apply the changes by restarting Apache:

sudo systemctl restart apache2

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

If your domain names are not yet live, map them to your server’s IP in your local machine’s hosts file:

sudo nano /etc/hosts

Add the following lines:

your-server-ip example1.com www.example1.com
your-server-ip example2.com www.example2.com

Save and close the file.


Step 8: Verify Configuration

Open a web browser and navigate to:

http://example1.com
http://example2.com

You should see the sample content for each site.


Optional Step: Enable HTTPS with Let’s Encrypt

To secure your websites with SSL, install Certbot:

sudo apt install certbot python3-certbot-apache -y

Obtain and install SSL certificates:

sudo certbot --apache

Follow the prompts to configure HTTPS for your domains.

Read Also | Top 11 Open Source Hosting Control Panels for Effective Server Management


Conclusion

Configuring Virtual Hosts in Apache is an essential skill for hosting multiple websites on a single server. By following this guide, you’ve learned how to configure vHost Apache in Linux, from setting up directories and creating configuration files to enabling and testing virtual hosts.

With this setup, you can efficiently manage multiple domains, improve organization, and optimize server performance. For enhanced security, consider enabling HTTPS with Let’s Encrypt.

Keep exploring more Apache configurations and server optimizations to enhance your hosting experience. Stay tuned to Magnetbyte for more in-depth tutorials and expert tech insights!

Related Posts


About Author

Photo of author

Jay

I specialize in web development, hosting solutions, and technical support, offering a unique blend of expertise in crafting websites, troubleshooting complex server issues, and optimizing web performance. With a passion for empowering businesses and individuals online, I provide in-depth reviews, tech tutorials, and practical guides to simplify the digital landscape. My goal is to deliver clear, reliable, and insightful content that helps readers make informed decisions and enhance their online presence.

Leave a Comment