Complete Ubuntu Production Server Setup Guide 2025
Master the art of setting up a secure, production-ready Ubuntu server from scratch. This comprehensive guide covers everything from basic security configuration to advanced hardening techniques, SSL certificates, Nginx setup, and ongoing maintenance.
Before beginning this Ubuntu server setup tutorial, ensure you have the following requirements met:
Fresh Ubuntu Server: Ubuntu 20.04 LTS or 22.04 LTS (recommended for production)
Server Access: Root or sudo administrative privileges
Domain Name: A domain pointing to your server's public IP address
SSH Access: Secure shell access to your server
Basic Linux Knowledge: Familiarity with command line operations
Important: This guide is designed for production environments. Always test configurations in a development environment first.
Phase 1: Initial Server Security Setup
Step 1: Update System Packages
Start by updating your Ubuntu server to ensure all packages are current and security patches are applied:
# Update package lists and upgrade system
sudo apt update && sudo apt upgrade -y
# Install essential security packages
sudo apt install -y curl wget vim ufw fail2ban unattended-upgrades
Step 2: Create Non-Root User Account
Creating a dedicated user account instead of using root enhances server security:
# Create new user (replace 'yourusername' with your desired username)
sudo adduser yourusername
# Add user to sudo group for administrative privileges
sudo usermod -aG sudo yourusername
# Test sudo access
su - yourusername
sudo whoami # Should return 'root'
Step 3: Configure SSH Security
Secure your SSH configuration to prevent unauthorized access:
# Backup original SSH configuration
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
# Edit SSH configuration file
sudo vim /etc/ssh/sshd_config
Critical SSH Security Configuration:
Port 2222
PermitRootLogin no
AllowUsers yourusername
PasswordAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
MaxAuthTries 3
MaxSessions 2
LoginGraceTime 60
X11Forwarding no
Step 4: Setup SSH Key Authentication
Implement key-based authentication for enhanced security:
# Generate SSH key pair on your local machine
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
# Copy public key to server
ssh-copy-id -p 2222 yourusername@your-server-ip
# Or manually create and configure:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
vim ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Step 5: Test SSH Configuration
Verify your SSH configuration before applying changes:
# Test SSH configuration syntax
sudo sshd -t
# Restart SSH service to apply changes
sudo systemctl restart sshd
Warning: Always test SSH configuration in a separate terminal session before closing your current connection to avoid lockout.
Phase 2: Firewall Configuration (UFW)
Step 6: Configure UFW Firewall
Set up Ubuntu's Uncomplicated Firewall (UFW) to control network traffic:
# Allow SSH first (use your custom port)
sudo ufw allow 2222/tcp
# Allow HTTP and HTTPS traffic
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Set default firewall policies
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Enable UFW firewall
sudo ufw enable
# Check firewall status and rules
sudo ufw status verbose
Expected UFW Status Output:
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
-- ------ ----
2222/tcp ALLOW IN Anywhere
80/tcp ALLOW IN Anywhere
443/tcp ALLOW IN Anywhere
Phase 3: Install and Configure Nginx
Step 7: Install Nginx Web Server
Install and configure Nginx for serving web content: