forked from cryptosharks131/lndg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx.sh
154 lines (133 loc) · 3.88 KB
/
nginx.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
INSTALL_USER=${SUDO_USER:-${USER}}
NODE_IP=$(hostname -I | cut -d' ' -f1)
if [ "$INSTALL_USER" == 'root' ]; then
HOME_DIR='/root'
else
HOME_DIR="/home/$INSTALL_USER"
fi
LNDG_DIR="$HOME_DIR/lndg"
function check_path() {
if [ -e $LNDG_DIR/lndg/wsgi.py ]; then
echo "Using LNDg installation found at $LNDG_DIR"
else
echo "LNDg installation not found at $LNDG_DIR/, please provide the correct path:"
read USR_DIR
if [ -e $USR_DIR/lndg/wsgi.py ]; then
LNDG_DIR=$USR_DIR
echo "Using LNDg installation found at $LNDG_DIR"
else
echo "LNDg installation still not found, exiting..."
exit 1
fi
fi
}
# Ensure the lndg directory exists
mkdir -p $LNDG_DIR
mkdir -p $LNDG_DIR/.venv/bin
mkdir -p /var/log/uwsgi
function install_deps() {
apt-get update
apt-get install -y python3-dev build-essential python3-pip uwsgi nginx
python3 -m pip install uwsgi
}
function setup_uwsgi() {
# Creating the lndg.ini file
cat << EOF > $LNDG_DIR/lndg.ini
# lndg.ini file
[uwsgi]
# Django-related settings
chdir = $LNDG_DIR
module = lndg.wsgi
home = $LNDG_DIR/.venv
logto = /var/log/uwsgi/%n.log
# process-related settings
master = true
processes = 1
socket = $LNDG_DIR/lndg.sock
chmod-socket = 660
vacuum = true
EOF
# Creating the uwsgi_params file
cat << EOF > $LNDG_DIR/uwsgi_params
uwsgi_param QUERY_STRING \$query_string;
uwsgi_param REQUEST_METHOD \$request_method;
uwsgi_param CONTENT_TYPE \$content_type;
uwsgi_param CONTENT_LENGTH \$content_length;
uwsgi_param REQUEST_URI "\$request_uri";
uwsgi_param PATH_INFO "\$document_uri";
uwsgi_param DOCUMENT_ROOT "\$document_root";
uwsgi_param SERVER_PROTOCOL "\$server_protocol";
uwsgi_param REQUEST_SCHEME "\$scheme";
uwsgi_param HTTPS "\$https if_not_empty";
uwsgi_param REMOTE_ADDR "\$remote_addr";
uwsgi_param REMOTE_PORT "\$remote_port";
uwsgi_param SERVER_PORT "\$server_port";
uwsgi_param SERVER_NAME "\$server_name";
EOF
# Creating the uwsgi.service systemd unit file
cat << EOF > /etc/systemd/system/uwsgi.service
[Unit]
Description=Lndg uWSGI app
After=syslog.target
[Service]
ExecStart=$LNDG_DIR/.venv/bin/uwsgi --ini $LNDG_DIR/lndg.ini
User=$INSTALL_USER
Group=www-data
Restart=on-failure
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
EOF
usermod -a -G www-data $INSTALL_USER
}
function setup_nginx() {
# Creating the Nginx configuration file
cat << EOF > /etc/nginx/sites-available/lndg
upstream django {
server unix://$LNDG_DIR/lndg.sock; # for a file socket
}
server {
listen 8889;
server_name _;
charset utf-8;
client_max_body_size 75M;
proxy_read_timeout 180;
location /static {
alias $LNDG_DIR/gui/static; # your Django project's static files - amend as required
}
location / {
uwsgi_pass django;
include $LNDG_DIR/uwsgi_params; # the uwsgi_params file
}
}
EOF
# Remove the default site and link the new site
rm /etc/nginx/sites-enabled/default
ln -sf /etc/nginx/sites-available/lndg /etc/nginx/sites-enabled/
}
function start_services() {
touch /var/log/uwsgi/lndg.log
touch $LNDG_DIR/lndg.sock
chgrp www-data /var/log/uwsgi/lndg.log
chgrp www-data $LNDG_DIR/lndg.sock
chmod 660 /var/log/uwsgi/lndg.log
systemctl start uwsgi
systemctl restart nginx
systemctl enable uwsgi
systemctl enable nginx
}
function report_information() {
echo "Nginx and uWSGI have been set up with user $INSTALL_USER at $NODE_IP:8889."
}
##### Main #####
echo -e "Setting up, this may take a few minutes..."
check_path
install_deps
setup_uwsgi
setup_nginx
start_services
report_information