程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python web development 01

編輯:Python

Get New knowledge :

nginx + gunicorn + flask Deploy web project

nginx Installation and configuration

One 、 Install compilation tools and library files
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
Two 、 install PCRE( Give Way Nginx Support Rewrite function )
# Download the latest version of , Be careful not to use pcre2
wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gz
tar -xvf pcre-8.42.tar.gz
cd pcre-8.42
# Installation and compilation
./configure
make && make install
# see pcre edition
pcre-config --version
3、 ... and 、 install nginx
# download
wget https://nginx.org/download/nginx-1.15.9.tar.gz
tar -xvf nginx-1.15.9.tar.gz
cd nginx-1.15.9
# Compilation and installation
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.42
make && make install
# View version
/usr/local/webserver/nginx/sbin/nginx -v
3、 ... and 、nginx To configure
# establish Nginx Users who run it www
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
# You can also not create a new user , But use root Users can
# Settings contain multiple profiles , stay nginx.conf Add... At the bottom
include vhost/*.conf;
# start-up
/usr/local/webserver/nginx/sbin/nginx
/usr/local/webserver/nginx/sbin/nginx -s reload # Reload configuration file
/usr/local/webserver/nginx/sbin/nginx -s reopen # restart Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop # stop it Nginx
# /usr/local/webserver/nginx/conf/vhost/demo.tilesrow.com.conf
server
{
# nginx Default listening 80 Port number , You can customize
listen 80;
#listen [::]:80;
# server_name It's the local address
server_name xx.com;
# Static file resources accessed by default
index index.html index.htm index.php default.html default.htm default.php;
root /data/project/demo1;
access_log /usr/local/webserver/nginx/logs/xx.com.log;
}

Intranet mapping port configuration

server
{
listen 80;
#listen [::]:80;
server_name demo.xx.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:8001;
}
access_log /usr/local/webserver/nginx/logs/demo.xx.com.log;
}

The result configuration is as follows :

server{
listen 8000;
server_name 192.168.123.201;
location {
include uwsgi_params;
# uwsgi_pass 127.0.0.1:9000;
proxy_pass http://127.0.0.1:9000/;
}
}

gunicorn Installation configuration

Pay attention to python In the virtual environment of the project .
Use pip install gunicorn install gunicorn

After installation, you can start and stop the service by command .

The start of the project

Use the following command to start the project
gunicorn -w 5 -b 127.0.0.1:9000 wsgi:application
among 127.0.0.1:9000 yes flask The boot port of the application and IP,wsgi Is a startup project under the root directory of the project. The file name is wsgi.py application yes flask Instance object name .

Or use custom gunicorn.py The configuration file
gunicorn -c app/gunicorn_config.py wsgi:application
among .py It's a custom profile

as follows :

import multiprocessing
bind = '0.0.0.0:9000' # binding ip And port number 
backlog = 512 # Listening to the queue 
chdir = '/usr/local/webserver/nginx/software_project' #gunicorn To switch to > working directory 
timeout = 40 # Overtime 
worker_class = 'gevent' # Use gevent Pattern , You can also use sync Pattern , The default is sync Pattern 
workers = multiprocessing.cpu_count() * 2 # Number of processes 
threads = 2 # Specify the number of threads to open per process 
# Set maximum concurrency 
worker_connections = 2000
# Set the process file directory 
pidfile = '/var/run/gunicorn.pid'

centos Use yum Online installation python3

step :

  1. install epel
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  1. install epel-release Tools
yum install epel-release
  1. Find the support in the library python3 edition
yum list | grep python3 | more
  1. install python3
yum install python36
  1. Test the installation for success
python3

Linux Lower installation Python A virtual environment Virtualenv

It needs to be installed first python3 Environment .
1、 First , Use pip Command to install virtualenv modular :

# pip3 install virtualenv

Test for successful installation

# virtualenv --version

Installation successful , The installed virtualenv Version number of

3、 Create a stand-alone operating environment , And name

# virtualenv --no-site-packages venv

venv Is the name of the virtual environment , You can name it according to your needs .
among Direct use command virtualenv You can create an independent Python Running environment , Add parameters **–no-site-packages**, You can install to the system Python The third-party packages in the environment are not controlled in the independent environment , In this way, we can get a without any third-party packages Python The environment .

4、 New Python The environment is placed in the current directory venv Catalog , With venv This Python Environmental Science , We can start using this virtual environment , It needs to be activated :

# source venv/bin/activate

It should be noted that source In virtual environment bin In the catalog activate , After successful activation , We can see that the name of the virtual environment is added in front of the terminal command prompt

5、 If you don't use this virtual environment for the time being , sign out venv Environmental Science

(venv) # deactivate

6、 Other commands

① View the current virtual machine environment Directory

# worken

② Switch virtual environment

# workon venv2

③ Exit virtual environment

# deactivate

④ Delete virtual environment

# rmvirtualenv venv

Install and configure python A virtual environment virtualenv good after i, Is to install the third-party package required by the project , You can deploy the project to the server .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved