-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dush-t/autohost
Autohost
- Loading branch information
Showing
3 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <ProjectName> /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 <ProjectName>`, where <ProjectName> is the name of the project that you used earlier when hosting it. | ||
3. You're done. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <<EOF | ||
NAME="${PROJECT_NAME}" | ||
SOCKFILE="${HOSTING_DIR}/run/gunicorn.sock" | ||
DJANGODIR=${PROJECT_DIR} | ||
USER=root | ||
GROUP=webapps | ||
NUM_WORKERS=4 | ||
DJANGO_SETTINGS_MODULE=${PROJECT_NAME}.settings | ||
DJANGO_WSGI_MODULE=${PROJECT_NAME}.wsgi | ||
EOF | ||
|
||
cat >> $GUNICORN_SCRIPT <<\EOF | ||
echo "Starting ${NAME} as root" | ||
EOF | ||
|
||
cat >> $GUNICORN_SCRIPT <<EOF | ||
source "${HOSTING_DIR}/venv/bin/activate" | ||
EOF | ||
|
||
cat >> $GUNICORN_SCRIPT <<\EOF | ||
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE | ||
export PYTHONPATH=$DJANGODIR:$PYTHONPATH | ||
EOF | ||
|
||
cat >> $GUNICORN_SCRIPT <<EOF | ||
mkdir "${HOSTING_DIR}/run" | ||
EOF | ||
|
||
cat >> $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" <<EOF | ||
[program:${PROJECT_NAME}] | ||
command=${GUNICORN_SCRIPT} | ||
user = root | ||
stdout_logfile = "${HOSTING_DIR}/logs/gunicorn-supervisor.log" | ||
redirect_stderror = true | ||
EOF | ||
|
||
sudo supervisorctl reread | ||
sudo supervisorctl update | ||
|
||
echo " " | ||
echo "Setting up nginx" | ||
echo "Saving nginx configuration at /etc/nginx/sites-available/${PROJECT_NAME}" | ||
sudo rm -rf /etc/nginx/sites-available/${PROJECT_NAME} | ||
sudo rm -rf /etc/nginx/sites-enabled/${PROJECT_NAME} | ||
touch /etc/nginx/sites-available/${PROJECT_NAME} | ||
|
||
sudo cat >> /etc/nginx/sites-available/${PROJECT_NAME} <<EOF | ||
upstream ${PROJECT_NAME}_app_server { | ||
server unix:${HOSTING_DIR}/run/gunicorn.sock fail_timeout=0; | ||
} | ||
server { | ||
listen ${HOST_PORT}; | ||
server_name ${HOST_IP}; | ||
client_max_body_size 4G; | ||
access_log ${HOSTING_DIR}/logs/nginx-access.log; | ||
error_log ${HOSTING_DIR}/logs/nginx-error.log; | ||
location /static/ { | ||
alias $PROJECT_DIR/static/; | ||
} | ||
location /media/ { | ||
alias $PROJECT_DIR/media; | ||
} | ||
EOF | ||
cat >> /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 <<EOF | ||
proxy_pass http://${PROJECT_NAME}_app_server; | ||
break; | ||
} | ||
} | ||
# Error pages | ||
error_page 500 502 503 504 /500.html; | ||
location = /500.html { | ||
root ${PROJECT_DIR}/static/; | ||
} | ||
} | ||
EOF | ||
|
||
sudo ln -s /etc/nginx/sites-available/${PROJECT_NAME} /etc/nginx/sites-enabled/ | ||
|
||
sudo nginx -s reload | ||
sudo nginx -s reopen | ||
sudo supervisorctl status $PROJECT_NAME | ||
|
||
|
||
echo "Your project is up and running at ${HOST_IP}:${HOST_PORT}" | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export PROJECT_NAME=$1 | ||
|
||
echo "Unhosting your website..." | ||
|
||
echo "Removing hosting files..." | ||
sudo rm -rf "/autohost/${PROJECT_NAME}_hostingdata" | ||
echo "Removing nginx configurations..." | ||
sudo rm -rf "/etc/nginx/sites-enabled/${PROJECT_NAME}" | ||
sudo rm -rf "/etc/nginx/sites-available/${PROJECT_NAME}" | ||
echo "Removing supervisor program..." | ||
sudo rm -rf "/etc/supervisor/conf.d/${PROJECT_NAME}.conf" | ||
|
||
echo "Done" |