{"id":798,"date":"2020-08-23T16:09:12","date_gmt":"2020-08-23T20:09:12","guid":{"rendered":"https:\/\/itp.nyu.edu\/networks\/?page_id=798"},"modified":"2024-08-06T10:53:18","modified_gmt":"2024-08-06T14:53:18","slug":"setting-up-a-virtual-host","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/networks\/setting-up-a-virtual-host\/","title":{"rendered":"Setting Up a Virtual Host"},"content":{"rendered":"\n<p>A Virtual Host is an instance of an operating system operating somewhere remotely over which you have sole control. <\/p>\n\n\n\n<p>A computer running at a web hosting data center is generally made up of multiple CPUs and running a <strong>hypervisor<\/strong>, 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.<\/p>\n\n\n\n<p>For many network development tasks, it&#8217;s helpful to have a POSIX-based host on a public IP address. If you&#8217;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&#8217;t accidentally mess up someone else&#8217;s work.<\/p>\n\n\n\n<p>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&#8217;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&#8217;s <a href=\"https:\/\/github.com\/ZhuJingwen\/DigitalOceanWorkshop\">Digital Ocean Workshop<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Set_Up_Accounts\"><\/span>Set Up Accounts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Set up a a <a href=\"https:\/\/github.com\/\">GitHub account<\/a> if you don&#8217;t already have one, and then register for the <a href=\"https:\/\/education.github.com\/pack\">GitHub Student Developer Pack<\/a>. Then set up a <a href=\"https:\/\/www.digitalocean.com\/\">Digital Ocean <\/a>account, and use the student credits from the GitHub student pack. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_Droplet\"><\/span>Create a Droplet<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pick_an_Operating_System_and_Size\"><\/span>Pick an Operating System and Size<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>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 &#8220;Choose an Image&#8221;, choose Ubuntu, then choose the latest version, whatever it may be; as of this writing, it&#8217;s 22.04 (LTS) x64. Under &#8220;Choose a plan&#8221; choose Basic.<\/p>\n\n\n\n<p>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&#8217;t need to add block storage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pick_a_Data_Center\"><\/span>Pick a Data Center<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Under Pick a datacenter region, pick one that&#8217;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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pick_a_Password_and_Hostname\"><\/span>Pick a Password and Hostname<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It&#8217;s more secure to authenticate using an SSH key, but for your first time out, choose Password Authentication, as you&#8217;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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configure_Your_Virtual_Host\"><\/span>Configure Your Virtual Host<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Your droplet&#8217;s been created, now it&#8217;s time to log into it and configure it for use. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Set_up_a_User\"><\/span>Set up a User<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>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 <code>root<\/code>, and that&#8217;s what they mailed you. However, you should avoid logging into any remote host as root, so here&#8217;s how to set up a custom user. <\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ssh root@YOUR_IP_ADDRESS<\/pre>\n\n\n\n<p>When prompted for a password, give the password you were sent.  When you&#8217;ve successfully connected, you should see something like this:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.\nECDSA key fingerprint is SHA256:M3G1jZ\/5Byx03SeXiDSwefwdn83yUL\/qzaxO8DsdfsUiAct1Y.\nAre you sure you want to continue connecting (yes\/no\/[fingerprint])?<\/pre>\n\n\n\n<p>Type <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yes<\/pre>\n\n\n\n<p>This should successfully log you in, and you&#8217;ll get a screenful of text from the host which ends like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@your_hostname:~#&nbsp;<\/pre>\n\n\n\n<p>This is the command prompt of your virtual host. Congratulations, you got it set up!  Next, add a user that&#8217;s not root. Type the following at the prompt, replacing <code>YOUR_USERNAME<\/code> with a username you like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo adduser YOUR_USERNAME<\/pre>\n\n\n\n<p>The <code>sudo<\/code> command lets you perform tasks as a superuser. Technically you don&#8217;t need it here, since root is the original superuser, but it&#8217;s good to be aware of the fact that sudo lets you override restrictions.<\/p>\n\n\n\n<p>The system will respond like so, ending with a prompt for the new user:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Adding user `YOUR_USERNAME' ...\nAdding new group `YOUR_USERNAME' (1000) ...\nAdding new user `YOUR_USERNAME' (1000) with group `YOUR_USERNAME' ...\nCreating home directory `\/home\/YOUR_USERNAME' ...\nCopying files from `\/etc\/skel' ...\nNew password:&nbsp;<\/pre>\n\n\n\n<p>Fill in a password, retype it, and then you&#8217;ll get asked to fill in your name, room number and so forth. You can leave these blank. once you&#8217;re done with that, you can add the new user to the superusers group by typing:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo adduser YOUR_USERNAME sudo<\/pre>\n\n\n\n<p>Finally, you should lock the root user password, then logout as root and log back in as the new user:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo passwd -l root\nlogout<\/pre>\n\n\n\n<p>Once you&#8217;ve logged out, you&#8217;ll be back on the command prompt of your local machine. Log back in, this time as the new user:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ssh YOUR_USERNAME@YOUR_IP_ADDRESS<\/pre>\n\n\n\n<p>Once you&#8217;re logged in, there are a few more housekeeping tasks to take care of.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Upgrade_the_Operating_System\"><\/span>Upgrade the Operating System<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Any time you set up a new host, or use one that you haven&#8217;t used in awhile, you should check for new operating system updates. Linux has a tool called <code>apt<\/code> (<a href=\"https:\/\/wiki.debian.org\/Apt\">Advanced Package Tool<\/a>) that keeps track of the progams installed on the system and whether or not there are updates available for them. You&#8217;ll use it a lot to update the system and install new software. Start by getting the latest list of software versions using apt:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt update<\/pre>\n\n\n\n<p>Since this is the first time you&#8217;re using sudo since logging in as the new user, you&#8217;ll get asked for your password. Enter it, and the system will get a list of updates it can perform. Then type<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt upgrade<\/pre>\n\n\n\n<p>This will make the system upgrade any outdated pieces of software. It may take a few minutes. Once it&#8217;s done, you&#8217;ve got an updated system. It&#8217;s time to install some software. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Install_Software\"><\/span>Install Software<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install ufw<\/pre>\n\n\n\n<p>Once it&#8217;s installed, you can configure it according to <a href=\"https:\/\/itp.nyu.edu\/networks\/setting-up-a-firewall-on-an-embedded-linux-device\/\">these directions<\/a>. Once you&#8217;ve done that, you might want to install a couple other tools, like the network tools. To do this, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install net-tools<\/pre>\n\n\n\n<p>This will install useful utilities like <code>ifconfig<\/code>, which lets you see the state of your network interfaces. To check that the install worked type <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ifconfig<\/pre>\n\n\n\n<p>You&#8217;ll get a response listing the configuration for each of your network interfaces, listing the internet address, the MAC address, and much more. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Install_a_Programming_Language\"><\/span>Install a Programming Language<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you&#8217;re doing development work, you&#8217;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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install nodejs<\/pre>\n\n\n\n<p>This will download all the packages you need and then prompt you at the end to install them. Follow the instructions until it&#8217;s done. Once it&#8217;s done, you can check which version of node is installed by typing:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">node -v<\/pre>\n\n\n\n<p>You&#8217;ll get a response like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">v10.19.0<\/pre>\n\n\n\n<p>Now your virtual host is ready to use. Before you go any further you should <a href=\"https:\/\/itp.nyu.edu\/networks\/setting-up-a-firewall-on-an-embedded-linux-device\/\">configure ufw, the firewall that you installed above<\/a>.<\/p>\n\n\n\n<p>Once you&#8217;ve done that, if you want to shut it down, type <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo poweroff<\/pre>\n\n\n\n<p>Or if you just want to logout and leave it running, type<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">logout<\/pre>\n\n\n\n<p>And that will be the end of your session. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Extra_Security_Add_ssh_Keys\"><\/span>Extra Security: Add ssh Keys<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Finally, there&#8217;s an extra security step you can take if you know you&#8217;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&#8217;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&#8217;s never transmitted from your local computer, so it&#8217;s more secure than a password. You&#8217;ll only be able to log into your host from the computer that holds the private key. Digital Ocean has good instructions on <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-configure-ssh-key-based-authentication-on-a-linux-server\">creating an ssh key on your computer<\/a> and copying the public key to your host. They also have instructions on <a href=\"https:\/\/www.digitalocean.com\/docs\/droplets\/how-to\/add-ssh-keys\/to-account\/\">uploading public keys to your Digital Ocean account<\/a>, so that if you create other droplets, you can automatically add the public key so you never have to log in using a password. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/itp.nyu.edu\/networks\/setting-up-a-virtual-host\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting Up a Virtual Host&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-798","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages\/798"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/comments?post=798"}],"version-history":[{"count":16,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages\/798\/revisions"}],"predecessor-version":[{"id":1239,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages\/798\/revisions\/1239"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/media?parent=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}