Self-Hosting Tutorial 1: A Step-by-Step Guide to Running Your First Self-Hosted

Started by 2704growing, Sep 30, 2024, 05:54 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


cacajem

Self-hosting involves running a software application on your own hardware or a rented server, giving you full control over your data and service. This tutorial focuses on using a Virtual Private Server (VPS) and Docker for an easy, modern approach to running your first self-hosted application.

For this guide, we'll use the example of hosting Memos, a lightweight, open-source note-taking and knowledge-sharing app.

Part 1: Setting Up Your Server Environment
Step 1: Get a Virtual Private Server (VPS)
A VPS is a rented server in a data center. It's the most common starting point for self-hosting due to its reliability and low cost.

Choose a Provider: Select a reliable provider (e.g., DigitalOcean, Linode, Vultr, or Hetzner).

Create an Instance: Choose a low-cost plan (e.g., 1GB RAM / 1 CPU).

Select OS: For simplicity and compatibility, choose Ubuntu Server (LTS)—a stable and well-supported Linux distribution.

Connect via SSH: After your VPS is created, the provider will give you an IP address and initial credentials. Use an SSH client (like Terminal on macOS/Linux or PuTTY/PowerShell on Windows) to connect:

Bash

ssh root@<Your_VPS_IP_Address>
Step 2: Secure and Update the Server
Update Packages: Run these commands to ensure your operating system is up-to-date:

Bash

sudo apt update
sudo apt upgrade -y
Create a Non-Root User (Security Best Practice): Operating as the root user is risky. Create a new, standard user and give it administrative privileges (sudo):

Bash

adduser <your_new_username>
usermod -aG sudo <your_new_username>
Configure a Firewall: The Uncomplicated Firewall (UFW) is a simple way to secure your server. Allow SSH access so you don't lock yourself out.

Bash

sudo ufw allow OpenSSH
sudo ufw enable
(You will also open ports 80 and 443 later for web access.)

Part 2: Installing Docker and Docker Compose
Docker is the industry standard for self-hosting. It packages an application and all its dependencies into a container, isolating it from the rest of your system and making installation simple. Docker Compose is a tool for managing multi-container Docker applications.

Step 3: Install Docker
Install Necessary Packages:

Bash

sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Add Docker's GPG Key and Repository:

Bash

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker Engine:

Bash

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y
Add Your User to the Docker Group: This lets you run Docker commands without sudo:

Bash

sudo usermod -aG docker $USER
# You must log out and log back in for this change to take effect!
exit
Log back in with ssh <your_new_username>@<Your_VPS_IP_Address>

Step 4: Install Docker Compose
Modern Ubuntu versions use the docker-compose-plugin, which is installed via apt:

Bash

sudo apt install docker-compose-plugin
docker compose version
If a version number is displayed, the installation was successful.

Part 3: Running Your First Application (Memos)
Step 5: Configure and Run the Memos Container
Memos is an ideal first app because it runs as a single container and is easy to configure.

Create a Project Directory: This will hold the configuration file (docker-compose.yml) and persistent data.

Bash

mkdir memos
cd memos
Create the Docker Compose File: Use a command-line editor like nano to create the file.

Bash

nano docker-compose.yml
Paste the Configuration: This simple file tells Docker which app image to download, where to store the data, and which port to use.

YAML

version: "3.0"
services:
  memos:
    image: ghcr.io/usememos/memos:latest
    container_name: memos
    ports:
      - "5230:5230"
    volumes:
      - /home/$USER/memos/data:/var/opt/memos
    restart: always
Press Ctrl+X, then Y, then Enter to save and exit in nano.

Open the Port in the Firewall: The configuration exposes Memos on port 5230. You must open this port in the server's firewall.

Bash

sudo ufw allow 5230/tcp
Start the Application: Use Docker Compose to download the image and start the container in the background (-d).

Bash

docker compose up -d
Part 4: Access and Next Steps
Step 6: Access Your Self-Hosted App
Verify Running Container: Check the status of your new application:

Bash

docker ps
You should see a container named memos with the status Up.

Open in Browser: Open your web browser and navigate to:

http://<Your_VPS_IP_Address>:5230
You should see the Memos welcome screen! You are now running your first self-hosted application.

Next Steps (Recommended)
To move beyond just the IP address and make your application secure and professionally accessible, you would typically:

Get a Domain Name: Purchase a domain (e.g., mynotes.com).

Set Up a Reverse Proxy: Use a tool like Nginx Proxy Manager (itself a Docker container) to manage traffic. This allows you to host multiple apps on the same server, all accessible through standard port 443 (HTTPS).

Add SSL/TLS: The reverse proxy automatically obtains a Let's Encrypt certificate, securing your connection with HTTPS (locking the padlock 🔒).

Set Up Backups: Implement a regular backup strategy to save your /home/$USER/memos/data directory to a separate location, preventing data loss.

Didn't find what you were looking for? Search Below