Skip to content

mahafuz/local-vps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Local Premium VPS on Docker

Complete VPS environment running locally on Mac M1 with all premium features.

🎯 Services that works

  • βœ… SSH works properly
  • βœ… No volume conflicts
  • βœ… Persistent data storage
  • βœ… All services stable
  • βœ… Proper user management
  • βœ… Security features enabled

πŸ“ Project Files

Save these files in your project directory (~/local-vps):

~/local-vps/
β”œβ”€β”€ docker-compose.yml    # Main configuration
β”œβ”€β”€ Dockerfile            # VPS image definition
β”œβ”€β”€ entrypoint.sh        # Container startup script
β”œβ”€β”€ .env                 # Passwords (change these!)
β”œβ”€β”€ .gitignore           # Git ignore file
β”œβ”€β”€ setup.sh             # Setup script
β”œβ”€β”€ prometheus/
β”‚   └── prometheus.yml   # Monitoring config
└── shared/              # Shared folder with Mac

πŸš€ Quick Setup

# 1. Create directory
mkdir ~/local-vps && cd ~/local-vps

# 2. Save all 7 files from the artifacts above

# 3. Make scripts executable
chmod +x setup.sh entrypoint.sh

# 4. IMPORTANT: Edit .env and change passwords
nano .env

# 5. Run setup
./setup.sh

πŸ”‘ Access Your VPS

SSH Access (Like a Real VPS)

# As root
ssh root@localhost -p 2222
# Password: ChangeMe123! (or what you set in .env)

# As admin user
ssh admin@localhost -p 2222
# Password: Admin123! (or what you set in .env)

Direct Docker Access

# As root
docker exec -it my-vps bash

# As admin user
docker exec -it -u admin my-vps bash

πŸ“Š Management Interfaces

πŸ—„οΈ Database Access

PostgreSQL

# From your Mac
psql -h localhost -p 5432 -U pgadmin -d maindb
# Password: PostgresSecure123!

# From inside VPS
apt install postgresql-client
psql -h postgres -U pgadmin -d maindb

MySQL

# From your Mac
mysql -h 127.0.0.1 -P 3306 -u root -p
# Password: MysqlRoot123!

# From inside VPS
apt install mysql-client
mysql -h mysql -u root -p

Redis

# From your Mac
redis-cli -h localhost -p 6379 -a RedisSecure123!

# From inside VPS
apt install redis-tools
redis-cli -h redis -a RedisSecure123!

MongoDB

# From inside VPS
apt install mongodb-clients
mongosh mongodb://mongoadmin:MongoSecure123!@mongodb:27017

πŸ› οΈ Installing Software

Nginx Web Server

ssh root@localhost -p 2222

# Install
apt update
apt install -y nginx

# Start
systemctl start nginx
systemctl enable nginx

# Test
curl localhost

# On your Mac browser: http://localhost

Node.js

# Install Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs

# Verify
node --version
npm --version

# Install PM2 for process management
npm install -g pm2

# Create test app
mkdir -p /opt/myapp && cd /opt/myapp
cat > server.js << 'EOF'
const http = require('http');
http.createServer((req, res) => {
  res.end('Hello from Node.js!');
}).listen(3000, () => console.log('Running on :3000'));
EOF

# Run with PM2
pm2 start server.js --name myapp
pm2 save
pm2 startup

# Test: http://localhost:3000

PHP

# Install PHP 8.1
apt install -y php8.1-fpm php8.1-cli php8.1-mysql \
  php8.1-curl php8.1-xml php8.1-mbstring

# Start
systemctl start php8.1-fpm
systemctl enable php8.1-fpm

# Test
php --version

Python

apt install -y python3 python3-pip python3-venv
python3 --version

πŸ”’ Security Setup

1. Change All Passwords

# Inside VPS
ssh root@localhost -p 2222

# Change root password
passwd

# Change admin password
passwd admin

2. SSH Key Authentication (Recommended)

# On your Mac, generate SSH key
ssh-keygen -t ed25519 -C "your_email@example.com"

# Copy to VPS
ssh-copy-id -p 2222 admin@localhost

# Test key login
ssh -p 2222 admin@localhost

# Disable password auth (inside VPS)
nano /etc/ssh/sshd_config
# Set: PasswordAuthentication no
systemctl restart ssh

# Disable root login
# Set: PermitRootLogin no

3. Enable Firewall

# Inside VPS
ufw enable
ufw status

# The following ports are already configured:
# 22 (SSH), 80 (HTTP), 443 (HTTPS)
# 3000, 3001, 8080 (App ports)

4. Monitor with Fail2ban

# Check fail2ban status
fail2ban-client status
fail2ban-client status sshd

# View banned IPs
fail2ban-client get sshd banned

πŸ“¦ Backup & Restore

Backup

# Backup web files
docker exec my-vps tar -czf /shared/www-backup.tar.gz /var/www

# Backup configs
docker exec my-vps tar -czf /shared/config-backup.tar.gz /etc/nginx

# Backup PostgreSQL
docker exec vps-postgres pg_dump -U pgadmin maindb > ~/local-vps/shared/postgres.sql

# Backup MySQL
docker exec vps-mysql mysqldump -u root -p${MYSQL_ROOT_PASSWORD} appdb > ~/local-vps/shared/mysql.sql

# Files are now in: ~/local-vps/shared/

Restore

# Restore web files
docker exec my-vps tar -xzf /shared/www-backup.tar.gz -C /

# Restore PostgreSQL
cat ~/local-vps/shared/postgres.sql | docker exec -i vps-postgres psql -U pgadmin maindb

# Restore MySQL
cat ~/local-vps/shared/mysql.sql | docker exec -i vps-mysql mysql -u root -p${MYSQL_ROOT_PASSWORD} appdb

πŸŽ›οΈ Container Management

# Check status
docker-compose ps

# View logs
docker-compose logs -f my-vps
docker-compose logs -f postgres

# Restart services
docker-compose restart my-vps
docker-compose restart

# Stop all
docker-compose stop

# Start all
docker-compose start

# Remove all (keeps data)
docker-compose down

# Remove all + DELETE DATA
docker-compose down -v

# Rebuild from scratch
docker-compose down
docker-compose build --no-cache
docker-compose up -d

🌐 Network Architecture

Mac M1 Host (Your Computer)
β”‚
β”œβ”€ localhost:2222  β†’ my-vps:22 (SSH)
β”œβ”€ localhost:80    β†’ my-vps:80 (HTTP)
β”œβ”€ localhost:443   β†’ my-vps:443 (HTTPS)
β”œβ”€ localhost:3000  β†’ my-vps:3000 (App)
β”œβ”€ localhost:5432  β†’ postgres:5432
β”œβ”€ localhost:3306  β†’ mysql:3306
β”œβ”€ localhost:6379  β†’ redis:6379
β”œβ”€ localhost:27017 β†’ mongodb:27017
β”œβ”€ localhost:9443  β†’ portainer:9443
β”œβ”€ localhost:3002  β†’ grafana:3000
└─ localhost:9090  β†’ prometheus:9090

Docker Network (172.20.0.0/16)
β”œβ”€ 172.20.0.10 - my-vps
β”œβ”€ 172.20.0.20 - postgres
β”œβ”€ 172.20.0.21 - mysql
β”œβ”€ 172.20.0.22 - redis
β”œβ”€ 172.20.0.23 - mongodb
β”œβ”€ 172.20.0.30 - portainer
β”œβ”€ 172.20.0.31 - prometheus
β”œβ”€ 172.20.0.32 - grafana
└─ 172.20.0.33 - node-exporter

Inside VPS, databases are accessible by name:

  • postgres:5432
  • mysql:3306
  • redis:6379
  • mongodb:27017

πŸ”§ Troubleshooting

SSH Connection Refused

# Check if container is running
docker ps | grep my-vps

# Check SSH status inside container
docker exec my-vps ps aux | grep sshd

# Restart container
docker-compose restart my-vps

# View logs
docker logs my-vps

Port Already in Use

# Check what's using the port
lsof -i :2222

# Change port in docker-compose.yml
# "2223:22" instead of "2222:22"

Container Keeps Restarting

# Check logs
docker logs my-vps

# Try starting without detached mode to see errors
docker-compose up

# Rebuild clean
docker-compose down
docker-compose build --no-cache
docker-compose up -d

Forgot Password

# Access directly
docker exec -it my-vps bash

# Change password
passwd root
passwd admin

Reset Everything

# WARNING: This deletes ALL data
docker-compose down -v
rm -rf shared/*
./setup.sh

πŸ“š Learning Resources

βœ… Security Checklist

  • Changed all passwords in .env
  • Changed root password: passwd
  • Changed admin password: passwd admin
  • Set up SSH key authentication
  • Disabled password SSH authentication
  • Disabled root SSH login
  • Enabled UFW firewall: ufw enable
  • Tested fail2ban: fail2ban-client status
  • Set up regular backups
  • Configured monitoring in Grafana

πŸŽ“ What You Can Learn

  • Linux Administration: Users, permissions, services
  • Web Servers: Nginx, Apache configuration
  • Application Deployment: Node.js, PHP, Python apps
  • Database Management: SQL, NoSQL, backups
  • Security: Firewall, SSH hardening, intrusion detection
  • Monitoring: Prometheus, Grafana dashboards
  • Networking: Ports, proxies, load balancing
  • DevOps: Docker, containers, orchestration

πŸš€ You're able to:

  1. Deploy a real application (WordPress, Ghost, custom app)
  2. Set up Nginx as reverse proxy for multiple apps
  3. Configure SSL with self-signed certificates
  4. Create automated backup scripts with cron
  5. Set up monitoring dashboards in Grafana
  6. Learn Docker Compose for multi-container apps
  7. Practice security hardening
  8. Experiment with CI/CD pipelines

Note: This is for learning and local development. For production websites, use real VPS providers with proper infrastructure, backups, and security.

About

This a complete VPS build as docker container for local machine

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors