Skip to content

Setup in Production

zuzelvp edited this page Apr 27, 2012 · 13 revisions

Setup in Production with Apache and WSGI

Configure a new virtualhost for the site

vim /etc/apache2/sites-available/lernanta

This is an example of configuration (replace the values between brackets) ::

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName [domain]
    ErrorLog /var/log/apache2/lernanta-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog /var/log/apache2/lernanta-access.log combined

    # run mod_wsgi process for django in daemon mode
    # this allows avoiding confused timezone settings when
    # another application runs in the same virtual host
    WSGIDaemonProcess Lernanta
    WSGIProcessGroup Lernanta

    # force all content to be served as static files
    # otherwise django will be crunching images through itself wasting time
    Alias /media/ "[path to the source code]/media/"
    <Directory "[path to the source code]/media">
        Order deny,allow
        Allow from all
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
    </Directory>

    Alias /en/admin-media/ "[path to the virtualenv]/lib/python2.6/site-packages/django/contrib/admin/media/"
    <Directory "[path to the virtualenv]/lib/python2.6/site-packages/django/contrib/admin/media">
        Order deny,allow
        Allow from all
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
    </Directory>

    #this is your wsgi script described in the prev section
    WSGIScriptAlias / [path to the source code]/wsgi/batucada.wsgi
</VirtualHost>

Add the necessary paths to sitedir on wsgi/batucada.wsgi (replace the values between brackets)

site.addsitedir(os.path.abspath(os.path.join(wsgidir, '[path to the virtualenv]/lib/python2.6/site-packages')))
site.addsitedir(os.path.abspath(os.path.join(wsgidir, '[path to the virtualenv]/src')))

Reload apache ::

/etc/init.d/apache reload

Update the Site instance's domain from the admin interface and configure your SUPERFEEDR username and password (now in settings.py, but soon in settings_local.py).

Configure email settings (DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_HOST_PASSWORD, EMAIL_HOST_USER) and the email backend

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

If you have to update the source code in production, remember to mark the .wsgi file as updated

touch wsgi/batucada.wsgi

We are using MySQL but if you want to try with PostgreSQL you could read a bit of documentation on https://github.com/p2pu/lernanta/pull/25.

Setup Celery Tasks

To use celery we need to create a RabbitMQ user, a virtual host and allow that user access to that virtual host (note that you need to replace mypassword and fill out BROKER_PASSWORD on settings_local.py):

sudo apt-get install rabbitmq-server
rabbitmqctl add_user lernanta_user mypassword
rabbitmqctl add_vhost lernanta_vhost
rabbitmqctl set_permissions -p lernanta_vhost lernanta_user ".*" ".*" ".*"

See how to configure your system hostname here.

You can then set the configuration options of rabbitmq on settings_local.py (e.g., BROKER_USER, BROKER_PASSWORD, BROKER_VHOST) and set CELERY_ALWAYS_EAGER to False to delegate the execution of tasks to rabbitmq. Then test your configuration by running:

python manage.py celeryd -l info

If it works then go ahead to run celery as a daemon. The following is an example of the configuration for celeryd that you can put at /etc/default/celeryd:

# Name of nodes to start, here we have a single node
CELERYD_NODES="w1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"

# Where to chdir at start.
CELERYD_CHDIR="[path to the source code]/"

# Virtual Env
LERNANTA_VIRTUALENV_DIR="[path to the virtualenv]/"

# How to call "manage.py celeryd_multi"
CELERYD_MULTI="$LERNANTA_VIRTUALENV_DIR/bin/python $CELERYD_CHDIR/manage.py celeryd_multi"

# How to call "manage.py celeryctl"
CELERYCTL="$LERNANTA_VIRTUALENV_DIR/bin/python $CELERYD_CHDIR/manage.py celeryctl"

# Extra arguments to celeryd
CELERYD_OPTS="--time-limit=1200 --concurrency=8"

# Name of the celery config module.
CELERY_CONFIG_MODULE="celeryconfig"

# %n will be replaced with the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_LEVEL=DEBUG

# Workers should run as an unprivileged user.
CELERYD_USER="www-data"
CELERYD_GROUP="www-data"

# Name of the projects settings module.
export DJANGO_SETTINGS_MODULE="settings_local"

See also:

Clone this wiki locally