Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct setup for multiple web sites with nginx, django and celery

Im trying to find some information on the correct way of setting up multiple django sites on a linode (Ubuntu 12.04.3 LTS (GNU/Linux 3.9.3-x86_64-linode33 x86_64)

Here is what I have now:

Webserver: nginx

Every site is contained in a .virtualenv

Django and other packages is installed using pip in each .virtualenv

RabbitMQ is installed using sudo apt-get rabbitmq, and a new user and vhost is created for each site.

Each site is started using supervisor script:

[group:<SITENAME>]
programs=<SITENAME>-gunicorn, <SITENAME>-celeryd, <SITENAME>-celerycam


[program:<SITENAME>-gunicorn]
directory = /home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/
command=/home/<USER>/.virtualenvs/<SITENAME>/bin/gunicorn <PROJECT>.wsgi:application -c /home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/server_conf/<SITENAME>-gunicorn.py

user=<USER>
autostart = true
autorestart = true
stderr_events_enabled = true
redirect_stderr = true
logfile_maxbytes=5MB


[program:<SITENAME>-celeryd]
directory=/home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/
command=/home/<USER>/.virtualenvs/<SITENAME>/bin/python /home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/manage.py celery worker -E -n <SITENAME> --broker=amqp://<SITENAME>:<SITENAME>@localhost:5672//<SITENAME> --loglevel=ERROR
environment=HOME='/home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/',DJANGO_SETTINGS_MODULE='<PROJECT>.settings.staging'

user=<USER>
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600

[program:<SITENAME>-celerycam]
directory=/home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/
command=/home/<USER>/.virtualenvs/<SITENAME>/bin/python /home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/manage.py celerycam
environment=HOME='/home/<USER>/.virtualenvs/<SITENAME>/<PROJECT>/',DJANGO_SETTINGS_MODULE='<PROJECT>.settings.staging'

user=<USER>
autostart=true
autorestart=true
startsecs=10

Question 1: Is this the correct way? Or is it a better way to do this?

Question 2: I have tried to install celery flower, but how does that work with multiple sites? Do I need to install one flower-package for each .virtualenv, or could I use one install for every site? How do I setup nginx to display the flower-page(s) on my server?

like image 372
Tomas Jacobsen Avatar asked Nov 02 '22 13:11

Tomas Jacobsen


1 Answers

Answer 1

There are - as so often :) - several ways to go. We do set it up in a similar way.

For the supervisor configuration I would suggest to use a little bit less verbose way, below an example for running web/tasks for 'example.com':

/etc/supervisor/conf.d/example.com.conf
(we usually have the config files in the repository as well, and just symlink them. So this file could be a symlink to:
/var/www/example.com/conf/supervisord.conf )

[group:example.com]
programs=web, worker, cam

[program:web]
command=/srv/example.com/bin/gunicorn project.wsgi -c /var/www/example.com/app/gunicorn.conf.py
directory=/var/www/example.com/app/

user=<USER>
autostart=true
autorestart=true
redirect_stderr=True
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=5
stdout_logfile=/var/log/apps/web.example.com.log


[program:worker]
command=/srv/example.com/bin/celery -A project worker -l info
directory=/var/www/example.com/app/

user=<USER>
autostart=true
autorestart=true
redirect_stderr=True
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=5
stdout_logfile=/var/log/apps/web.example.com.log


[program:flower]
command=/srv/example.com/bin/celery flower -A project --broker=amqp://guest:guest@localhost:5672//example.com/ --url_prefix=flower --port 5001
directory=/var/www/example.com/app/

...

So you have less to type and it is easier to read..

# restart all 'programs'
supervisorctl restart example.com:* 

# restart web/django
supervisorctl restart example.com:web

etc. 

Answer 2

Not totally sure if it is the best way, but what I would do here (and usually do):

  • run flower separately for every app (see config above)
    with respective vhost (and url_prefix)
  • ad nginx reverse-proxy (directory with same name as url_prefix)

/etc/nginx/sites-enabled/example.conf

server {

    ...

    location /flower {
        proxy_pass http://127.0.0.1:5001;

        ...

Access the flower interface at example.com/flower

like image 180
ohrstrom Avatar answered Nov 09 '22 05:11

ohrstrom