DigitalOcean Droplet

by Gisle Hannemyr

This chapter describes how to set up a cloud server instance (droplet) as a vhost on the DigitalOcean, and how to use specific tools (such as the console) provided by DigitalOcean.

Table of contents

Introduction

This document is part of the internal documentation for Hannemyr Nye Medier AS. Its main audience is staff helping clients to set up managed hosting for Drupal, Grav and WordPress on IaaS provided by our preferred hosting company: DigitalOcean. It is not read-protetected by choice. We are a transparent company and also want our clients and others to have public access to this information.

Our company provide managed hosting on the following WCMS platforms:

DigitalOcean is a cloud service provider that rents access to virtual machines, called droplets suitable for use as a virtual private server (VPS) to run as a web host.

This chapter describes to select, set up, and interact with one such virtual machine.

Selecting the right size droplet

For reference, check out the system requirements for Drupal 7, Drupal 9, Grav and Wordpress.

Here is some resources data about the the current servers and vhosts we offer:

do14

This configuration is used for legacy websites that require PHP 5. Note that EOL for this version of Ubuntu was 2019. It uses Ubuntu Advantage for extended security maintenance. This product is no longer available to new clients.

do20s

This configuration is the smallest one we recommended for Grav, Wordpress and Drupal, provided you do not install composer.

You do not need composer on a production website. Even if you use composer for dependency management on your development website, you can set up a workflow to sync the codebase, configuration and database from development to production without having composer installed on the production website.

do20

This configuration is the smallest one we recommended for Drupal 9. It uses a swap file to satisfy the memory requirements of composer. This is OK for a development site. However, since this configuration uses SSD, a swap file may case hardware degradation over time if the site receives heavy traffic, so you should not this configuration on a high traffic production website.

$ lsb_release -a 
No LSB modules are available
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal
$ grep MemTotal /proc/meminfo 
MemTotal:       2 035 552 kB
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            978M     0  978M   0% /dev
tmpfs           199M  932K  198M   1% /run
/dev/vda1        49G  7.7G   41G  16% /
tmpfs           994M     0  994M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/vda15      105M  3.9M  101M   4% /boot/efi
/dev/loop0       55M   55M     0 100% /snap/core18/1754
/dev/loop1       55M   55M     0 100% /snap/core18/1705
/dev/loop2       69M   69M     0 100% /snap/lxd/14804
/dev/loop3       70M   70M     0 100% /snap/lxd/14890
/dev/loop4       28M   28M     0 100% /snap/snapd/7264
tmpfs           199M     0  199M   0% /run/user/1000
$ free -m
              total        used        free      shared  buff/cache   available
Mem:           1987         684         327          34         975        1089
Swap:          4095          95        4000
$ nproc 
1
$ uname -i 
x86_64
$ mysql --version
mysql  Ver 8.0.20-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
$ php --version
PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

The memory requirements of composer is on the heavy side so you should make sure that there is at least 4 GB of RAM available. If you're using a traditional hard disk you may resolve this by creating swap file to accomodate this. If your VM uses SSD, a swap file may case hardware degradation over time, and you may need to go with a plan with 4 GB of physical RAM.

do20l

This configuration the one we recommend for Drupal 9 in order to run composer on a production website.

However, we recommend that you do not run composer on a production website. Instead, set up configuration do20s for production, and have a separate development configuration, and sync the codebase, configuration and database from development to production.

Minimum plans

The minimum plan from DO that is adequate for a vanilla Grav and Drupal site is do20s ($/5 month) provided composer is not used. Drupal sites with big databases may find 49 GB disk a little tight tho'. For em>Drupal 9 this requires that development is carried out on a separate website.

The minimum plan from that is adequate for a Drupal 9 development site managed with composer is do20 ($10/month). The 2GB RAM is a bit tight for composer and requires swap to be installed.

We always recommend that clients start with the minimum plan unless they know that they need more resources. It is usually trivial to scale a droplet up to a larger size when your business or website grows.

Creating a DO droplet

It is very simple to create new droplets of various sizes from the DO dashboard. Use Create » Droplets (top right). You pick the operating system and version, plan, resources, datacenter region, etc. New droplets usually takes less than 10 seconds to deploy.

noteFor initial authentication, use the “One time password” method. If you use “SSH-keys”, they will be set up for the user root and if you haven't a key-pair for this user, you will be locked out and have to login as root using the web-based DO console to edit /etc/ssh/sshd_config to re-enable password login. You probably need to reset the root password first. See the section “Troubleshooting ssh” for details.

Provided initial authentication is set up to use the “One time password” method, you will receive the root password by email. You can then use ssh to login and change the root password.

To learn how to log on top the new droplet using a terminal emulator, read this section: The terminal emulator.

After creating a droplet as described above, and before setting up DNS, you can access it using its IP-address and the root password that is mailed to you when you create the droplet. The mail will look like this:

From: DigitalOcean <support@support.digitalocean.com>
Subject: Your New Droplet: …

Your new Droplet is all set to go! You can access it using the following credentials:

Droplet Name: …
IP Address: …
Username: root
Password: …

To login, use the following command:

$ ssh root@ip_address

You will immedately be propted for a password change after logging in for the first time.

When you log in, you may see the following message:

*** System restart required ***

This message indicates the presence of the file /var/run/reboot-required. Ubuntu packages can trigger the creation of this file in their post-installation script postinst.

A restart is usually required when an automatic update to the Linux kernel has been installed. These are often security patches that only come into effect after reboot.

To reboot, do the following:

$ sudo shutdown -r now

Initially, only the root user exists. That user should only be used for system administration via the sudo command. For everyday use, you need to create a non-root user:

# adduser bob 

Add the user to the sudo group:

# usermod -aG sudo bob

To use SSH key pairs for passwordless login (strongly recommended) you need to add SSH-keys to the new droplets. How to do this is descripted in a seperate section about key-based authentication.

To not having to use the IP-address for login, you may set up DNS to have a domain pointing the server, and set up the config file in the .ssh with an alias. Example:

Host example
     Hostname example.com
     User bob

If username is omitted, current username is used unless an alias is used. Remote login examples:

$ ssh tolfa.no 
$ ssh bob@example.com 
$ ssh example 

At this point, you may want to set up the server's fully qualified domain name (FQDN) in the file /etc/hostname. This is described in another note in the section: Set hostname and mailname.

After creating a new droplet, we always install some packages considerd essential for our workflow:

$ sudo apt install emacs-gtk
$ sudo apt install xterm
$ sudo apt install zip
$ sudo apt install make

Finally, check locale:

$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

If the locale for LC_ALL is not set, edit the file /etc/environment and put the following at the end:

…
LC_ALL="en_US.UTF-8"

You will need to log off and on again for the setting to take effect.

tipTo find out where your droplet is located in, look under the droplet name in the DO dashboard. You should see a line like “2 GB / 50 GB Disk / FRA1 - Ubuntu 18.04.3 (LTS) x64”. The four letter code identifies the data center and city (i.e. here “FRA1” means Frankfurt, Germany). The same code in lowercase is also part of the droplet identifier (e.g. ubuntu-s-1vcpu-2gb-fra1-01).

Source DO: Which country.

Add disk space

To add disk space to the root partition on a DigitalOcean droplet, you must must upgrade to a more expensive droplet plan that uses the same CPU architecture. Even if you only want more disk space, you also has to take whatever increase in vCPUs, RAM and bandwidth that is bundled with the plan.

For instance, you can resize from the first to the second of the standard plans listed below to double the disk space:

  1. Basic, shared CPU, 2 vCPUs, 4 GB RAM, 80 GB SSD, 4TB, $20/mo
  2. Basic, shared CPU, 4 vCPUs, 8 GB RAM, 160 GB SSD, 5TB, $40/mo

To resize, go to the droplet you want to resize, in the pulldown menu on the right ("More"), select Press "resize droplet". To add disk, make sure the radio button above the plans are set to "Disk CPU and RAM". Select a plan.

Then power down the droplet, using the command line:

$ sudo shutdown -h now  

If the "resize" button is grayed out, turn off droplet. The link to do so is in this text:

This Droplet is on a Basic plan. You must turn off your Droplet to resize it.

Then, hit "Resize". Wait for spinner to finish.

Resizing can take up to 1 minute per GB of data used by the droplet, but in practice, it takes less (resizing from 80 GB to 160 GB has taken me approx. 4-15 minutes).

When done, return to the dashboard for the droplet, and toggle it on again:

do_toggleon.png
Toggle the droplet on again.

Source: How to Resize Droplets.

It is also possibleto add block storage to droplets. This is cheaper, but can only added as separate partitions that can contain things such as logs, backups and home pages. They cannot be used to increase the partition size of the root partition.

Sources DO: Block Storage Volumes, DO: Cannot merge added storage block.

Set up swap

The droplet I refer to as “do20” only have 1987 MB of memory. This is unfortunately too little for composer. Composer will terminate with the message “killed” when it runs out of memory. To fix this, create 4 MB swap space.

The steps below show how to allocate swap space, protect the swap file, mark the file as swap space, and enable it:

$ sudo fallocate -l 4G /swapfile
$ sudo chmod 600 /swapfile
$ ls -lh /swapfile
-rw------- 1 root root 4.0G Oct  5 09:13 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=7555be35-1411-4188-b320-6a918e88a526
$ sudo swapon /swapfile
$ sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   4G   0B   -2

To automatically enable swap file after each reboot, first back up /etc/fstab in case anything goes wrong:

$ sudo cp /etc/fstab /etc/fstab.bak

Then add this line to /etc/fstab:

/swapfile none swap sw 0 0  

Some useful command related to managing memory and swap devices:

$ free -m      # Display the amout of free and used memory.
$ swapon -show # Display swap usage summary by device.
$ swapon -a    # Enable swapping for all devices.
$ swapoff -a   # Disable swapping for all devices.

Source: DO.com.

Troubleshooting

Typing “/” in the DO console

The DO console assumes you have an US-layout keyboard. To type “/”, SHIFT+7 will not work. Instead, use the one on the numeric keypad.

Final word

DigitalOcean provides some excellent tutorials to their service:

  1. DO: Initial server setup.
  2. DO: apache.
  3. DO: LAMP.
  4. DO: vhost (18.04).
  5. DO: MySQL.
  6. DO: Postfix (18.04).
  7. DO: phpmyadmin (18.04).
  8. DO: new user (18.04).

You may want to study these for background information.


Last update: 2020-07-27 [gh].