Hosting platforms

AWS EC2 for a small website: the setup choices that matter

In this article12 sections

Amazon EC2 is a powerful way to host a website, and an easy way to get a surprise bill or a hacked account. It was built for engineers who assemble their own infrastructure, so it hands you every decision a shared host would make for you: the network rules, the disk, the backups, the address, the monitoring, and the cost controls. Make those decisions on purpose at the start, and a small EC2 server is stable and cheap. Skip them, and you learn them from an incident.

This guide covers the choices that matter for a small website or application on a single EC2 instance. It is the setup I would put in place before installing a web server. It assumes you are comfortable with SSH and Linux; for the server side see the guides on hardening SSH and the Ubuntu hardening checklist.

Is EC2 the right choice?

For one WordPress site or a small app, EC2 gives you control and a pay-for-what-you-use model, and costs you your time. Amazon Lightsail, a simpler product from the same company with fixed monthly prices and a simpler interface, suits many small sites better. A managed host or a conventional VPS may be simpler still. Choose EC2 when you need its surroundings (other AWS services, private networking, the ability to grow), not because it is the famous name.

1. Secure the account before you launch anything

An AWS account holds your billing and everything you build in it, so a stolen login is expensive.

  • Turn on multi-factor authentication for the root user, and then stop using the root user for daily work.
  • Create an administrator identity (an IAM user, or better, an identity from IAM Identity Center) with MFA, and use that.
  • Never create access keys for the root user. Programs and scripts get their own limited identities.
  • Set a billing alert or budget (AWS Budgets) at an amount that would surprise you, with email notification. It is the single best protection against runaway costs.
  • Pick a region close to your visitors and stay in it; resources in other regions are easy to forget.

2. Choose the instance deliberately

  • Burstable instances (the T family) are cost-effective for sites with low average load and occasional spikes. They earn CPU credits while idle and spend them under load. Know which credit mode your instance uses: in the “unlimited” mode, sustained use above the baseline is allowed but billed extra; in the “standard” mode, the CPU is throttled when credits run out. Check which one applies, and watch the credit balance in CloudWatch.
  • Graviton (ARM) instances, such as the t4g family, are often cheaper for the same performance, and run most Linux software well. Check that every program you need has an ARM build before you commit.
  • Memory matters more than CPU for most PHP and database workloads. Size from measurements, not from guesses. For a small WordPress site, a modest amount of memory plus a page cache is often enough.
  • Choose a current Ubuntu LTS image (or another supported Linux), and plan for the day its support ends.

3. The disk: EBS volumes

Your instance’s disk is an EBS volume.

  • Use the current general-purpose SSD type (gp3), which lets you set size and performance separately.
  • Size for growth, and know that you can enlarge a volume later without downtime. After enlarging it, extend the partition and the filesystem (see the commands in the guide on finding what is eating your disk).
  • Turn on encryption by default for new volumes in the region’s EC2 settings; it costs nothing noticeable and protects snapshots.
  • Delete on termination is on for the root volume by default, which is usually right, but know it, so that terminating an instance does not surprise you by taking data with it. Turn on termination protection for the instance.

4. The network: security groups and addresses

A security group is the firewall around your instance. It is stateful: replies to allowed traffic are allowed automatically. The rules to start with:

  • Inbound 80 and 443 from anywhere (0.0.0.0/0 and ::/0).
  • Inbound 22 (SSH) only from your own address, or none at all if you use AWS Systems Manager Session Manager, which gives you a shell through the AWS console without opening a port. Never leave SSH open to the entire internet, even with key-only login.
  • No database port (3306, 5432) from outside. Bind databases to the local machine, or allow them only from another security group.
  • Everything else closed. The default outbound rule (all traffic) is fine for a small server.

An Elastic IP is a static public address that stays with you when you stop and start an instance. Without one, the public address changes when the instance is stopped. Allocate one, associate it with your instance, and point your DNS at it. AWS now charges for public IPv4 addresses, including ones that are in use, so check the current price on the EC2 pricing page and release addresses you no longer use; an unattached Elastic IP still costs money.

Require IMDSv2. The instance metadata service can hand out the instance’s credentials, and the older version is vulnerable to request-forgery attacks from a vulnerable web application. Require the token-based version:

aws ec2 modify-instance-metadata-options --instance-id i-0123456789abcdef0 --http-tokens required --http-endpoint enabled

You can also choose this at launch. If your server uses an IAM role (for backups to S3, for example), give it the narrowest permissions it needs and nothing more.

5. Backups: snapshots, and a restore you have tried

An EBS snapshot is a point-in-time copy of a volume, stored in S3. Take them automatically, using Amazon Data Lifecycle Manager to schedule snapshots and delete old ones, so that they do not pile up and cost you.

Two cautions. A snapshot of a running database volume is “crash-consistent”, not guaranteed clean, so for databases also take a proper dump (mysqldump --single-transaction) to a separate location. And test restoring: create a volume from a snapshot, attach it to a test instance, and look at the data. Keep a copy of important backups outside the account, or at least in another region, in case the account itself is compromised.

6. Email: port 25 and SES

By default, AWS blocks outbound traffic on port 25 from EC2 instances to the public internet, to prevent spam, so a mail server on a new instance cannot deliver to other servers. You have two choices:

  • Request removal of the restriction, by submitting AWS’s “Request to Remove Email Sending Limitations” form with your use case and Elastic IP. Approval is not guaranteed, and you must also set up reverse DNS and keep your IP’s reputation clean.
  • Send through a mail relay on port 587 or 465, such as Amazon SES, SendGrid or Mailgun. This is the right answer for most websites: it needs no port 25, and the provider handles reputation. New SES accounts start in a sandbox with sending limits, and you request production access.

Either way, set SPF, DKIM and DMARC for your domain (see SPF, DKIM and DMARC for several services). If something is rejected, the guide to reading email bounces helps.

7. Monitoring that you will see

CloudWatch gives you basic metrics (CPU, network, disk operations, status checks) without any setup. It does not show memory or disk space inside the machine unless you install the CloudWatch agent. Do these:

  • A status-check alarm that notifies you, and optionally auto-recovers the instance.
  • A disk and memory check inside the server, either with the CloudWatch agent or a script that pings an external heartbeat monitor (see monitoring a small server).
  • An external uptime monitor on your site, because the AWS console will not tell you the website is down.

8. The cost traps

  • A public IPv4 address per resource, charged hourly, whether or not it is in use.
  • Data transfer out to the internet is billed per gigabyte. A site with heavy downloads or video can outgrow the free allowance quickly; a CDN in front helps.
  • NAT gateways are expensive for a small setup. A single public instance does not need one.
  • Load balancers left running after a test.
  • Forgotten volumes and snapshots after you terminate an instance.
  • Burstable CPU in unlimited mode under constant load.
  • Resources in other regions that you forgot about.

Look at the Cost Explorer once a month for the first three months.

9. After launch: the server itself

An EC2 instance is a normal Linux server from this point. Create a named administrator, key-only SSH, a firewall (the security group plus the server’s own), automatic security updates, and backups, as in the hardening SSH guide. Serve the site behind a valid certificate (see SSL problems and fixes).

A launch checklist

  1. Root MFA on, no root keys, an admin identity, a billing alert.
  2. Instance type and credit mode chosen; EBS encrypted.
  3. Security group: 80/443 open, 22 restricted, nothing else.
  4. Elastic IP attached; IMDSv2 required; termination protection on.
  5. Snapshot schedule with deletion; a restore tested.
  6. Outbound mail via a relay or port 25 requested.
  7. Status alarm, internal checks and an external uptime monitor.
  8. Cost Explorer checked after the first month.

When it is worth getting help

AWS is not hard, but it is wide, and the mistakes (an open port, a missing alert, an unattached resource) are quiet. If you are launching a business site on EC2, moving from another host, or inheriting an AWS account and want someone to review it, I set up and administer EC2 servers, migrate sites to and from AWS, and hand over documentation.

You can see my profile on Upwork, or read about hosting platform setup and server migration.