Denny Lim
Setting up an Ubuntu server for application hosting or database hosting requires proper configuration to ensure performance, security, and reliability. Whether you're deploying a Node.js app, Python backend, MySQL, PostgreSQL, or MongoDB, this guide covers the essential steps.
In this tutorial, we’ll walk through:
✅ Initial Ubuntu Server Setup using Ubuntu 24.04
✅ Security Hardening (SSH, Firewall, Fail2Ban)
✅ Web Server Setup (Nginx/Apache)
✅ Database Server Configuration (MySQL, PostgreSQL, MongoDB)
✅ Performance Optimisation (Swap, Caching, Process Managers)
Update & Upgrade System
Always start with the latest packages:
sudo apt update && sudo apt upgrade -y
Create a Non-Root User
Avoid using root
for daily tasks:
adduser deploy usermod -aG sudo deploy
Set Up SSH Key Authentication
Disable password login for security:
mkdir -p ~/.ssh chmod 700 ~/.ssh nano ~/.ssh/authorized_keys # Paste your public key chmod 600 ~/.ssh/authorized_keys
Disable password login in /etc/ssh/sshd_config
:
PasswordAuthentication no PermitRootLogin no
Restart SSH:
sudo systemctl restart ssh
Configure UFW Firewall
Allow only necessary ports:
sudo ufw allow 22 # SSH sudo ufw allow 80 # HTTP sudo ufw allow 443 # HTTPS sudo ufw enable
Install Fail2Ban (Prevent Brute-Force Attacks)
sudo apt install fail2ban -y sudo systemctl enable --now fail2ban
Automatic Security Updates
sudo apt install unattended-upgrades -y sudo dpkg-reconfigure unattended-upgrades # Select "Yes"
Option A: Install Nginx (Recommended for High Performance)
sudo apt install nginx -y sudo systemctl enable --now nginx
Option B: Install Apache
sudo apt install apache2 -y sudo systemctl enable --now apache2
Set Up a Reverse Proxy (For Node.js/Python Apps)
Edit Nginx config (/etc/nginx/sites-available/your_app
):
server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:3000; # Node.js app proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
Enable the site:
sudo ln -s /etc/nginx/sites-available/your_app /etc/nginx/sites-enabled/ sudo nginx -t && sudo systemctl restart nginx
MySQL Setup
sudo apt install mysql-server -y sudo mysql_secure_installation # Follow prompts to secure
PostgreSQL Setup
sudo apt install postgresql postgresql-contrib -y sudo systemctl enable --now postgresql
MongoDB Setup
sudo apt install -y mongodb sudo systemctl enable --now mongodb
Enable Swap Memory
Prevent out-of-memory crashes:
sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Optimize Database Performance
For MySQL, edit /etc/mysql/my.cnf
:
[mysqld] innodb_buffer_pool_size = 1G # Use 50-70% of available RAM query_cache_size = 128M
For PostgreSQL, edit /etc/postgresql/[version]/main/postgresql.conf
:
shared_buffers = 1GB effective_cache_size = 3GB
Use PM2 for Node.js (Auto-Restart Apps)
sudo npm install -g pm2 pm2 start app.js pm2 startup pm2 save
Your Ubuntu server is now optimized for hosting applications and databases securely. Next steps could include: