Setting up your own VPN (Virtual Private Network) server can offer enhanced privacy, secure connections, and the ability to bypass geo-restrictions. AWS EC2 provides an excellent platform to create a scalable, reliable VPN server. In this guide, we’ll walk through the process of creating a VPN server on AWS EC2.
Before we dive in, make sure you have the following:
t2.micro
instance (with free tier eligibility) is sufficient for a personal VPN server.1194
.Once the instance is running:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
your-key.pem
with your private key file and your-ec2-public-ip
with the public IP of your EC2 instance.sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install openvpn -y
sudo apt-get install easy-rsa -y
make-cadir ~/openvpn-ca cd ~/openvpn-ca
vars
file:nano vars
export KEY_COUNTRY="US" export KEY_PROVINCE="CA" export KEY_CITY="SanFrancisco" export KEY_ORG="MyVPN" export KEY_EMAIL="[email protected]" export KEY_OU="MyVPN" export KEY_NAME="server"
source vars ./clean-all ./build-ca
./build-key-server server ./build-dh openvpn --genkey --secret keys/ta.key
./build-key client1
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ cd /etc/openvpn/ sudo gunzip server.conf.gz sudo nano server.conf
server.conf
file:ca /etc/openvpn/ca.crt cert /etc/openvpn/server.crt key /etc/openvpn/server.key dh /etc/openvpn/dh2048.pem tls-auth /etc/openvpn/ta.key 0 cipher AES-256-CBC user nobody group nogroup
sudo nano /etc/sysctl.conf
net.ipv4.ip_forward=1
sudo sysctl -p
sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE sudo apt-get install iptables-persistent sudo netfilter-persistent save sudo netfilter-persistent reload
sudo systemctl start openvpn@server sudo systemctl enable openvpn@server
client1.crt
), key (client1.key
), and the ca.crt
file to your local machine.client.ovpn
):client dev tun proto udp remote your-ec2-public-ip 1194 resolv-retry infinite nobind user nobody group nogroup persist-key persist-tun ca ca.crt cert client1.crt key client1.key tls-auth ta.key 1 cipher AES-256-CBC
client.ovpn
file.Congratulations! You’ve successfully set up a VPN server on AWS EC2. This setup provides you with a secure and private connection to the internet, ideal for protecting your data and bypassing network restrictions. Remember to monitor your EC2 instance and apply updates regularly to maintain security.