Skip to main content

Deploy FastAPI to Digitalocean Droplet

· 4 min read
Femi Adigun
Senior Software Engineer & Coach

Digitalocean is one of the leading cloud infrastructure service providers.

Provision a droplet

Visit digitalocean and create and account or login with your existing account. Select Ubuntu (my preferred, you can use any Linux distro) The $4/month plan is okay for a test/simple project Select a datacenter closer to your targeted audience not necessarily closest to you Choose SSH Key as authentication method Add your ssh key to the droplet so you can easily and securely access the droplet from your local computer

Generate SSH Key

To generate an SSH key, you can use the ssh-keygen command in your mac terminal. Here's how:

  1. Open your terminal.
  2. Type the following command and press Enter:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    Replace "your_email@example.com" with your own email address.
  3. You will be prompted to choose a location to save the key. Press Enter to accept the default location (/Users/your_username/.ssh/id_rsa) or specify a different location.
  4. Next, you will be prompted to enter a passphrase. It's optional, but recommended for added security. Press Enter if you don't want to set a passphrase.
  5. The SSH key pair will be generated and saved in the specified location.
  6. You can now use the public key (id_rsa.pub) to add it to your DigitalOcean droplet.

Remember to keep your private key (id_rsa) secure and never share it with anyone.

Create a User Account

To create a user account on Linux, you can use the adduser command. Here's how:

  1. SSH into your droplet using the command:

    ssh username@your_droplet_ip

    Replace username with the desired username and your_droplet_ip with the IP address of your droplet.

  2. Once logged in, run the following command to create a new user:

    sudo adduser new_username

    Replace new_username with the desired username for the new account.

  3. You will be prompted to set a password for the new user. Follow the instructions to set a secure password.

  4. Provide additional information for the user, such as their full name and contact information, or press Enter to leave them blank.

  5. Confirm the information and press Enter.

Grant Privileges to the User

After creating the user account, you may want to grant administrative privileges to the user. Here's how:

  1. Run the following command to add the user to the sudo group:

    usermod -aG sudo new_username

    Replace new_username with the username you created earlier.

    Note: You may need to create a .ssh directory for the new user and copy the ssh you added to the user's .ssh directory.

    ADD SSH To User

    • login as root or current user
    • change user :

    su - new_username

    • Create .ssh directory

    mkdir ~/.ssh

    • Change directory permission

    chmod 700 ~/.ssh

    • Copy the SSH key (.pub file) and paste the content

    nano ~/.ssh/authorized_keys`

    • Exit and now login as the new user.

    read more here on how to add new user to your droplet

  2. The user now has administrative privileges and can execute commands with sudo.

Remember to replace new_username with the actual username you created. It's important to choose a strong password and keep it secure.

To SSH into your droplet, use the following command:

ssh username@your_droplet_ip

Update the droplet

Once connected to your droplet, use the folowing command to update the machine

sudo apt update && apt upgrade -y

NGINX

we wil be using NGINX a popular open-source web server software that serves as a reverse proxy server, load balancer, media streamer and HTTP Cache. use this code to install nginx

sudo apt install nginx -y

use the following command to start and enable nginx

systemctl start nginx
systemctl enable nginx

Docker

We will be using docker to containerize our application. Use this command to install docker

sudo apt-get install docker-ce docker-ce-cli containerd.io

setup docker compose

curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Your FastAPI Aplication

create a new directory for your project

mkdir -p /var/www/myapi