Complete Ubuntu Server Setup Guide for VPS
Setting up an Ubuntu server properly is crucial for security, performance, and maintainability. This comprehensive guide covers everything from initial configuration to production-ready setup.
Initial Server Setup
Update System
# Update package lists
sudo apt update
# Upgrade all packages
sudo apt upgrade -y
# Install essential packages
sudo apt install -y curl wget git vim htop net-tools ufw
Create Non-Root User
# Add new user
sudo adduser yourusername
# Add user to sudo group
sudo usermod -aG sudo yourusername
# Switch to new user
su - yourusername
SSH Configuration
SSH Key Setup
# Generate SSH key pair on local machine
ssh-keygen -t ed25519 -C "your_email@example.com"
# Copy public key to server
ssh-copy-id yourusername@your-server-ip
# Disable password authentication
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
# Change SSH port (optional)
sudo sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config
# Restart SSH service
sudo systemctl restart sshd
Firewall Configuration
UFW Setup
# Enable UFW
sudo ufw enable
# Allow SSH (use custom port if changed)
sudo ufw allow 2222/tcp
# Allow HTTP and HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Check status
sudo ufw status
Web Server Installation
Nginx Installation
# Install Nginx
sudo apt install -y nginx
# Start and enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Basic configuration
sudo vim /etc/nginx/sites-available/default
Apache Installation (Alternative)
# Install Apache
sudo apt install -y apache2
# Start and enable Apache
sudo systemctl start apache2
sudo systemctl enable apache2
Database Setup
MySQL/MariaDB Installation
# Install MariaDB
sudo apt install -y mariadb-server
# Secure installation
sudo mysql_secure_installation
# Create database and user
sudo mysql -u root -p
PostgreSQL Installation
# Install PostgreSQL
sudo apt install -y postgresql postgresql-contrib
# Switch to postgres user
sudo -u postgres psql
# Create database and user
CREATE DATABASE yourdb;
CREATE USER youruser WITH PASSWORD 'yourpassword';
GRANT ALL PRIVILEGES ON DATABASE yourdb TO youruser;
SSL/TLS Certificates
Let's Encrypt with Certbot
# Install Certbot
sudo apt install -y certbot python3-certbot-nginx
# Obtain certificate
sudo certbot --nginx -d yourdomain.com
# Test renewal
sudo certbot renew --dry-run
Monitoring and Logging
System Monitoring
# Install monitoring tools
sudo apt install -y htop iotop ncdu
# Install monitoring stack
sudo apt install -y prometheus-node-exporter
# Check system resources
htop
df -h
free -h
Log Management
# Install log rotation
sudo apt install -y logrotate
# Configure logrotate for custom logs
sudo vim /etc/logrotate.d/yourapp
Security Hardening
Automatic Updates
# Install unattended-upgrades
sudo apt install -y unattended-upgrades
# Configure automatic updates
sudo dpkg-reconfigure --priority=low unattended-upgrades
Fail2Ban Installation
# Install Fail2Ban
sudo apt install -y fail2ban
# Configure jails
sudo vim /etc/fail2ban/jail.local
# Restart service
sudo systemctl restart fail2ban
Backup Strategy
Automated Backups
# Create backup directory
sudo mkdir -p /backup
# Create backup script
sudo vim /usr/local/bin/backup.sh
Cron Jobs for Automation
# Edit crontab
sudo crontab -e
# Add backup job (daily at 2 AM)
0 2 * * * /usr/local/bin/backup.sh
Performance Optimization
System Tuning
# Optimize sysctl settings
sudo vim /etc/sysctl.conf
# Apply changes
sudo sysctl -p
Nginx Optimization
# Optimize Nginx configuration
sudo vim /etc/nginx/nginx.conf
# Test configuration
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginx
Final Steps
System Information
# Check system status
sudo systemctl status nginx
sudo systemctl status mysql
sudo ufw status
# System information
uname -a
lsb_release -a
Documentation
Keep detailed documentation of your setup, including:
- Server specifications
- Installed software versions
- Configuration changes
- Backup procedures
- Emergency contacts
Your Ubuntu server is now properly configured and ready for production use. Regular maintenance, monitoring, and security updates are essential for keeping your server secure and performant.
Maya Poudel
DevOps Engineer
DevOps Engineer specializing in Linux systems administration and cloud infrastructure automation.


