How to Set Up a Linux Server from Scratch
You've probably deployed applications on shared hosting or managed platforms. But there comes a point when you need full control over your infrastructure. Setting up a Linux server from scratch gives you that control, but it also means you're responsible for everything—security, updates, networking, and performance tuning.
This guide walks you through deploying a production-ready Linux server from bare metal or a cloud provider. We'll use Ubuntu Server 22.04 LTS as our reference distribution, but the principles apply to any Linux distribution.
Choosing the Right Linux Distribution
Before you even boot the installer, you need to pick a distribution. The choice affects your tooling, documentation, and community support.
| Factor | Ubuntu LTS | Debian | Rocky Linux | AlmaLinux |
|---|---|---|---|---|
| Release Cycle | 2 years (LTS) | 2 years | 1 year | 1 year |
| Package Management | apt | apt | dnf | dnf |
| Community Size | Largest | Large | Growing | Growing |
| Enterprise Support | Canonical | Free | Red Hat Partner | Red Hat Partner |
| Default Kernel | 5.x LTS | 5.x | 5.x | 5.x |
| Documentation | Excellent | Good | Good | Good |
Ubuntu LTS is the safest choice for most users. It has the largest community, the most tutorials, and enterprise backing from Canonical. Debian is excellent if you want a pure, community-driven distribution. Rocky Linux and AlmaLinux are Red Hat alternatives if you're migrating from RHEL.
For this guide, we'll use Ubuntu Server 22.04 LTS.
Step 1: Provision the Server
If you're using a cloud provider, create a new instance with these minimum specifications:
- OS: Ubuntu Server 22.04 LTS 64-bit
- RAM: 2GB minimum (4GB recommended)
- CPU: 1 vCPU minimum (2 vCPUs recommended)
- Storage: 40GB SSD minimum
- Network: Public IP address required
- Security Group: Allow SSH (22), HTTP (80), HTTPS (443)
If you're using bare metal, boot from the Ubuntu Server installation media and follow the on-screen prompts. Select "Install Ubuntu Server" and choose your language.
Important: Always use a strong password during installation. The default password is never secure enough for production.
Step 2: Initial Security Configuration
The moment your server gets an IP address, it becomes a target. Attackers scan the internet for open ports and vulnerable services. You need to harden your server before installing anything else.
Change the Default SSH Port
The default SSH port (22) is scanned constantly by bots. Changing it reduces automated attacks significantly.
Now connect using ssh -p 2222 user@your-server-ip. Update your firewall to allow port 2222 instead of 22.
Configure the Firewall
Ubuntu uses ufw (Uncomplicated Firewall) by default. Enable it and allow only necessary ports:
Your firewall should show only SSH, HTTP, and HTTPS as allowed incoming connections.
Create a Non-Root User
Never log in as root directly. Create a regular user with sudo privileges instead:
You should see root output, confirming sudo works.
Configure SSH Key Authentication
Password authentication is convenient but insecure. SSH keys are the standard for secure remote access.
Now disable password authentication entirely in /etc/ssh/sshd_config:
Restart SSH and verify you can still log in with your key.
Step 3: System Updates and Basic Tools
Your server comes with outdated packages. Update everything before installing additional software:
Why these tools?
curlandwget: HTTP client tools for testing and downloadsgit: Version controlvim: Text editor (you'll need it for configuration files)htop: Interactive process viewer (better than top)ufw: Firewall managementfail2ban: Brute-force attack protectionunattended-upgrades: Automatic security updates
Step 4: Install and Configure Fail2Ban
Fail2Ban monitors log files and bans IP addresses that repeatedly fail authentication attempts. It's your first line of defense against brute-force attacks.
Add this configuration to jail.local:
Restart fail2ban to apply changes:
Now test it by intentionally failing SSH authentication three times. You'll see the IP banned in the fail2ban status.
Step 5: Set Up Automatic Security Updates
Unpatched vulnerabilities are the #1 cause of server compromises. Configure unattended-upgrades to install security patches automatically.
This configuration updates packages weekly and applies security upgrades automatically. You can also configure email notifications for failed updates.
Step 6: Configure Time Synchronization
Accurate time is critical for logging, SSL certificates, and distributed systems. Use systemd-timesyncd, which is included by default:
You should see NTP service: active and System clock synchronized: yes.
Step 7: Set Up a Swap File
If your server runs out of RAM, the kernel starts swapping to disk, which dramatically slows performance. A swap file provides a safety net for memory spikes.
Check swap status with free -h. You should now see the swap file listed.
Step 8: Install and Configure a Web Server
Now that your server is secure, install a web server. We'll use Nginx because it's lightweight, fast, and widely used.
Visit your server's IP address in a browser. You should see the default Nginx welcome page.
Configure Nginx
Edit the default site configuration:
Update the server block:
Test the configuration:
If the test passes, reload Nginx:
Step 9: Set Up a Database
Most applications need a database. We'll install PostgreSQL, a robust relational database.
PostgreSQL creates a postgres user by default. Switch to this user and create a new database:
Exit the PostgreSQL prompt and test the connection:
You should see the PostgreSQL version output.
Step 10: Configure SSL/TLS with Let's Encrypt
HTTPS is no longer optional. Let's Encrypt provides free SSL certificates that automatically renew.
Install Certbot
Obtain and Configure the Certificate
Certbot will ask for your email and agree to the terms. It will then configure SSL and set up automatic renewal.
Verify the renewal works:
If you see "Simulation successful," you're all set. Certbot will automatically renew certificates before they expire.
Step 11: Set Up Monitoring
You can't manage what you don't measure. Install basic monitoring tools to track CPU, memory, and disk usage.
Create a Basic Monitoring Script
Create a script to check server health:
Add this content:
Make it executable:
Run it to see your server's current state:
Step 12: Deploy Your First Application
Now deploy a simple application to test everything. We'll use a Node.js application.
Install Node.js
Create a Simple Application
Add this code to app.js:
Run the Application
Visit http://your-server-ip:3000 in your browser. You should see the "Hello from your Linux server!" message.
Configure Nginx as a Reverse Proxy
Update your Nginx configuration to proxy traffic to the Node.js application:
Replace the location / block with:
Test and reload Nginx:
Now visit http://your-domain.com (or your server's IP). The request is proxied to Node.js, and you see the application response.
Step 13: Set Up Automated Backups
Data loss is inevitable. You need automated backups with a retention policy.
Install Backup Tools
Create a Backup Script
Add this content:
Make it executable:
Test the Backup
Check the backup directory:
You should see a timestamped archive file.
Schedule Automatic Backups
Add this line to run backups daily at 2 AM:
Verify Cron is Running
Your backups will now run automatically every day.
Step 14: Configure Log Rotation
Logs grow indefinitely and can fill your disk. Configure log rotation to manage log file sizes.
Add this configuration:
This configuration:
- Rotates logs daily
- Keeps 14 days of logs
- Compresses old logs
- Doesn't rotate if the log is empty
- Creates new logs with proper permissions
Test the configuration:
Check if the log files were rotated and compressed.
Step 15: Document Your Server
Documentation is often overlooked until you need it. Create a server documentation file with all your configurations.
Add this template:
Keep this file updated whenever you make changes to your server.
Conclusion
Setting up a Linux server from scratch is a multi-step process that requires attention to security, automation, and documentation. You've now configured:
- A hardened server with SSH key authentication and a firewall
- Automatic security updates and fail2ban protection
- A web server (Nginx) with SSL/TLS certificates
- A database (PostgreSQL) for your applications
- Monitoring tools to track server health
- Automated backups with a retention policy
- Log rotation to manage disk space
The next step is to deploy your actual application. Platforms like ServerlessBase can simplify the deployment process by handling reverse proxy configuration, SSL certificates, and monitoring automatically, so you can focus on building your application rather than managing infrastructure.
Remember that server administration is an ongoing process. Keep your system updated, monitor your logs regularly, and document any changes you make. A well-maintained server is secure, reliable, and easy to manage.
Next Steps
- Deploy your application: Follow your application's deployment guide, using the Nginx reverse proxy configuration you set up.
- Set up monitoring alerts: Configure alerts for high CPU usage, disk space, or failed logins.
- Implement a backup strategy: Test your backups regularly and verify they can be restored.
- Learn more about Linux administration: Explore topics like systemd services, containerization, and Kubernetes.
- Consider a management platform: Tools like ServerlessBase can automate many of these tasks and provide a unified interface for managing multiple servers.
Your server is now production-ready. Treat it with care, and it will serve your applications reliably for years.