teaching machines

CS 268: Homework 0 – Blog Server

February 2, 2020 by . Filed under specifications, spring-2020, webdev.

In this first ungraded homework, you will set up a web server that really lives on the web. You will begin serving out a directory for a blog that you will populate throughout the semester, documenting your foray into the world of web development.

Digital Ocean

You will set up your server with Digital Ocean, a company that hosts virtual machines—which they call droplets. Normally, droplets cost money. Thanks to the GitHub Student Developer Pack, you can get a $50 credit for Digital Ocean. This credit supports ten months of a $5/month droplet, long enough to get you through the semester without spending a penny.

Unfortunately, you need to enter a credit card on Digital Ocean, even though you won’t be spending any money for this course.

Follow these steps to get an effectively-free droplet:

  1. Sign up for the GitHub Student Developer Pack. You will need to verify your studenthood. Ensure that you are on a campus network, as they check your IP. It’s not clear how long it takes to be approved.
  2. Once approved, revisit GitHub’s page. Scroll down, copy the Digital Ocean promo code, and click on the link to redeem your credit.
  3. Once your are logged in to Digital Ocean and have confirmed your credit, click the Create button and create a droplet. Configure it in the following way:
    • Distribution: Ubuntu 18.04
    • Size/plan: $5/month
    • Data center: in the US
    • Authentication: either is fine; choose one-time password if you haven’t used SSH keys before
    • Hostname: something reasonable (this is not a domain name; only you will see it in your Digital Ocean profile)
    Submit the form.
  4. On the Droplets page, note the IP address of your new droplet.
  5. Using the IP address and the password that was emailed to you, log in to your droplet with your favorite SSH client. You will log in as the root user, having full privileges on the machine.
  6. Running as root is dangerous. Make a separate account that doesn’t automatically have administrator privileges with these commands:
    adduser YOUR-USERNAME
    usermod -aG sudo YOUR-USERNAME
    Replace YOUR-USERNAME with a username of your choice, preferrably in all lowercase letters. The second command adds you to the sudoers group, which means that you can run a command with elevated privileges by prefixing it with sudo and entering your password.
  7. Log out and log back in with your new, non-root account.

Be sure to decommission your droplet after the semester is over and before your credit runs out to avoid charges.

Apache

Your next step is to get the web server Apache up and running. While logged in to Digital Ocean, run these commands to install the software:

sudo apt update
sudo apt install apache2

Normally Apache serves out files that are stored in the /var/www/html directory. To make your life easier, let’s configure it to serve out a directory named blog that lives in your home directory. Run these commands to create and move to the directory:

mkdir ~/blog
cd ~/blog

In this directory, create a file named index.html and enter some simple HTML using your favorite command-line text editor.

Two of the most popular text editors are vi and emacs. You are encouraged to learn one of them. You may be intimidated or disgusted by the prospect of command-line editing. That’s okay. The benefits of knowing one of these editors outweigh the costs.

Once you are done editing, give the directory and all of its children (recursive with -R) permissions so that the Apache server, which runs as a different user, can read them. You add read (r) and execute (capital X) permissions to the group (g) and others (o) with this command:

chmod -R go+rX ~/blog

Now you need to set up a configuration file that tells Apache about your blog. Create the file with one of the following commands:

sudo vi /etc/apache2/sites-available/blog.conf
sudo emacs /etc/apache2/sites-available/blog.conf

Add the following text to the file, replacing YOUR-IP-ADDRESS with your droplet’s actual IP address and YOUR-USERNAME with your actual username:

Listen 8000

<VirtualHost *:8000>
  ServerAdmin webmaster@localhost
  ServerName YOUR-IP-ADDRESS:8000
  DocumentRoot /home/YOUR-USERNAME/blog

  # Allow access to all files.
  <Directory /home/YOUR-USERNAME/blog>
    Require all granted
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This configuration serves the directory out on port 8000. Normally we serve out HTTP on port 80, but we may want to use that for something else later in the semester.

Now you must enable the site and restart Apache with these commands:

sudo a2ensite blog
sudo service apache2 restart

Visit http://YOUR-IP-ADDRESS:8000 in a browser and marvel at your accomplishment.

Link

Send your instructor a link to your blog with a direct message in Slack.