Click Below to subscribe

How to Host Multiple Domains With Apache2 Ubuntu Web Server

In this article, we gonna learn, how to host multiple domains on the apache2 web server. Also how to setup SSL for these domains

Ubuntu Version:- 20.04

Let's take an example of two domains example1.com and example2.com

1. Create A record on DNS records that points to your server.

2. Log into your server via SSH.

ssh root@your_server_ip

3. Update apt and install apache2.

sudo apt update
sudo apt install apache2

4. Enable firewall.

Check firewall status.

sudo ufw status
root@ultimateakash:~# sudo ufw status
Status: inactive

If firewall's status is inactive, activate it by hitting the below command.

sudo ufw enable
root@ultimateakash:~# sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

5. Update ufw application profiles.

List the ufw application profiles.

sudo ufw app list
root@ultimateakash:~# sudo ufw app list
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH

These apache profiles were added during apache2 installation.

Allow Apache Full and OpenSSH profiles.

sudo ufw allow 'Apache Full'
sudo ufw allow OpenSSH

Check ufw status.

sudo ufw status
root@ultimateakash:~# sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Apache Full                ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
Apache Full (v6)           ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)

6. Check apache2 status

sudo systemctl status apache2
root@ultimateakash:~# sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-07-03 13:53:50 UTC; 10min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 2301 (apache2)
      Tasks: 55 (limit: 2339)
     Memory: 5.1M
     CGroup: /system.slice/apache2.service
             ├─2301 /usr/sbin/apache2 -k start
             ├─2303 /usr/sbin/apache2 -k start
             └─2304 /usr/sbin/apache2 -k start

Jul 03 13:53:50 ultimateakash systemd[1]: Starting The Apache HTTP Server...
Jul 03 13:53:50 ultimateakash systemd[1]: Started The Apache HTTP Server.

Open your domain/server IP in the browser. You will see the default apache2 installation page(/var/www/html/index.html ).

Apache2 has one server block enabled by default that is configured to serve documents from the /var/www/html directory.  

7. Setup Virtual Hosts.

Create the directory for your domains.

sudo mkdir /var/www/example1.com
sudo mkdir /var/www/example2.com

Assign ownership of the directory with the $USER environment variable.

sudo chown -R $USER:$USER /var/www/example1.com
sudo chown -R $USER:$USER /var/www/example2.com

Grant 775 permission to www directory.

sudo chmod -R 755 /var/www

Create a sample index.html for example1.com

sudo nano /var/www/example1.com/index.html
<html>
    <head>
        <title>example1.com</title>
    </head>
    <body>
        <h1>Welcome to example1.com</h1>
    </body>
</html>

press ctrl + x and press y then hit enter.

Create a sample index.html for example2.com

sudo nano /var/www/example2.com/index.html
<html>
    <head>
        <title>example2.com</title>
    </head>
    <body>
        <h1>Welcome to example2.com</h1>
    </body>
</html>

press ctrl + x and press y then hit enter.

Create a config file for example1.com.

sudo nano /etc/apache2/sites-available/example1.com.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

press ctrl + x and press y then hit enter.

Create a config file for example2.com.

sudo nano /etc/apache2/sites-available/example2.com.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example2.com
    ServerAlias www.example2.com
    DocumentRoot /var/www/example2.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

These three lines

ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain

Disable the default configuration

sudo a2dissite 000-default.conf

Enable these new configurations.

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

Reload apache2 to implement the changes.

sudo systemctl reload apache2

8. Next, open your domains in the browser. you will see our sample index.html

http://example1.com

http://example2.com

9. Install SSL certificates.

Install Certbot

sudo apt install certbot python3-certbot-apache

Obtaining SSL certificates.

sudo certbot --apache -d example1.com -d www.example1.com -d example2.com -d www.example2.com

you can pass multiple domains with -d option. you can even use wildcards.

-d *.example1.com

After hitting the above command you need to pass your email also you need to provide a few answers.

Checkout the sample below.

After completing the above step SSL certificates will be installed on your domains. You can verify it by hitting the https protocol.

https://example1.com

https://example2.com

Let’s Encrypt’s certificates are only valid for 90 days. but don't worry certbot takes care of renewals.

Check certbot's renewal service status.

sudo systemctl status certbot.timer
root@ultimateakash:~# sudo systemctl status certbot.timer
● certbot.timer - Run certbot twice daily
     Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Sun 2022-07-03 15:37:21 UTC; 5s ago
    Trigger: Mon 2022-07-04 06:42:10 UTC; 15h left
   Triggers: ● certbot.service

Jul 03 15:37:21 ultimateakash systemd[1]: Started Run certbot twice daily.

Leave Your Comment