Django deployed online Ubuntu + nginx + uwsgi + django3.1 + Python 3.8

Keywords: Python Django Nginx Ubuntu

Django project deployment Online

preface

Because many pits were taken during the deployment of Django project, write down the pits encountered in the deployment process for reference only.

Environment and third party Library

The deployment environment and third-party libraries used in this article are as follows: (different environments and versions may have different results in the same step)
Environmental Science:

Django 3.1
Python 3.8 (3.7 here is also OK. 3.8 is configured by default when purchasing ECs, so the version is not returned to 3.7)
uwsgi 2.0.14
Ubantu 20.04

Third party Library

DjangoUeditor
Pillow

Before you start, make sure your computer has the most basic Python environment and Pip installed

Installing Nginx

The command is as follows:

sudo add-apt-repository ppa:nginx/stable
apt-get update
apt-get install nginx

After the installation is completed, Nginx will start automatically. At this time, enter the ifconfig command to query the local IP address. Enter the IP address in the browser to check whether the installation is successful.
Seeing this interface indicates that the installation is successful:

Install uWSGI

Enter command

pip install uwsgi==2.0.14

The installation is complete.

After installation, enter uwsgi to see if this command is available
If not, reinstall

uWsgi installation failed

If no error is reported in the previous step, please skip this step
When purchasing ECs, you can enter the above command directly, and an error will be reported. The reasons are as follows:

  • pip not updated
  • No dependencies were added

Therefore, in the console, enter:

 python -m pip install --upgrade pip  (pip to update)
 apt-get install libpython3.8-dev  (Add dependency according to Python (modify the command with the version number of)

The version number of Python can be viewed by entering Python on the console

After execution, enter pip install uwsgi==2.0.14 again to install

Environment installation

During deployment, the project needs to configure the corresponding third library on the ECS. Please skip the configured one.

pip install Pillow 
pip install Django==3.1 

Django runs the test and runs the command under the project root directory:

python manage.py runserver 0.0.0.0:1234

Check whether the project can run normally through the browser and check whether the function is defective

DjangoUeditor reports an error
If it can run normally before deployment, change to the server and report an error. The information is as follows (random cut Figure)

 Please find out the error file and vim Go in and comment out 93 lines
vi /usr/local/lib/python3.8/dist-packages/django/forms/boundfield.py

Please refer to this blog for details

https://blog.csdn.net/qq_38320702/article/details/82685821

So far, the environment required for online deployment has been configured. Next, enter the configuration section

Nginx configuration

First, let's put back the root directory of the Linux system

cd ../../../../../

Enter the default file and edit it

cd /etc/nginx/sites-available/
vi default

Empty all the contents of the modified file and replace with:

server {
    listen 80;

    listen [::]:80;

    server_name 127.0.0.1;

    location / {
        include  uwsgi_params;
        uwsgi_pass  127.0.0.1:8000;  
    }

    location /static {   
               alias /home/yanfriends_server/static;
    }
    location /media {
                alias /home/ubuntu/website/media;
    }
    location /api {
               autoindex on;
    }
}

The path of static is based on your static_ The path of root is consistent
The path of media is the path of dynamic resources in your project

After the change is completed, restart the Nginx service

service nginx restart

uWSGI configuration

Please enter the path of your project, that is, the directory with the following files, and create the uwsgi.ini file

InI file configuration information is as follows:

[uwsgi]
chdir = /home/ubuntu/website
module = website.wsgi:application 
socket = 127.0.0.1:8000
master = true 

# The above four are core configuration items

#vhost = true / / multi station mode
#No site = true / / the entry module and file are not set in the multi site mode
#workers = 2 / / number of child processes
#reload-mercy = 10
#vacuum = true / / clean up files on exit and restart
#max-requests = 1000
#limit-as = 512
#buffer-size = 30000
#pidfile = /var/run/uwsgi9090.pid    
daemonize = /home/ubuntu/website/website/run.log
disable-logging = true 

Among them,
chdir is the root path of the project
module is the main app path of the project (I don't know what to say, right? After entering, there must be a wsgi.py file)
Daemon is the running log file of uwsgi, which is needed for later debug ging

Please modify the file path of your project according to the above information

After modification, run in the uwsig.ini file directory:

uwsgi --ini uwsgi.ini 

At this point, the deployment of the project is completed. You can view the project through IP address access

Django deployment online media file access denied (403)

A solution to this problem:
Enter directory

cd .../.../.../.../.../
cd /etc/nginx
vi nginx.congif

If you have something to do, you can change it first and make it up later

Posted by garek007 on Fri, 03 Sep 2021 17:48:05 -0700