diff --git a/nginx.sh b/nginx.sh index ec1e840c..31a5c7dc 100644 --- a/nginx.sh +++ b/nginx.sh @@ -1,9 +1,9 @@ +#!/bin/bash + 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 +if [ "$INSTALL_USER" == 'root' ]; then HOME_DIR='/root' else HOME_DIR="/home/$INSTALL_USER" @@ -26,68 +26,64 @@ function check_path() { 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 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 + apt-get update + apt-get install -y python3-dev build-essential python3-pip uwsgi nginx + python3 -m pip install uwsgi } function setup_uwsgi() { - cat << EOF > $HOME_DIR/lndg/lndg.ini + # Creating the lndg.ini file + cat << EOF > $LNDG_DIR/lndg.ini # lndg.ini file [uwsgi] - # Django-related settings -# the base directory (full path) chdir = $LNDG_DIR -# Django's wsgi file module = lndg.wsgi -# the virtualenv (full path) home = $LNDG_DIR/.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 = $LNDG_DIR/lndg.sock -# ... with appropriate permissions - may be needed chmod-socket = 660 -# clear environment on exit vacuum = true EOF - 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"; + + # 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 +ExecStart=$LNDG_DIR/.venv/bin/uwsgi --ini $LNDG_DIR/lndg.ini User=$INSTALL_USER Group=www-data Restart=on-failure @@ -97,50 +93,39 @@ StandardError=syslog NotifyAccess=all [Install] -WantedBy=sockets.target +WantedBy=multi-user.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 - + # 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 { - # 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 //fullchain.pem; - #ssl_certificate_key //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) + server_name _; charset utf-8; - - # max upload size - client_max_body_size 75M; # adjust to taste - - # max wait for django time + client_max_body_size 75M; proxy_read_timeout 180; - # Django media location /static { alias $LNDG_DIR/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 $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() { @@ -156,22 +141,7 @@ function start_services() { } 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 "================================================================================================================================" + echo "Nginx and uWSGI have been set up with user $INSTALL_USER at $NODE_IP:8889." } ##### Main ##### @@ -181,4 +151,4 @@ install_deps setup_uwsgi setup_nginx start_services -report_information +report_information \ No newline at end of file