Start Gunicorn on boot

955 views
Skip to first unread message

Nonverbis M

unread,
Oct 20, 2017, 4:13:20 AM10/20/17
to Django users

Ubuntu 16.04.3 Desktop version.

gunicorn (version 19.7.1)

virtualenv 15.1.0


I'd like Gunicorn to start on boot. Gunicorn is integrated with Django.


/etc/init/photoarchive.conf


description "Gunicorn server for photoarchive"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid server
setgid server
chdir /home/server/photoarchive/photoarchive

exec /home/server/photoarchive/venv/photoarchive/bin/gunicorn photoarchive.wsgi


What is done for self-check:

1. 

  1. cd /home/server/photoarchive/photoarchive ls

I can see manage.py.


  1. 2.

  2. cd /home/server/photoarchive/venv/photoarchive/bin/ I can see gunicorn file.

  3. cd /var/log/upstart

It is empty.


  1. 3. 

  2. $ ps aux | grep gunicorn

    server 2715 0.0 0.0 21292 948 pts/6 S+ 10:12 0:00 grep --color=auto gunicorn

Well, this is grep has grepped itself.



4. 
  1. Activate virtualenv.

    cd /home/server/photoarchive/photoarchive gunicorn photoarchive.wsgi

Now gunicorn is running. So, manually it starts.

Could you help me organize running gunicorn on start?

Antonis Christofides

unread,
Oct 20, 2017, 4:42:56 AM10/20/17
to django...@googlegroups.com

Hello,

Is this upstart? I thought that Ubuntu doesn't use upstart any more, but that it uses systemd instead (since version 15.04 IIRC). Maybe it contains upstart for backwards compatibility, but I think it's not a good idea to invest in learning upstart right now.

If you want to do it with systemd, which is what I recommend, you could try my recorded webinar, which also contains a cheat sheet with the systemd configuration. Otherwise you could use supervisor, which was the most recommended solution until recently (because upstart and/or init scripts were cumbersome), and which people still use more than systemd, first because systemd is newer, and second because systemd's documentation is suboptimal so it's harder to get started.

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e021369b-9846-4daf-906d-246d52b2dbbd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nonverbis M

unread,
Oct 20, 2017, 5:23:40 AM10/20/17
to Django users
Antonis, thank you.
Yes, this is upstart. I'll try it with systemd as you suggested.

Ricardo Daniel Quiroga

unread,
Oct 20, 2017, 9:09:42 AM10/20/17
to django...@googlegroups.com
Use supervisor to start gunicorn on init



gunicorn_start.bash
#!/bin/bash
NAME="hello_app" # Name of the application
DJANGODIR=/webapps/hello_django/hello # Django project directory
SOCKFILE=/webapps/hello_django/run/gunicorn.sock # we will communicte using this unix socket
USER=hello # the user to run as
GROUP=webapps # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=hello.settings # which settings file should Django use
DJANGO_WSGI_MODULE=hello.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-

2017-10-20 6:23 GMT-03:00 Nonverbis M <nonv...@gmail.com>:
Antonis, thank you.
Yes, this is upstart. I'll try it with systemd as you suggested.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--

Ricardo Daniel Quiroga

Reply all
Reply to author
Forward
0 new messages