|
|
- 一、在Ubuntu上安装Python3.7
-
- ```shell
- # 升级包索引和软件
- sudo apt update
- sudo apt upgrade -y
-
- # 安装编译所需包
- sudo apt install -y build-essential zlib1g-dev libbz2-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
-
- # 官网下载 Python3.7
- wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
-
- # 解压
- tar -xzvf Python-3.7.4.tgz
-
- # 查看已安装 Python 的位置
- whereis python
-
- # 编译安装
- cd Python-3.7.4
- ./configure --prefix=/usr/lib/python3.7 # 配置安装位置
- sudo make
- sudo make install
-
- # 建立软链接
- sudo ln -s /usr/lib/python3.7/bin/python3.7 /usr/bin/python3.7
- sudo ln -s /usr/lib/python3.7/bin/pip3.7 /usr/bin/pip3.7
-
- # 替换系统默认(建议不要替换,会使系统发生改变,使得ufw无法使用)
- sudo rm -rf /usr/bin/python3
- sudo ln -s /usr/lib/python3.7/bin/python3.7 /usr/bin/python3
-
- # 默认ubuntu系统中没有pip3,直接建立软连接即可, 如果有先删除
- sudo ln -s /usr/lib/python3.7/bin/pip3.7 /usr/bin/pip3
-
- # pip 换源
- sudo mkdir ~/.pip
- sudo vi ~/.pip/pip.conf
-
- # 写入
- [global]
- index-url = http://mirrors.aliyun.com/pypi/simple/
-
- [install]
- trusted-host=mirrors.aliyun.com
- # 保存并关闭
- ```
-
- 二、安装 JDK-11
- ```shell
- sudo apt update
- sudo apt install -y openjdk-11-jdk
-
- java -version
-
- sudo update-alternatives --config java
-
- sudo nano /etc/environment
-
- JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
-
- source /etc/environment
-
- echo $JAVA_HOME
-
-
- 三、创建 Python 虚拟环境
-
- ```shell
- # 更新并安装组件
- sudo apt update
- sudo apt install -y python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools
-
- # 安装 python3-venv 软件包
- sudo apt install -y python3-venv
-
- # 创建虚拟环境
- cd ~
- python3.7 -m venv be
-
- # 激活虚拟环境
- source ~/be/bin/activate
-
- # 上传代码
- # 从本地向云主机上传代码
- # scp ...
-
- # 安装依赖(激活虚拟环境时都应该使用pip而不是pip3)
- pip install -r requirements.txt
-
- # 安装 uwsgi
- pip install uwsgi
-
- # 创建 wsgi.py 作为程序入口点,告诉 uWSGI 服务器如何与程序进行交互
- ```
-
-
- 四、安装 Nginx
- ```shell
- sudo apt-get install -y nginx
-
- # 启动
- sudo /etc/init.d/nginx start
-
- sudo systemctl restart nginx
- sudo systemctl start nginx
- sudo systemctl stop nginx
- sudo systemctl status nginx
-
-
- 五、配置 uWSGI
- ```shell
- # 启用UFW防火墙,允许访问端口5000
- sudo ufw allow 5000
-
- # 测试 uWSGI 服务(若本地开启了代理注意关闭,在虚拟环境下执行)
- uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
-
- # 从本地浏览器输入 http://<IP address>:5000
-
- # 关闭虚拟环境
- deactivate
-
- # 创建 uWSGI 配置文件
- nano ~/be/be.ini
-
- # 从 [uwsgi] 标头开始,以便 uWSGI 知道应用设置
- # 需要指定两件事:模块本神,通过引用 wsgi.py 减去扩展名,以及文件中的 callable 对象,即 app
- [uwsgi]
- module = wsgi:app
-
- # 接下来,告诉 uWSGI 以主模式启动并生成5个工作进程提供实际请求
- [uwsgi]
- module = wsgi:app
-
- master = true
- processes = 5
-
- # 接下来
- [uwsgi]
- module = wsgi:app
-
- master = true
- processes = 5
-
- socket = be.sock
- chmod-socket = 660
- vacuum = true
-
- die-on-term = true
-
- # 保存并关闭
- ```
-
-
- 六、创建 systemd 单元文件
- ```shell
- sudo nano /etc/systemd/system/be.service
-
- [Unit]
- Description=uWSGI instance to serve be
- After=network.target
-
- [Service]
- User=ubuntu
- Group=www-data
- WorkingDirectory=/home/ubuntu/be
- Environment="PATH=/home/ubuntu/be/bin"
- ExecStart=/home/ubuntu/be/bin/uwsgi --ini be.ini
-
- [Install]
- WantedBy=multi-user.target
- # 保存并关闭
-
- # 现在可以启动创建的 uWSGI 服务并启用它,以便它在启动时启动
- sudo systemctl start be
- sudo systemctl enable be
-
- # 查看状态
- sudo systemctl status be
- ```
-
-
- 七、将 Nginx 配置为代理请求
- ```shell
- sudo nano /etc/nginx/sites-available/be
-
- # 写入
- server {
- listen 80;
- server_name <公网IP>;
-
- location / {
- include uwsgi_params;
- uwsgi_pass unix:/home/ubuntu/be/be.sock;
- }
- }
- # 保存并关闭
-
- # 若要启用该配置,需将文件链接至 sites-enabled 目录
- sudo ln -s /etc/nginx/sites-available/be /etc/nginx/sites-enabled
-
- # 通过如下命令测试语法错误
- sudo nginx -t
-
- # 返回成功则重启 Nginx 进程以读取新配置
- sudo systemctl restart nginx
-
- # 再次调整防火墙,不再需要通过5000端口访问,可以删除该规则,最后可以允许访问Nginx服务器
- sudo ufw delete allow 5000
- sudo ufw allow 'Nginx Full'
-
- # 现在可以直接通过 IP 地址访问
-
- # 遇到错误使用如下方式检查
- sudo less /var/log/nginx/error.log # 检查Nginx错误日志
- sudo less /var/log/nginx/access.log # 检查Nginx访问日志
- sudo journalctl -u nginx # 检查Nginx进程日志
- sudo journalctl -u be # 检查你的Flask应用程序的uWSGI日志
- ```
-
-
-
|