Hosting Multiple Websites on a CPU Server (Apache)

March 03, 2022 Kehinde Olawuwo 3 minutes

    Introduction

    Sometimes, when you can buy a dedicated CPU server on AWS, GCP, Azure Linode or any other server providers, you may want to host more than one website on the server. In this article, I will be highlighting a step-by-step procedure on how to archive this.

    Also, add a free SSL certificate to your website.

    For this tutorial, I will be using a 1 GB Shared CPU instances on Linode with 1 GB RAM, 1 CPUs and 25 GB SSD storage.

    Requirements

    • A server (minimum specified above)
    • Domain Name
    • Your laptop, lol 😂

    Step 1 - Login to your server via SSH and update Ubuntu (Linux)

    Login to your server using SSH client from terminal using your server IP address.

    $ ssh root@{IP-Address}
    

    Once you have successfully logged into the server, you will need update the Ubuntu server using the command below.

    NOTE: You might use sudo depending on the sever user.

    $ apt update
    

    You will have to reboot the server after the update has been made by using the command below.

    $ reboot
    

    Step 2 - Install Apache HTTP Server

    To host your websites, you will need an Apache HTTP Server

    $ apt install apache2
    

    Once the Apache Server has been installed, you need to start the server by using the command

    $ systemctl start apache2
    

    Step 3 - Setting Up Virtual Hosts

    You need to set up virtual hosts for your configuration settings. Note that apache has a default configuration.

    Now, you need to create a directory for each of the websites. It is ideal to use the domain name while creating the directory and configuration settings.

    $ mkdir /var/www/firstdomain.com
    

    Using the command below, you can write a sample index.html for testing

    $ nano /var/www/firstdomain.com/index.html
    
    <html>
        <head>
            <title>Welcome to First Domain!</title>
        </head>
        <body>
            <h1>Success! The First Domain virtual host is working!</h1>
        </body>
    </html>
    

    After this, you will need to create a new configuration for each of the websites you would like to host.

    This will allow Apache to currently serve your websites.

    Create a configuration file using /etc/apache2/sites-available/firstdomain.com.conf

    <VirtualHost firstdomain.com:80>
      ServerName firstdomain.com
      ServerAdmin admin-email@firstdomain.com
      DocumentRoot /var/www/firstdomain.com/
      <Directory /var/www/firstdomain.com/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
      </Directory>
      ErrorLog ${APACHE_LOG_DIR}/firstdomain.com_error.log
      LogLevel warn
      CustomLog ${APACHE_LOG_DIR}/firstdomain.com_access.log combined
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =firstdomain.com [OR]
    RewriteCond %{SERVER_NAME} =www.firstdomain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    </VirtualHost>
    

    After saving the configuration file for the firstdomain.com, you will need to enable the virtual host configuration file for the website by using the command below.

    $ a2ensite firstdomain.com.conf
    

    Also, you have to disable the 000-default.conf

    $ a2dissite 000-default.conf
    

    You can repeat this steps for multiple websites as much as you can by changing the name firstdomain.com and replacing with your website domain name.

    After all the configuration has been made, don’t forget to restart your apache server to implement your changes.

    $ systemctl restart apache2
    

    Step 4 - Install SSL to secure your website.

    To make your website secured with HTTPS, you will need to install SSL (Free Let’s Encrypt) on your website.

    Using the instruction from Certbot Instructions

    $ snap install core; sudo snap refresh core
    
    $ sudo snap install --classic certbot
    
    $ sudo ln -s /snap/bin/certbot /usr/bin/certbot
    

    Install your certificates

    Run this command to get a certificate and have Certbot edit your apache configuration automatically to serve it, turning on HTTPS access in a single step.

    $ certbot --apache
    

    Follow the instructions and enter the information from on the command line prompt.

    Lastly, if all configuration and set up has been made successfully, confirm by visiting your website https://firstdomain.com or https://www.firstdomain.com

    If you have any error or issue, please reach out on Twitter.

    If you find this useful, kindly share and repost. Thank you and happy coding 😊!