Django+Nginx+UWSGI deployment project

Keywords: Nginx pip vim Django

For projects written locally, the running results can only be viewed by ourselves and cannot be used by others.
If you want to go online, you can deploy the project on the cloud server through Django+Nginx+UWSGI. At this time, other users only need to enter the URL address to use the project.

Pre deployment server related configuration

The ECS used in this article is Alibaba cloud server, and the system is Ubuntu 18.04.

  1. Install dependent environment
apt update
apt -y install man gcc make lsof ssh openssl tree vim dnsutils
apt -y install psmisc sysstat curl telnet traceroute wget iputils-ping
apt -y install net-tools libbz2-dev libpcre3 build-essential
apt -y install libpcre3-dev libreadline-dev libsqlite3-dev
apt -y install libssl-dev llvm zlib1g-dev git zip p7zip
apt -y install mysql-server mysql-client libmysqlclient-dev
  1. Install Nginx and UWSGI
apt install nginx
apt install uwsgi
  1. Install Python environment

The system comes with python3, and pip3 needs to be installed to install other libraries.

apt install python3-pip
  1. Install and configure virtual development environment
pip3 install virtualenv
pip3 install virtualenvwrapper

Create a directory to place the virtual development environment.

mkdir ~/.virtualenvs

Modify environment variables

vim ~/.bashrc

In the last part, add the virtual environment directory location, python location, and virtualenvwrapper.sh file location. (change according to the actual situation)

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

source is required to take effect after modification

source ~/.bashrc

Note:
How to view related paths

which python3
find / -name virtualenvwrapper.sh

After the environment variables are configured, create a virtual development environment for Django

mkvirtualenv env_django

Enter the virtual development environment

workon env_django

Then upload the project to the server (for example, place it in the directory / var/www /), and install the related dependency Library in the virtual development environment

# Run under project root
pip freeze > requirement.txt
# Then in the virtual development environment
pip freeze -r requirement.txt

After successful execution, all the dependent libraries required for the project are installed.

At this point, the installation and configuration of the virtual environment are completed.

Nginx configuration

Next, you need to configure Nginx.
Enter the directory / etc / nginx / sites available / and copy the configuration file for modification

cd /etc/nginx/sites-available/
cp default blog
vim blog

The configuration of Nginx is as follows

server {
		# Listening port. You need to add configuration to alicloud console and open this port
        listen 8002;

        index index.html index.htm index.nginx-debian.html;
		# Server IP address
        server_name xxx.xxx.xxx.xxx;
        location / {
                include uwsgi_params;
                # Need to be consistent with the configuration file of UWSGI
                uwsgi_pass 127.0.0.1:8923;
        }
        # Project static resource path
        location /static {
                alias /var/www/simple_blog/static;
        }


}

Then go to the directory CD / etc / nginx / sites enabled /, and create a soft connection

ln -s ../sites-available/blog blog

Restart the Nginx service for the modification to take effect

service nginx restart

In this case, you can create a new file (for example, 1.txt) in the static resource directory of the project, add a line of data (for example, hello), and then enter it in the browser

http://xxx.xxx.xxx.xxx:8002/static/1.txt

If the data hello is returned, it proves that Nginx is working properly. Otherwise, the related error information needs to be processed.

UWSGI configuration

First, install the uwsgi Library in the virtual development environment

pip install uwsgi

Then create the uwsgi.ini configuration file in the root directory of the project, as follows

[uwsgi]
# External access address should be consistent with Nginx configuration
socket = 127.0.0.1:8923
# Project root
chdir = /var/www/simple_blog
module = simple_blog.wsgi:application
master = true
# Log location. You need to create a file to store logs in the relevant directory
daemonize = /root/blog/uwsgi_f.log

Start UWSGI

uwsgi --ini uwsgi.ini

So far, UWSGI configuration is complete

Result example

After the configuration is completed, the project is online, and the browser can enter the URL to access the project.

Published 53 original articles, won praise and 1010 visitors
Private letter follow

Posted by pinxxx on Fri, 06 Mar 2020 01:05:24 -0800