python+Nginx+uWSGI instructions

Installation Environment

Remote: CentOS 7.4 x64 (django.example.com)

Python: Python3.6.5

Django: Django 2.0.4

nWSGI:   uwsgi -2.0.15

Nginx:  nginx- 1.10.2-1.el6

 

 

 

1. System environment configuration

1. Turn off iptables and selinux

# su - root

# service iptables stop

# setenforce 0

# vi /etc/sysconfig/selinux

Revise

SELINUX=disabled

2. Add local host DNS

# vi /etc/hosts

127.0.0.1    django.example.com

2. Python configuration

1. Install python3.6.5 source and dependencies

# yum install epel-release -y

# yum groupinstall "Development tools" -y

# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel -y

 

2. Compile and install python3.6.5 and pip package manager

# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz --no-check-certificate

# tar xf Python-3.6.5.tar.xz

# cd Python-3.6.5

# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"

# make && make altinstall

 

3. Install virtualenv

# pip3.6 install --upgrade pip

# pip3.6 install virtualenv

 

3. Nginx configuration

1. Install nginx package

# yum install nginx -y

 

2.配置nginx with nWSGI

# vi /etc/nginx/conf.d/ django .conf

 
server {
    listen  80;
    server_name  django.example.com;  

    charset utf-8;

    access_log  /var/log/nginx/django_access.log;
    error_log   /var/log/nginx/django_error.log;
    
    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /usr/share/nginx/html/django.example.com;
    }

    client_max_body_size 20M;
    
    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/etc/uwsgi/uwsgi-django.sock;
        uwsgi_read_timeout 30s;
        uwsgi_send_timeout 30s;
    }

}

4. Django+uWSGI configuration

1. uWSGI configuration

# mkdir -p /etc/uwsgi

# vi / etc / uwsgi / uwsgi- django .ini

[uwsgi]
project = django.example.com
base = /data/www

chdir = %(base)/%(project)
home = %(base)/%(project)/.py3env
module = myproject.wsgi:application

pidfile = /tmp/uwsgi-master-django.pid

master = true
processes = 2
enable-threads = true

# use unix socket because it is more secure and faster than TCP socket
socket = /etc/uwsgi/uwsgi-django.sock
chmod-socket = 660
uid = nginx
gid = nginx

vacuum = true
die-on-term = true

logto = /var/log/nginx/uwsgi-django.log

2. Configure the Django base folder

# cd /usr/share/nginx/html

# mkdir django.example.com

# cd django.example.com

# virtualenv -p /usr/local/bin/python3 .py3env

 

3. Open virtualenv python3 environment

# source .py3env/bin/activate

 

4. Install Django related modules in this environment

# pip install django uwsgi PyMySQL

 

5. Create the uWSGI startup script

# mkdir -p /etc/uwsgi/bin

# vi /etc/systemd/system/uwsgi-django.service

[Unit]
    Description=uWSGI instance to serve myproject

[Service]
    BASE=/data/www/django.example.com
    ENV=$BASE/.py3env
    ExecStartPre=-/usr/bin/bash -c 'chown -R nginx:nginx /etc/uwsgi'
    ExecStart=/usr/bin/bash -c 'source /usr/share/nginx/html/django.example.com/.py3env/bin/activate; uwsgi --ini /etc/uwsgi/uwsgi-django.ini'

[Install]
    WantedBy=multi-user.target

Five. Django project configuration

1. Ensure that the virtualenv python3 environment is enabled

# cd /usr/share/nginx/html/django.example.com/

# source .py3env/bin/activate

 

2. Create a Django project

# django-admin startproject myproject .

 

3. Add static directory

# vi myproject/settings.py

Add at the end of the line:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

4. Create a local SQLlite file

Tip: Here we use SQLlite instead of other databases as the DB of our project

# ./manage.py makemigrations
# ./manage.py migrate

5. Create a project administrator account

# ./manage.py createsuperuser

6. Generate project static file directory

# ./manage.py collectstatic

 

7. Modify the wsgi entry file

# vi myproject / wsgi.py

import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
sys.path.append('/usr/share/nginx/html/django.example.com')

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

8. Add ALLOWED_HOSTS

# vi myproject/settings.py

ALLOWED_HOSTS = ['django.example.com']

9. Modify permissions (executable and consistent with the nginx startup user)

# chmod -R 755 /etc/uwsgi

# chown -R nginx:nginx /etc/uwsgi

# chmod -R 755 /usr/share/nginx/html/django.example.com

# chown -R nginx:nginx /usr/share/nginx/html/django.example.com

 

10. Start nginx+uwsgi

# systemctl restart nginx

# systemctl restart uwsgi-django

 

ps. Owes flask, multiple instances, optimization, detailed explanation of each configuration file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324821889&siteId=291194637