跳到正文
DEV Community· Samir Hashimov·· 4 小時前AI 評分46

為何離開雲端服務並自建伺服器:自我託管逐步教學

Why I Left Cloud Services and Built My Own Server: A Step-by-Step Self-Hosting Guide

AI 導讀

作者因後端部署受限及 Firebase 免費方案不足,轉向自我託管並從頭建立伺服器,分享選擇硬件及設定 VPS 的逐步教學。文章以 Oracle Cloud 為示範,提及其 ARM 免費方案提供 12GB RAM;自我託管可在自有伺服器運行開源服務,但安全、排查錯誤及維護均由用戶負責。

正文

Server Setup Guide

As developers, our daily work primarily involves programming various applications. As these applications scale and we want to deploy them live for everyone to use, we face a crucial question: "Where and how should we host these applications?"

My self-hosting journey actually started with this question. While it's possible to deploy the front-end of my applications using technologies like Vercel or Netlify, I encountered heavy limitations, especially when deploying the back-end. When searching for alternative solutions to overcome these issues, I came across the concept of self-hosting. Yes, I had heard about self-hosting on tech channels before, but I used to think of it mostly as a service that keeps our data local to increase security.

Recently, the concept of self-hosting has expanded significantly. Thanks to open-source software, free and open-source alternatives now exist for many of the services we use in our daily lives. This allows us to run similar versions of programs and services—which normally impose limits and force us to upgrade to paid plans—right on our own private servers.

Today, I don't just run websites on my server; I also host open-source alternatives to the services I use every day.

In this article, I will share how I abandoned ready-made cloud services, built my own server from scratch, which technologies I used, and what this experience has taught me.


What is Self-Hosting?

When using cloud services, all our data is processed on the cloud provider's servers. Additionally, certain services are restricted and gated behind "Pro" or "Plus" subscription plans. Self-hosting a program or service means taking an open-source project and running it on your own server. Because the software is hosted entirely on our own infrastructure, no personal data leaves our hands. And since there are no artificial plan limits, the software can be used without restrictions. The only limits are defined by the server's own hardware resources (RAM, CPU, etc.).

Self-hosting provides both privacy and control, while also letting you use normally paid services for free. However, alongside these benefits, there are downsides you need to be aware of. For instance, since you are both the user and the system administrator of the server, diagnosing and fixing bugs, ensuring security, and maintenance all fall entirely on your shoulders.


Why Did I Leave Cloud Services?

For the back-end of my projects, I often deployed using Google Firebase. However, Firebase had certain limitations on its free tier. While Firebase's features were initially sufficient, as my projects grew more complex, the free tier started falling short. For one of my projects, I needed Firebase Functions, but because that feature required a higher-tier subscription, I couldn't integrate it.

A while later, while casually browsing the internet, I came across a tool called n8n. For those who don't know, this is a tool designed to automate repetitive tasks in your daily workflow. I wanted to try out this program. There were two ways to use it: joining their Cloud subscription or self-hosting.

Self-hosting was completely free, while Cloud was paid:

n8n Pricing Plans

After seeing this, I started thinking: Is self-hosting more cost-effective than paying for a monthly subscription? Let's do a quick calculation:

Cost Comparison

I estimated the server cost at $10, but later I'll also explain how you can get a server completely for free.

As you can see, even when compared to the most affordable plan, self-hosting is more cost-effective than paying for separate services individually. The price difference might not seem huge for just a single service, but because the same server can host n8n alongside other services, the overall math swings heavily in favor of self-hosting.


Hardware/Server Selection

To self-host, you first need a server to install and store all your applications. If the word "server" brings to mind expensive, inaccessible, bulky systems, think again. A server can be an old computer lying around your house, a Raspberry Pi device, or a VPS (Virtual Private Server).

While you can turn your old home PC or a Raspberry Pi into a server, keep in mind that they won't be resilient against power or internet outages.

The best approach here is to use a VPS. A VPS is offered at affordable prices by many companies. Microsoft Azure, AWS, and Oracle Cloud are among the most popular cloud systems. However, if you are using a VPS for personal projects and want something easier to manage, you can use Hostinger.


It is also possible to set up a server completely for free. For this, you can use Oracle Cloud. Oracle Cloud offers an ARM-based Free Tier server with 12GB of RAM, which is quite sufficient to get started.

Oracle Cloud Free Tier

Setting Up Our Own Server From Scratch

Regardless of which provider you choose to build your server with, the configuration is mostly the same. In this guide, I will perform the steps using Oracle Cloud as the baseline.

  1. Go to Oracle Cloud Free Tier and create a new account. A bank card is required during registration, and a symbolic amount is charged for verification and then refunded. Pay close attention when selecting your Home Region during registration, as available resource capacities vary by region.

  2. In the Networking > Virtual Cloud Networks section, click Create VCN to create a VCN and a Public Subnet.

  3. In the Compute > Instances section, click Create Instance. You will be asked to select configurations for your VPS. In the Image and shape section, select Ubuntu for the Image, and choose a shape that fits your budget (free tiers are available).

  4. In the following steps, you will be provided with SSH keys. Download the Private and Public keys and keep them in a secure place.

  5. Click Create to spin up your first VPS. Congratulations!

Oracle has two firewalls: one managed via the Oracle Dashboard, and another managed from the server's terminal. Let's start by opening our basic ports. In the Oracle Dashboard, navigate to Compute > Instances > Your VPS > Networking > Subnet > Security > Security List > Security Rules. Click on the Add Ingress Rules button and fill out the fields as follows:

Source Type: CIDR
Source CIDR: 0.0.0.0/0
Destination Port Range: 22

Next, add another rule using + Another Ingress Rule setting the Destination Port Range to 80 while keeping the rest the same. Then, repeat the same process for port 443. These ports allow SSH, HTTP, and HTTPS connections.


Now, let's connect to the server. To connect to the server, open PowerShell on your computer. The connection template looks like this:

ssh -i "$env:USERPROFILE\<private_key_path>" ubuntu@<server_ip>

You need two parameters here:

<private_key_path> - Provide the path where you saved the Private key file downloaded when creating the server. Example: Downloads\ssh.key.

<server_ip> - Enter the public IP of the server. You can find this in the Compute > Instances section. If it appears blank, you need to enable Assign a public IPv4 address in the settings.

Replace these placeholders in the template and run the final command in PowerShell. Remember this command, as you will need to run it every time you connect to the server.

Once you execute the command, you will be connected to the server's terminal. First, run these commands:

sudo apt update && sudo apt upgrade -y
sudo apt install ufw -y
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

These commands will update your server and install necessary files. Additionally, we will have allowed SSH, HTTP, and HTTPS ports in the internal firewall as well.

Docker and Docker Compose

Once you have successfully logged into the server, it's time to install applications. But before moving on to that, you need to understand Docker. Under normal conditions, we can install a single application onto a VPS without issues. However, as the number of applications grows, things start getting messy. Applications can step on each other's toes, break, and crash. To solve these problems, Docker technology is used.

Docker runs all services inside separate containers, ensuring no application depends on another. Instead of digging through individual folders to uninstall a program, you simply delete its container, keeping the system clean.

The official script to install Docker is as follows:

curl -fsSL https://get.docker.com | sudo sh
sudo usermod -aG docker $USER

Run the following commands to make sure Docker is installed correctly:

docker --version
docker compose version
docker run hello-world

Docker Compose is a technology that allows you to save these configuration parameters instead of running a container with a long single-line command every time, enabling you to spin it up whenever you need with a short command.

The main parts of the file:

  • services: The list of services that will run.
  • image: The pre-built package (image) the service will be built from.
  • ports: How the container's port maps to the server's port.
  • volumes: Where data will be stored.
  • environment: Service parameters (timezone, etc.).
  • restart: How the service behaves when the server restarts or if the container crashes.

Docker stores data temporarily. This means if we create a Docker container, use a service, and then shut down the container, all data generated up to that point will be lost. To prevent this, volumes are used.

A Volume is a folder stored outside the Docker container and attached to it. Even if the container is deleted, the volume is not deleted, keeping your data safe.

For more detailed information about Docker, check out the official documentation page.


Installing Our First Application on the Server

Installing applications on the server is really simple. Let's continue with the n8n example I mentioned earlier. To install n8n on the server, we need to follow these steps:

First, we create a directory where n8n files and database will be stored, and navigate into it:

mkdir -p /home/ubuntu/n8n-docker
cd /home/ubuntu/n8n-docker

Open the configuration file with the Nano text editor:

nano docker-compose.yml

Paste the following Docker configuration into the blank screen:

version: '3.8'

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n:latest
    container_name: n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    environment:
      - N8N_HOST=<server_ip>
      - N8N_PORT=5678
      - N8N_PROTOCOL=http
      - NODE_ENV=production
      - WEBHOOK_URL=http://<server_ip>:5678/
      - GENERIC_TIMEZONE=Asia/Baku
    volumes:
      - ./n8n_data:/home/node/.n8n
      - /home/ubuntu/rclone-cron.log:/home/ubuntu/rclone-cron.log:ro

Don't forget to replace the <server_ip> part with your actual server IP.

Press Ctrl + O to save, Enter to confirm, and Ctrl + X to exit.

After saving the file, start the n8n container in the background (in detached mode -d):

docker compose up -d

To confirm that n8n started without errors, check the container logs:

docker logs n8n

At the end of the logs, you should see the message n8n ready on 0.0.0.0, port 5678.

Now, let's add a port rule for n8n just like we did for SSH, HTTP, and HTTPS. Go to Compute > Instances > Your VPS > Networking > Subnet > Security > Security List > Security Rules and click + Add Ingress Rules. Fill out the following information:

Source Type: CIDR
Source CIDR: 0.0.0.0/0
Destination Port Range: 5678

Finally, visit http://<server_ip>:5678 in your browser. The n8n login page should greet you. With this, we have successfully installed our first application on our server.


What's Running on My Server?

We've learned how to install our first application. Now, I will share the most useful applications that I recommend using on your server and that I actively run on mine.


Uptime Kuma

As a web developer, I am constantly building and deploying new applications. You can use Uptime Kuma to monitor these applications. Uptime Kuma continuously monitors Docker containers, websites, and many other services, sending you alerts if a problem or crash occurs.

Uptime Kuma Dashboard


Paperless ngx

If, like me, you're tired of keeping all your documents in physical form, you can use Paperless ngx to digitize them. You add your documents, receipts, and other files to Paperless ngx. Paperless ngx analyzes these documents, allowing you to search and filter them down to the text printed on the pages.

Paperless ngx Interface


FreshRSS

If you want to follow content creators on YouTube and other platforms without relying on algorithms, or read news and blogs without ads and distractions, you can use FreshRSS.

By the way, if you want to follow my blog via RSS, just paste https://samirrhashimov.substack.com/feed into FreshRSS.

FreshRSS Interface


n8n

n8n is a recently emerged app that I love using the most. n8n allows you to create custom automations. It is entirely a no-code app, meaning it requires no programming language. Examples of automations you can build with n8n include solutions that automatically read your emails, analyze them using AI, and categorize them.

n8n Workflow Editor


Supabase, MongoDB, PostgreSQL

You can set up databases on your server. This ensures your data stays with you while giving you more administrative control over your databases.


Excalidraw

This is one of the simplest web apps I use. If you want to visually build diagrams and take handwritten-style notes, you can use Excalidraw. For example, I prepared some of the visual graphics in this article using this application.

Excalidraw Canvas


IT-Tools

An application that brings together the tools programmers need most. It includes many tools like Hash Text, Lorem Ipsum Generator, and UUID Generator.

IT-Tools Dashboard


These are the most essential applications I use. However, there are many more apps available, and you can find and install apps suited to your needs with a quick search.


Who is it for?

While setting up a server gives you incredible freedom and control, it's not always the best choice. For instance, instead of configuring and running a small project on your personal server, using services like Vercel is a more logical choice. That's because Vercel's offerings are often completely sufficient for small projects. As the project grows, migrating to your own server becomes a sensible option.

Another factor to consider before moving to your own server is the Time vs. Money ratio. You can run certain paid applications for free on your own server, but you will need to spend some time to achieve the quick configuration you'd get by paying for a managed service. When an error occurs, you will have to fix it yourself, not the company you bought the service from. Furthermore, there won't be customer support to answer your questions, because you are the customer, and you are the customer support.

Setting up your own server is truly a fantastic experience. I recommend carefully reading all the pros and cons I've listed so far and making the decision that suits you best.


Conclusion and Next Steps

Setting up my own VPS taught me how to level up in the Linux world, how to use Docker, how to master Terminal commands, and most importantly, how to build and manage a digital ecosystem.

Being able to deploy my personal projects live without being dependent on any limits and without paying high monthly subscription fees was the greatest value of this endeavor.


If you are also tired of the limitations of cloud services and want complete control over your own applications, you can follow this path to step into the world of self-hosting:

  1. First, install Docker on your own computer and run an open-source tool.
  2. Create your first server via a VPS or an old computer at home.
  3. Gradually expand your system by adding the databases, automation, and monitoring tools you need.

Where do you host your personal projects and services?


Thanks for reading!


Join our community so you don't miss new posts

Subscribe now

來源:DEV Community · dev.to