由于ubuntu16默认用的python3.5,需要重新安装一个3.8的python
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz
tar -xf Python-3.8.0.tar.xz
cd Python-3.8.0
./configure prefix=/usr/local/python3
make
sudo make altinstall
sudo rm /usr/bin/python
sudo ln -s /usr/local/python3/bin/python3.8 /usr/bin/python
sudo rm /usr/bin/python
sudo ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
sudo rm /usr/bin/pip
sudo ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip
sudo rm /usr/bin/pip3
sudo ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
cd到项目目录,安装缺失的库
sudo pip3 install -r requirement.txt
运行时候会出现_sqlite3找不到
Modulenetfounderror: no module named'_sqlite3'
安装下面这个库
sudo apt-get install libsqlite3-dev
重新上面的步骤,编译一把python即可
cd Python-3.8.0
./configure prefix=/usr/local/python3
make
sudo make altinstall
安装ngnix
sudo apt-get install nginx
sudo /etc/init.d/nginx start
打开浏览器访问你的服务器,输入http://localhost/,你应该能看到Nginx欢迎页.
配置Nginx
首先删除掉Nginx的默认配置文件:
sudo rm /etc/nginx/sites-enabled/default
创建一个我们应用使用的新配置文件/etc/nginx/sites-enabled/my_nginx.conf:
upstream flask{
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
server {
# 监听80端口
listen 80;
# 本机
server_name localhost;
# 默认请求的url
location / {
#请求转发到gunicorn服务器
proxy_pass http://flask;
#设置请求头,并将头信息传递给服务器端
proxy_set_header Host $host;
}
}
配置gunicorn
安装命令如下:
pip3 install gunicorn
安装信息如下:
[root@server01 ~]# pip3 install gunicorn
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting gunicorn
Downloading https://mirrors.aliyun.com/pypi/packages/69/ca/926f7cd3a2014b16870086b2d0fdc84a9e49473c68a8dff8b57f7c156f43/gunicorn-20.0.4-py2.py3-none-any.whl (77kB)
|████████████████████████████████| 81kB 7.1MB/s
Requirement already satisfied: setuptools>=3.0 in /usr/local/python3/lib/python3.7/site-packages (from gunicorn) (39.0.1)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.0.4
WARNING: You are using pip version 19.2.1, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
配置全局命令:安装完毕之后,全局环境是无法直接执行的,需要找到二进制文件软链接到/usr/bin路径下。
# 安装之后,无法直接执行命令
[root@server01 ~]# gunicorn -h
-bash: gunicorn: command not found
搜索安装之后,gunicorn二进制可执行文件的位置:
[root@server01 ~]# find / -name "*gunicorn*" -ls | grep python3 | grep bin
405121 4 -rwxr-xr-x 1 root root 236 Dec 12 08:31 /usr/local/python3/bin/gunicorn
设置软链接如下:
[root@server01 ~]# ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn
[root@server01 ~]#
[root@server01 ~]# ls -ll /usr/bin/gunicorn
lrwxrwxrwx 1 root root 31 Dec 12 08:38 /usr/bin/gunicorn -> /usr/local/python3/bin/gunicorn
配置软链接之后,就可以全局环境使用gunicorn了,例如查看版本如下:
[root@server01 ~]# gunicorn -v
gunicorn (version 20.0.4)
启动项目
cd到项目目录下
export环境变量
export DEBUG=True
export FLASK_ENV=development
gunicorn -w 4 -b 127.0.0.1:5000 --access-logfile access.log --error-logfile error.log run:app
重启nginx
sudo nginx -s reload
后台运行的话加一个-D参数
gunicorn -D -w 4 -b 127.0.0.1:5000 --access-logfile access.log --error-logfile error.log run:app