Mrli
别装作很努力,
因为结局不会陪你演戏。
Contacts:
QQ博客园

阿里云nginx+wsgi部署flask

2019/10/23 flask Python 环境部署
Word count: 1,325 | Reading time: 7min

云服务器nginx+wsgi部署flask

更新源

1
2
sudo apt-get update -y
sudo apt-get upgrade -y

安装nginx

1
2
3
sudo apt-get install nginx -y
sudo /etc/init.d/nginx start #(start可以改成restart/stop)
#或是sudo service nginx start

然后浏览器输入服务器IP或是127.0.0.1,观察是否有welcom to nginx!

安装py3和virtualenv

1
2
sudo apt-get install git python3 python3-pip -y
sudo pip3 install virtualenv

修改python版本:

法一:(不建议)
1
2
3
4
5
6
7
8
9
$ gedit ~/.bashrc   	#gedit .bash_aliases
在顶部加入一行alias python=python3
$ source ~/.bashrc #或是source ~/.bash_aliases o
$ python --version
就会发现是Python 3.5.2啦

/*or
Open your .bashrc file nano ~/.bashrc. Type alias python=python3 on to a new line at the top of the file then save the file with ctrl+o and close the file with ctrl+x. Then, back at your command line type source ~/.bashrc. Now your alias should be permanent.
*/
Ubuntu16.04切换python3和python2
▲.切换Python3为默认版本:(建议)
1
2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

切换Python2为默认版本:

1
$ sudo update-alternatives --config python

安装虚拟环境

virtualenv
1
2
3
$ cd /var/www
# 最好指定下python解释器
$ sudo virtualenv -p /usr/bin/python3 env35
pipenv
1
$ pipenv install

MySQL

1
2
3
4
5
6
7
8
9
$ sudo apt install mysql-server mysql-client -y
$ cd /etc/mysql/mysql.conf.d
$ mysql -p -u root
> password for root:

> use mysql;
> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
> flush privileges;
最后按Ctrl + z 退出

安装、测试uwsgi

1
$ sudo pip3 install uwsgi

编辑/var/www下的uwsgi.ini

1
2
3
4
5
6
7
8
9
10
11
12
[uwsgi]
chdir=/home/apollo3d/Documents/Beidou
wsgi-file=wsgi.py
home=/home/apollo3d/Documents/env
callable=app;
master=True
processes=10
socket= :81
chmod socket=666
vacuum=True
max-requests=5000
#pythonpath=/var/www/env

编辑/var/www下的nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name 127.0.0.1;
charset utf-8;
client_max_body_size 75M;
location /static{
alias /home/apollo3d/Documents/Beidou/static;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:81;
# uwsgi_param UWSGI_PYHOME /home/apollo3d/Documents/Beidou/env;
uwsgi_param UWSGI_SCRIPT app:app; # 启动flask的文件:Flask的实例
}

2004/10/20 |

编辑/var/www下的uwsgi_params

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
uwsgi_param QUERY_STRING 	$query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;

uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;

uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

软连接

1
2
3
4
$ rm /etc/nginx/sites-enabled/default
$ cd /etc/nginx/sites-enabled
$ sudo ln -s /var/www/nginx.conf Beidou
$ ls

重启nginx

sudo /etc/init.d/nginx restart

运行uwsgi

1
2
cd /var/www
uwsgi --ini uwsgi.ini

让uwsgi自启动

vim /etc/rc.local(注意非虚拟环境也得安装uwsgi模块)

1
2
3
添加下面代码:
/usr/local/bin/uwsgi --ini /var/www/uwsgi.ini
exit 0

uwsgi的热启动

在uwsgi.ini中加入

py-autoreload=1

重启一下:killall -9 uwsgi/usr/local/bin/uwsgi --ini /var/www/uwsgi.ini


总结

关于etc/ linit. d

如果你使用过inux系统,那么你一定听说过 init. d目录,这个目录到底是干嘛的呢?它归根结底只做了一件事情,但这件事情非同小可,是为整个系统做的,因此它非常重要。init.d目录包含许多系统各种服务的启动和停止脚本

关于 /etc/rc.local

rc.local也是我经常使用的一个脚本,该脚本是在系统初始化级别脚本运行之后再执行的,因此可以安’, '地在里面添加你想在系统启动之后执行的脚本.

总结

Linux是灵活的,正因为它的灵活性,我们总是可以找到许多不同的办法来解决同一个问题,服务的例子就是一个很好的佐证,有了 /etc/init.d目录下的脚本,再加上 /etc/rc. local这个利器,你可以放心的确保你的服务可以完美的启动和运行

/etc/nginx/sites-available/default

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

Author: Mrli

Link: https://nymrli.top/2019/01/17/阿里云nginx-wsgi部署flask/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
12-27 Team Leader
NextPost >
前端Vue框架学习
CATALOG
  1. 1. 云服务器nginx+wsgi部署flask
    1. 1.0.1. 更新源
    2. 1.0.2. 安装nginx
    3. 1.0.3. 安装py3和virtualenv
    4. 1.0.4. 修改python版本:
      1. 1.0.4.1. 法一:(不建议)
      2. 1.0.4.2. Ubuntu16.04切换python3和python2
      3. 1.0.4.3. ▲.切换Python3为默认版本:(建议)
    5. 1.0.5. 安装虚拟环境
      1. 1.0.5.1. virtualenv
      2. 1.0.5.2. pipenv
    6. 1.0.6. MySQL
    7. 1.0.7. 安装、测试uwsgi
    8. 1.0.8. 编辑/var/www下的uwsgi.ini
    9. 1.0.9. 编辑/var/www下的nginx.conf
    10. 1.0.10. 编辑/var/www下的uwsgi_params
    11. 1.0.11. 软连接
    12. 1.0.12. 重启nginx
    13. 1.0.13. 运行uwsgi
    14. 1.0.14. 让uwsgi自启动
    15. 1.0.15. uwsgi的热启动
    16. 1.0.16. 总结