Setting Up Multiple Websites on a Single Apache Server

Setting Up Multiple Websites on a Single Apache Server

This guide will walk you through the steps to install Apache, host multiple websites using Virtual Hosts, and enable SSL for secure access using Let’s Encrypt certificates.

Step 1: Install Apache

First, ensure your server’s package index is up-to-date, then install Apache:

sudo apt update
sudo apt install apache2 -y

After the installation completes, start Apache and enable it to start on boot:

sudo systemctl start apache2
sudo systemctl enable apache2

To verify that Apache is running, check its status:

sudo systemctl status apache2

Step 2: Create Directories for Websites

Create separate directories for each website you plan to host:

sudo mkdir -p /var/www/html/site1
sudo mkdir -p /var/www/html/site2

Step 3: Set Ownership and Permissions

Assign the ownership of these directories to your user and set the correct permissions:

sudo chown -R $USER:$USER /var/www/html/site1
sudo chown -R $USER:$USER /var/www/html/site2
sudo chmod -R 755 /var/www/html/

Step 4: Create an Index Page for Each Site

To verify each site’s configuration, add a simple HTML file for each site:

echo "<h1>Welcome to Site 1</h1>" | sudo tee /var/www/html/site1/index.html
echo "<h1>Welcome to Site 2</h1>" | sudo tee /var/www/html/site2/index.html

Step 5: Create Apache Configuration Files for Each Site

Define separate Apache Virtual Hosts for each site by creating configuration files in the sites-available directory.

Configuration for Site 1

Open a configuration file for Site 1:

sudo vi /etc/apache2/sites-available/site1.conf

Add the following content:

<VirtualHost *:80>
    ServerAdmin admin@site1.dineshcloud.in
    ServerName site1.dineshcloud.in
    DocumentRoot /var/www/html/site1

    <Directory /var/www/html/site1>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site1-error.log
    CustomLog ${APACHE_LOG_DIR}/site1-access.log combined
</VirtualHost>

Configuration for Site 2

Similarly, create a configuration file for Site 2:

sudo vi /etc/apache2/sites-available/site2.conf

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin admin@site2.dineshcloud.in
    ServerName site2.dineshcloud.in
    DocumentRoot /var/www/html/site2

    <Directory /var/www/html/site2>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site2-error.log
    CustomLog ${APACHE_LOG_DIR}/site2-access.log combined
</VirtualHost>

Step 6: Enable the Sites

Activate each site configuration:

sudo a2ensite site1.conf
sudo a2ensite site2.conf

Step 7: Reload Apache

Reload Apache to apply the new configurations:

sudo systemctl reload apache2

Step 8: Verify the Setup

Visit each site by navigating to the configured domain (e.g., http://site1.dineshcloud.in and http://site2.dineshcloud.in). Each domain should display the content from the respective index.html.

Step 9: Configure SSL with Certbot (Optional)

To secure your sites with HTTPS, use Certbot to generate SSL certificates from Let’s Encrypt.

  1. Install Certbot and the Apache plugin:

     sudo apt install certbot python3-certbot-apache
    
  2. Run Certbot to configure SSL for each domain:

     sudo certbot --apache -d site1.dineshcloud.in
     sudo certbot --apache -d site2.dineshcloud.in
    

Certbot will automatically handle the SSL setup, allowing you to access each site securely via HTTPS.


Conclusion

By following this guide, you’ve successfully set up multiple websites on a single Apache server, configured Virtual Hosts, and enabled SSL for enhanced security. With Apache and Certbot, hosting multiple sites securely becomes a streamlined process.