1+ #! /bin/bash
2+
3+ # ChittyPro Streamlink Server Setup Script
4+ # For Ubuntu/Debian servers (including derail.me)
5+ # Run this script on your production server to prepare for CI/CD deployment
6+
7+ set -e
8+
9+ echo " 🚀 Setting up ChittyPro Streamlink production server..."
10+
11+ # Update system packages
12+ echo " 📦 Updating system packages..."
13+ sudo apt update && sudo apt upgrade -y
14+
15+ # Install Node.js 20.x
16+ echo " 🟢 Installing Node.js 20.x..."
17+ curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
18+ sudo apt-get install -y nodejs
19+
20+ # Install PostgreSQL
21+ echo " 🐘 Installing PostgreSQL..."
22+ sudo apt install -y postgresql postgresql-contrib
23+
24+ # Install Nginx
25+ echo " 🌐 Installing Nginx..."
26+ sudo apt install -y nginx
27+
28+ # Install PM2 globally
29+ echo " ⚡ Installing PM2..."
30+ sudo npm install -g pm2
31+
32+ # Install FFmpeg for streaming
33+ echo " 🎥 Installing FFmpeg..."
34+ sudo apt install -y ffmpeg
35+
36+ # Install additional tools
37+ echo " 🔧 Installing additional tools..."
38+ sudo apt install -y git curl wget htop ufw
39+
40+ # Create deploy user if it doesn't exist
41+ if ! id " deploy" & > /dev/null; then
42+ echo " 👤 Creating deploy user..."
43+ sudo adduser --disabled-password --gecos " " deploy
44+ sudo usermod -aG sudo deploy
45+
46+ # Set up SSH directory for deploy user
47+ sudo -u deploy mkdir -p /home/deploy/.ssh
48+ sudo -u deploy chmod 700 /home/deploy/.ssh
49+ sudo -u deploy touch /home/deploy/.ssh/authorized_keys
50+ sudo -u deploy chmod 600 /home/deploy/.ssh/authorized_keys
51+
52+ echo " ✅ Deploy user created. Add your public SSH key to /home/deploy/.ssh/authorized_keys"
53+ else
54+ echo " ✅ Deploy user already exists"
55+ fi
56+
57+ # Create application directory
58+ echo " 📁 Setting up application directory..."
59+ sudo mkdir -p /var/www/chittypro-streamlink
60+ sudo chown deploy:deploy /var/www/chittypro-streamlink
61+
62+ # Set up PostgreSQL database
63+ echo " 🗄️ Setting up PostgreSQL database..."
64+ sudo -u postgres psql -c " CREATE DATABASE chittypro_streamlink;" || echo " Database may already exist"
65+ sudo -u postgres psql -c " CREATE USER streamlink WITH PASSWORD 'streamlink123';" || echo " User may already exist"
66+ sudo -u postgres psql -c " GRANT ALL PRIVILEGES ON DATABASE chittypro_streamlink TO streamlink;"
67+
68+ # Configure Nginx
69+ echo " 🌍 Configuring Nginx..."
70+ sudo tee /etc/nginx/sites-available/chittypro-streamlink > /dev/null << EOF
71+ server {
72+ listen 80;
73+ server_name derail.me www.derail.me;
74+
75+ # Redirect HTTP to HTTPS
76+ return 301 https://\$ server_name\$ request_uri;
77+ }
78+
79+ server {
80+ listen 443 ssl http2;
81+ server_name derail.me www.derail.me;
82+
83+ # SSL configuration (Let's Encrypt certificates)
84+ ssl_certificate /etc/letsencrypt/live/derail.me/fullchain.pem;
85+ ssl_certificate_key /etc/letsencrypt/live/derail.me/privkey.pem;
86+ ssl_protocols TLSv1.2 TLSv1.3;
87+ ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
88+ ssl_prefer_server_ciphers off;
89+
90+ # Security headers
91+ add_header X-Frame-Options DENY;
92+ add_header X-Content-Type-Options nosniff;
93+ add_header X-XSS-Protection "1; mode=block";
94+ add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
95+
96+ # Serve static files
97+ location /assets/ {
98+ alias /var/www/chittypro-streamlink/dist/client/assets/;
99+ expires 1y;
100+ add_header Cache-Control "public, immutable";
101+ }
102+
103+ # WebSocket proxy for streaming
104+ location /ws {
105+ proxy_pass http://localhost:3000;
106+ proxy_http_version 1.1;
107+ proxy_set_header Upgrade \$ http_upgrade;
108+ proxy_set_header Connection 'upgrade';
109+ proxy_set_header Host \$ host;
110+ proxy_set_header X-Real-IP \$ remote_addr;
111+ proxy_set_header X-Forwarded-For \$ proxy_add_x_forwarded_for;
112+ proxy_set_header X-Forwarded-Proto \$ scheme;
113+ proxy_cache_bypass \$ http_upgrade;
114+ }
115+
116+ # API routes
117+ location /api/ {
118+ proxy_pass http://localhost:3000;
119+ proxy_set_header Host \$ host;
120+ proxy_set_header X-Real-IP \$ remote_addr;
121+ proxy_set_header X-Forwarded-For \$ proxy_add_x_forwarded_for;
122+ proxy_set_header X-Forwarded-Proto \$ scheme;
123+ }
124+
125+ # Frontend app
126+ location / {
127+ proxy_pass http://localhost:3000;
128+ proxy_set_header Host \$ host;
129+ proxy_set_header X-Real-IP \$ remote_addr;
130+ proxy_set_header X-Forwarded-For \$ proxy_add_x_forwarded_for;
131+ proxy_set_header X-Forwarded-Proto \$ scheme;
132+ }
133+ }
134+ EOF
135+
136+ # Enable the site
137+ sudo ln -sf /etc/nginx/sites-available/chittypro-streamlink /etc/nginx/sites-enabled/
138+ sudo nginx -t
139+
140+ # Install Certbot for Let's Encrypt SSL
141+ echo " 🔒 Installing Certbot for SSL certificates..."
142+ sudo apt install -y certbot python3-certbot-nginx
143+
144+ # Configure firewall
145+ echo " 🔥 Configuring UFW firewall..."
146+ sudo ufw allow ssh
147+ sudo ufw allow ' Nginx Full'
148+ sudo ufw --force enable
149+
150+ # Set up PM2 startup
151+ echo " 🔄 Configuring PM2 startup..."
152+ sudo pm2 startup systemd -u deploy --hp /home/deploy
153+ sudo systemctl enable pm2-deploy
154+
155+ # Create environment file template
156+ echo " 📝 Creating environment file template..."
157+ sudo -u deploy tee /var/www/chittypro-streamlink/.env.example > /dev/null << EOF
158+ # Production Environment Variables
159+ NODE_ENV=production
160+ PORT=3000
161+
162+ # Database
163+ DATABASE_URL=postgresql://streamlink:streamlink123@localhost:5432/chittypro_streamlink
164+
165+ # Authentication
166+ JWT_SECRET=your-jwt-secret-here
167+ SESSION_SECRET=your-session-secret-here
168+
169+ # Security
170+ ALLOWED_ORIGINS=https://derail.me,https://www.derail.me
171+
172+ # Optional: Google Drive Integration
173+ GDRIVE_CLIENT_ID=
174+ GDRIVE_CLIENT_SECRET=
175+ GDRIVE_REFRESH_TOKEN=
176+ GDRIVE_FOLDER_ID=
177+ EOF
178+
179+ # Create log directories
180+ echo " 📋 Setting up log directories..."
181+ sudo mkdir -p /var/log/chittypro-streamlink
182+ sudo chown deploy:deploy /var/log/chittypro-streamlink
183+
184+ echo " ✅ Server setup complete!"
185+ echo " "
186+ echo " 🔑 Next steps:"
187+ echo " 1. Add your SSH public key to /home/deploy/.ssh/authorized_keys"
188+ echo " 2. Configure environment variables in /var/www/chittypro-streamlink/.env"
189+ echo " 3. Obtain SSL certificate: sudo certbot --nginx -d derail.me -d www.derail.me"
190+ echo " 4. Test SSH access: ssh deploy@derail.me"
191+ echo " 5. Configure GitHub secrets and push to deploy"
192+ echo " "
193+ echo " 🌍 Your server is ready for ChittyPro Streamlink deployment!"
0 commit comments