python – Help with going to production with Django 1.9

Question:

I need help to deploy a django project on a production server, I have done the following process:

  1. I have the Ubuntu server configured
  2. I have created the virtual environment
  3. I have installed django.
  4. I run ./manage.py runserver domain: port

But when accessing localhost the apache default page is shown, with the message "It Works".

I don't know where I should locate the files of my application and I would like to know if perhaps I am skipping an important step or should I do some additional configuration in Apache?

My configuration is:

  • Web Server: Apache2
  • Hosting: Strato
  • Python: 3.5
  • Django: 1.9

EDIT: One question I have followed the official documentation, I've read a couple of times is where you have to point the WSGIScriptAlias routes and WSGIPythonPath within the Apache2 configuration.

WSGIScriptAlias ​​/ /path/to/mysite.com/mysite/wsgi.py (the first bar is what makes me doubt, I don't know if it has to point to the root like here or the vhost where I want to have the web).

WSGIPythonPath /path/to/mysite.com (and in theory this should point to where I have python installed, correct?).

EDIT:

(In the end I have followed apache since I cannot migrate the server to nginx).

After having configured the VirtualHost in Apache2 following the example:

  • Now when I access mydomain.com I get a 403 Forbidden. (What could that be due to? What do I have to give permissions to the project folder? Or is there a badly referenced directory?)

  • Is the VirtualHost enough or should I start a process to start django?

  • Do you have to redirect port 8000 to 80?

EDIT: Here I leave my VirtualHost

<VirtualHost *:80>
    DocumentRoot "/var/www/vhosts/dominio.com/chewbacca.dominio.com"

    Alias "/media" "/var/www/vhosts/dominio.com/chewbacca.dominio.com/django-project/chewbacca_project/media"
    Alias "/static" "/var/www/vhosts/dominio.com/chewbacca.dominio.com/django-project/chewbacca_project/static"

    <Directory /var/www/vhosts/dominio.com/chewbacca.dominio.com/django-project/chewbacca_project/static/>
            Order deny,allow
            Allow from all
    </Directory>

    <Directory /var/www/vhosts/dominio.com/chewbacca.dominio.com/django-project/chewbacca_project/media/>
            Require all granted
    </Directory>

    WSGIDaemonProcess chewie python-path=/var/www/vhosts/dominio.com/chewbacca.dominio.com/django-project/:/var/www/vhosts/dominio.com/chewbacca.dominio.com/django-env/lib/python3.5/site-packages
    WSGIProcessGroup chewie
    WSGIScriptAlias / /var/www/vhosts/dominio.com/chewbacca.dominio.com/django-project/chewbacca_project/chewbacca_project/wsgi.py
    WSGIPassAuthorization On

    <Directory /var/www/vhosts/dominio.com/chewbacca.dominio.com/django-project/chewbacca_project/chewbacca_project>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>

EDIT: I think one of the problems I have is that the server works with PLESK , but hey.

The Apache log gives me the following error:

  • Fatal Python error: PyEval_AcquireThread: NULL new thread state

[ssl: warn] [pid 14845] AH01909: RSA certificate configured for chewbacca.mydomain.com:443 does NOT include an ID which matches the server name

EDIT: In the end I have been able to fix the problems, one of the problems I had (and important) is that I had not created the symbolic link from pip3 to pip ( ln -s / usr / bin / pip3 / usr / bin / pip ) after of all that, it has already worked for me. NOW SIIIII! 😉 The mod_python and mod_wsgi I could find it in google and I did.

Thanks a lot!

EDIT: In the end I have managed to put the project and everything perfect, but installing the dependencies ( DjangoRestFramework, Pillow, Psycopg2 ) then I have done a service apache2 restart and poof … it has stopped working it has returned to give error 500. I recommend installing all dependencies before so python script doesn't get corrupted

EDIT: After 15 days of going crazy every day, I have managed to make the WSGI work for me, the problem comes now that I am making changes in the project and they are not reflected in the part in production, now if I do a runserver and enter through port 8000 if it shows me the real project (with the changes that I have made), someone would be so kind to tell me why that happens to me? It is not enough to put all that, I leave something else?

I do not get any errors from imports, or anything, I have even done a ./manage.py check –deploy and it gives me 8 warnings (normal), what am I missing? I am doing something wrong? I have also done a ./manage.py test project and I get that there is no fault.

Answer:

Regarding your doubts:

WSGIScriptAlias: it has to point to the absolute path with respect to your virtualhost i.e / . If you put /miruta , your url would be, for example, midominio.com/miruta/

WSGIPythonPath: you must point where you have installed python surely your virtual environment, for example: /var/www/[PATH_AL_PROYECTO]/venv/local/bin/python2.7

A possible configuration for your apache could be something like this:

<VirtualHost *:80>    
    DocumentRoot /var/www/[RAIZ_DEL_APACHE/HOSTING]

    Alias /media /var/www/[PATH_A_CARPETA_MEDIA]
    Alias /static /var/www/[PATH_A_CARPETA_STATICS]

    <Directory /var/www/[PATH_A_CARPETA_STATICS]>
            Order deny,allow
            Allow from all
    </Directory>

    <Directory /var/www/[PATH_A_CARPETA_MEDIA]>
            Require all granted
    </Directory>

    WSGIDaemonProcess [NOMBRE_QUE_QUIERAS] python-path=/var/www/[PATH_AL_PROYECTO]:/var/www/[PATH_AL_PROYECTO]/venv/lib/python3.5/site-packages
    WSGIProcessGroup [NOMBRE_QUE_QUIERAS]
    WSGIScriptAlias / /var/www/[PATH_AL_PROYECTO]/[PATH_DEL_WSGI]/wsgi.py
    WSGIPassAuthorization On

    <Directory /var/www/[PATH_AL_PROYECTO]>
    <Files wsgi.py>
            Require all granted
    </Files>
    </Directory>
</VirtualHost>

To better understand what you have to replace, let's assume the following:

Your project has these routes:

/var/www/htm/
|--miproyecto/
   |--venv/ --> carpeta donde has creado tu entorno virtual, supongamo con virtualenv
   |--statics/  --> carpeta donde se almacenan los ficheros estáticos cuando haces python manage.py collectstatic
   |--media/ --> carpeta de los ficheros multimedia
   |--miapp/
   |  |--wsgi.py
   |--manage.py

With this, the apache example above would look like:

<VirtualHost *:80>    
    DocumentRoot /var/www/html

    Alias /media /var/www/html/miproyecto/media/
    Alias /static /var/www/html/miproyecto/statics/

    <Directory /var/www/html/miproyecto/statics/>
            Order deny,allow
            Allow from all
    </Directory>

    <Directory /var/www/html/miproyecto/media/>
            Require all granted
    </Directory>

    WSGIDaemonProcess miproyectoweb python-path=/var/www/html/miproyecto/:/var/www/html/miproyecto/venv/lib/python3.5/site-packages
    WSGIProcessGroup miproyectoweb
    WSGIScriptAlias / /var/www/html/miproyecto/miapp/wsgi.py
    WSGIPassAuthorization On

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

Anyway, if we talk about deployments to production in general with Django, the most preferred deployment is with Django + Gunicorn + Supervisor + Nginx .

I also recommend, if you want to carry out a continuous deployment project, take a look at libraries such as fabric

Scroll to Top