一、知识概述
1.uWSGI
uWSGI是Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
2.Nginx
nginx是一款轻量级的Web服务器、反向代理服务器及电子邮件(IMAP/POP3)代理服务器。主要有反向代理,负载均衡的功能。
3.
实验步骤
一、更新系统软件软件包
[root@www2 ~]#yum update -y
二、安装可能使用的依赖
[root@www2 ~]yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
[root@www2 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@www2 ~]# 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 libffi-devel
已加载插件:fastestmirror
[root@www2 ~]# yum install libffi-devel -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
三、安装python3
[root@www2 ~]#yum -y install python3
[root@www2 ~]# python3 -V
Python 3.6.8
[root@www2 ~]# pip3 install --upgrade setuptools -i
[root@www2 ~]# pip3 -V
pip 21.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
[root@www2 ~]#ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@www2 ~]#ln -s /usr/local/python3/bin/pip3.7 /usr/local/bin/pip3
四、配置虚拟环境。
1.更新pip并下载安装virtualenv、virtualenvwrapper
[root@www2 ~]#pip3 install virtualenv -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com --proxy=192.168.156.218:10000
[root@www2 ~]# python3 -m pip install --upgrade virtualenv -i https://pypi.tuna.tsinghua.edu.cn /simple/ --trusted-host pypi.tsinghua.edu.cn --proxy=192.168.156.218:10000
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
[root@www2 ~]#pip3 install virtualenvwrapper -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tsinghua.edu.cn --proxy=192.168.156.218:10000
2.修改.bashrc文件
[root@www2 ~]# find / -name "virtualenvwrapper.sh"
/usr/local/bin/virtualenvwrapper.sh
[root@www2 ~]# whereis python3
python3: /usr/bin/python3 /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3.6-config /usr/bin/python3.6m-config /usr/bin/python3.6m-x86_64-config /usr/lib/python3.6 /usr/lib64/python3.6 /usr/local/lib/python3.6 /usr/include/python3.6m /usr/share/man/man1/python3.1.gz
[root@www2 ~]# vim ~/.bashrc
[root@www2 ~]# source ~/.bashrc
[root@www2 ~]# vim ~/.bashrc
[root@www2 ~]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 # 指定virtualenvwrapper执行的python版本
export WORKON_HOME=/root/app # 指定虚拟环境存放目录,.virtualenvs目录名可自拟
source /usr/local/bin/virtualenvwrapper.sh # virtualenvwrapper.sh所在目录
[root@www2 ~]#
在.bashrc文末添加内容 如上图所示位置 如下内容
VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3 # 指定virtualenvwrapper执行的python版本
export WORKON_HOME=$HOME/.virtualenvs # 指定虚拟环境存放目录,.virtualenvs目录名可自拟
source /usr/local/python3/bin/virtualenvwrapper.sh # virtualenvwrapper.sh所在目录
假若source文件时出现情况-bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
则说明.bashrc文件中的virtualenvwrapper.sh所在目录错误。解决方法:
[root@www2 ~]# find / -name "virtualenvwrapper.sh"
/usr/local/bin/virtualenvwrapper.sh #.sh文件地址,找到virtualenvwrapper.sh所在目录并修改.bashrc文件[root@www2 ~]#source /usr/local/bin/virtualenvwrapper.sh #virtualenvwrapper.sh所在目录
3.建立虚拟环境软连接
[root@www2 ~]# find / -name "virtualenvwrapper.sh"
/usr/local/python3/bin/virtualenvwrapper.sh
[root@www2 ~]# ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
4.创建虚拟环境
[root@www2 ~]# mkvirtualenv -p python3 py3env
created virtual environment CPython3.6.8.final.0-64 in 967ms
(py3env) [root@www2 ~]# lsvirtualenv
py3env
======
(py3env) [root@www2 ~]# deactivate
5.创建一个名为pytest的文件夹来存放项目
[root@www2 ~]# cd /root/app/
[root@www2 app]# ll
总用量 0
drwxr-xr-x. 5 root root 77 9月 22 14:50 py3env
[root@www2 app]# mkdir -p /root/app/test
[root@www2 app]# cd /root/app/test
6.为这个项目创建并开启虚拟环境
[root@www2 test]# virtualenv env
created virtual environment CPython3.6.8.final.0-64 in 438ms
[root@www2 test]# source env/bin/activate
五、进入项目virtualenv环境中安装flask
(env) [root@www2 test]# pip3 install flask -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tsinghua.edu.cn --proxy=192.168.156.218:10000
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
上传第一个项目文档app.py
(env) [root@www2 test]# mkdir -p /root/app/pytest
(env) [root@www2 test]# vi /root/app/pytest/app.py
(env) [root@www2 test]# cat /root/app/pytest/app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!\r"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
(env) [root@www2 test]#
运行测试程序
(env) [root@www2 test]# systemctl start firewalld
(env) [root@www2 test]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
(env) [root@www2 test]# firewall-cmd --permanent --add-port=80/tcp
success
(env) [root@www2 test]# firewall-cmd --permanent --add-port=5000/tcp
success
(env) [root@www2 test]# python3 /root/app/pytest/app.py
* Serving Flask app 'app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
新开一个shell窗口执行curl http://127.0.0.1:8080/ 可以看到有Hello World返回说明应用在flask框架下运行没有问题
[root@www2 ~]# curl http://127.0.0.1:8080
Hello World!
六、virtualenv环境中安装、配置uwsigi
uWSGI是一个Web Server,并且独占uwsgi协议,但是同时支持WSGI协议、HTTP协议等,它的功能是把HTTP协议转化成语言支持的网络协议供python使用。有点类似于Java的web服务容器中间件tomcat
(env) [root@www2 test]# pip3 install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trust ed-host pypi.tsinghua.edu.cn --proxy=192.168.156.218:10000
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting uwsgi
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/24/fd/93851e4a076719199868d4c918cc93a5 2742e68370188c1c570a6e42a54f/uwsgi-2.0.20.tar.gz (804 kB)
|████████████████████████████████| 804 kB 280 kB/s
Preparing metadata (setup.py) ... done
Building wheels for collected packages: uwsgi
Building wheel for uwsgi (setup.py) ... done
Created wheel for uwsgi: filename=uWSGI-2.0.20-cp36-cp36m-linux_x86_64.whl size=547307 sha256 =b67cc5c603ea8c3048e22469a6feaf070eefeafc34130ca6c8e43c3651821588
Stored in directory: /root/.cache/pip/wheels/f6/25/ce/48ed46c53614f603a4b4b5ead0159c974937332 62a92fa13b2
Successfully built uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.20
新建一个uwsgi.ini配置文件
(env) [root@www2 test]# vi /root/app/test/uwsgi.ini
(env) [root@www2 test]# cat /root/app/test/uwsgi.ini
[uwsgi]
# uwsgi 启动时所使用的地址与端口
http = 127.0.0.1:8001
#虚拟环境目录
home = /root/app/test/env
#项目目录
pythonpath = /root/app/pytest
#指向网站根目录
chdir = /root/app/pytest
#python启动程序文件
wsgi-file = app.py
#python程序内用于启动的application变量名
callable = app
#处理器数
processes = 4
#线程数
threads = 2
#状态监测地址
stats = 127.0.0.1:9191
(env) [root@www2 test]#
运行uwsgi
(env) [root@www2 test]# uwsgi uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.20 (64bit) on [Fri Sep 23 11:17:31 2022] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-44) on 22 September 2022 07:43:10
os: Linux-3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022
nodename: www2
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /root/app/test
detected binary path: /root/app/test/env/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /root/app/test
your processes number limit is 31191
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:8001 fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
PEP 405 virtualenv detected: /root/app/test/env
Set PythonHome to /root/app/test/env
Python main interpreter initialized at 0x90b0e0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 416880 bytes (407 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
added /root/app/pytest/ to pythonpath.
failed to open python file app.py
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 12679)
spawned uWSGI worker 1 (pid: 12680, cores: 2)
spawned uWSGI worker 2 (pid: 12681, cores: 2)
spawned uWSGI worker 3 (pid: 12682, cores: 2)
spawned uWSGI worker 4 (pid: 12683, cores: 2)
*** Stats server enabled on 127.0.0.1:9191 fd: 15 ***
invalid request block size: 21573 (max 4096)...skip
^CSIGINT/SIGTERM received...killing workers...
worker 1 buried after 0 seconds
worker 3 buried after 0 seconds
worker 4 buried after 0 seconds
worker 2 buried after 1 seconds
goodbye to uWSGI.
启动以后通过访问curl http://127.0.0.1:8001有Hello World!的返回信息表示uwsgi已经成功启动,并且应用程序正常。
[root@www2 ~]# curl http://127.0.0.1:8001
Hello World!
四、配置Nginx反向代理
[root@www2 ~]# yum install nginx -y
已加载插件:fastestmirror
[root@www2 ~]# vim /etc/nginx/nginx.conf
[root@www2 ~]# cat /etc/nginx/nginx.conf
# 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 4096;
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 8081;
server_name 127.0.0.1;
root /usr/share/nginx/html;
# access_log /root/app/test/logs/access.log;
# error_log /root/app/test/logs/error.log;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
#proxy_pass http://127.0.0.1:8001;
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
uwsgi_param UWSGI_PYHOME /root/app/test/env;
uwsgi_param UWSGI_CHDIR /root/app/test;
uwsgi_param UWSGI_SCRIPT app:app;
}
# error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# 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 HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
[root@www2 ~]#
nginx的server配置如下:
server {
listen 8081;
server_name 127.0.0.1;
root /usr/share/nginx/html;
# access_log /root/app/test/logs/access.log;
# error_log /root/app/test/logs/error.log;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
#proxy_pass http://127.0.0.1:8001;
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
uwsgi_param UWSGI_PYHOME /root/app/test/env;
uwsgi_param UWSGI_CHDIR /root/app/test;
uwsgi_param UWSGI_SCRIPT app:app;
}
修改后保存并退出,重启nginx服务
[root@www2 nginx]# systemctl start nginx
[root@www2 nginx]# netstat -npl|grep :
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 14557/python3
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 994/nginx: master p
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 947/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1282/master
tcp6 0 0 :::22 :::* LISTEN 947/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1282/master
udp 0 0 127.0.0.1:323 0.0.0.0:* 713/chronyd
udp6 0 0 ::1:323 :::* 713/chronyd
raw 0 0 0.0.0.0:112 0.0.0.0:* 7 6387/keepalived
raw 0 0 0.0.0.0:112 0.0.0.0:* 7 6387/keepalived
[root@www2 nginx]#
启动以后通过访问curl http://127.0.0.1:8081有Hello World!的返回信息表示已经成功启动,并且应用程序正常。
[root@www2 ~]# curl http://127.0.0.1:8081
Hello World!