-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeploy_https.sh
executable file
·124 lines (99 loc) · 3.38 KB
/
deploy_https.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
#!/bin/bash
# Step 1: Ask For input
# require sudo?
# domain_name - Domain
# challenge_ip - Challenge VPS IP
read -p "Does your VPS Require sudo(yes/no): " require_sudo
if [ "$require_sudo" == "yes" ]; then
sudo_needed="sudo"
elif [ "$require_sudo" == "no" ]; then
sudo_needed=""
else
echo "Invalid input!"
exit 1
fi
read -p "Enter your domain name: " domain_name
read -p "Enter your challenge server IP: " challenge_ip
# STEP 2: Generating Random 32 Bytes SECRET_KEY
echo "Generating 32-byte hex secret..."
secret_key=$(openssl rand -hex 32)
backend_env="./Backend/.env"
echo "SECRET_KEY=$secret_key" > "$backend_env"
echo "Secret key saved in $backend_env"
# Step 3: Run docker-compose up in detached mode for Backend
echo "Starting Docker containers..."
docker-compose down
docker-compose up -d --build
# Step 4: Changing the .env for Frontend and preparing for Nginx
frontend_env="./Frontend/.env"
echo "VITE_BACKEND_IP='https://$domain_name/api'" > "$frontend_env"
cd ./Frontend
python3 changer.py $challenge_ip
chmod 777 ./install.sh
./install.sh
cp -r ./dist/* /var/www/$domain_name/html
cd ..
# Step 5: Get the IP address of the 'react-casino-heist_web' container
container_id=$(docker ps --filter "ancestor=react-casino-heist_web" --format "{{.ID}}")
if [ -z "$container_id" ]; then
echo "No running container found with image 'react-casino-heist_web'."
exit 1
fi
echo "Container ID found: $container_id"
container_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$container_id")
echo "$container_id has the IP of: $container_ip"
if [ -z "$container_ip" ]; then
echo "Failed to get container IP. Make sure the container is running."
exit 1
fi
echo "Container IP for $container_name: $container_ip"
# Step 6: Create the Nginx configuration file
nginx_config_path="/etc/nginx/sites-available/$domain_name"
cat > "$nginx_config_path" <<EOL
server {
root /var/www/${domain_name}/html;
index index.html index.htm index.nginx-debian.html;
server_name ${domain_name} www.${domain_name};
location / {
try_files \$uri \$uri/ /index.html;
}
location /api/ {
proxy_pass http://${container_ip}:8000;
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;
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/${domain_name}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/${domain_name}/privkey.pem;
error_page 404 /index.html;
}
server {
if (\$host = www.${domain_name}) {
return 301 https://\$host\$request_uri;
}
if (\$host = ${domain_name}) {
return 301 https://\$host\$request_uri;
}
listen 80;
listen [::]:80;
server_name ${domain_name} www.${domain_name};
return 404;
}
EOL
# Step 7: Create symlink to enable the site
rm -rf /etc/nginx/sites-enabled/$domain_name
ln -s "$nginx_config_path" /etc/nginx/sites-enabled/
# Step 8: Test NGINX configuration
nginx -t
if [ $? -ne 0 ]; then
echo "NGINX configuration test failed. Please check the config."
exit 1
fi
# Step 9: Restart NGINX service
echo "Restarting Nginx..."
$sudo_needed service nginx restart
echo "Deployment completed successfully!"
echo "Your website is deployed on https://$domain_name"