-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd-node
More file actions
executable file
·99 lines (82 loc) · 2.52 KB
/
add-node
File metadata and controls
executable file
·99 lines (82 loc) · 2.52 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
#!/bin/bash
#
# Add a new site with support for Node.js
# Ensure running as root
if [ "$(id -u)" -ne 0 ]; then
echo 'This script is designed to run as root'
exit
fi
# Check arguments
if [ $# -lt 3 ]; then
echo 'Syntax: add-node <site-name> <domain> <local-port>'
exit
fi
# Include common functions
source "${0%/*}/common"
# Determine site information
SITENAME=$1
SITEDOMAIN=$2
LOCALPORT=$3
# Check number of dots in domain and use if we should add a www subdomain
DOTCOUNT=$(awk -F. '{print NF-1}' <<<"$SITEDOMAIN")
SERVERNAME=$SITEDOMAIN
if [ $DOTCOUNT -le 1 ]; then
SERVERNAME="$SERVERNAME www.$SITEDOMAIN"
fi
# Determine site directory and user
DESTDIR=/var/www/$SITENAME
SITE_USER='www-data'
SITE_GROUP='www-data'
SERVICE_PATH='/etc/systemd/system/kestrel-$SITENAME.service'
# Create destination directories
sub_dirs=(public logs)
for subdir in "${sub_dirs[@]}"; do
if [ ! -d "$DESTDIR/$subdir" ]; then
mkdir -p "$DESTDIR/$subdir"
fi
done
# Create nginx configuration
# Rename any existing file before creating the new one
if [ -f /etc/nginx/sites-available/$SITENAME.conf ]; then
mv /etc/nginx/sites-available/$SITENAME.conf /etc/nginx/sites-available/$SITENAME.conf.old
fi
tee /etc/nginx/sites-available/$SITENAME.conf > /dev/null << EOF
server {
listen 80;
server_name $SERVERNAME;
access_log $DESTDIR/logs/access.log;
error_log $DESTDIR/logs/error.log;
root $DESTDIR/public;
index index.html;
location /.well-known {
default_type "text/plain";
allow all;
}
location / {
proxy_pass http://localhost:$LOCALPORT;
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;
}
}
EOF
ln -sf /etc/nginx/sites-available/$SITENAME.conf /etc/nginx/sites-enabled/$SITENAME.conf
# Set permissions for configuration file
chmod 0660 /etc/nginx/sites-available/$1.conf
# Copy template files and set permissions on site content
cp -R "$(dirname $0)/templates/node/." "$DESTDIR"
set_site_permissions $DESTDIR $SITE_USER $SITE_GROUP
# Check that nginx is happy with configuration
nginx -t
if [ $? -ne 0 ]; then
echo 'Nginx reported error in configuration'
exit
fi
nginx -s reload
# Configure PM2 to run the process
pm2 start $DESTDIR/app.js --name $SITENAME
# Create Letsencrypt certificate
create_certificate $SITENAME $SITEDOMAIN $DOTCOUNT
echo "Created node site $SITENAME successfully at $DESTDIR"