I've seen this question asked before but none of the responses have helped.
I'm trying to deploy my Python3/Django project to AWS with gunicorn + nginx. The project name is vicver, and when I try to run
$ gunicorn --bind 0.0.0.0:8000 vicver.wsgi
I can see my site on the AWS public IP port 8000 but without any js/css or images. When I do the same with the runserver command it works fine.
My settings.py contains the following paths:
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR,'vicver/static') ]
And here's a screenshot of the terminal
https://imgur.com/a/KhgcXeT
It works when you run runserver command because you're in development mode. But when you want to deploy your application to production env, you should use some of these options to serve static files Deploying static files
Normally I use Nginx to serve static files, and create a reverse proxy to proxy other requests into the app which served by Gunicorn. You can take a look on this Deploying django application to serve your app by using Gunicorn too.
You can not serve your static and media file in the production mode(Debug=False). The best way to serve statics and media with this structure is by using Nginx.
First, you need to collect your statics using the following command:
python manage.py collectstatic
Then you need to edit your "nginx.conf" file using the following codes. Be careful to replace "/usr/src/app/" with your project path.
server {
listen 80 backlog=2048;
location = /favicon.ico { access_log off; log_not_found off;}
location /static/ {
alias /usr/src/app/vicver/static/;
}
location /media/ {
alias /usr/src/app/vicver/media/;
}
.
.
.
}
Related
i have a linux server running under Ubuntu server 20.04 LTS, i'm using NGINX as a web server and uWSGI as an application server, i have a Django website is already installed and running perfectly, and the exact way i did that is by following the instructions from this video https://www.youtube.com/watch?v=ZpR1W-NWnp4&t=2s, but the thing is that i have several websites that needs to be installed, so i tried to redo all of it for a second website on the same server, but that did not work at all.
My question is: once i install a Django project exactly the way shown in the video using Nginx and uWSGI, how to install another Django project in a practical manner, in other words how to install multiple Django projects using Nginx and uWSGI.
this is my nginx server block saved at /etc/nginx/sites-available/project_name.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///home/user/project_name/project_name.sock;
}
# configuration of the server
server {
listen 80;
server_name domain.com www.domain.com;
charset utf-8;
# max upload size
client_max_body_size 75M;
# Django media and static files
location /media {
alias /home/user/project_name/media;
}
location /static {
alias /home/user/project_name/static;
}
# Send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/user/project_name/uwsgi_params;
}
}
Try out this bash script I wrote. It set ups as many Django websites as you want with a simple command. The script installs all the dependencies on your server, creates a virtual environment for each site, and configures a database server and Gunicorn connector.
I am new to production stuff. I deployed my first Django app on digital ocean . It's working.
The code is updating fine but the problem is with the static files which are serving through nginx.
I updated the static files and restart nginx and gunicorn but they are not present in my prod website.
I checked with chrome dev tools and it's serving old static files.
I checked these changes are present in the prod repo but somehow nginx don't seem to be using the latest static files . The steps I took
Run collectstatic command
Restart the nginx
Restart the gunicorn
The nginx config for static files
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/djangoadmin/pyapps/hkc_project;
}
This is static settings:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'hkc/static')
]
Is there anything else I have to do for this to work?
Thanks in advance
As far as I know nginx, the best way to serve the static files with it is:
location /static {
autoindex on;
alias /home/djangoadmin/pyapps/hkc_project;
}
alias is the proper method to serve entire directories.
autoindex processes requests ending with the slash character (‘/’) and produces a directory listing.
I am configuring a server with Nginx that redirect subdomains to websites (made with Django) on distant servers (on the same local network). It is working fine to serve the content of each site, but I have trouble to serve static and media files (for instance css). Here is the content of the configuration file :
server {
listen 80;
server_name myaddress.fr
location / {
proxy_pass http://192.168.0.85:8000;
}
}
And here is the end of settings.py in the Django website (which listen at 192.168.0.85:8000) :
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
Usually, when my websites are on the same server than nginx, I just have to add these lines to the nginx conf file:
location /media/ {
alias /local/path/to/media/;
}
location /static/ {
alias /local/path/to/static/;
}
How can I generalize this config to serve media and static files on a distant server (here at 192.168.0.85)? Should I install another webserver on the distant machine?
Thanks in advance for your help!
You need some way of providing a route to show those files.
One way of doing so would be to install nginx on the remote servers and proxy to it just as you do with the app itself.
An alternative, since you say this is all within the same local network. would be to use something like NFS to mount the static directories onto the proxy server, so that the existing nginx could serve them directly.
A similar option would be to configure the staticfiles app, as well as the storage of user-uploaded files, to save their files directly on the proxy.
Im using Docker to containerize my Django environment, which looks like this (simplified a bit):
A Nginx (official image) docker container
An Ubuntu docker container with uwsgi and Django
The Nginx container are serving the uwsgi just fine, but I have not found a way to serve static files.
upstream proceed {
server proceed:8000;
}
server {
listen 80;
server_name mydomain.com;
location /static {
alias /srv/www/proceed/static/; # What to do here?
}
location / {
uwsgi_pass proceed;
include uwsgi_params;
}
}
Question: Whats the best way to serve static files from another container? A solution not involving volumes are preferable.
As said #larsks you need share volumes between containers.
Your django Dockerfile need to contains volume definitions
FROM ubuntu
....
VOLUME /srv/www/proceed/static # there is path to your static
When you run nginx container you need add volumes-from argument
docker run nginx --volumes-from django
And than you nginx config will works fine.
Note! path to static content into django container and into nginx container must be the same!
I just configured my first django server for a very basic django-website. I'm using Django 1.4 and Nginx 1.0.14 with supervisor and gunicorn.
The problem is that Nginx is not serving the static files as suposed, but I don't know the why because is my first time using it.
This is the path where really lives my static files:
/home/cristian/envs/santalupe.com/santalupe/santalupe/static
And this is the setting I've in my nginx config file:
# Django admin media.
location /media/ {
autoindex on;
alias /home/cristian/envs/santalupe.com/lib/python2.7/site-packages/django/contrib/admin/static/;
}
# Site media
location /static/ {
autoindex on;
alias /home/cristian/envs/santalupe.com/santalupe/santalupe/static/;
}
Please let me know what I need to do in this case because I have not idea about the real problem.
You aren't serving the admin media from Nginx, just normal media. Try something like:
location /admin/media/ {
# this changes depending on your python version
root /home/cristian/envs/santalupe.com/lib/python2.7/site-packages/django/contrib;
}
Note how there's no trailing slash and the path ends at contrib. I'm using almost exactly this code successfully in production.
Maybe manage.py collectstatic on server help you?
Here is good description on right solution https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production