Skip to content

Instantly share code, notes, and snippets.

@ianfitzpatrick
Last active August 29, 2015 14:26
Show Gist options
  • Save ianfitzpatrick/f6f83ddde3124fe3d8fa to your computer and use it in GitHub Desktop.
Save ianfitzpatrick/f6f83ddde3124fe3d8fa to your computer and use it in GitHub Desktop.
Debian init script for running New Relic Python wrapper w/ uWSGI + Virtualenv
#!/bin/sh
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: uwsgi wrapped by new relic
# Description: Support for uwsgi with new relic and virtualenv
### END INIT INFO
# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e
# Specify username. Run command under user's UID
USER=www-data
#This is the command to be run, give the full pathname
NAME=uwsgi
DAEMON=/home/$USER/.virtualenvs/myvirtualenv/bin/newrelic-admin
DAEMON_OPTS="run-program /home/$USER/.virtualenvs/myvirtualenv/bin/uwsgi --ini /etc/uwsgi/apps-enabled/uwsgi.ini"
# New Relic Settings
NR_CONFIG_FILE=/var/www/myproject/newrelic.ini
NR_STARTUP_DEBUG=true
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
case "$1" in
start)
echo -n "Starting daemon: "$NAME
export NEW_RELIC_CONFIG_FILE=$NR_CONFIG_FILE
export NEW_RELIC_STARTUP_DEBUG=$NR_STARTUP_DEBUG
start-stop-daemon --start --quiet --chuid $USER --exec $DAEMON -- $DAEMON_OPTS
echo "."
;;
stop)
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --chuid $USER --oknodo --signal 9
echo "."
;;
restart)
echo -n "Restarting daemon: "$NAME
start-stop-daemon --stop --chuid $USER --oknodo --signal 9 --retry 30
start-stop-daemon --start --chuid $USER --exec $DAEMON -- $DAEMON_OPTS
echo "."
;;
*)
echo "Usage: "$1" {start|stop|restart}"
exit 1
esac
exit 0
@ianfitzpatrick
Copy link
Author

A few notes:

  • uWSGI must be installed within virtualenv (pip install uwsgi)
  • If you had plugin = python in your uwsgi.ini file, comment it out.
  • Make sure to use module = <module_name>.wsgi:application (not module = django.core.wsgi:get_wsgi_application()) in uwsgi.ini

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