-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
Β·122 lines (106 loc) Β· 3.66 KB
/
setup.sh
File metadata and controls
executable file
Β·122 lines (106 loc) Β· 3.66 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
#!/bin/bash
set -e
echo "π Setting up Docker Multi-Domain Infrastructure..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if .env exists
if [ ! -f .env ]; then
echo -e "${YELLOW}β οΈ .env file not found. Creating from .env.example...${NC}"
if [ -f .env.example ]; then
cp .env.example .env
echo -e "${GREEN}β
Created .env file. Please edit it with your domain and email.${NC}"
echo -e "${YELLOW}β οΈ Run this script again after updating .env${NC}"
exit 0
else
echo -e "${RED}β .env.example not found!${NC}"
exit 1
fi
fi
# Load environment variables
source .env
# Validate required variables
if [ -z "$PRIMARY_DOMAIN" ] || [ "$PRIMARY_DOMAIN" = "yourdomain.com" ]; then
echo -e "${RED}β Please set PRIMARY_DOMAIN in .env file${NC}"
exit 1
fi
if [ -z "$EMAIL" ] || [ "$EMAIL" = "admin@yourdomain.com" ]; then
echo -e "${RED}β Please set EMAIL in .env file${NC}"
exit 1
fi
echo -e "${GREEN}β
Environment variables loaded${NC}"
echo " Domain: $PRIMARY_DOMAIN"
echo " Email: $EMAIL"
# Create necessary directories
echo ""
echo "π Creating directory structure..."
mkdir -p nginx/conf.d
mkdir -p nginx/ssl
mkdir -p nginx/logs
mkdir -p certbot/logs
echo -e "${GREEN}β
Directories created${NC}"
# Generate self-signed certificate for default server
echo ""
echo "π Generating self-signed certificate for default server..."
if [ ! -f nginx/ssl/default.crt ] || [ ! -f nginx/ssl/default.key ]; then
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout nginx/ssl/default.key \
-out nginx/ssl/default.crt \
-subj "/CN=default" \
2>/dev/null
echo -e "${GREEN}β
Self-signed certificate created${NC}"
else
echo -e "${YELLOW}β οΈ Default certificate already exists, skipping...${NC}"
fi
# Start services
echo ""
echo "π³ Starting Docker services..."
docker compose up -d
# Wait for nginx to be healthy
echo ""
echo "β³ Waiting for nginx to be ready..."
sleep 5
if docker compose ps | grep -q "infrastructure-nginx.*Up"; then
echo -e "${GREEN}β
Nginx is running!${NC}"
else
echo -e "${RED}β Nginx failed to start. Check logs with: docker compose logs nginx${NC}"
exit 1
fi
# Setup cron job for nginx reload
echo ""
echo "β° Setting up cron job for nginx reload..."
CRON_JOB="0 0,12 * * * docker exec infrastructure-nginx nginx -s reload"
# Check if cron job already exists
if crontab -l 2>/dev/null | grep -q "infrastructure-nginx"; then
echo -e "${YELLOW}β οΈ Cron job already exists, skipping...${NC}"
else
# Add cron job
(crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
echo -e "${GREEN}β
Cron job added (nginx reload at midnight and noon)${NC}"
fi
# Remove old certbot cron if exists
if crontab -l 2>/dev/null | grep -q "certbot renew"; then
echo -e "${YELLOW}β οΈ Removing old certbot cron job...${NC}"
crontab -l | grep -v "certbot renew" | crontab -
echo -e "${GREEN}β
Old certbot cron job removed${NC}"
fi
# Instructions
echo ""
echo -e "${GREEN}β
Setup complete!${NC}"
echo ""
echo "π Next steps:"
echo " 1. Add your first subdomain config to nginx/conf.d/"
echo " Example: cp nginx/conf.d/example.conf nginx/conf.d/app.yourdomain.com.conf"
echo ""
echo " 2. Obtain SSL certificate:"
echo " ./add-domain.sh app.yourdomain.com container-name:port"
echo ""
echo " 3. Your other Docker projects should include:"
echo " networks:"
echo " ${DOCKER_NETWORK_NAME:-infrastructure-web}:"
echo " external: true"
echo ""
echo "π Check status: docker compose ps"
echo "π View logs: docker compose logs -f"