1👍
Docker + NGINX + Gunicorn + Django
Django project:
djangoapp
- ...
- media
- static
- manage.py
- requirements.txt
nginx
- Dockerfile
- nginx.conf
docker-compose.yml
Dockerfile
Dockerfile:
FROM ubuntu:20.04
# Prevents Python from writing pyc files to disc
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0
# https://hub.docker.com/r/fnndsc/ubuntu-python3/dockerfile
RUN apt-get update \
&& apt-get install -y python3-pip gunicorn
RUN mkdir /app
COPY djangoapp/requirements.txt requirements.txt
COPY djangoapp /app/
RUN pip3 install -r requirements.txt
WORKDIR /app/
EXPOSE 8000
CMD ["gunicorn3", "-b", "0.0.0.0:8000", "djangoapp.wsgi:application", "--workers=2"]
docker-compose.yml:
version: '3'
services:
djangoapp:
build: .
container_name: djangoapp1
volumes:
- .:/djangoapp
- static:/app/static
- media:/app/media
nginx:
build: ./nginx
container_name: nginx
environment:
- SERVER_NAME=8.43.162.54
ports:
- "80:80"
volumes:
- .:/djangoapp
- static:/app/static
- media:/app/media
restart: always
networks:
default:
driver: bridge
volumes:
media:
static:
nginx/Dockerfile:
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/
nginx/nginx.conf:
server {
listen 80;
server_name $SERVER_NAME;
location /static/ {
alias /app/static/;
}
location /media/ {
alias /app/media/;
}
location / {
proxy_set_header Host $host;
proxy_pass http://djangoapp1:8000;
}
}
settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = []
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
1👍
sudo nano /etc/nginx/sites-enabled/inventory.conf
the upstream component nginx needs to connect to
upstream django {
server unix:///home/user/Inventory-System/inventory.sock;
}
#configuration of the server
server {
listen 80;
server_name trading.code www.trading.code;
charset utf-8;
# max upload size
client_max_body_size 75M;
# Django media and static files
location /media {
autoindex on;
alias home/user/Inventory-System/media/;
}
location /static {
autoindex on;
alias home/user/Inventory-System/static/;
}
# Send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include home/user/Inventory-System/uwsgi_params;
} }
then
user@user: /etc/nginx/sites-enabled$ sudo rm default
user@user: $ sudo /etc/init.d/nginx restart
finally
check http://127.0.0.0/media/
- [Django]-Unusual warning with djcelery, billiard, and the django_settings_module
- [Django]-Django: Should I use LocMemCache caching?
- [Django]-Loading a pickled list just once – Django\Python
- [Django]-How can I make Django use select_related() on a foreign key in the admin site?
- [Django]-Model_to_dict is not returning DateTimeField with auto_now_add = True
0👍
I also faced this problem and I add this in my sudo nano /etc/nginx/sites-available/project file
server {
listen 80;
server_name 45.76.42.234;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /static/ {
root /home/django/Django_tutorials/21_Django-deployment;
}
location /media/ {
root /home/django/Django_tutorials/21_Django-deployment;
}
location /Images/ {
alias /home/django/Django_tutorials/21_Django-deployment/Images/;
}
}
where Images is my image folder that contains uploaded images
- [Django]-Adding context variable into change_view not possible because extra_context not a dictionary
- [Django]-How to route to specific viewset methods Django
- [Django]-Combine two model managers for a queryset
- [Django]-SlugField in Django and overriding save
Source:stackexchange.com