A Virtual Host is an instance of an operating system operating somewhere remotely over which you have sole control.
A computer running at a web hosting data center is generally made up of multiple CPUs and running a hypervisor, which is a software program that can support multiple virtual instances of an operating system on the same machine. To you, the customer, each one looks like its own machine. To the web hosting machine, each instance looks like a process running under the hypervisor.
For many network development tasks, it’s helpful to have a POSIX-based host on a public IP address. If you’re building a server application or a database application, or just learning about network administration, it can help. First, because Linux, Unix, and other POSIX operating systems were designed with the internet in mind; and second, because having total control over your own system means you won’t accidentally mess up someone else’s work.
Many services offer virtual hosting: Digital Ocean, Amazon Web Services, Heroku, Dreamhost, and many more. Different services have different benefits. Most will configure the system for you in a custom way, with the databases, programming tools, or services you plan to use. Some will also offer domain name registration, mirroring, backups, load balancing, and much more. This page explains how to set up a simple virtual host on Digital Ocean, with few of the extras mentioned above. This will give you an idea what’s common to different hosting systems, and give you a template from which you can build more complicated hosts. Digital Ocean is reasonably priced, with a good student bundle, and offers good services and excellent help tutorials. This tutorial is based on Jingwen Zhu’s Digital Ocean Workshop.
Set Up Accounts
Set up a a GitHub account if you don’t already have one, and then register for the GitHub Student Developer Pack. Then set up a Digital Ocean account, and use the student credits from the GitHub student pack.
Create a Droplet
Digital Ocean refers to each individual virtual host as a droplet (a droplet in the digital ocean, get it?). You can have multiple projects in your account on Digital Ocean, and multiple droplets per account.
Pick an Operating System and Size
To set up your first droplet, sign into your Digital Ocean account and click on Droplets. Then click the Create option menu, and choose Droplets. This will take you to a new page, where you configure a new droplet. Under “Choose an Image”, choose Ubuntu, then choose the latest version, whatever it may be; as of this writing, it’s 22.04 (LTS) x64. Under “Choose a plan” choose Basic.
Next, choose the size and power of your droplet. The more you pay, the more you get. For most individual development projects, the smallest, $5/mo. will do the job. With the student developer credits from GitHub, you can run that for about ten months for free. You can skip the next section, since you don’t need to add block storage.
Pick a Data Center
Under Pick a datacenter region, pick one that’s closest to you geographically. If you were hosting a commercial application, you might pick the one closest to most of your customers. You can skip the next few options until you get to Authentication.
Pick a Password and Hostname
It’s more secure to authenticate using an SSH key, but for your first time out, choose Password Authentication, as you’re already familiar with it. Type in the password you want to use for the host. Pick a secure password. Give your host a unique name, too. Then you can skip to the end and click Create Droplet.
Configure Your Virtual Host
Your droplet’s been created, now it’s time to log into it and configure it for use.
Set up a User
DigitalOcean will email your your credentials for the new droplet, including IP address, username and password. The first user on any POSIX host is called root
, and that’s what they mailed you. However, you should avoid logging into any remote host as root, so here’s how to set up a custom user.
Using a terminal application like the MacOS Terminal application, or Windows PuTTY application, log into your droplet using the username root and the password you were sent. In Terminal, type:
$ ssh root@YOUR_IP_ADDRESS
When prompted for a password, give the password you were sent. When you’ve successfully connected, you should see something like this:
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established. ECDSA key fingerprint is SHA256:M3G1jZ/5Byx03SeXiDSwefwdn83yUL/qzaxO8DsdfsUiAct1Y. Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type
yes
This should successfully log you in, and you’ll get a screenful of text from the host which ends like this:
root@your_hostname:~#
This is the command prompt of your virtual host. Congratulations, you got it set up! Next, add a user that’s not root. Type the following at the prompt, replacing YOUR_USERNAME
with a username you like:
sudo adduser YOUR_USERNAME
The sudo
command lets you perform tasks as a superuser. Technically you don’t need it here, since root is the original superuser, but it’s good to be aware of the fact that sudo lets you override restrictions.
The system will respond like so, ending with a prompt for the new user:
Adding user `YOUR_USERNAME' ... Adding new group `YOUR_USERNAME' (1000) ... Adding new user `YOUR_USERNAME' (1000) with group `YOUR_USERNAME' ... Creating home directory `/home/YOUR_USERNAME' ... Copying files from `/etc/skel' ... New password:
Fill in a password, retype it, and then you’ll get asked to fill in your name, room number and so forth. You can leave these blank. once you’re done with that, you can add the new user to the superusers group by typing:
sudo adduser YOUR_USERNAME sudo
Finally, you should lock the root user password, then logout as root and log back in as the new user:
sudo passwd -l root logout
Once you’ve logged out, you’ll be back on the command prompt of your local machine. Log back in, this time as the new user:
ssh YOUR_USERNAME@YOUR_IP_ADDRESS
Once you’re logged in, there are a few more housekeeping tasks to take care of.
Upgrade the Operating System
Any time you set up a new host, or use one that you haven’t used in awhile, you should check for new operating system updates. Linux has a tool called apt
(Advanced Package Tool) that keeps track of the progams installed on the system and whether or not there are updates available for them. You’ll use it a lot to update the system and install new software. Start by getting the latest list of software versions using apt:
sudo apt update
Since this is the first time you’re using sudo since logging in as the new user, you’ll get asked for your password. Enter it, and the system will get a list of updates it can perform. Then type
sudo apt upgrade
This will make the system upgrade any outdated pieces of software. It may take a few minutes. Once it’s done, you’ve got an updated system. It’s time to install some software.
Install Software
There are a few pieces of software you should install right away. The first is a firewall. A firewall blocks all attempts to connect to your host or to send messages out from it, except in ways that you decide. To install ufw, the Uncomplicated Firewall application, type:
sudo apt install ufw
Once it’s installed, you can configure it according to these directions. Once you’ve done that, you might want to install a couple other tools, like the network tools. To do this, type:
sudo apt install net-tools
This will install useful utilities like ifconfig
, which lets you see the state of your network interfaces. To check that the install worked type
ifconfig
You’ll get a response listing the configuration for each of your network interfaces, listing the internet address, the MAC address, and much more.
Install a Programming Language
If you’re doing development work, you’ll need to install a programming language as well, maybe more than one. Node.js is a popular tool for server-side programming in JavaScript. The distribution package used by apt includes a version of node.js that will probably work fine for most cases. To install it, type:
sudo apt install nodejs
This will download all the packages you need and then prompt you at the end to install them. Follow the instructions until it’s done. Once it’s done, you can check which version of node is installed by typing:
node -v
You’ll get a response like this:
v10.19.0
Now your virtual host is ready to use. Before you go any further you should configure ufw, the firewall that you installed above.
Once you’ve done that, if you want to shut it down, type
sudo poweroff
Or if you just want to logout and leave it running, type
logout
And that will be the end of your session.
Extra Security: Add ssh Keys
Finally, there’s an extra security step you can take if you know you’ll always be logging into your host from the same computer. You can add an ssh public key as a credential for login. If you do this, what happens is that instead of using your password to login, you’ll use a long, encrypted string called an encryption key to log in. Your local computer will have a private version of the key, and your virtual host will have the public version. Only someone who knows the private key can log in, and it’s never transmitted from your local computer, so it’s more secure than a password. You’ll only be able to log into your host from the computer that holds the private key. Digital Ocean has good instructions on creating an ssh key on your computer and copying the public key to your host. They also have instructions on uploading public keys to your Digital Ocean account, so that if you create other droplets, you can automatically add the public key so you never have to log in using a password.