Python3+django+nginx+uwsgi using Scenario Deployment

Source: Internet
Author: User
Tags install openssl virtual environment virtualenv

1 Environment Introduction and preparation
Python3+django+nginx+uwsgi using Scenario Deployment

When we finish writing a complete set of website features, we need to live in the production environment and need to accommodate high concurrent access requests. Therefore, we need to deploy the above to ensure the quality of the site's services.

1.1 Basic Environment Preparation:
Echo ' lang= ' En_gb.utf8 ' >/etc/sysconfig/i18n
Lang= "En_gb.utf8"

1.2 Dependent Environment:
(ENV) [Email protected] nginx]# python-v
Python 3.6.2
(ENV) [Email protected] nginx]# cat/etc/redhat-release
CentOS Release 6.8 (Final)
(ENV) [[Email protected] nginx]# pip List|grep Django
Django 1.11.4
(ENV) [Email protected] opsweb]# mysql-v
MySQL Ver 14.14 distrib 5.6.37
1.3 Modifying the Yum Source address:
Yum Install wget
sudo mv/etc/yum.repos.d/centos-base.repo/etc/yum.repos.d/centos-base.repo.backup
sudo wget-o/etc/yum.repos.d/centos-base.repo Http://mirrors.aliyun.com/repo/Centos-6.repo

1.4 Environment Installation:
Yum install wget openssl-devel-y # GCC compilation to use
Yum Install gcc zlib-devel openssl-devel-y

2 Installing Python
Yum-y Install Openssl-devel Readline-devel Unzip
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
Tar-xzf python-3.6.2.tgz
CD Python-3.6.2
sudo./configure--prefix=/usr/local/python36
sudo make
sudo make install
2.1 Configuring the PIP source
sudo tee/etc/pip.conf <<-' EOF '
[Global]
Index-url = Http://pypi.douban.com/simple
Trusted-host = pypi.douban.com
[List]
Format=columns
Eof

2.2 Installing the VIRTUALENV virtual environment
SUDO/USR/LOCAL/PYTHON36/BIN/PIP3 Install Virtualenv

    1. Installing Django
      3.1 Initializing the environment
      mkdir ~/reboot
      /usr/local/python36/bin/virtualenv ~/reboot/env
      3.2 Installing Django 1.11
      Source/root/reboot/env/bin/activate
      Pip Install "django>=1.11"
      Pip Install Ipython

4 MySQL Environment preparation:

Sohu Image: http://mirrors.sohu.com/

sudo rpm-ivh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
sudo yum install-y mysql mysql-server mysql-devel zlib-devel
Source/home/djangoenv/pythonenv/bin/activate
#python依赖:
Pip Install Pymysql

To modify the configuration/etc/my.cnf:

[Mysqld]
Default-storage-engine = InnoDB
Innodb_file_per_table
Collation-server = Utf8_general_ci
Init-connect = ' SET NAMES UTF8 '
Character-set-server = UTF8

Start:

sudo service mysqld start
4.1 Security Settings:
Mysql_upgrade-uroot # If the upgrade needs to be performed (uninstall the original MySQL lower version is also required)
Mysql_secure_installation # command line execution set account password, restrict remote connection, etc.

4.2 Create the underlying database:
Create database CMDB CHARACTER SET UTF8; # Create a CMDB library
CREATE USER ' CMDB ' @ '% ' identified by ' Nice '; # Create a CMDB user, password CMDB
GRANT all on cmdb.* to ' CMDB ' ('% '); # give CMDB user all CMDB library permissions

4.3 MySQL basic backup restore
Export: mysqldump-h127.0.0.1-uroot-p123456 Django >/backupfile.sql
Restore: mysql-h127.0.0.1-uroot-pruanchunxia2324 Django < Backupfile.sql

-----------------------------------Uwsgi------------------------------------------------------
5 Installation: Uwsgi
Source/root/reboot/env/bin/activate
Pip Install Uwsgi--no-cache-dir

5.1 UWSGI installation Completed query version
(ENV) [Email protected]/]# pip List|grep uwsgi
Uwsgi 2.0.15

5.2 Write the configuration file to the project directory
(ENV) [Email protected] nginx]# Cd/root/reboot/opsweb
(ENV) [Email protected] opsweb]# vim Uwsgi.ini

To set the boot configuration Uwsgi.ini:

[Uwsgi]

Project directory

ChDir =/root/reboot/opsweb

Specify the application of the project

Module=opsweb.wsgi

Number of processes

workers=2
Pidfile=/var/log/uwsgi/uwsgi.pid

Specify IP Port

http=10.10.40.112:8001

specifying static files

Static=/root/reboot/opsweb/static

Start UWSGI user name and user group

Uid=root
Gid=root

Enable the main process

Master=true

Automatically remove UNIX sockets and PID files when the service stops

Vacuum=true

Serialization of accepted content, if possible

Thunder-lock=true

Enable threads

Enable-threads=true

Set self-interrupt time

Harakiri=30

Set buffering

post-buffering=4096

Set the log directory

Daemonize=/var/log/uwsgi/opsweb.log

Specify the file path for sock

Socket=/var/log/uwsgi/uwsgi.sock

5.3 Uwsgi Start command:
sudo uwsgi--ini Uwsgi.ini
Uwsgi--reload/var/log/uwsgi/uwsgi.pid
Uwsgi--start/var/log/uwsgi/uwsgi.pid
Uwsgi--stop/var/log/uwsgi/uwsgi.pid
-----------------------------------Nginx------------------------------------------------------
6 Installing Nginx:
Yum Install Nginx-y

Configuration file Modification:
  server {      listen       80;      server_name  10.10.40.112;      location /static {          alias /root/reboot/opsweb/static;      }      location / {          include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
Uwsgi_pass 10.10.40.112:8001;
          uwsgi_pass unix:/var/log/uwsgi/uwsgi.sock;      }  }

7 Django Configuration
7.1 Django Configuration Modifications:

setting.py to add a static resource path to the Nginx call

Django can put static resources under its own templates. That's a command. All static resources are collected.

Static_root = Os.path.join (Base_dir, "static/")

#然后到项目目录下执行:
Python manage.py collectstatic

Modifying a configuration file

DEBUG = False allowed_hosts = [' * ']

    1. Fault Error debugging
      8.1 Fault Detection

      Found unable to find static file
      8.2 Locating errors by viewing the Nginx error log
      Locating the problem is unable to access the file under the/static directory

      8.3 Attempt to grant permissions to the configuration directory

      Discovery directory permissions are in effect

      and restart the Nginx service to find that the problem persists.

      By checking Nginx service discovery, Nginx thread is started for Nginx user

      So

You need to reload the Nginx configuration file here
/etc/init.d/nginx Reload

8.4 Final Agent Effect
Note: Before acting as an agent, such as access, IP behind the need to follow the port, after the successful agent only enter the IP of the proxy to access the backend website, thereby increasing security and concurrent traffic.

Python3+django+nginx+uwsgi using Scenario Deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.