How to Add an Apache Virtual Host on Ubuntu

Introduction

In this guide, we’ll show you how to configure an Apache virtual host on an Ubuntu server. Virtual hosts allow you to run multiple websites from a single server, each responding to its own domain name. This can be useful for hosting multiple websites or web applications with unique configurations while optimizing server resources.

By the end of this guide, you’ll know how to:

  • Set up multiple websites on a single Apache server.
  • Configure domain-specific document roots, logs, and server names.

Prerequisites

Before proceeding, ensure you have the following ready:

  • An Ubuntu server with Apache installed.
  • Root or sudo user access to your server.
  • A registered domain name that points to your server’s public IP address (for live environments). If you’re testing locally, you can use a dummy domain.

Step 1: Create a Directory for the New Virtual Host

A virtual host requires a directory to store website files. By default, Apache uses /var/www/ as the root directory for web files. Create a folder for the website you plan to host under /var/www:

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

Explanation:

  • -p: This option ensures that all necessary directories are created if they don’t already exist. For example, if /var/www/yourdomain.com/ doesn’t exist, the command will create both that directory and the public_html folder.
  • yourdomain.com: Replace this with your actual domain name.

Best Practice:

It’s a good idea to organize your website files inside a public_html subdirectory within the domain’s folder. This makes it easier to manage other files (like logs, backups, or configuration files) that are related to the site but shouldn’t be publicly accessible.


Step 2: Set Permissions

For Apache to serve your website correctly, ensure the correct ownership and permissions for your new directory:

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

This command assigns ownership of the directory to the current user ($USER) and the user’s group ($USER). If your Apache server runs as a different user (e.g., www-data), make sure the server has access to read and execute files from the directory.

Set directory permissions:

sudo chmod -R 755 /var/www/yourdomain.com

These permissions allow the owner to read, write, and execute files, while others can only read and execute.

Additional Notes:

  • For more sensitive setups, you might consider limiting file permissions further and using different ownership models depending on your server environment.

Step 3: Create a Sample HTML File

It’s helpful to have a simple webpage in place to test whether the virtual host is working. Let’s create a basic HTML file in the directory we just set up:

nano /var/www/yourdomain.com/public_html/index.html

Add the following HTML content:

<html>
<head>
<title>Welcome to YourDomain.com</title>
</head>
<body>
<h1>Yay its working</h1>
<p>This is a placeholder page for <strong>yourdomain.com</strong>.</p>
</body>
</html>

Save and exit the editor by pressing CTRL+X, then Y, and Enter.


Step 4: Configure the Virtual Host

To create the virtual host configuration, we’ll use Apache’s sites-available directory. Virtual hosts in Apache are defined in individual configuration files located in /etc/apache2/sites-available/.

Create a new configuration file for your domain:

nano /etc/apache2/sites-available/yourdomain.com.conf

Add the following configuration block:

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

Key Parameters:

  • <VirtualHost *:80>: Tells Apache to listen for requests on port 80 (HTTP) for all IP addresses. You can modify this if you’re hosting over HTTPS (<VirtualHost *:443>).
  • ServerAdmin: The email address for the server administrator. Apache will display this address in error messages.
  • ServerName: The domain name the virtual host should respond to.
  • ServerAlias: Alternative domain names or subdomains (e.g., www.yourdomain.com).
  • DocumentRoot: Specifies the directory where your website files are located.
  • ErrorLog and CustomLog: Paths for storing error logs and access logs specific to this virtual host.

Step 5: Enable the Virtual Host

After creating the configuration file, enable the new virtual host using a2ensite (Apache’s site enabling command):

sudo a2ensite yourdomain.com.conf

Disable the default virtual host if you no longer need it:

sudo a2dissite 000-default.conf

Reload Apache to apply these changes:

sudo systemctl reload apache2

Step 6: Check Apache Configuration for Syntax Errors

Before restarting Apache, it’s always good to check for syntax errors in your configuration files:

sudo apache2ctl configtest

If the configuration is correct, you’ll see a message like Syntax OK. If there’s an issue, the error message will indicate what’s wrong.


Step 7: Restart Apache

Once everything checks out, restart Apache to apply the virtual host configuration:

sudo systemctl restart apache2

Step 8: Update Local Hosts File (For Testing)

If your domain name hasn’t propagated yet, or you’re testing locally, update your local hosts file so your machine can resolve the domain to your server’s IP.

Open the /etc/hosts file:

sudo nano /etc/hosts

Add the following line (replace yourdomain.com with your domain):

127.0.0.1 yourdomain.com

This will allow you to access yourdomain.com in a browser, and it will resolve to your local server.


Troubleshooting Tips

  • If your virtual host isn’t working: Double-check that you’ve enabled the virtual host with a2ensite and restarted Apache.
  • If you’re getting permission denied errors: Ensure the correct file permissions are set for the web directories.
  • If your domain isn’t resolving: Check your DNS records or the hosts file for local development.

Additional Configurations

Setting up HTTPS (Optional):

If you want your virtual host to serve traffic over HTTPS, you’ll need to install an SSL certificate and configure a virtual host for port 443. You can use Let’s Encrypt for free SSL certificates. Here’s a basic configuration example for HTTPS:

<VirtualHost *:443>
    ServerAdmin [email protected]
    ServerName yourdomain.com
    DocumentRoot /var/www/yourdomain.com/public_html
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/yourdomain.com.crt
    SSLCertificateKeyFile /etc/ssl/private/yourdomain.com.key
</VirtualHost>

Conclusion

You’ve now successfully set up a new Apache virtual host on your Ubuntu server. With virtual hosts, you can host multiple websites on a single server, each with its own domain name and configuration. Don’t forget to repeat the process for any additional domains you want to host.

Leave a Reply

Your email address will not be published. Required fields are marked *