Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Deploy WireGuard Server With Web UI Through Docker

Introduction

This tutorial will show you how to quickly deploy your own WG Easy wireguard server, which I found to be the easiest and fastest way to deploy a VPN server, which even worked behind my dedicated server's NAT (after having configured port forwarding).

If you wish to deploy a Wireguard server from scratch (e.g. not using Docker and without a nice web UI, then I would suggest you watch Christian Lempa's video on Wireguard Installation and configuration instead.

Prerequisites

  • Docker and Docker Compose

Steps

Create The Docker Compose File

Log into your server and create the following docker-compose.yml file wherever you wish to "install" this.

services:

  wg-easy:
    image: ghcr.io/wg-easy/wg-easy:${WG_VERSION}
    container_name: wg-easy
    ports:
      - "51820:51820/udp"
      - "51821:51821/tcp"
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    sysctls:
      - net.ipv4.ip_forward=1
      - net.ipv4.conf.all.src_valid_mark=1
    volumes:
      - ./volumes/wireguard:/etc/wireguard
    environment:
      - WG_HOST
      - PASSWORD

Create The Environment File

Now create a file called .env with the following content, in the same directory as the docker-compose.yml file you just created. Be sure to update the values as appropriate to you.

# Specify the version of WG Easy to use. Check out 
# https://github.com/wg-easy/wg-easy/pkgs/container/wg-easy/versions
# or use the value "latest"
WG_VERSION="12"


# Specify the IP/FQDN that client's should try to connect to.
# E.g. your server's public IP address. E.g. "wireguard.mydomain.com" or "168.52.43.28"
WG_HOST=""


# Specify a strong password for logging into the web UI with.
# This should be especially strong, because there is no username, so 
# door knockers will have an easier time trying to brute-force this.
PASSWORD=""

Extra - Full List Of Possible Configuration Options

In case you are interested, the full list of possible environment variables can be found on the GitHub page. If you make use of any additional environment variables, be sure to add them to the docker-compose.yml file as appropriate. E.g. just adding them to the .env file won't work on its own.

A common setting that you may wish to set is the WG_DEFAULT_DNS which you may wish to change to an internal DNS server that you deployed, or 8.8.8.8, 8.8.4.4 if you would prefer to use Google DNS instead of the default, which is Cloudflare's on 1.1.1.1.

You may also wish to add UI_TRAFFIC_STATS with a value of true, in order to turn on traffic stats which default to being off.

Deploy

Run the following command to start the Wireguard server now that we have created the docker-compose.yml and .env files.

docker-compose up -d

Optional - Nginx Configuration

I am using a manually deployed Nginx reverse proxy for which I added the following example configuration to have it serve up the site. Tweak it as you need, making sure to update the FQDN and your wireguard servers internal IP address.

server {
    listen 80;
    server_name wireguard.mydomain.com;
    return 302 https://wireguard.mydomain.com$request_uri;
}

server {
    listen 443 ssl;

    server_name wireguard.programster.org;

    access_log  /var/log/nginx/access.log;

    ssl_certificate      ssl/wireguard.mydomain.com/site.crt;
    ssl_certificate_key  ssl/wireguard.mydomain.com/private.pem;

    ssl_protocols        TLSv1.3;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    keepalive_timeout    60;
    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;

    location / {
        proxy_pass      http://w.x.y.z:51821/;
        include         /etc/nginx/proxy.conf;
    }
}

Alternatively, you may be interested in using something simpler like Nginx Proxy Manager.

Optional - Change The UDP Port

Unfortuantely, I have a TURN server deployed on that IP whose UDP port range is 49152 - 65535, for which the default wireguard UDP Port of 51820 is right in the middle of. Thus I opted to change the Wireguard server to operate on port 40000 instead. This was as easy as adding WG_PORT=40000 to the enviornment variables, and remapping the udp port to be listening on the host on port 40000. Wireguard in the container is always using port 51820, but the WG_PORT tells the website to generate the client configurations such that it tells the client to connect on that port, and we did the port remapping at the Docker level. Below is the updated docker-compose.yml file:

services:

  wg-easy:
    image: ghcr.io/wg-easy/wg-easy:${WG_VERSION}
    container_name: wg-easy
    ports:
      - "40000:51820/udp"
      - "51821:51821/tcp"
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    sysctls:
      - net.ipv4.ip_forward=1
      - net.ipv4.conf.all.src_valid_mark=1
    volumes:
      - ./volumes/wireguard:/etc/wireguard
    environment:
      - WG_HOST
      - PASSWORD
      - WG_PORT=40000

If you have already downloaded client configuration files, you will need to either re-download them to get the updated version, or manually update the existing ones to have :4000 on the end of the Endpoint value (assuming you also used port 4000).

Creating A Client

Now that we have deployed our Wireguard server, we need to create a client connection, and download the configuration file in order to use it. Each client will have a fixed IP on this network, meaning that client's can communicate with each other as well.

If you are not using a proxy, then one should be able to connect to the server in one's browser with HTTP on port 51821. If you are using a proxy, then you have probably configured it so that you can connect on HTTPS without specifying a port number.

http://192.168.0.18:51821/


There is no username, and the password is whatever you set in the environment file earlier.


Click on the New button in the top-right corner in order to create a new client connection configuration.


Give it a name to remember it by.

Download it's configuration by clicking on this icon.

The client configuration file will be named after the name you gave the client connection (1). If you didn't change the DNS through the environment variables, you may wish to just manaully change it here (2). A value of 1.1.1.1 is for Cloudflare's DNS. The server's FQDN or IP address should show up as the Endpoint (3). Here I have port 4000 because I manually changed the server configuration to not clash with my TURN server's UDP port range.

Using Wireguard

Install Required Packages

In order to use Wireguard on Ubuntu, you need to install wireguard and the openresolv packages.

sudo apt update \
  && sudo apt install -y wireguard openresolv

You need to install the openresolv otherwise later you will get:

/usr/bin/wg-quick: line 32: resolvconf: command not found

Move Configuration File Into Place

Move the downloaded configuration file to /etc/wireguard/$NAME_FOR_CONNECTION.conf and make sure that the permissions are set to 700 (that file contains your key/secret).

Connect

wg-quick up $NAME_FOR_CONNECTION

If you're used to using OpenVPN, you may find it unusual that the output just stops. Nothing went wrong and you are still connected.

Disconnect

To disconnect, run:

wg-quick down $NAME_FOR_CONNECTION

Using Wireguard On Windows / Mac / Android / iOS

You can download a client from the Wireguard website.

References

Last updated: 18th May 2024
First published: 30th April 2024