nginx + uwsgi deploy django project

Because the project needs, need to deploy django project, here is the foundation of nginx with uwsgi deploy django, by way of follow-up will be deployed docker

surroundings:

centos7

python3.5.4

django2.1.4

uwsgi2.0.18

nginx1.1.10

Specific steps and precautions:

1, in the centos compatible mounting py3.5.4

First install the dependencies

yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

Create an empty folder, and then download a different version of Python3 according to their needs, I downloaded Python3.5.4

mkdir /usr/local/python3

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

If the speed is fast enough, you can go to the official website to download directly, use WinSCP other software specified location to the repository, my store directory is / usr / local / python3, use the command:

Then extracting archive into the directory, the installation Python3

tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure --prefix=/usr/local/python3
make && make install

Finally, create a soft link

ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

Python3 test input on the command line

 

2, installation py3 in centos virtual environment

pip install virtualenv

 

3, third-party packages to restore the system in a virtual environment (using the requirement.txt)

pip install –r requirement.txt

 

4, the installation uwsgi in a virtual key (in Section 3 can also be unified installation)

pip install uwsgi

Test uwsgi:

Create a script test.py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

This code is py3 version of the test code

File into the directory:

uwsgi --http :8001 --wsgi-file test.py

Open a browser to access: 127.0.0.1: 8001 Returns a string that is tested

Note that if you are using a virtual machine do not forget to start slogan

 

5, the installation and configuration nginx

(1) Installation make, install g ++:

yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++

(2) the selected installation directory

cd /usr/local/src

(3) install the PCRE library

wget    ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz 
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.34
./configure
make && make install

(4) installed zlib library

# Enter the directory 
cd / usr / local / src
 # download the installation file 
wget http://zlib.net/zlib-1.2.11 .tar.gz
 # -extracting file 
tar -zxvf zlib-1.2.11 .tar.gz 
cd zlib -1.2.11 
. / the configure 
the Make && the make install

(5) Installation openssl (not installed by default in some vps ssl)

cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar -zxvf openssl-1.0.1t.tar.gz

(6) mounted nginx

cd /usr/local/src
wget http://nginx.org/download/nginx-1.1.10.tar.gz
tar -zxvf nginx-1.1.10.tar.gz
cd nginx-1.1.10
./configure
Make && make install

Note: This error may occur

 

yum -y install openssl openssl-devel

(7) arranged nginx (nginx.conf under niginx directory)

Note several points:

{Server 
        the listen        8089 ; port # nginx Foreign exposed 
        server_name   127.0.0.1 ; # exposed external ip, can also be used to access the domain name instead of ip: www.xxx.com
     #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {  #uwsgi配置
            uwsgi_params the include; 
            uwsgi_pass 127.0.0.1:8000 ; # corresponds to the configuration and uwsgi 
            uwsgi_read_timeout 2 ; 
        } 
    
    LOCATION / static { 
        Alias / usr / local / py3workspace / Pro / dboxStandard / StaticFile / ; # Nginx static file system access django address
         BREAK ; 
    }

(8) Start nginx

Nginx to the installation directory in / sbin: ./nginx

(9) Restart

In sbin nginx installation directory / in nginx -s reload can restart nginx

Also:

Uwsgi and nginx query port number corresponding to kill the process

netstat -nap | grep 80

Kill -9 process ID

And then restart uwsi nginxs

 

6 Notes

(1) project root directory must have third-party packages requestment.txt used to restore python virtual environment

Generate requirement.txt: 

pip freeze > requirement.txt

Restore third-party libraries:  

pip install –r requirement.txt

(2) the establishment of the project root directory profile uwsgi.ini

# uwsgi.ini
[uwsgi]

# When using nginx connection
socket = 127.0.0.1:8000
# When doing web server directly
# http=127.0.0.1:8000

# the base directory (full path)
chdir = /usr/local/py3workspace/pro/dboxStandard

# Django s wsgi file
module = dboxStandard.wsgi

# existence master
master = true

# clear environment on exit
vacuum = true

# Save the file that started the main process PID
pidfile = /usr/local/py3workspace/pro/dboxStandard/uwsgi.pid

# As daemon output, save log information to uwsgi.log. log and run in the background
daemonize = /usr/local/py3workspace/pro/dboxStandard/uwsgi.log

# maximum number of worker processes
processes = 4

# Specify a virtual environment
virtualenv = /usr/localpy3workspace/env/dbox_matching_env

socket: uwsgi external address (nginx configuration files require corresponding)

chdir: Django project root directory

master: the existence of the main process

vacuum: Empty on exit

module: Project Name .wsgi; 

daemonize: uwsgi log is located

processes: maximum number of processes

virtualen: python virtual environment directory

(3) setting.Py profile Django system needs to be added at the end:

STATIC_ROOT = os.path.join (BASE_DIR, "staticfile") Interview with Section 6.7 makes file django static nginx to find the (otherwise there is no reception style rendering)

Staticfile project is still true file storage directory

(4) mutual calls between different systems Django app or package needs to be added in the windows in a virtual environment .pth directory file, Linux systems also need to be deployed, there is a directory

It may be because a firewall to restrict access, you need to open ports (5) to access the system

 

7, system access django

External address nginx configuration: ip / domain: port / System Path

Such as: 127.0.0.1: 8089 / index

 

 

That is all I django system in the entire process of deployment process, which involves databases and message queues did not mention, which will involve the installation version of the database with django

If it is used celery before starting the system, you need to start it

celery -A Celery_pro worker -l info -P eventlet

 

Guess you like

Origin www.cnblogs.com/fclbky/p/11235923.html