Build your first personal server at home
この記事の目次
1. Purchase a Server that fits your needs
The first step in building your server is selecting the right hardware. Depending on your needs, whether for hosting websites, running applications, or managing data, you’ll have different options:
Cloud VPS/VM Provider: if you prefer a flexible cloud device with no physical limit, purchasing a machine from a cloud service like Contabo providers might be a good choice. After that, you’ll have multiple ways to connect or remote control to the purchased server and do your work.
Dedicated Server Hardware: if you prefer a dedicated machine and do not bother with the physical space that might occupied, you can purchase a used or new server from brands like Dell, HP, or Supermicro. These are robust and designed for 24/7 operation.
Repurposed Desktop: if you’re on a budget, an old desktop or laptop can be repurposed into a server. Ensure it has adequate RAM and storage for your intended use.
Raspberry Pi: for lightweight applications or learning purposes, a Raspberry Pi can serve as an entry-level server.
Your server should have enough RAM, storage space, and processing power for your intended use. Also, consider the server’s power consumption and noise levels, especially if it runs in your home.
2. Install the OS, set up the dev-environment
1. Operating system
⭐ The next step is to install an operating system. You might choose a Linux distribution for a server due to its stability, security, and cost-effectiveness. Here are some popular choices, in this guide, I prefer to install the Ubuntu 22.04 image for the stable on the system.
- Download the ISO: go to the official Ubuntu website and download the 22.04 ISO file.
- Create a Bootable USB Drive: use tools like Rufus (for Windows) or Etcher (for macOS/Linux) to create a bootable USB drive.
- Boot from USB: insert the USB drive into your server and boot from it. You may need to adjust the BIOS settings to boot from USB.
- Follow the Installation Wizard: follow the prompts to install the OS. Select your disk, set up partitions, and configure basic settings.
2. Recommended tools
⭐ Install some recommended tools and libraries based on your needs (mostly install using the ‘apt’ command, so you should run sudo apt-get update command first).
- Git: for repository access and coop works.
- Language: Nodejs (with npm), Java SE Development kit, package manager tools (maven, grade, yarn,..). Install the proper libraries and tools based on your needs, and your application requirements
- Desktop application (with GUI): Firefox, VSCode, GNorm,… helps you have more efficiency working when you have already established the remote desktop control on your server.
3. Development environment
⭐ After installation, you’ll need to set up your development environment.
- Disable Password Authentication: go to the sshd config: vim /etc/ssh/sshd_config. Then look for the line ‘PasswordAuthentication yes’ and replace yes with no. After all, restart the service to apply the change: service sshd restart
- Set up an SSH connection: use ssh-agent and ssh-keygen to gen the key pair on your machine, then add the public key to known_host on the server. After that, you can establish the connection from your working machine to your server with the private key
- Power failure adaption: when the server is turning on, connect the physical keyboard and try to press F2 button to enter BIOS setting. In the BIOS, look for something like “always restart on power failure”. This will help the server automatically restart if our home has a power cut, to ensure zero downtime for our business.
- Assign static IP/ Open port: after connecting the LAN cable, go to the modem setting (usually http://192.168.1.1), based on your network provider, try to assign a static IP address for your server. We can also open the public port to the internet, so our server can be accessed anywhere rather than in the local network.
3. Install Docker, Portainer and Ngnix
Docker is a powerful tool for containerization, allowing you to run applications in isolated environments. Portainer provides a user-friendly interface for managing Docker containers.
Install Docker (Docker CE): https://docs.docker.com/engine/install/ubuntu/
Install Portainer:
docker volume create portainer_data
docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
Access Portainer: by navigating to http://<your-server-ip>:9000 in your web browser. Set up your admin account and start managing your Docker containers with ease.
Ngnix Proxy Management: a high-performance web server and reverse proxy server. It can be used to manage incoming traffic and serve applications. The installation steps can be view here
Run your first application and open the port
With Docker and Nginx set up, you can now deploy your first application. Here’s a quick example using a simple NextJS web application:
Create a simple NextJS application: Create a simple application, and define all the steps needed to create a Docker Image for our application like the one below
Run the application: transfer the project’s source code to our VPS, then use the docker-compose to run the application, our application is now ready at our defined port in docker-compose.yaml file
Open the port: open the port and make our application publicly accessible through the internet, this step is easier with the Ngnix Proxy Manager
4. Extra: Purchase a domain and Cloudflare set up
To make your server accessible via a domain name, follow these steps:
- Purchase a Domain: Use registrars like Namecheap, GoDaddy, or Google Domains to buy a domain name.
- Configure DNS Records: Set up A record to point your domain to your server’s IP address. This is usually done through your domain registrar’s DNS management panel.
To enhance security and performance, use Cloudflare as your DNS provider:
- Sign Up for Cloudflare: Create an account at Cloudflare.
- Add Your Domain: Follow the prompts to add your domain to Cloudflare.
- Update DNS Records: Cloudflare will provide you with nameservers that you’ll need to set in your domain registrar’s DNS settings.
- Configure SSL/TLS: Enable SSL/TLS through Cloudflare to secure your connections.
5. Extra: Monitoring our server with more tools
Should-have tools that could be installed on our local hosting Ubuntu server:
htop: shows and monitors the usage per CPU and also a text graph of memory and swap usage.
MySQL Workbench: design, develop, and manage the MYSQL databases.
Zabbix: monitoring virtual machines and network devices in real-time.
Nmap: used for network discovery by sending packets and analyzing the responses.
cPanel: hosting control panel with GUI that helps us manage our servers.
Cron: a task scheduler helps run automated processes at a certain time.
By following these steps, you’ll have a personal server up and running, capable of hosting applications, and accessible via your domain. Enjoy your new server setup and the capabilities it brings 😉.
カテゴリー: