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?).
EDIT:
(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?)
Is VirtualHost enough or do I need to start a process for 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.
Thank you very much!
EDIT: In the end I managed to put the project and everything was perfect, but installing the dependencies ( DjangoRestFramework, Pillow, Psycopg2 ) immediately afterwards I did 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 is now 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 have made), would someone be so kind as to tell me why this happens to me? Is it not enough to put all that, do 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: 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 : it has to point where you have python installed, surely, to 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:
To better understand what you have to replace, let's assume the following:
Your project has these routes:
With this, the previous Apache example would look like:
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