Deploy django application in ubuntu through nginx+uwsgi

Pay attention to those passing by:

  1. Install python3-pip -------------sudo apt-get install python3-pip

  2. Install virtual environment and virtual environment management pack
    sudo pip3 install virtualenv (If you report an error, you need to install pip as well)
    sudo pip3 virtualenvwrapper
    Add in ~/.bashrc
    export WORKON_HOME=~/Envs
    source /usr/local/bin/virtualenvwrapper.sh (If there is no virtualenvwrapper.sh, create this file in this directory)
    source .bashrc
    Create a virtual environment: mkvirtualenv ShangOnline –p /usr/bin/python3

  3. The package that the installation project depends on in the virtual environment (mysqlclient will have a problem, you need to install the following package first)
    We can export the corresponding information of the windows virtual environment installation package through pip freeze> requirements.txt to move to ubuntu
    pip3 install -r requirements.txt mysqlclient will report an error during the installation process, follow the two steps below
    sudo apt-get install libmysqlclient-dev
    pip install mysqlclient

  4. database:
    sudo apt-get install mysql-server
    Enter the database to create your own account and assign all permissions, and refresh permissions, bind 0.0.0.0 in the configuration file
    Create the database shangonline we use in ubuntu, and transfer the data in the windows database to ubuntu through Navicat of windows
    Drag our project folder into our virtual environment, enter the project python manage.py runserver to ensure that the project can be pulled up

  5. Install uwsgi-------pip3 install uwsgi
    Test uwsgi---------uwsgi --http :8000 --module ShangOnline.wsgi

  6. Installation and configuration nginx------sudo apt-get install nginx
    After the installation is complete, the nginx service will be started automatically. When we directly access the ubuntu ip in the external windows browser, we will enter the nginx environment interface
    Create a new folder config-------new sol_nginx.conf in the project root directory

the upstream component nginx needs to connect to

upstream django {

server unix:///path/to/your/mysite/mysite.sock; # for a file socket

server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}

configuration of the server

server {

the port your site will be served on

listen 80;

the domain name it will serve for

server_name your ip address; # substitute your machine's IP address or FQDN
charset utf-8;

max upload size

client_max_body_size 75M; # adjust to taste

Django media

location /media {
alias /home/ly/ShangOnline/static/media; # points to django's media directory
}

location /static {
alias /home/ly/ShangOnline/static; # points to the static directory of django
}

Finally, send all non-media requests to the Django server.

location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
7. Add the configuration file to the startup configuration file of nginx
sudo cp /home/ly/ShangOnline/config/sol_nginx.conf /etc/nginx/conf.d/
8. Pull all required static files to the same directory
In the setting file of django, add the following line of content:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
Run command
python manage.py collectstatic
9. Run nginx
sudo /usr/sbin/nginx
It should be noted here that it must be started directly with nginx command, do not start nginx with systemctl, otherwise there will be permission problems
10. Start uwsgi through the configuration file
Create a new uwsgi.ini configuration file with the following content:

mysite_uwsgi.ini file

[ uwsgi]

Django-related settings

the base directory (full path)

chdir = /home/ly/ShangOnline/

Django's wsgi file

module = ShangOnline.wsgi

the virtualenv (full path)

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:8000

... with appropriate permissions - may be needed

chmod-socket = 664

clear environment on exit

vacuum = true
virtualenv = /home/ly/Envs/ShangOnline

logto = /tmp/mylog.log

******Note:
chdir: Indicates the directory that needs to be manipulated, that is, the directory of the project
module: The path of the wsgi file
processes: number of processes
virtualenv: the directory of the virtual environment

workon ShangOnline
uwsgi -i /home/ly/ShangOnline/config/uwsgi.ini &
10. access
http://your ip address/

Recommended Posts

Deploy django application in ubuntu through nginx+uwsgi
How to deploy Django on Ubuntu 14.04
Deploy springboot project through docker under Ubuntu
How to deploy Clojure web application on Ubuntu 14.04
Tutorial for deploying nginx+uwsgi in django project under Centos8
Install mysql-pytho in Ubuntu
Nagios3 in ubuntu serve
ubuntu16.04 deploy GPU environment
Use supervisor in ubuntu
Install Django on ubuntu
Install python in Ubuntu
Install JDK in Ubuntu19.10
How to control the ubuntu system in win10 through ssh
Detailed steps for installing Django under Python 3.6 in Ubuntu 16.04 environment