Skip to content

Instantly share code, notes, and snippets.

@paul-schwendenman
Last active February 19, 2018 19:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paul-schwendenman/6801288 to your computer and use it in GitHub Desktop.
Save paul-schwendenman/6801288 to your computer and use it in GitHub Desktop.
Gunicorn setup
#!/bin/bash
# /etc/init.d/gunicorn
### BEGIN INIT INFO
# Provides: gunicorn
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Gunicorn server
# Description: Starts the gunicorn server
### END INIT INFO
# :author: paul schwendenman
# :date: 02 oct 2013
# PATH TO GUNICORN
# i.e Type "which gunicorn" in a shell with your virtualenv active
# No need to otherwise activate the virtualenv
GUNICORN=/path/to/gunicorn
# PATH TO GUNICORN CONF FILE
GUNICORN_CONFIG=/path/to/gunicorn_conf
GUNICORN_APPLICATION=project.wsgi:application
# Gunicorn project name (use if running more than one gunicorn application)
GUNICORN_PROJECT="gunicorn"
# The pid file from the config file
PID_FILE=/path/to/pid_file.pid
# Set the proc_name in your config
PROC_NAME="gunicorn"
test_pid () {
pgrep -F $1 &> /dev/null
# You could also use something like (but I had issues)
#ps $(cat $1) &> /dev/null
# If you set "proc_name" in your config
#pgrep -F $1 -f $PROC_NAME &> /dev/null
}
gu_start() {
if test_pid $PID_FILE
then
echo "$GUNICORN_PROJECT was already running"
else
$GUNICORN -c $GUNICORN_CONFIG $GUNICORN_APPLICATION
fi
}
gu_kill() {
if test_pid $PID_FILE
then
kill $(cat $PID_FILE)
i=0
while test_pid $PID_FILE
do
if [ $i = '0' ]
then
echo "... waiting"
else
echo -n "."
fi
i=$(($i + 1))
sleep .1
done
else
echo "$GUNICORN_PROJECT was not running"
fi
}
gu_reload() {
if test_pid $PID_FILE
then
kill -HUP $(cat $PID_FILE)
else
echo "$GUNICORN_PROJECT was not running"
fi
}
case "$1" in
start)
echo -n "Starting $GUNICORN_PROJECT... "
gu_start
;;
stop)
echo -n "Stopping $GUNICORN_PROJECT... "
gu_kill
;;
restart)
echo -n "Restarting $GUNICORN_PROJECT... "
gu_kill
gu_start
;;
reload)
echo -n "Reloading $GUNICORN_PROJECT... "
gu_reload
;;
status)
if test_pid $PID_FILE
then
echo "$GUNICORN_PROJECT is running."
else
echo "$GUNICORN_PROJECT is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}" >&2
exit 1
;;
esac
exit 0
# Configuration file for gunicorn
# Bind address
bind = "127.0.0.1:8001"
# Number of workers
workers = 3
# Maximum number of simultaneous clients
#max_worker = 1000
# Maximum number of requests before a worker restarts
max_requests = 2000
# Directory for loading app
chdir = "/path/to/app"
# note: this should be the directory with "manage.py"
# Daemonize the process
daemon=True
# Process id file
pidfile = "/path/to/gunicorn.pid"
# User to run the workers as
user = "user"
group = "user"
# Set x_forwarded_for_header
#x_forwarded_for_header = True
# Forwarded allow ips
forwarded_allow_ips = "127.0.0.1"
# Access log file
accesslog = "/path/to/log/access.log"
# Error log file
errorlog = "/path/to/log/error.log"
# Log level
#loglevel = "DEBUG"
#Log config
#logconfig = ""
# Process Naming
proc_name = "project"
@oca159
Copy link

oca159 commented Feb 19, 2018

awesome tips! I will use it in my next project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment