-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_proxy.sh
More file actions
133 lines (110 loc) · 4.03 KB
/
setup_proxy.sh
File metadata and controls
133 lines (110 loc) · 4.03 KB
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
#!/bin/bash
set -e
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
echo -e "${GREEN}Starting Proxy Setup...${NC}"
# 1. Load Environment Variables
if [ ! -f backend/.env ]; then
echo -e "${RED}Error: backend/.env file not found!${NC}"
exit 1
fi
echo "Loading configuration..."
export $(grep -v '^#' backend/.env | xargs)
if [ -z "$APP_URL" ]; then
echo -e "${YELLOW}Warning: APP_URL is not set in backend/.env${NC}"
echo "Please add APP_URL=https://yourdomain.com or APP_URL=http://localhost:3000 to backend/.env"
exit 1
fi
echo "APP_URL is set to: $APP_URL"
# Extract Domain and Protocol
PROTOCOL=$(echo $APP_URL | grep :// | sed -e's,^\(.*://\).*,\1,g')
URL_NO_PROTO=$(echo $APP_URL | sed -e s,$PROTOCOL,,g)
DOMAIN=$(echo $URL_NO_PROTO | cut -d/ -f1 | cut -d: -f1)
echo "Detected Domain: $DOMAIN"
# 2. Check if Localhost
if [ "$DOMAIN" = "localhost" ] || [ "$DOMAIN" = "127.0.0.1" ]; then
echo -e "${GREEN}APP_URL is localhost. Skipping Nginx/SSL setup.${NC}"
echo "You can access the application at $APP_URL"
exit 0
fi
# 3. Install Nginx and Certbot
echo -e "${GREEN}Installing Nginx and Certbot...${NC}"
sudo apt-get update
sudo apt-get install -y nginx certbot python3-certbot-nginx
# 4. Configure Nginx
echo -e "${GREEN}Configuring Nginx for $DOMAIN...${NC}"
CONFIG_FILE="/etc/nginx/sites-available/$DOMAIN"
# Create Nginx Config
sudo bash -c "cat > $CONFIG_FILE" <<EOF
server {
listen 80;
server_name $DOMAIN;
# Frontend Proxy
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;
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;
}
# Backend API Proxy
location /api {
proxy_pass http://localhost:5000;
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;
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;
}
# Webhook Proxy (for WhatsApp and other webhooks)
location /webhook {
proxy_pass http://localhost:5000;
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;
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;
}
}
EOF
# Enable Site
if [ ! -f "/etc/nginx/sites-enabled/$DOMAIN" ]; then
sudo ln -s $CONFIG_FILE /etc/nginx/sites-enabled/
fi
# Remove default if it exists
if [ -f "/etc/nginx/sites-enabled/default" ]; then
sudo rm /etc/nginx/sites-enabled/default
fi
# Test and Reload Nginx
echo "Testing Nginx configuration..."
sudo nginx -t
sudo systemctl reload nginx
# 5. Setup SSL with Let's Encrypt
echo -e "${GREEN}Setting up SSL with Let's Encrypt...${NC}"
echo "Running Certbot..."
# We use --nginx plugin.
# --non-interactive requires --agree-tos and --email.
# Since we don't have the user's email, we'll run it interactively if possible,
# or ask the user to run it manually if this script is automated.
# However, for a helper script, we can try to run it.
if sudo certbot --nginx -d $DOMAIN --register-unsafely-without-email --agree-tos --redirect; then
echo -e "${GREEN}SSL Certificate installed successfully!${NC}"
else
echo -e "${RED}Certbot failed. You may need to run it manually:${NC}"
echo "sudo certbot --nginx -d $DOMAIN"
fi
echo -e "${GREEN}Proxy setup complete!${NC}"
echo "Your application should now be accessible at https://$DOMAIN"