From 671b47864000a15e0efe30b141cd8a9789966023 Mon Sep 17 00:00:00 2001 From: Dushyant Yadav Date: Wed, 2 Oct 2019 10:23:02 +0530 Subject: [PATCH 1/2] [AutoHost] Add script to deploy django app using nginx, gunicorn and supervisor --- django-autohost/host.sh | 165 ++++++++++++++++++++++++++++++++++++++ django-autohost/remove.sh | 13 +++ 2 files changed, 178 insertions(+) create mode 100644 django-autohost/host.sh create mode 100755 django-autohost/remove.sh diff --git a/django-autohost/host.sh b/django-autohost/host.sh new file mode 100644 index 00000000..bb16e730 --- /dev/null +++ b/django-autohost/host.sh @@ -0,0 +1,165 @@ +#!/bin/bash + +export PROJECT_NAME=$1 +export PROJECT_DIR=$2 +export HOST_IP=$3 +export HOST_PORT=$4 + +echo "Running autohost on ${PROJECT_NAME}..." + +#------ +HOSTING_DIR="/autohost/${PROJECT_NAME}_hostingdata" +echo "Saving hosting data in ${HOSTING_DIR}" + +sudo rm -rf $HOSTING_DIR +sudo mkdir -p $HOSTING_DIR +sudo mkdir "${HOSTING_DIR}/logs" +sudo touch "$HOSTING_DIR/logs/gunicorn-supervisor.log" +sudo touch "$HOSTING_DIR/logs/nginx-access.log" +sudo touch "$HOSTING_DIR/logs/nginx-error.log" + +#------ +echo "Setting up Gunicorn..." +GUNICORN_SCRIPT="${HOSTING_DIR}/gunicorn-start.sh" + +sudo touch $GUNICORN_SCRIPT + +cat >> $GUNICORN_SCRIPT <<\EOF +#!/bin/bash +EOF + +cat >> $GUNICORN_SCRIPT <> $GUNICORN_SCRIPT <<\EOF +echo "Starting ${NAME} as root" +EOF + +cat >> $GUNICORN_SCRIPT <> $GUNICORN_SCRIPT <<\EOF +export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE +export PYTHONPATH=$DJANGODIR:$PYTHONPATH +EOF + +cat >> $GUNICORN_SCRIPT <> $GUNICORN_SCRIPT <<\EOF +exec gunicorn ${DJANGO_WSGI_MODULE}:application \ + --name $NAME \ + --workers $NUM_WORKERS \ + --user=$USER \ + --bind unix:$SOCKFILE +EOF + +sudo chmod u+x $GUNICORN_SCRIPT + + +#------ +python3 -m venv "${HOSTING_DIR}/venv" +source "${HOSTING_DIR}/venv/bin/activate" + +pip install -r "${PROJECT_DIR}/requirements.txt" +pip install gunicorn +pip install setproctitle + +python "${PROJECT_DIR}/manage.py" collectstatic + +deactivate + +#----- +echo "Installing dependencies" +sudo apt-get install nginx +sudo apt-get install supervisor + +echo " " +echo "Setting up supervisor" +echo "${PROJECT_NAME}.conf" + +sudo rm -rf "/etc/supervisor/conf.d/${PROJECT_NAME}.conf" +sudo cat >> "/etc/supervisor/conf.d/${PROJECT_NAME}.conf" <> /etc/nginx/sites-available/${PROJECT_NAME} <> /etc/nginx/sites-available/$PROJECT_NAME <<\EOF + location / { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_redirect off; + if (!-f $request_filename) { +EOF + +cat >> /etc/nginx/sites-available/$PROJECT_NAME < Date: Wed, 2 Oct 2019 10:24:45 +0530 Subject: [PATCH 2/2] Add readme for autohost --- django-autohost/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 django-autohost/README.md diff --git a/django-autohost/README.md b/django-autohost/README.md new file mode 100644 index 00000000..88a3463f --- /dev/null +++ b/django-autohost/README.md @@ -0,0 +1,33 @@ +# Autohost +Shell scripts for quick production deployment of web apps. + +## For Django - + +Before everything, make sure you have a requirements.txt file inside your project's root which contains all your dependencies. +(Installing dependencies after hosting is kinda messy.) + +1. Clone this repository to your local machine/server. +2. Navigate to django-autohost directory and give host.sh executable permissions by `sudo chmod +x ./host.sh` +3. Run `sudo ./host.sh /path/to/project/root/directory "ip_address" "port"` + +Your project root is the folder which contains the manage.py file. The path to project root should be absolute. +For example, if I had a project named MyProject at +/django/MyProject and wanted to host it locally on port 8080, I would use - + +`sudo ./host.sh MyProject /django/MyProject "localhost" "8080"` (Notice the quotation marks over IP address and port) + +This would store hosting-related info in a directory with path /autohost/MyProject_hostingdata (yes, it is necessary to use the same name as your django project name). +After running that command, you will be able to view your website at localhost:8080. Don't forget the sudo, this script requires root permissions to place config files in the right directories. + +#### Note: If you want to host at localhost, make sure you write "localhost" there and not "127.0.0.1". Nginx will have issues if you write 127.0.0.1. + +#### Note - +This script will use nginx and gunicorn (with 4 process workers) to host your web app. The script is ONLY for hosting, and not for managing your project. +This means once hosted, you will still need to manage your app. Things like making migrations and collectstatic still need to be +done manually (the script will collect static for you once, at the time of running). + +#### How to "un-host" - +To remove your webapp from your server, do the following - +1. Navigate to django-autohost directory and give remove.sh executable permissions by `sudo chmod +x ./remove.sh` +2. Execute `./remove.sh `, where is the name of the project that you used earlier when hosting it. +3. You're done.