How to Host Two Sites with Apache: A Step-by-Step Guide

Hosting multiple sites on a single server is a common requirement for web developers and businesses. Apache is a popular web server that can handle multiple sites with ease. In this blog post, we will guide you on how to host two sites with Apache on a single server.

Step 1: Install Apache The first step is to install Apache on your server. Apache is available on most Linux distributions and can be easily installed with the package manager. For example, on Ubuntu, you can install Apache by running the following command:

sudo apt-get update
sudo apt-get install apache2

Step 2: Configure Apache Once you have installed Apache, you need to configure it to host multiple sites. The configuration files for Apache are located in the /etc/apache2 directory. The main configuration file is httpd.conf, but we will be using apache2.conf and sites-enabled directory.

Step 3: Create Virtual Hosts Virtual Hosts are used to host multiple sites on a single server. To create a virtual host, you need to create a new configuration file in the /etc/apache2/sites-available directory. For example, if you want to host two sites, example1.com and example2.com, you would create two configuration files called example1.com.conf and example2.com.conf.

You can create the configuration files using the following command:

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

In the file, you need to add the following configuration:

<VirtualHost *:80>
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com/public_html
    ErrorLog /var/www/example1.com/error.log
    CustomLog /var/www/example1.com/access.log combined
</VirtualHost>

You can repeat this step to create a configuration file for example2.com.

Step 4: Enable Virtual Hosts After creating the virtual host configuration files, you need to enable them by creating a symbolic link from the sites-available directory to the sites-enabled directory. You can do this using the following command:

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

Step 5: Restart Apache After enabling the virtual hosts, you need to restart Apache for the changes to take effect. You can do this using the following command:

sudo service apache2 restart

Step 6: Test Your Sites You can now test your sites by entering the domain name into your web browser. If everything is configured correctly, you should see the default Apache page or the content that you have added to your site’s document root.

Conclusion In this blog post, we have explained how to host two sites with Apache on a single server. The process involves installing Apache, configuring virtual hosts, enabling them, and restarting Apache. With these simple steps, you can host multiple sites on your server with ease.

Leave a Comment