-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
154 lines (120 loc) · 4.7 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
# Env Vars
DOMAIN_NAME="drewpledger.com" # replace with your own
EMAIL="[email protected]" # replace with your own
MAKESWIFT_SITE_API_KEY=24a9b2ff-e6f3-45e4-a756-d63b5d991545
# Script Vars
REPO_URL="https://github.com/makeswift/digital-ocean.git"
APP_DIR=~/myapp
SWAP_SIZE="1G" # Swap size of 1GB
# Update package list and upgrade existing packages
sudo apt update && sudo apt upgrade -y
# Add Swap Space
echo "Adding swap space..."
sudo fallocate -l $SWAP_SIZE /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make swap permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Install Docker
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" -y
sudo apt update
sudo apt install docker-ce -y
# Install Docker Compose
sudo rm -f /usr/local/bin/docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Wait for the file to be fully downloaded before proceeding
if [ ! -f /usr/local/bin/docker-compose ]; then
echo "Docker Compose download failed. Exiting."
exit 1
fi
sudo chmod +x /usr/local/bin/docker-compose
# Ensure Docker Compose is executable and in path
sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
# Verify Docker Compose installation
docker-compose --version
if [ $? -ne 0 ]; then
echo "Docker Compose installation failed. Exiting."
exit 1
fi
# Ensure Docker starts on boot and start Docker service
sudo systemctl enable docker
sudo systemctl start docker
# Clone the Git repository
if [ -d "$APP_DIR" ]; then
echo "Directory $APP_DIR already exists. Pulling latest changes..."
cd $APP_DIR && git pull
else
echo "Cloning repository from $REPO_URL..."
git clone $REPO_URL $APP_DIR
cd $APP_DIR
fi
echo "MAKESWIFT_SITE_API_KEY=$MAKESWIFT_SITE_API_KEY" >> "$APP_DIR/.env"
# Install Nginx
sudo apt install nginx -y
# Remove old Nginx config (if it exists)
sudo rm -f /etc/nginx/sites-available/myapp
sudo rm -f /etc/nginx/sites-enabled/myapp
# Stop Nginx temporarily to allow Certbot to run in standalone mode
sudo systemctl stop nginx
# Obtain SSL certificate using Certbot standalone mode
sudo apt install certbot -y
sudo certbot certonly --standalone -d $DOMAIN_NAME --non-interactive --agree-tos -m $EMAIL
# Ensure SSL files exist or generate them
if [ ! -f /etc/letsencrypt/options-ssl-nginx.conf ]; then
sudo wget https://raw.githubusercontent.com/certbot/certbot/main/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf -P /etc/letsencrypt/
fi
if [ ! -f /etc/letsencrypt/ssl-dhparams.pem ]; then
sudo openssl dhparam -out /etc/letsencrypt/ssl-dhparams.pem 2048
fi
# Create Nginx config with reverse proxy, SSL support, rate limiting, and streaming support
sudo cat > /etc/nginx/sites-available/myapp <<EOL
limit_req_zone \$binary_remote_addr zone=mylimit:20m rate=10r/s;
server {
listen 80;
server_name $DOMAIN_NAME;
# Redirect all HTTP requests to HTTPS
return 301 https://\$host\$request_uri;
}
server {
listen 443 ssl;
server_name $DOMAIN_NAME;
ssl_certificate /etc/letsencrypt/live/$DOMAIN_NAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN_NAME/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# Enable rate limiting
limit_req zone=mylimit burst=20 nodelay;
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_cache_bypass \$http_upgrade;
# Disable buffering for streaming support
proxy_buffering off;
proxy_set_header X-Accel-Buffering no;
}
}
EOL
# Create symbolic link if it doesn't already exist
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/myapp
# Restart Nginx to apply the new configuration
sudo systemctl restart nginx
# Build and run the Docker containers from the app directory (~/myapp)
cd $APP_DIR
sudo docker-compose up --build -d
# Check if Docker Compose started correctly
if ! sudo docker-compose ps | grep "Up"; then
echo "Docker containers failed to start. Check logs with 'docker-compose logs'."
exit 1
fi
# Output final message
echo "Deployment complete. Your Next.js app is now running.
Next.js is available at https://$DOMAIN_NAME.
The .env file has been created with the following values:
- MAKESWIFT_SITE_API_KEY