forked from cryptosharks131/lndg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx.sh
166 lines (146 loc) · 5.19 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
155
156
157
158
159
160
161
162
163
164
165
166
INSTALL_USER=${SUDO_USER:-${USER}}
NODE_IP=$(hostname -I | cut -d' ' -f1)
RED='\033[0;31m'
NC='\033[0m'
if [ "$INSTALL_USER" == 'root' ] >/dev/null 2>&1; then
HOME_DIR='/root'
else
HOME_DIR="/home/$INSTALL_USER"
fi
function install_deps() {
apt install -y python3-dev >/dev/null 2>&1
apt install -y build-essential python >/dev/null 2>&1
apt install -y uwsgi >/dev/null 2>&1
apt install -y nginx >/dev/null 2>&1
$HOME_DIR/lndg/.venv/bin/python -m pip install uwsgi >/dev/null 2>&1
}
function setup_uwsgi() {
cat << EOF > $HOME_DIR/lndg/lndg.ini
# lndg.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = $HOME_DIR/lndg
# Django's wsgi file
module = lndg.wsgi
# the virtualenv (full path)
home = $HOME_DIR/lndg/.venv
#location of log files
logto = /var/log/uwsgi/%n.log
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 1
# the socket (use the full path to be safe
socket = $HOME_DIR/lndg/lndg.sock
# ... with appropriate permissions - may be needed
chmod-socket = 660
# clear environment on exit
vacuum = true
EOF
cat <<\EOF > $HOME_DIR/lndg/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
cat << EOF > /etc/systemd/system/uwsgi.service
[Unit]
Description=Lndg uWSGI app
After=syslog.target
[Service]
ExecStart=$HOME_DIR/lndg/.venv/bin/uwsgi \
--ini $HOME_DIR/lndg/lndg.ini
User=$INSTALL_USER
Group=www-data
Restart=on-failure
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=sockets.target
EOF
usermod -a -G www-data $INSTALL_USER
}
function setup_nginx() {
cat << EOF > /etc/nginx/sites-enabled/lndg
# the below setting can sometimes help resolve permission issues
# user $INSTALL_USER
upstream django {
server unix://$HOME_DIR/lndg/lndg.sock; # for a file socket
}
server {
# the port your site will be served on, use port 80 unless setting up ssl certs, then 443
listen 8889;
# optional settings for ssl setup
#ssl on;
#ssl_certificate /<path_to_certs>/fullchain.pem;
#ssl_certificate_key /<path_to_certs>/privkey.pem;
# the domain name it will serve for
server_name _; # you can substitute your node IP address or a custom domain like lndg.local (just make sure to update your local hosts file)
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# max wait for django time
proxy_read_timeout 180;
# Django media
location /static {
alias $HOME_DIR/lndg/gui/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include $HOME_DIR/lndg/uwsgi_params; # the uwsgi_params file
}
}
EOF
rm /etc/nginx/sites-enabled/default
}
function start_services() {
touch /var/log/uwsgi/lndg.log
touch $HOME_DIR/lndg/lndg.sock
chgrp www-data /var/log/uwsgi/lndg.log
chgrp www-data $HOME_DIR/lndg/lndg.sock
chmod 660 /var/log/uwsgi/lndg.log
systemctl start uwsgi
systemctl restart nginx
sudo systemctl enable uwsgi
sudo systemctl enable nginx
}
function report_information() {
echo -e ""
echo -e "================================================================================================================================"
echo -e "Nginx service setup using user account $INSTALL_USER and an address of $NODE_IP:8889."
echo -e "You can update the IP or port used by modifying this configuration file and restarting nginx: /etc/nginx/sites-enabled/lndg"
echo -e ""
echo -e "uWSGI Status: ${RED}sudo systemctl status uwsgi.service${NC}"
echo -e "Nginx Status: ${RED}sudo systemctl status nginx.service${NC}"
echo -e ""
echo -e "To disable your webserver, use the following commands."
echo -e "Disable uWSGI: ${RED}sudo systemctl disable uwsgi.service${NC}"
echo -e "Disable Nginx: ${RED}sudo systemctl disable nginx.service${NC}"
echo -e "Stop uWSGI: ${RED}sudo systemctl stop uwsgi.service${NC}"
echo -e "Stop Nginx: ${RED}sudo systemctl stop nginx.service${NC}"
echo -e ""
echo -e "To re-enable these services, simply replace the disable/stop commands with enable/start."
echo -e "================================================================================================================================"
}
##### Main #####
echo -e "Setting up, this may take a few minutes..."
install_deps
setup_uwsgi
setup_nginx
start_services
report_information