1.安裝python
略
2.虛擬環(huán)境
2.1安裝vertualenv
pip3 install virtualenv
2.2創(chuàng)建虛擬環(huán)境
創(chuàng)建保存環(huán)境的目錄:
mkdir venvs
創(chuàng)建虛擬環(huán)境:
[root@root /]# virtualenv /home/xxx/venvs/flask2 --python=python3
查看虛擬環(huán)境:
[root@root venvs]# ls
flask2
2.3激活虛擬環(huán)境
activiate是激活虛擬環(huán)境的命令腳本,在虛擬環(huán)境的bin目錄下
[root@root bin]# ls
activate activate.fish activate.ps1 deactivate.nu pip3 pip3.7 python3 wheel wheel-3.7
activate.csh activate.nu activate_this.py pip pip-3.7 python python3.7 wheel3 wheel3.7
執(zhí)行activate激活環(huán)境
[root@root bin]# source activate
(flask2) [root@root bin]#
3.環(huán)境-uwsgi
3.1安裝uwsgi
激活虛擬環(huán)境,安裝uwsgi
source activate
pip install uwsgi
3.2基于uwsgi運(yùn)行flask項(xiàng)目
3.2.1命令的方式
uwsgi --http :8080 --wsgi-file app.py --callable app
3.2.2配置文件(推薦)
uwsgi.ini
[uwsgi]
socket = 127.0.0.1:8001
chdir = /home/xxx/data/code/xxx
wsgi-file = app.py
callable = app
processes = 1
virtualenv = /home/xxx/venvs/flask2
啟動(dòng)命令
uwsgi --ini uwsgi.ini
ctrl + c停止
后臺(tái)啟動(dòng)
uwsgi -d --ini uwsgi.ini
停止
(flask2) [root@root flask]# ps -ef | grep uwsgi
root 7114 6277 0 22:13 pts/1 00:00:00 uwsgi --ini uwsgi.ini
root 7118 6277 0 22:15 pts/1 00:00:00 grep --color=auto uwsgi
(flask2) [root@root flask]# kill -9 7114
4.環(huán)境-nginx
4.1安裝
4.1.1 yum安裝
yum install nginx -y
yum安裝失敗,未找到nginx包,換使用壓縮包編譯安裝
4.1.2 編譯安裝
原文:https://www.kuangstudy.com/bbs/1511610238649233410
下載nginx包
下載鏈接:https://nginx.org/en/download.html
1.nginx的環(huán)境依賴下載
編譯工具gcc,一般系統(tǒng)都存在
yum install gcc-c++
pcre正則表達(dá)式庫
yum install -y pcre pcre-devel
zlib解壓和壓縮庫
yum install -y zlib zlib-devel
OpenSSL安全套接字密碼庫
yum install -y openssl openssl-devel
2.解壓
tar -zxvf nginx-1.18.0.tar.gz
3.執(zhí)行configure
./configure
說明:–prefix參數(shù)表示把nginx編譯到指定目錄
如:--prefix=/www/server/nginx/
表示編譯到/www/server/nginx/
目錄下
4.編譯
make
5.安裝
make install
默認(rèn)安裝目錄為/usr/local/nginx
查看nginx安裝是否成功
在/usr/local/nginx/sbin目錄下執(zhí)行,無報(bào)錯(cuò)則啟動(dòng)成功
./nginx
瀏覽器輸入ip:80查看
如果訪問失敗,首先檢查安全組是否開放80端口,若開放查看防火墻
查看防火墻是否開啟
systemctl status firewalld
修改iptables防火墻規(guī)則,允許訪問80
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
4.2配置
普通請(qǐng)求 -> 8001
/static/ -> /home/xxx/data/flask/static
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream flask {
server 127.0.0.1:8001;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
uwsgi_pass flask;
include uwsgi_params;
}
location /static {
alias /home/xxx/data/flask/static;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
4.3啟動(dòng)
在/usr/local/nginx/sbin目錄下執(zhí)行,無報(bào)錯(cuò)則啟動(dòng)成功
./nginx -s restart
5.部署
使用uwsgi后臺(tái)啟動(dòng)項(xiàng)目,在瀏覽器輸入url查看文章來源:http://www.zghlxwxcb.cn/news/detail-498288.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-498288.html
到了這里,關(guān)于python Flask web項(xiàng)目uwsgi + nginx部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!