Table of contents
Subscribe to our newsletter.
No spam
100% Great content
Special deals
Subscribe now

OpenVPN bash script installer on Ubuntu (OpenVZ environment)

Published on 25 April 2022 Last updated on 12 August 2022

This OpenVPN installer is being developed and tested for Ubuntu 20.04 LTS and in a OpenVZ container. This script is used in our Dedicated VPN service.



IP=$(hostname -I | awk '{print $2}')
PasswordGenerator=$(</dev/urandom tr -dc '[:alnum:]' | head -c15; echo "")
export DEBIAN_FRONTEND=noninteractive

#Set proper mirrors
mv /etc/apt/sources.list /etc/apt/sources.list_backup
tee /etc/apt/sources.list <<EOF
deb focal main restricted universe
deb focal-updates main restricted universe
deb focal-security main restricted universe multiverse
deb focal partner

#Install Software
apt-get -yq --allow-releaseinfo-change update
printf '\n' | apt-get install -y apt-transport-https ca-certificates curl software-properties-common apache2-utils

#Install and configure nginx + SSL + Proxy pass
apt-get install -y nginx
mkdir /etc/nginx/ssl
openssl genrsa -out /etc/nginx/ssl/priv.key 2048
printf '\n\n\n\n\n\n\n\n' | openssl req -key /etc/nginx/ssl/priv.key -new -x509 -days 3650 -out /etc/nginx/ssl/ssl.crt
htpasswd -c -B -b /etc/nginx/.htpasswd dedicatedvpn $PasswordGenerator

tee -a /etc/nginx/sites-available/ui.conf <<EOF
server {

    listen              7654 ssl;
    listen              [::]:7654 ssl;
    error_page 497 https://$IP:7654;
    server_name         $IP;
    root                /var/www/html/;
    error_log   /dev/null   crit;
    access_log  /dev/null;

    # SSL
    ssl_certificate     $SslCertPath;
    ssl_certificate_key $SslKeyPath;

    # reverse proxy
    location / {
        auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;


ln -s /etc/nginx/sites-available/ui.conf /etc/nginx/sites-enabled/ui.conf
rm -rf /etc/nginx/sites-enabled/default /etc/nginx/sites-available/default
systemctl restart nginx

#Install docker and docker compose
curl -fsSL | apt-key add -
add-apt-repository "deb [arch=amd64]  $(lsb_release -cs)  stable"
apt-get -yq --allow-releaseinfo-change update
printf '\n' | apt-get -yq install docker-ce
curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

#Enable docker
systemctl start docker
systemctl enable docker

#Setup OVPN and the UI
mkdir /home/ovpn-admin 
cd /home/ovpn-admin
tar xvf ovpn-*
sed -i "s/$IP/g" docker-compose.yaml

docker update --restart unless-stopped $(docker ps -q)

#Configure SSH.
sed -i "s/#Port 22/Port 22000/g" /etc/ssh/sshd_config
systemctl restart sshd

#Create first user
cat <<EOF

until curl -s -f -o /dev/null -X POST -d "$(data)" ""
  sleep 5

apt-get purge exim* apache2* pwgen tcpdump telnet -y

#Upgrade the server system
printf '\n' | apt-get -yq upgrade
apt-get clean

Choose your service now with instant activation.

Get started with no risk - we offer 30-day moneyback guarantee.

Get started