Spinning up a single-domain web and email server

This tutorial will show you how to take a fresh Ubuntu 16.04 LTS VPS and turn it into a web and email server for a single domain.  Many VPS providers offer $5 solutions with 1GB of RAM and 1 vCPU.  This is more than enough.

The full script is available at the end of this post.  Between here and there, I’m going to explain the different sections of the bash file, and what you need to change.

Step 0: Header

At the beginning of our bash file we have some header items.

Step 1: Allow you to SSH in as root with your key.

Some providers, like DigitalOcean, automatically add your SSH key to the root user, so you don’t need this step.  However, others like Linode do not.  If you need you add your SSH key, use the template below.

Line 12 will place your SSH key into the authorized_keys for root.  Be sure to add your key.

Step 2: Remove Snaps

Newer Ubuntu installations come with snaps.  They are handy, if that’s what you want.  We don’t, however.

Step 3: Create swap file

This will create a 2GB swap file on the disk for us.  The exact size of this depends on the server you are running.  Some VPS providers set you up with one out of the box.  This is optional, if it’s a small site with minimal database usage, you probably don’t need this.

Step 4: Add new repos for CertBot and newest PHP

Ubuntu, out-of-the-box, does not have the repositories that contain CertBot or PHP 7.2.  (As of the time of writing)  We need to add them.

UPDATE (20 Apr 2018): I’ve changed the order of this script and added the software-properties-common.  While Digital Ocean had that package installed, Linode did not.

Step 5: Install Software

This will install Apache, CertBot, PHP 7.2, MySQL, Postfix, Dovecot, and OpenDKIM, as well as their modules.

Apache

CertBot

PHP

I have commented out the install command for 7.0 and 7.1, leaving 7.2 to be installed.  If you need a different version, feel free to change this.

MySQL

When MySQL installs, you will need to set up a root password.

Postfix, Dovecot, and OpenDKIM

When Postfix installs, you will need to answer two questions.  The first, you are installing it on an internet site; and the second, put in the domain, like example.com or lupecode.com.

Step 6: Set the domain variable.

To make your editing a little easier, we will store the domain, like example.com or lupecode.com, in a console variable.

We’ll also save it so the new site script can use it.

UPDATE (20 Apr 2018): I’ve had to add the update of the hostname.  Digital Ocean does this when the instance is spun up, but Linode does not.

Step 7: Setup Certbot

Now we want to activate CertBot to get a wildcard certificate for our domain.

You will need to update your DNS records by hand.

You will also need to have the domain, like example.com or lupecode.com, pointing to the server already as you will need to create a challenge file.

Step 8: Configure Apache

We added our domain as a variable to the console, so you can just copy these four cats.  They set up the domain with a SSL site and reditect the non-SSL.  HSTS is also included, as well as a sweet landing page.

The /var/www/$domain/index.html file

The /etc/apache2/sites-available/000-default.conf file

The /etc/apache2/sites-available/000-default-le-ssl.conf file

The /etc/letsencrypt/options-ssl-apache.conf file

And restart Apache.

 Step 9: Setup Postfix.

There will be multiple files here as well.

The /etc/postfix/main.cf file

The /etc/postfix/master.cf file

The /etc/postfix/mysql-virtual-mailbox-domains.cf file

The /etc/postfix/mysql-virtual-alias-maps.cf file

The /etc/postfix/mysql-virtual-mailbox-maps.cf file

The /etc/postfix/mysql-virtual-email2email.cf file

That’s finished.

 Step 10: Setup MySQL Database

We’re going to create an SQL file and run it!

You will have the database prepped with the domain, an admin@ account with the password you set before, and a catchall forwarding to the admin@ account.

Step 11: Dovecot Setup

First we need to add the vmail user.

The /etc/dovecot/dovecot.conf file

The /etc/dovecot/conf.d/10-mail.conf file

The /etc/dovecot/conf.d/10-auth.conf file

The /etc/dovecot/conf.d/auth-sql.conf.ext file

The /etc/dovecot/dovecot-sql.conf.ext file

The /etc/dovecot/conf.d/10-master.conf file

The /etc/dovecot/conf.d/10-ssl.conf file

And restart Dovecot

Step 12: OpenDKIM Setup

First we will need a few directories.

Then some config files.

The /etc/opendkim.conf file

The /etc/default/opendkim file

The /etc/opendkim/TrustedHosts file

The /etc/opendkim/KeyTable file

The /etc/opendkim/SigningTable file

And now to generate the key.

And restart Postfix and OpenDKIM

Step 13: DNS

You will want to take the DNS record in /etc/opendkim/keys/$domain/mail.txt and add it to your DNS.  This is the last step to get DKIM to work.

Summary

I have this entire thing as a shell file for those that want it.  Just be sure to change the SSH key near the top.

The repository for this post can be found on GitLab here .

The repository for this post can be found on Lupe Code’s GitLab mirror here .