Virtual Hosting allows multiple domains or websites to operate on a single server. It’s especially useful in shared hosting environments, where one server hosts thousands of websites, all sharing its resources. This guide will demonstrate how to set up Virtual Hosts on an Apache2 server for Ubuntu, Debian, and LinuxMint systems.

Advertisement

For those interested in a similar setup with Nginx on Ubuntu, you can refer to “Set Up Virtual Hosts in Nginx on Ubuntu.

Step 1: Installing Apache

If Apache is not already installed on your system, you can install it using the following commands. Remember to update your repositories first for best practice.

sudo apt-get update 
sudo apt-get install apache2 

Step 2: Setup Application Directory

Next, create the necessary directory structure and set the appropriate permissions and ownership:

mkdir -p /var/www/example.com/httpdocs 
chmod 755 /var/www/example.com/httpdocs 
chown www-data.www-data /var/www/example.com/httpdocs 

Then, upload your project files to the /var/www/example.com/httpdocs directory. For testing, a simple index.html can be created:

echo "<h2>It works</h2>" > /var/www/example.com/httpdocs/index.html 

Step 3: Configure the Virtual Host

Ubuntu stores Apache’s virtual host configuration files in /etc/apache2/sites-available. A default virtual host file is included with a new Apache installation. Create a new Virtual Host configuration file by duplicating the default file:

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

Edit the new file to match your requirements. An example configuration for example.com might look like this:


<VirtualHost *:80>
    # Basic server details
    ServerAdmin webmaster@example.com
    ServerName example.com
	ServerAlias www.example.com
    DocumentRoot /var/www/example.com/httpdocs

    # <Directory /> is often considered too permissive and could pose security risks.
    # Uncomment if specific use case requires it.
    # <Directory />
    #    Options FollowSymLinks
    #    AllowOverride None
    # </Directory>

    # Directory settings for /var/www/
    <Directory /var/www/example.com>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None

        # Use 'Require all granted' for the latest Apache versions
        # Replaces the older 'Order allow,deny' and 'allow from all'
        Require all granted
    </Directory>

    # CGI script settings
    # Uncomment if you are using CGI scripts
    # ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    # <Directory "/usr/lib/cgi-bin">
    #    AllowOverride None
    #    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    #    Require all granted
    # </Directory>

    # Logging settings
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn

    # Combined access log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save file and close it.

Step 4: Configure the SSL Virtual Host

To enhance the security of your website, it’s important to set up SSL (Secure Sockets Layer) for encrypted connections. Here’s how to create an SSL Virtual Host on your Apache server

  1. Install SSL/TLS Certificates: If you don’t have an SSL certificate for your site, you can obtain one from a Certificate Authority (CA) like Let’s Encrypt. For this example, we’ll use a self-signed certificate:
    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    	-keyout /etc/ssl/private/example.com.key \
    	-out /etc/ssl/certs/example.com.crt 
    

    This command generates a private key and a self-signed certificate valid for one year. Fill out the required information during the certificate creation process.

  2. Configure the SSL Virtual Host: Create a new SSL virtual host configuration file or edit an existing one:
    sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/example.com-ssl.conf 
    sudo nano /etc/apache2/sites-available/example.com-ssl.conf
    

    Modify the configuration to include the SSL directives and point to your SSL certificates:

    
    <VirtualHost *:443>
        # Basic server details
        ServerAdmin webmaster@example.com
        ServerName example.com
    	ServerAlias www.example.com
        DocumentRoot /var/www/example.com/httpdocs
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/example.com.crt
        SSLCertificateKeyFile /etc/ssl/private/example.com.key
        ...
    </VirtualHost>
    
    


  3. Enable SSL Module: Ensure that the SSL module is enabled in Apache:
    sudo a2enmod ssl
    

Step 4: Enable VirtualHosts

After creating the virtual host, enable it with:

a2ensite example.com 
a2ensite example.com-ssl

This command creates a symlink in the /etc/apache2/sites-enabled/ directory.

Step 5: Reload Changes

To activate the new configuration, reload Apache:

service apache2 reload 

You can now access the site at http://example.com. If DNS is not configured, add a local mapping in your /etc/hosts file.

Conclusion

Virtual Hosting on Apache servers is an efficient way to host multiple websites on a single server, maximizing resource utilization and simplifying management. This process not only makes web hosting more cost-effective but also allows for easy scalability as more websites can be added with minimal adjustments. Whether you’re managing a few websites or several, understanding and implementing virtual hosting can significantly streamline your web hosting experience.

Share.

4 Comments

  1. István Radics on

    Hi, Rahul,
    the second site take me to the apache default page ?
    Can you tell me what did I wrong?
    BR,
    István

  2. Robin Chaudhary on

    i got stuck while copying default configuration host file
    could not get the same result as above.
    can you help me?

Leave A Reply

Exit mobile version