1

I'm new to Django and I want to configure my app with apache2. I just follow the guide and the other question, but I can't figure out! My simple configuration file sites-available/000-default.conf:

<VirtualHost *:80>
    WSGIScriptAlias / /var/www/html/mysite/mysite/wsgi.py
    WSGIPythonPath /var/www/html/mysite

    <Directory /var/www/html/mysite/mysite>
    <Files wsgi.py>
    Require all granted
    </Files>
    </Directory>
</VirtualHost>

after apache2 restart there is a syntax error in WSGIPythonPath

if I put utside WSGIPythonPath inside apache2.conf file, the application does not work. What is the problem?

I follow https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/ but WSGIDaemonProcess seems not to work

Pankaspe
  • 91
  • 1
  • 7
  • 2
    I don't like apache :) That's why Im going to recommend you nginx + uwsgi or nginx + gunicorn. They are super easy to configure – sebb Sep 27 '16 at 10:49
  • 3
    what does apache says in the logs? – dahrens Sep 27 '16 at 10:59
  • oh, and i agree with @sebb - nginx + uwsgi perform quite well and are easy to configure. – dahrens Sep 27 '16 at 11:03
  • 1
    You need to supply the actual error messages from the Apache error logs or what is shown by Apache at startup. It is not really possible to surmise what the problem is without knowing the exact error message. If the whole ``WSGIPythonPath`` directive is being rejected by Apache, it would generally mean mod_wsgi isn't loaded as someone has already pointed out as possible solution. – Graham Dumpleton Sep 27 '16 at 22:01

2 Answers2

1

Some things to check: Do you load the wsgi module?

LoadModule wsgi_module modules/mod_wsgi.so

Also look for this answer and a potential gotcha in the Apache2 configuration: 403 Forbidden error with Django and mod_wsgi

Community
  • 1
  • 1
Sir Cornflakes
  • 675
  • 13
  • 26
-2

Ok, recommending it not very helpful, so here is a basic configuration for your nginx and gunicorn.

In order ot make it simple, let's assume your app is located in this directory: /home/root/app/src/ and we're gonna use root user (but you should create separate user for your app)

First your nginx. You have to insert a new file to your /etc/nginx/sites-enabled/yourapp.conf, if there is a file named default.conf - remove it.

Bellow I'm posting a nginx conf file, which will try to run your service with using gunicorn.sock:

upstream yourappname {
    server        unix:/home/root/app/src/gunicorn.sock fail_timeout=0;
}

server {
    root        /home/root/app/src/;

    listen        80;
    server_name   yourdomain.com *.yourdomain.com

    charset       utf-8;

    client_max_body_size                        100m;

    access_log        /home/root/app/src/logs/nginx-access.log; #you have to have logs folder in src
    error_log         /home/root/app/src/logs/nginx-error.log;

    location /static/ {
        alias         /home/root/app/src/static/;
    }


    location /media/ {
        alias         /home/root/app/src/media/;
    }

}

so now on gunicorn start script .

#!/bin/bash

ME="root"
DJANGODIR=/home/root/app/src
SOCKFILE=/home/root/app/src/gunicorn.sock
USER=root
GROUP=webapps
NUM_WORKERS=3
DJANGO_SETTINGS_MODULE=yourapp.yoursettings
DJANGO_WSGI_MODULE=yourapp.wsgi  
echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR

source /home/root/app/env/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 /home/root/app/env/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name root \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=unix:$SOCKFILE \
  --log-level=debug \
  --log-file=-

ok now in order to be able to run your gunicorn start script it has to have execution model enabled so

sudo chmod u+x gunicorn_start

now you will be able to start your gunicorn server with just using ./gunicorn_start

in top of that I'll post also supervisor conf, which will try to start your app whenever it fail, or just when system boots.

At first install supervisor. Then create a .conf file in your main directory /etc/supervisor/conf.d/your_conf_file.conf

inside insert:

[program:yourappname]
command = /home/root/app/src/gunicorn_start
user = root
stdout_logfile = /home/root/app/src/logs/gunicorn_supervisor.log
redirect_stderr = true

so having that done we have to tell our supervisor that we have just added new configuration file. Simply run those commands:

sudo supervisorctl reread sudo supervisorctl update

and in order to check if your app is running correctly just run

sudo supervisorctl status yourappname

I hope this is helpful, if you have any questions just ask

sebb
  • 1,956
  • 1
  • 16
  • 28