Server Deployment Commands - Complete Guide
Published: September 25, 2024 | Reading time: 22 minutes
Deployment Commands Overview
Master server deployment with essential commands for different platforms and scenarios:
Deployment Categories
# Server Management
- Linux system commands
- Service management
- Process monitoring
# Application Deployment
- Docker deployment
- CI/CD pipelines
- Cloud platform commands
# Database Deployment
- Database setup
- Migration commands
- Backup operations
Linux Server Management
System Information
System Commands
# System information
uname -a # Kernel information
lsb_release -a # Distribution information
cat /etc/os-release # OS release details
hostnamectl # Hostname and system info
# Hardware information
lscpu # CPU information
free -h # Memory usage
df -h # Disk usage
lsblk # Block devices
lspci # PCI devices
lsusb # USB devices
# System resources
top # Real-time process monitor
htop # Enhanced process monitor
iotop # I/O monitor
nethogs # Network usage by process
iftop # Network traffic monitor
Service Management
Service Commands
# Systemctl commands
systemctl status nginx # Check service status
systemctl start nginx # Start service
systemctl stop nginx # Stop service
systemctl restart nginx # Restart service
systemctl reload nginx # Reload configuration
systemctl enable nginx # Enable auto-start
systemctl disable nginx # Disable auto-start
# Service management
systemctl list-units # List all units
systemctl list-units --type=service # List services
systemctl list-units --failed # List failed units
systemctl daemon-reload # Reload systemd configuration
# Legacy service commands
service nginx status # Check status
service nginx start # Start service
service nginx stop # Stop service
service nginx restart # Restart service
Package Management
APT (Ubuntu/Debian)
APT Commands
# Update package lists
sudo apt update
# Upgrade packages
sudo apt upgrade
sudo apt full-upgrade
# Install packages
sudo apt install nginx
sudo apt install nginx=1.18.0-0ubuntu1
# Remove packages
sudo apt remove nginx
sudo apt purge nginx
# Search packages
apt search nginx
apt show nginx
# List packages
apt list --installed
apt list --upgradable
# Clean up
sudo apt autoremove
sudo apt autoclean
sudo apt clean
# Add repositories
sudo add-apt-repository ppa:nginx/stable
sudo apt update
YUM/DNF (CentOS/RHEL)
YUM/DNF Commands
# Update packages
sudo yum update
sudo dnf update
# Install packages
sudo yum install nginx
sudo dnf install nginx
# Remove packages
sudo yum remove nginx
sudo dnf remove nginx
# Search packages
yum search nginx
dnf search nginx
# List packages
yum list installed
dnf list installed
# Clean up
sudo yum clean all
sudo dnf clean all
# Add repositories
sudo yum install epel-release
sudo dnf install epel-release
Docker Deployment
Container Deployment
Docker Commands
# Build and run application
docker build -t myapp:latest .
docker run -d -p 3000:3000 --name myapp myapp:latest
# Docker Compose deployment
docker-compose up -d
docker-compose down
docker-compose restart
# Production deployment
docker run -d \
--name myapp \
--restart unless-stopped \
-p 80:3000 \
-e NODE_ENV=production \
-v /var/log/myapp:/app/logs \
myapp:latest
# Health checks
docker ps
docker logs myapp
docker stats myapp
# Update deployment
docker pull myapp:latest
docker stop myapp
docker rm myapp
docker run -d -p 3000:3000 --name myapp myapp:latest
Docker Swarm
Swarm Commands
# Initialize swarm
docker swarm init
# Join swarm
docker swarm join --token SWMTKN-1-xxx 192.168.1.100:2377
# Deploy service
docker service create \
--name web \
--replicas 3 \
--publish 80:80 \
nginx:latest
# Scale service
docker service scale web=5
# Update service
docker service update --image nginx:1.19 web
# List services
docker service ls
docker service ps web
# Remove service
docker service rm web
CI/CD Deployment
GitHub Actions
GitHub Actions Workflow
# .github/workflows/deploy.yml
name: Deploy to Production
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build application
run: npm run build
- name: Deploy to server
uses: appleboy/ssh-action@v0.1.5
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/myapp
git pull origin main
npm ci --production
npm run build
sudo systemctl restart myapp
GitLab CI/CD
GitLab CI Pipeline
# .gitlab-ci.yml
stages:
- test
- build
- deploy
variables:
NODE_VERSION: "18"
test:
stage: test
image: node:$NODE_VERSION
script:
- npm ci
- npm test
only:
- main
- develop
build:
stage: build
image: node:$NODE_VERSION
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
only:
- main
deploy:
stage: deploy
image: alpine:latest
before_script:
- apk add --no-cache openssh-client
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
script:
- ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "cd /var/www/myapp && git pull origin main"
- scp -o StrictHostKeyChecking=no -r dist/* $SSH_USER@$SSH_HOST:/var/www/myapp/dist/
- ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "sudo systemctl restart myapp"
only:
- main
Cloud Platform Deployment
AWS Deployment
AWS CLI Commands
# Configure AWS CLI
aws configure
# Deploy to EC2
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--count 1 \
--instance-type t3.micro \
--key-name my-key \
--security-group-ids sg-12345678
# Deploy to ECS
aws ecs create-cluster --cluster-name my-cluster
aws ecs register-task-definition --cli-input-json file://task-definition.json
aws ecs create-service \
--cluster my-cluster \
--service-name my-service \
--task-definition my-task:1 \
--desired-count 3
# Deploy to Lambda
aws lambda create-function \
--function-name my-function \
--runtime nodejs18.x \
--role arn:aws:iam::123456789012:role/lambda-role \
--handler index.handler \
--zip-file fileb://function.zip
# Deploy to S3
aws s3 sync ./dist s3://my-bucket --delete
aws s3 website s3://my-bucket --index-document index.html
DigitalOcean Deployment
DigitalOcean CLI
# Create droplet
doctl compute droplet create my-server \
--size s-1vcpu-1gb \
--image ubuntu-20-04-x64 \
--region nyc1 \
--ssh-keys 12345678
# List droplets
doctl compute droplet list
# Create load balancer
doctl compute load-balancer create \
--name my-lb \
--forwarding-rules entry_protocol:http,entry_port:80,target_protocol:http,target_port:3000 \
--health-check protocol:http,port:3000,path:/health
# Deploy app
doctl apps create-deployment my-app-id
# Update app
doctl apps update-deployment my-app-id --force-rebuild
Database Deployment
MySQL Deployment
MySQL Commands
# Install MySQL
sudo apt install mysql-server
sudo mysql_secure_installation
# Start MySQL service
sudo systemctl start mysql
sudo systemctl enable mysql
# Create database and user
mysql -u root -p
CREATE DATABASE myapp;
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';
FLUSH PRIVILEGES;
# Run migrations
mysql -u myapp -p myapp < migrations/001_initial.sql
mysql -u myapp -p myapp < migrations/002_add_users.sql
# Backup database
mysqldump -u myapp -p myapp > backup_$(date +%Y%m%d_%H%M%S).sql
# Restore database
mysql -u myapp -p myapp < backup_20241225_120000.sql
PostgreSQL Deployment
PostgreSQL Commands
# Install PostgreSQL
sudo apt install postgresql postgresql-contrib
# Start PostgreSQL service
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Create database and user
sudo -u postgres psql
CREATE DATABASE myapp;
CREATE USER myapp WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE myapp TO myapp;
\q
# Run migrations
psql -U myapp -d myapp -f migrations/001_initial.sql
psql -U myapp -d myapp -f migrations/002_add_users.sql
# Backup database
pg_dump -U myapp myapp > backup_$(date +%Y%m%d_%H%M%S).sql
# Restore database
psql -U myapp -d myapp < backup_20241225_120000.sql
Web Server Configuration
Nginx Deployment
Nginx Commands
# Install Nginx
sudo apt install nginx
# Start Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Configure site
sudo nano /etc/nginx/sites-available/myapp
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
# SSL with Let's Encrypt
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d myapp.com
sudo certbot renew --dry-run
# Nginx configuration example
server {
listen 80;
server_name myapp.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;
}
}
Apache Deployment
Apache Commands
# Install Apache
sudo apt install apache2
# Start Apache
sudo systemctl start apache2
sudo systemctl enable apache2
# Enable modules
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod proxy_http
# Configure site
sudo nano /etc/apache2/sites-available/myapp.conf
sudo a2ensite myapp.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
# SSL with Let's Encrypt
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d myapp.com
# Apache configuration example
ServerName myapp.com
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
Monitoring and Logging
System Monitoring
Monitoring Commands
# System monitoring
htop # Process monitor
iotop # I/O monitor
nethogs # Network monitor
iftop # Network traffic
# Log monitoring
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
journalctl -f -u nginx
journalctl -f -u myapp
# System logs
tail -f /var/log/syslog
tail -f /var/log/auth.log
tail -f /var/log/kern.log
# Application logs
tail -f /var/log/myapp/app.log
tail -f /var/log/myapp/error.log
# Log rotation
sudo logrotate -f /etc/logrotate.conf
sudo logrotate -d /etc/logrotate.d/myapp
Security and Firewall
UFW Firewall
Firewall Commands
# Install UFW
sudo apt install ufw
# Enable firewall
sudo ufw enable
# Allow SSH
sudo ufw allow ssh
sudo ufw allow 22
# Allow HTTP/HTTPS
sudo ufw allow 80
sudo ufw allow 443
# Allow specific port
sudo ufw allow 3000
# Allow from specific IP
sudo ufw allow from 192.168.1.100
# Deny port
sudo ufw deny 8080
# List rules
sudo ufw status
sudo ufw status verbose
# Reset firewall
sudo ufw --force reset
SSH Security
SSH Configuration
# SSH configuration
sudo nano /etc/ssh/sshd_config
# Key security settings
Port 2222
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
X11Forwarding no
MaxAuthTries 3
# Restart SSH
sudo systemctl restart ssh
# Generate SSH key
ssh-keygen -t ed25519 -C "user@server"
# Copy public key to server
ssh-copy-id -p 2222 user@server
# Test SSH connection
ssh -p 2222 user@server
Deployment Scripts
Automated Deployment
Deployment Script
#!/bin/bash
# deploy.sh - Automated deployment script
set -e
# Configuration
APP_NAME="myapp"
APP_DIR="/var/www/$APP_NAME"
BACKUP_DIR="/var/backups/$APP_NAME"
REPO_URL="https://github.com/user/$APP_NAME.git"
BRANCH="main"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
exit 1
}
warn() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Create backup
create_backup() {
log "Creating backup..."
local backup_name="${APP_NAME}_$(date +%Y%m%d_%H%M%S)"
if [ -d "$APP_DIR" ]; then
cp -r "$APP_DIR" "$BACKUP_DIR/$backup_name"
log "Backup created: $backup_name"
else
warn "No existing application directory to backup"
fi
}
# Deploy application
deploy() {
log "Starting deployment..."
# Create application directory if it doesn't exist
mkdir -p "$APP_DIR"
cd "$APP_DIR"
# Clone or update repository
if [ -d ".git" ]; then
log "Updating existing repository..."
git fetch origin
git reset --hard "origin/$BRANCH"
else
log "Cloning repository..."
git clone "$REPO_URL" .
fi
# Install dependencies
if [ -f "package.json" ]; then
log "Installing Node.js dependencies..."
npm ci --production
fi
# Set permissions
chown -R www-data:www-data "$APP_DIR"
chmod -R 755 "$APP_DIR"
log "Deployment completed successfully"
}
# Restart services
restart_services() {
log "Restarting services..."
if systemctl is-active --quiet nginx; then
systemctl reload nginx
log "Nginx reloaded"
fi
if systemctl is-active --quiet "$APP_NAME"; then
systemctl restart "$APP_NAME"
log "Application service restarted"
fi
}
# Main deployment function
main() {
log "Starting deployment of $APP_NAME"
create_backup
deploy
restart_services
log "Deployment completed successfully"
}
# Run main function
main "$@"
Summary
Master server deployment with these essential command categories:
- System Management: service control, package management, system monitoring
- Container Deployment: Docker, Docker Compose, container orchestration
- CI/CD Pipelines: GitHub Actions, GitLab CI, automated deployment
- Cloud Platforms: AWS, DigitalOcean, cloud-specific commands
- Database Deployment: MySQL, PostgreSQL, migrations, backups
- Web Servers: Nginx, Apache, SSL configuration
- Security: Firewall, SSH, security hardening
- Automation: Deployment scripts, monitoring, logging
Need More Help?
Struggling with server deployment or need help automating your deployment process? Our deployment experts can help you implement robust, scalable deployment strategies.
Get Deployment Help