Engineering. Simplified!

Hosting Virtual Hosts with Apache on Ubuntu

Pull your content together.

You have probably generated the static content in a folder called staging. Create a compressed tar file and copy that to your destination machine. (Your SSH keys and configuration need to be appropriately set).

$ tar -zcvf staging.tgz staging
$ scp staging.tgz ubuntu@basu.net.in:staging.tgz

Create the Directory Structure

We’re creating content for the domain smplfd.basu.net.in. We’ll create directories for the content under /var/www and set permissions appropriately.

$ sudo mkdir -p /var/www/smplfd.basu.net.in/public_html
$ sudo chown -R $USER:$USER /var/www/smplfd.basu.net.in

Create a default index.html

Create a basic index.html to test your setup.

    <html>
        <head>
            <title>Welcome to smplfd.basu.net.in!</title>
        </head>
        <body>
            <h1>Success! The smplfd.basu.net.in virtual host is working!</h1>
        </body>
    </html>

Create New Virtual Host Files

Copy the default 000-default.conf for your domain: smplfd.basu.net.in.conf.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/smplfd.basu.net.in.conf

Edit the Virtual Host File

The final file should look like this:

<VirtualHost *:80>
    
    ServerAdmin admin@smplfd.basu.net.in
    ServerName smplfd.basu.net.in
    ServerAlias www.smplfd.basu.net.in
    DocumentRoot /var/www/smplfd.basu.net.in/public_html
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Enable The New Virtual Host

Enable the new virtual host using a2ensite command. Also ensure that the default 000-default.conf site is disabled using a2dissite.

$ sudo a2ensite smplfd.basu.net.in.conf
Enabling site smplfd.basu.net.in .
To activate the new configuration, you need to run:
  systemctl reload apache2
$
$ sudo a2dissite 000-default.conf
Site 000-default disabled.
To activate the new configuration, you need to run:
  systemctl reload apache2

Test For Configuration Errors

Use the provided apache2ctl tool to validate the configs.

$ sudo apache2ctl configtest
. . .
Syntax OK

Restart The Server

Restart using systemctl

sudo systemctl restart apache2

Add the Domain To Your DNS Provide

  • Log in to the AWS console, and access your Route53 console.
  • Add an entry for smplfd.basu.net.in as an A record pointing to the base record.

Create or Update SSL Certificate

Use LetsEncrypt to create or update your certificate.

Warning! This step only works if your DNS records are upto date. If this step fails, check your relevant DNS records.

$ sudo certbot --apache