Complete VPS Setup Guide for Developers
Setting up a VPS from scratch can seem daunting, but with this comprehensive guide, you'll have a fully configured server ready for production use. We'll cover everything from initial server setup to installing essential development tools.
Initial Server Setup
Before installing any software, let's secure and configure your server properly.
Step 1: Connect to Your VPS
# Connect via SSH
ssh root@your-server-ip
# Or if using a specific user
ssh username@your-server-ip -p 22
Step 2: Update System Packages
# For Ubuntu/Debian systems
sudo apt update && sudo apt upgrade -y
# For CentOS/RHEL systems
sudo yum update -y
Step 3: Create a Non-Root User
# Create new user
sudo adduser deploy
# Add to sudo group
sudo usermod -aG sudo deploy
# Switch to new user
su - deploy
Step 4: Configure SSH Security
# Generate SSH key on your local machine
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Copy public key to server
ssh-copy-id deploy@your-server-ip
# Edit SSH configuration
sudo nano /etc/ssh/sshd_config
# Disable root login and password authentication
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
# Restart SSH service
sudo systemctl restart ssh
Installing Essential Tools
Step 5: Install Basic Utilities
# Install essential packages
sudo apt install -y curl wget git unzip software-properties-common apt-transport-https ca-certificates gnupg lsb-release
# Install build tools
sudo apt install -y build-essential
Step 6: Configure Firewall
# Enable UFW firewall
sudo ufw enable
# Allow SSH
sudo ufw allow ssh
# Allow HTTP and HTTPS
sudo ufw allow 80
sudo ufw allow 443
# Check firewall status
sudo ufw status
Installing Python
Method 1: Using System Package Manager
# Install Python 3 and pip
sudo apt install -y python3 python3-pip python3-venv python3-dev
# Verify installation
python3 --version
pip3 --version
Method 2: Using pyenv (Recommended for Multiple Versions)
# Install pyenv dependencies
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl
# Install pyenv
curl https://pyenv.run | bash
# Add to shell profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
# Reload shell
source ~/.bashrc
# Install Python versions
pyenv install 3.11.0
pyenv install 3.12.0
pyenv global 3.11.0
Setting up Virtual Environments
# Create project directory
mkdir ~/projects
cd ~/projects
# Create virtual environment
python3 -m venv myproject_env
# Activate environment
source myproject_env/bin/activate
# Install common packages
pip install django flask fastapi requests numpy pandas
Installing Node.js
Method 1: Using NodeSource Repository
# Add NodeSource repository
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# Install Node.js
sudo apt install -y nodejs
# Verify installation
node --version
npm --version
Method 2: Using NVM (Recommended for Multiple Versions)
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Reload shell
source ~/.bashrc
# Install Node.js versions
nvm install 18
nvm install 20
nvm use 20
nvm alias default 20
# Install global packages
npm install -g pm2 yarn pnpm typescript nodemon
Setting up a Node.js Application
# Create application directory
mkdir ~/apps/myapp
cd ~/apps/myapp
# Initialize package.json
npm init -y
# Install dependencies
npm install express cors helmet morgan
# Create basic server
cat > app.js << 'EOF'
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.get('/', (req, res) => {
res.json({ message: 'Server is running!' });
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
EOF
# Start with PM2
pm2 start app.js --name "myapp"
pm2 startup
pm2 save
Installing and Configuring Nginx
Step 7: Install Nginx
# Install Nginx
sudo apt install -y nginx
# Start and enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Check status
sudo systemctl status nginx
Step 8: Configure Nginx for Node.js
# Create server block configuration
sudo nano /etc/nginx/sites-available/myapp
# Add configuration
server {
listen 80;
server_name your-domain.com www.your-domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
# Enable the site
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
# Test configuration
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginx
Step 9: SSL Certificate with Let's Encrypt
# Install Certbot
sudo apt install -y certbot python3-certbot-nginx
# Obtain SSL certificate
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
# Test auto-renewal
sudo certbot renew --dry-run
Installing Database Systems
PostgreSQL Installation
# Install PostgreSQL
sudo apt install -y postgresql postgresql-contrib
# Start and enable service
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Create database and user
sudo -u postgres psql
CREATE DATABASE myapp_db;
CREATE USER myapp_user WITH PASSWORD 'secure_password';
GRANT ALL PRIVILEGES ON DATABASE myapp_db TO myapp_user;
q
MySQL Installation
# Install MySQL
sudo apt install -y mysql-server
# Secure installation
sudo mysql_secure_installation
# Create database and user
sudo mysql
CREATE DATABASE myapp_db;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Redis Installation
# Install Redis
sudo apt install -y redis-server
# Configure Redis
sudo nano /etc/redis/redis.conf
# Set supervised to systemd
supervised systemd
# Restart Redis
sudo systemctl restart redis
sudo systemctl enable redis
# Test Redis
redis-cli ping
Installing Docker
Step 10: Install Docker
# Add Docker repository
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
# Add user to docker group
sudo usermod -aG docker $USER
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Monitoring and Maintenance
System Monitoring with htop
# Install htop
sudo apt install -y htop
# Install system monitoring tools
sudo apt install -y iotop nethogs iftop
Log Management
# View system logs
sudo journalctl -f
# View Nginx logs
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
# Configure log rotation
sudo nano /etc/logrotate.d/myapp
Automated Backups
# Create backup script
nano ~/backup.sh
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/home/deploy/backups"
# Create backup directory
mkdir -p $BACKUP_DIR
# Backup database
pg_dump -U myapp_user myapp_db > $BACKUP_DIR/db_backup_$DATE.sql
# Backup application files
tar -czf $BACKUP_DIR/app_backup_$DATE.tar.gz ~/apps/
# Remove backups older than 7 days
find $BACKUP_DIR -type f -mtime +7 -delete
# Make script executable
chmod +x ~/backup.sh
# Add to crontab for daily backups
crontab -e
0 2 * * * /home/deploy/backup.sh
Security Best Practices
Fail2Ban Installation
# Install Fail2Ban
sudo apt install -y fail2ban
# Configure Fail2Ban
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
[sshd]
enabled = true
[nginx-http-auth]
enabled = true
# Restart Fail2Ban
sudo systemctl restart fail2ban
System Updates
# Enable automatic security updates
sudo apt install -y unattended-upgrades
# Configure automatic updates
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
Performance Optimization
Nginx Optimization
# Edit Nginx configuration
sudo nano /etc/nginx/nginx.conf
# Optimize worker processes
worker_processes auto;
worker_connections 1024;
# Enable gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Add caching headers
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
System Limits
# Increase file descriptor limits
sudo nano /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
# Optimize kernel parameters
sudo nano /etc/sysctl.conf
# Network optimizations
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_max_syn_backlog = 65535
Conclusion
You now have a fully configured VPS with Python, Node.js, Nginx, and essential development tools. This setup provides a solid foundation for hosting web applications, APIs, and services. Remember to regularly update your system, monitor performance, and maintain security best practices.
For additional support and managed VPS hosting solutions, consider RexZ Cloud VPS hosting with pre-configured environments and 24/7 support.