I need help to deploy a django project on a production server, I have done the following process:
- I have the Ubuntu server configured
- I have created the virtual environment
- I have installed django.
- I run ./manage.py runserver domain:port
But when accessing localhost the default apache page is shown, with the message "It Works".
I don't know where I should place the files of my application and I would like to know if maybe I am missing some important step or I need to do some additional configuration in Apache?
My configuration is :
- Web server: Apache2
- Hosting: Stratus
- Python: 3.5
- Django: 1.9
EDIT: A question I have following the official documentation, which I've already read a couple of times, is where the WSGIScriptAlias and WSGIPythonPath routes should point within the Apache2 configuration.
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py (the first slash 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, right?).
(In the end I have followed in apache since I can not 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 this be due to? Because I have to give permissions to the project folder? Or is there directly a badly referenced directory?)
With the VirtualHost is it enough or do I have to start some process for the django to start?
Do we 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 was 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 All of that has already worked for me. NOW YES! 😉 The mod_python and mod_wsgi thing I could find on google and did it.
Thanks a lot!
EDIT: In the end I have managed to put the project and everything is perfect, but installing the dependencies ( DjangoRestFramework, Pillow, Psycopg2 ) immediately afterwards I have done a service apache2 restart and poof… it has stopped working it has given error 500 again. I recommend installing all dependencies before, so the python script doesn't get corrupted
EDIT: After 15 days of going crazy every day, I managed to get the WSGI to work for me, the problem now is that I am making changes to the project and they are not reflected in the production part, now if I make a runserver and enter through port 8000 if it shows me the real project (with the changes I've made), would someone be kind enough to tell me why this happens to me? Isn't it enough to put all that, did I leave something else?
I don't get any import errors, or anything, I've 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 it shows that there is no error.
Regarding your doubts:
WSGIScriptAlias - This has to point to the absolute path to your virtualhost, i.e.
/ . If you put
/miruta , your url would be, for example,
WSGIPythonPath : it has to point where you have python installed, surely, to your virtual environment , for example:
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 previous Apache example 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 production deployments 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, to take a look at libraries like fabric