Setting up a Mail Server in EC2
A Quick Guide.By Chris Rowles on 24th April 2019
This guide will teach you how to configure an email server for your domain, using an Amazon EC2 instance
- A domain name, with control over the DNS records
- An AWS account
- Familiarity with Linux and the AWS ecosystem
Firstly you'll want to setup a dedicated host for sending and receiving email. I would recommend configuring a new instance with the following, which is within free-tier limits:
- Amazon Linux 2 AMI (64-bit)
- Type t2.micro
- Default instance details
- Default storage details
- No tags
- A new security group (details are directly below)
Your mail server will listen for incoming mail via SMTP, so you should create a new dedicated security group and open inbound TCP port 25 so that you will be able to receive mail.
Finally, allocate an Elastic IP address and associate it to the newly created instance. This will provide a static IP address for your mail server.
Next, youll need to create the required DNS records in order for emails to be properly routed through your host.
Create an A record for your host from
mail.domain.tld to your new Elastic IP address
Then create an MX record for
NOTE: Anti-spam software will require that any server sending mail has a valid reverse DNS record. You will need to Contact AWS to create this record.
Now we'll need to SSH into the new instance to configure it for mail.
Firstly, ensure your host knows its own name:
$ /etc/hosts 127.0.0.1 mail.domain.tld localhost localhost.localdomain
$ /etc/sysconfig/network HOSTNAME=mail.domain.tld
We'll be using postfix to send and receive our mail. If it isn't already installed, please install it now:
$ sudo yum install -y postfix
The minimum changes we'll need to make are listed below:
$ /etc/postfix/main.cf mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain mydomain = domain.tld myhostname = mail.domain.tld mynetworks_style = host myorigin = $mydomain
After the required changes have been made, start postfix and enable it to start on reboot:
$ sudo systemctl start postfix $ sudo systemctl enable postfix
You should now have a functioning mail server! You can test this by installing a mail user agent such as mailx:
$ sudo yum install -y mailx
To test sending mail:
$ echo "Hello World" | mail -s subject firstname.lastname@example.org
To test receiving mail:
$ mail Heirloom Mail version 12.5. Type ? for help. "/var/spool/mail/ec2-user": 1 messages 1 new ... ...
I hope this quick guide helps! If you have any questions, please feel free to contact email@example.com :)