Skip to content

Installing OpenVPN on AWS EC2's Ubuntu 22.04 in 5 minutes

kangwijen

3 min read

I'm a cheapskate and don't want to spend $10-$15/month on commercial VPN services, so I decided to run my own VPN server on an AWS EC2 instance. It costs around $5/month with on-demand instances, less with other instance types, or nothing for 1 year if you have the free trial tier. You can learn something along the way too. This tutorial should apply to any Linux distro, but I'll use an Ubuntu 22.04 instance.

Launching an EC2 instance

This part is easy, head to your EC2 dashboard in the region you've selected and click the Launch Instance button. That will take you to a new setup page. In the "Name and tags" section, you can assign a name to your instance.

image-4

Next, you'll choose the OS image to install. For this guide, I'm using Ubuntu 22.04.

image

After that, select the instance type. Go with t2.micro if you're using the free tier, or t2.nano if you're aiming for minimal cost. Personally, I've been running t2.nano as my main VPN server, and it performs well.

image-1

Then you'll be prompted to select or create a key pair file, which is required for logging in to the instance. If you don't have one, click "Create new key pair". You'll need to name it, choose the encryption method, and pick a file format. Stick with the defaults unless your system requires something specific. The file will be downloaded automatically, store it well and don't lose it (And don't share it to anyone). You're going to access the instance with this file.

image-2

In the Network settings section, you can leave the defaults for now, but make a note of the network name since you'll be modifying it later.

image-5

Last section is the storage section, leave it as default.

image-6

Configuring the instance's firewall

After you've launched the EC2 instance, we're now going to configure the instance's firewall. It should be accessible from the left tab and the option "Security Groups" under the "Network & Security" section.

image-7

Remember the network name I told you to remember? Try clicking on the associated security group. It should send you to a new page

image-8

On the new page, click on "Inbound rules" and click on "Edit inbound rules" button on the right. Then click on "Add rule"

image-11 image-12

The set your configuration exactly like this

image-13

Click "Save rules" and we're done.

Installing OpenVPN on the EC2 instance

Click on the "Instances" button under the "Instances" section, then click on your instance and click on connect. You can choose "Connect using EC2 Instance Connect" and leave it as default as it is the easiest way. Or you can use any other way that you like. First thing to do is to elevate our user to sudo and update the OS.

bash
sudo su 
apt update -y && apt upgrade -y

Wait until it's done, then install OpenVPN with angristan's script (opens in a new tab). You don't need to download anything manually. Just run this command.

bash
curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh
chmod +x openvpn-install.sh
./openvpn-install.sh

Then just click enter until it asks you to input a username, choose one and it should generate an .ovpn file on the Ubuntu user (not root) home directory. You then can move this to your local machine via scp.

Connecting to the VPN server

Import the config file into your OpenVPN GUI, then click connect.

image-14

Here's whatsmyip result:

image-15

I get around 70-80 Mbps for both download and upload on my 100 Mbps internet connection.

image-16