forked from weam-ai/weam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx_setup.sh
More file actions
135 lines (115 loc) · 4.16 KB
/
nginx_setup.sh
File metadata and controls
135 lines (115 loc) · 4.16 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
# WEAM Nginx Setup Script (Cross-Platform)
# Supports: Ubuntu, macOS, and Windows (Git Bash / WSL / MSYS)
set -e
echo "🚀 Starting WEAM Nginx Setup..."
# -------------------------------
# Step 1: Load environment variables
# -------------------------------
if [ -f .env ]; then
set -a
source .env
set +a
echo "✅ Loaded environment variables from .env"
else
echo "❌ .env file not found. Please create one with NEXT_PUBLIC_DOMAIN_URL"
exit 1
fi
if [ -z "$NEXT_PUBLIC_DOMAIN_URL" ]; then
echo "❌ NEXT_PUBLIC_DOMAIN_URL not found in .env file"
exit 1
fi
DOMAIN=$(echo "$NEXT_PUBLIC_DOMAIN_URL" | sed -E 's|^[a-zA-Z]+:/{0,2}||' | sed -E 's|[:/].*$||')
echo "🌐 Using domain: $DOMAIN"
# -------------------------------
# Step 2: Detect environment (local vs cloud)
# -------------------------------
echo "🔍 Detecting environment..."
if curl -s --connect-timeout 1 http://169.254.169.254/ >/dev/null 2>&1; then
ENVIRONMENT_TYPE="cloud"
echo "☁️ Environment: Cloud Platform"
echo "ℹ️ Cloud environment detected - nginx setup will be skipped"
echo "✅ Cloud setup complete (no nginx configuration needed)"
exit 0
else
ENVIRONMENT_TYPE="local"
echo "🏠 Environment: Local"
fi
# -------------------------------
# Step 3: Detect OS
# -------------------------------
OS_TYPE="$(uname -s)"
echo "💻 Detected OS: $OS_TYPE"
# Default values
HOST_ENTRY="127.0.0.1 $DOMAIN"
# -------------------------------
# Step 4: Add domain entry to hosts file
# -------------------------------
echo "🌐 Adding host entry for $DOMAIN..."
if [[ "$OS_TYPE" == "Linux" ]]; then
HOSTS_FILE="/etc/hosts"
elif [[ "$OS_TYPE" == "Darwin" ]]; then
HOSTS_FILE="/etc/hosts"
elif [[ "$OS_TYPE" =~ MINGW|MSYS|CYGWIN ]]; then
# Windows (Git Bash, MSYS, or WSL)
HOSTS_FILE="/c/Windows/System32/drivers/etc/hosts"
else
echo "⚠️ Unsupported OS: $OS_TYPE"
exit 1
fi
# Check if domain already exists
if grep -qE "^[^#]*\b$DOMAIN\b" "$HOSTS_FILE"; then
echo "✅ Host entry for '$DOMAIN' already exists in $HOSTS_FILE"
else
echo "📝 Adding host entry to $HOSTS_FILE..."
if [[ "$OS_TYPE" =~ MINGW|MSYS|CYGWIN ]]; then
# Windows needs admin rights; use PowerShell if possible
powershell.exe -Command "Start-Process cmd -Verb runAs -ArgumentList '/c echo $HOST_ENTRY >> C:\\Windows\\System32\\drivers\\etc\\hosts'" || {
echo "⚠️ Failed to auto-edit hosts file. Please manually add this line:"
echo " $HOST_ENTRY"
}
else
# Linux or macOS
if echo "$HOST_ENTRY" | sudo tee -a "$HOSTS_FILE" >/dev/null; then
echo "✅ Added $HOST_ENTRY to $HOSTS_FILE"
else
echo "❌ Failed to add host entry. Run this manually:"
echo " sudo sh -c 'echo \"$HOST_ENTRY\" >> $HOSTS_FILE'"
exit 1
fi
fi
fi
# -------------------------------
# Step 5: Stop and remove existing nginx container
# -------------------------------
echo "🛑 Stopping existing nginx container..."
docker stop weam-nginx 2>/dev/null || true
docker rm weam-nginx 2>/dev/null || true
# -------------------------------
# Step 5.5: Ensure Docker network exists
# -------------------------------
# echo "🔧 Ensuring Docker network 'weam_app-network' exists..."
# if ! docker network inspect weam_app-network >/dev/null 2>&1; then
# echo "🛠️ Creating Docker network 'weam_app-network'..."
# docker network create weam_app-network
# echo "✅ Docker network 'weam_app-network' created"
# else
# echo "✅ Docker network 'weam_app-network' already exists"
# fi
# -------------------------------
# Step 6: Build and run nginx container (local only)
# -------------------------------
if [ "$ENVIRONMENT_TYPE" = "local" ]; then
echo "🐳 Building nginx Docker image..."
docker build -t weam-nginx:latest ./nginx
echo "🚀 Starting nginx container..."
docker run -d \
--name weam-nginx \
--network weam_app-network \
-p 80:80 \
-p 443:443 \
-e DOMAIN_NAME="$DOMAIN" \
weam-nginx:latest
echo "✅ Local nginx setup completed successfully!"
fi
echo "🎉 Setup Finished!"