Need help: (I’m new to Django)
I am using:
Nginx, Centos 7, Python 3.10, Django 4.1.6, Linode
What I want to achieve is:
domain.com/project1/
domain.com/project2/
and so on…
At root domain I’m using WordPress and I want to use Django in subdirectories (sub-folders), as mentioned above.
I have other websites running on this same VPS/IP as well (just informing).
I have been struggling online for the last week and cannot find a proper solution. Finally, writing here for help.
If I have missed any required information, then please ask me. Kindly guide me while considering that you are guiding a Django newbie!
Looking forward!
I’ve never used guicorn for a production deployment, so I can’t really help there. But I have done dozens of deployments using nginx and uwsgi, so I’m hoping what I can provide here will help.
There are at least three separate issues that need to be addressed.
- Associating what you’re calling
domain.com/project1/
with a project.
- Getting the static resources for each project deployed to their specific project’s staticfiles directory. (We use nginx to serve those static files.)
- Getting the
{% static %}
and {% url %}
tags to render to the right URLs.
This gives us the following configuration directives:
Our location
directive would look something like this:
location /project1/ {
uwsgi_pass unix:///run/project1/uwsgi.sock;
include /etc/nginx/uwsgi_params.txt;
proxy_set_header SCRIPT_NAME /project1;
}
You can repeat this for each project as necessary.
Note that we use unix domain sockets to connect nginx to uwsgi
Similarly, for the static files:
location /project1/static {
root /home;
rewrite ^/(.*)/$ /$1 break;
}
You would then set STATIC_ROOT = '/home/project1/static'
and STATIC_URL = 'static/'
Note: Because the first match gets the request, this section actually needs to appear before the section above in the configuration file.
That also means that if you’re using WordPress as a root URL as well, it needs to appear last.