SoFunction
Updated on 2024-10-26

Diango + uwsgi + nginx project deployment of the whole process (can be accessed outside the network)

preamble

I deployed the django project through nginx uwsgi, queried a lot of information, encountered a lot of problems, and finally completed the deployment, while in a happy mood, write an essay for the partners who have struggled to find a solution like me to provide some ideas.

The methodology is as follows

Install Nginx:

# Install nginx
sudo apt-get install nginx

# Some useful commands
#Start nginx
sudo /etc//nginx start 
# Restart nginx
 8sudo /etc//nginx restart
#stop nginx
sudo /etc//nginx stop

# A very violent approach, I like it
sudo killall nginx

Install uwsgi.

 pip install uwsgi
 
 #take note ofuwsgiNeeds to be run in a virtual environment

Configure uwsgi.

# Create a conf folder in the project directory and put the nginx and uwsgi files in it, not required # but a good habit to get into!

#my_uwsgi.ini
ite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir   = /to/your/project/# This is the path to the project
# Django's wsgi file
module   = #The project name should be replaced with the name of your own project, i.e. the folder where it is located
# the virtualenv (full path)
home   = /home/ubuntu/.virtualenvs/virtual environment name# This is the path to the virtual environment

# process-related settings
# master
master   = true
# maximum number of worker processes
processes  = 10
# the socket (use the full path to be safe
socket   = 127.0.0.1:8080# This is used to interface with Nginx, the port number can be changed, my project will be uwsgi as a local service, the external network can not be directly accessed, using nginx as a proxy, so use the local address.
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum   = true
~

Configuring nginx

# The following is in mysite_nginx.conf, and this file name can also be started arbitrarily
# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
 # server unix:///path/to/your/mysite/; # for a file socket
 server 127.0.0.1:8080; # This is used to interface with uwsgi, to be consistent with my_uwsgi.ini write
}

# configuration of the server
server {
 # the port your site will be served on
 listen  8000;#This port is used by nginx to listen to uwsgi, the default is 80, in short, the project is accessed through the following server_name:8000
 # the domain name it will serve for
 server_name  ; #This ip is the ip of the server
 charset  utf-8;

 # max upload size
 client_max_body_size 75M; # adjust to taste

 # Django media
 location /media {
  alias /your/project/media; # This directory is the project's meda directory
 }
 location /static {
  alias /your/project/static; # This directory is the project's static directory
 }

 # Finally, send all non-media requests to the Django server.
 location / {
  uwsgi_pass django;#This one interfaces with uwsgi
  include  uwsgi_params; # This parameter can be found if you write nginx the way I write it
 }
}

Link the nginx configuration file to the startup configuration directory:

# Pay attention to modify the following path and file name, haha do not just copy and paste ah!
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

Modify the files in your django project to add the

# To comment out STATICFILES_DIRS = [(BASE_DIR, 'static')], Debug in production mode also change to False
STATIC_ROOT = (BASE_DIR, "static/")

Package the static files and let nginx proxy them:

python  collectstatic

Starting nginx,uwsgi

 sudo /etc//nginx restart
# Go to the conf folder, or the directory where the configuration files are located
# Change it to your name
uwsgi -i 

Access:

ip:port (port as configured in)

Summary:

Write to this is almost up, the project can run up, nginx, uwsgi advanced configuration is still learning, I hope this article will help you, thank you.

Finally, I would like to remind you that there are a lot of configuration file templates on the Internet, compare and modify the places where I write comments, do not miss.

Well, the above is the full content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.

Reference: /en/latest/tutorials/Django_and_nginx.html

 /en/latest/