Deploy Django with nginx+uwsgi under centos

Try to use centos nginx + uwsgi to deploy a previous django project. The process of solving the problem is long and arduous, but it turns out to be worthwhile. If you are deploying a Django project, I hope it can help you

Prerequisites

What I use: python3.6 mysql5.7 django project 3.2

First, download python and pip in centos, and use git to download the django project in the gitee warehouse.

Text

Install virtualenv

pip install virtualenv

Create an aa — virtual environment

mkdir /env
virtualenv /env/aa --python=python3.6.8

Activate the virtual environment

source /env/aa/bin/activate

Exit the virtual environment is deactivate

Enter the virtual environment as shown in the figure

mysql installation and remote connection

The higher version of mysql is compatible with the lower version
Install mysql
yum install mysql-server
start mysql
sudo systemctl start mysqld
state
sudo systemctl status mysqld

The status after the installation is started is as follows

Connect to local mysql remotely

mysql -uroot -p

The first time I installed it, I directly entered the mysql interface (and…remember to add port 3306 to your security group). At this time, there was a problem about permissions in the remote connection, which made it impossible to connect to the database—>mysql– ->user needs to be changed

update user set host="%" where user = "root";

Open the local navacat mysql

Connection name: any

Host/ip address: your Alibaba Cloud virtual machine public network IP/virtual machine address

Don’t fill in others for the time being because no password is set for the first time

The connection test is completed. After creating the connection, directly create a db dier with the same name in navacat mysql to replace the previous database

Dump to the current database with the same name, and then try to query it in linux

Local operation of django project

Go to the directory of your project and run manage.py

cd /home/www/day1
python manage.py runserver

if python manage.py runserver 0.0.0.1:8000
You can try public network ip:8000 to visit (security group plus port)

Pictures of successful local operations

If not successful, such as the question: Did you install mysqlclient? You need to install the corresponding dependency package

pip install Django
pip install pillow
pip install mysqlclient or yum install mysql-devel
.....

Attachment: If your django project setting.py connects to the database with a password, you need to change the password. I won’t go into details here

Install uwsgi + nginx

In fact, the important thing is to modify the two files

pip install libpcre3 libpcre3-dev
pip install uwsgi
pip install nginx

Enter /etc/nginx to modify nginx.conf (here is written and copied directly)

Modify the server_name domain name in http—“server

Must change location /static {alias /home/www/day1/app02/static; }

It is the static file of the django project that has no style and these cannot be loaded

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name www.xxxa.cn xxxa.cn;
        charset utf-8;
        root /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location /static {
            alias /home/www/day1/app02/static;
            
        }

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

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name_;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }

}

systemctl start nginx starts the service or restarts the service in the sbin directory ./nginx -s reload

Then verify in the nginx directory

cd /etc/nginx
nginx -t

The following code appears to indicate that the configuration is complete

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

At this time, access the public network ip:80 as shown in the figure

Next configure uwsgi

Create a new file aa_uwsgi.ini in the django—“manage.py directory with content

chdir the project directory under the virtual environment

module = where wsgi.py in the project is located

virtualenv= about independent python environment

[uwsgi]

socket=127.0.0.1:8000
chdir=/home/www/day1
module = djago2jie.wsgi
virtualenv=/env/aa/
processes=1
master = true
vacuum = true

buffer-size=65535

Finish saving and then run this file uwsgi –ini aa_uwsgi.ini in the django project directory

Web page public network ip:80/home page access, mobile terminal can also access

And the port configured in the security group

Stay: domain name resolution

Remember to review!