Skip to content

Instantly share code, notes, and snippets.

@bradley
Last active February 5, 2022 04:05
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bradley/0c06d3f2d3c63b097ea5e27befd4beb3 to your computer and use it in GitHub Desktop.
Save bradley/0c06d3f2d3c63b097ea5e27befd4beb3 to your computer and use it in GitHub Desktop.
Digital Ocean SSH and Subdomains

Basic Getting Started Ubuntu 16.04

SSH into your server at root:

ssh root@myserver.com

Crate a new user:

adduser my_username

Make the new user a sudo user:

usermod -aG sudo my_username

Ensure your server allows password auth (or dont, but it's helpful for now) by opening sshd config and ensuring password authentication is allowed:

sudo vim /etc/ssh/sshd_config
... 
# Find line for PasswordAuthentication and set to `yes`
PasswordAuthentication yes
...
# Save

Reload sshd daemon:

sudo systemctl reload sshd

Now su into your new user and make the file needed for adding ssh key login to your new user:

su my_username
...
cd ~ && mkdir .ssh && touch .ssh/authorized_keys

Now exit and add your public key to each user:

exit
...
# Copy public key to authorized_keys for each server user
cat ~/.ssh/id_rsa.pub | ssh root@myserver.com 'cat >> .ssh/authorized_keys'
cat ~/.ssh/id_rsa.pub | ssh my_username@myserver.com 'cat >> .ssh/authorized_keys'

Subdomains

If you need to add subdomains for things like api.myserver.com or dev.myserver.com, use the following guide.

  1. Create a new droplet for your subdomain. Your subdomain will be handled by its own droplet with its own IP address. When you give it its hostname, name it whatever you want your subdomain's domain to be (so it's easily recognizable) - for this example, name it api.myserver.com.
  2. Go to Digital Ocean and go to the Networking tab (at the time of this writing that's at https://cloud.digitalocean.com/networking).
  3. Select your domain, myserver.com.
  4. Click to add a A record.
  5. Under "Hostname", enter api.
  6. Under "WILL REDIRECT TO", select your api droplet, api.myserver.com.
  7. Optionally, set the TTL to something else - idk why but I've been using 1800.
  8. Create the A record by clicking "Create Record".
  9. Click to add a CNAME record.
  10. Under "Hostname", enter *.api.
  11. Under "IS AN ALIAS OF", enter api.myserver.com. (note: The period at the end is not a typo, don't forget it).
  12. Optionally, set the TTL to something else. idk why but Digital Ocean defaults CNAME records to a longer TTL, I'm guessing because it has something to do with the "alias" aspect. I really don't know right now so I haven't been messing with it.
  13. Create the CNAME record by clicking "Create Record".

Should be good to go. If you want to test that it worked, you can ping you api and compare the IP address you get back with the one you get from pinging your server without the subdomain. Name servers may not have propogated but I havent ran into this issue yet. See here: https://www.digitalocean.com/community/tutorials/how-to-set-up-and-test-dns-subdomains-with-digitalocean-s-dns-panel.

@dgtlmonk
Copy link

works like a charm!

@bluemix
Copy link

bluemix commented Nov 11, 2019

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment