๐ฅ๏ธ Server Management Commands - Cheat Sheet
๐ Quick Status Check (Run This First!)
# Check all services at once
sudo systemctl status shipbot nginx postgresql
# Check what's listening on your ports
sudo ss -tlnp | grep -E "(80|5002|5432)"
๐ง Service Management
Service Controls
# Start the service
sudo systemctl start shipbot
# Stop the service
sudo systemctl stop shipbot
# Restart the service
sudo systemctl restart shipbot
# Check service status
sudo systemctl status shipbot
# Enable auto-start on boot
sudo systemctl enable shipbot
# Disable auto-start
sudo systemctl disable shipbot
View Logs
# Live log monitoring (Ctrl+C to exit)
sudo journalctl -u shipbot -f
# View last 50 log entries
sudo journalctl -u shipbot -n 50
# View logs with timestamps
sudo journalctl -u shipbot --no-pager -l
# View today's logs only
sudo journalctl -u shipbot --since today
๐ Nginx Web Server
Nginx Controls
# Start Nginx
sudo systemctl start nginx
# Stop Nginx
sudo systemctl stop nginx
# Restart Nginx
sudo systemctl restart nginx
# Reload config (no downtime)
sudo systemctl reload nginx
# Check Nginx status
sudo systemctl status nginx
# Test Nginx configuration
sudo nginx -t
Nginx Files
# Edit main site config
sudo nano /etc/nginx/sites-available/shipbot
# Check enabled sites
ls -la /etc/nginx/sites-enabled/
# View Nginx error logs
sudo tail -f /var/log/nginx/error.log
# View Nginx access logs
sudo tail -f /var/log/nginx/access.log
๐๏ธ PostgreSQL Database
Database Controls
# Start PostgreSQL
sudo systemctl start postgresql
# Stop PostgreSQL
sudo systemctl stop postgresql
# Restart PostgreSQL
sudo systemctl restart postgresql
# Check PostgreSQL status
sudo systemctl status postgresql
Database Access
# Connect as postgres superuser
sudo -u postgres psql
# Connect to specific database
sudo -u postgres psql -d database_name
# Connect as specific user
psql -h localhost -U username -d database_name
# Check database connections
sudo -u postgres psql -c "SELECT * FROM pg_stat_activity;"
Common Database Queries
-- Inside psql:
\l -- List all databases
\c database_name -- Connect to database
\dt -- List tables
\du -- List users
SELECT * FROM table; -- View table data
\q -- Quit psql
๐ File Management
Navigate to App
# Go to app directory
cd /root/shipbot
# Activate virtual environment
source venv/bin/activate
# List files
ls -la
# Check current directory
pwd
Edit Configuration Files
# Edit environment variables
nano .env
# Edit main application
nano ship_worker.py
# Edit systemd service file
sudo nano /etc/systemd/system/shipbot.service
# Edit Nginx config
sudo nano /etc/nginx/sites-available/shipbot
File Permissions
# Check file permissions
ls -la filename.py
# Make file executable
chmod +x filename.py
# Change ownership
sudo chown user:group filename.py
๐งช Testing & Debugging
Test Application Manually
cd /root/shipbot
source venv/bin/activate
# Test Python syntax
python3 -m py_compile ship_worker.py
# Run manually (Ctrl+C to stop)
python3 ship_worker.py
# Run with Gunicorn manually
gunicorn --bind 0.0.0.0:5002 ship_worker:app
Test Endpoints
# Test direct app (port 5002)
curl -X POST http://your-server-ip:5002/inbound \
-d "sender=test@example.com" \
-d "subject=Test" \
-d "body-plain=Hello test"
# Test through Nginx (port 80)
curl -X POST http://your-server-ip/inbound \
-d "sender=test@example.com" \
-d "subject=Test" \
-d "body-plain=Hello test"
Check Environment Variables
cd /root/shipbot
source venv/bin/activate
# Test if .env loads properly
python3 -c "
from dotenv import load_dotenv
import os
load_dotenv()
print('DATABASE_URL:', 'SET' if os.getenv('DATABASE_URL') else 'NOT SET')
print('API_KEY:', 'SET' if os.getenv('OPENAI_API_KEY') else 'NOT SET')
"
๐ Full Service Restart Sequence
When You Make Changes
# 1. Navigate to app directory
cd /root/shipbot
# 2. Test your changes
python3 -m py_compile ship_worker.py
# 3. If service file changed, reload systemd
sudo systemctl daemon-reload
# 4. Restart the service
sudo systemctl restart shipbot
# 5. Check if it started successfully
sudo systemctl status shipbot
# 6. Watch logs for any errors
sudo journalctl -u shipbot -f
๐จ Emergency Troubleshooting
App Won't Start
# 1. Check the service status
sudo systemctl status shipbot
# 2. View detailed logs
sudo journalctl -u shipbot -n 50
# 3. Try running manually to see errors
cd /root/shipbot
source venv/bin/activate
python3 ship_worker.py
# 4. Check for syntax errors
python3 -m py_compile ship_worker.py
Database Connection Issues
# 1. Check if PostgreSQL is running
sudo systemctl status postgresql
# 2. Test database connection
psql -h localhost -U username -d database_name
# 3. Check database logs
sudo tail -f /var/log/postgresql/postgresql-*-main.log
Nginx 502 Bad Gateway
# 1. Check if app service is running
sudo systemctl status shipbot
# 2. Check if app is listening on port
sudo ss -tlnp | grep 5002
# 3. Test direct connection to app
curl http://localhost:5002
# 4. Check Nginx error logs
sudo tail -f /var/log/nginx/error.log
๐ Monitoring Commands
System Resources
# Check disk space
df -h
# Check memory usage
free -h
# Check CPU usage
top
# Check running processes
ps aux | grep -E "(gunicorn|nginx|postgres)"
Port Monitoring
# Check all listening ports
sudo ss -tlnp
# Check specific ports
sudo ss -tlnp | grep -E "(80|5002|5432)"
# Check active connections
sudo ss -tuln
๐พ Backup Commands
Backup Database
# Create database backup
sudo -u postgres pg_dump database_name > backup_$(date +%Y%m%d).sql
# Restore from backup
sudo -u postgres psql database_name < backup_20250706.sql
Backup Configuration
# Backup important files
cp .env .env.backup
cp ship_worker.py ship_worker.py.backup
sudo cp /etc/systemd/system/shipbot.service /root/shipbot.service.backup
๐ฑ Quick Health Check
# Run this to check everything is working:
echo "=== SERVER HEALTH CHECK ==="
sudo systemctl is-active shipbot nginx postgresql
sudo ss -tlnp | grep -E "(80|5002|5432)"
curl -s http://localhost:5002 && echo "App responding" || echo "App not responding"