Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

224 linhas
4.7 KiB

há 3 anos
  1. 一、在Ubuntu上安装Python3.7
  2. ```shell
  3. # 升级包索引和软件
  4. sudo apt update
  5. sudo apt upgrade -y
  6. # 安装编译所需包
  7. sudo apt install -y build-essential zlib1g-dev libbz2-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
  8. # 官网下载 Python3.7
  9. wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
  10. # 解压
  11. tar -xzvf Python-3.7.4.tgz
  12. # 查看已安装 Python 的位置
  13. whereis python
  14. # 编译安装
  15. cd Python-3.7.4
  16. ./configure --prefix=/usr/lib/python3.7 # 配置安装位置
  17. sudo make
  18. sudo make install
  19. # 建立软链接
  20. sudo ln -s /usr/lib/python3.7/bin/python3.7 /usr/bin/python3.7
  21. sudo ln -s /usr/lib/python3.7/bin/pip3.7 /usr/bin/pip3.7
  22. # 替换系统默认(建议不要替换,会使系统发生改变,使得ufw无法使用)
  23. sudo rm -rf /usr/bin/python3
  24. sudo ln -s /usr/lib/python3.7/bin/python3.7 /usr/bin/python3
  25. # 默认ubuntu系统中没有pip3,直接建立软连接即可, 如果有先删除
  26. sudo ln -s /usr/lib/python3.7/bin/pip3.7 /usr/bin/pip3
  27. # pip 换源
  28. sudo mkdir ~/.pip
  29. sudo vi ~/.pip/pip.conf
  30. # 写入
  31. [global]
  32. index-url = http://mirrors.aliyun.com/pypi/simple/
  33. [install]
  34. trusted-host=mirrors.aliyun.com
  35. # 保存并关闭
  36. ```
  37. 二、安装 JDK-11
  38. ```shell
  39. sudo apt update
  40. sudo apt install -y openjdk-11-jdk
  41. java -version
  42. sudo update-alternatives --config java
  43. sudo nano /etc/environment
  44. JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
  45. source /etc/environment
  46. echo $JAVA_HOME
  47. 三、创建 Python 虚拟环境
  48. ```shell
  49. # 更新并安装组件
  50. sudo apt update
  51. sudo apt install -y python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools
  52. # 安装 python3-venv 软件包
  53. sudo apt install -y python3-venv
  54. # 创建虚拟环境
  55. cd ~
  56. python3.7 -m venv be
  57. # 激活虚拟环境
  58. source ~/be/bin/activate
  59. # 上传代码
  60. # 从本地向云主机上传代码
  61. # scp ...
  62. # 安装依赖(激活虚拟环境时都应该使用pip而不是pip3)
  63. pip install -r requirements.txt
  64. # 安装 uwsgi
  65. pip install uwsgi
  66. # 创建 wsgi.py 作为程序入口点,告诉 uWSGI 服务器如何与程序进行交互
  67. ```
  68. 四、安装 Nginx
  69. ```shell
  70. sudo apt-get install -y nginx
  71. # 启动
  72. sudo /etc/init.d/nginx start
  73. sudo systemctl restart nginx
  74. sudo systemctl start nginx
  75. sudo systemctl stop nginx
  76. sudo systemctl status nginx
  77. 五、配置 uWSGI
  78. ```shell
  79. # 启用UFW防火墙,允许访问端口5000
  80. sudo ufw allow 5000
  81. # 测试 uWSGI 服务(若本地开启了代理注意关闭,在虚拟环境下执行)
  82. uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
  83. # 从本地浏览器输入 http://<IP address>:5000
  84. # 关闭虚拟环境
  85. deactivate
  86. # 创建 uWSGI 配置文件
  87. nano ~/be/be.ini
  88. # 从 [uwsgi] 标头开始,以便 uWSGI 知道应用设置
  89. # 需要指定两件事:模块本神,通过引用 wsgi.py 减去扩展名,以及文件中的 callable 对象,即 app
  90. [uwsgi]
  91. module = wsgi:app
  92. # 接下来,告诉 uWSGI 以主模式启动并生成5个工作进程提供实际请求
  93. [uwsgi]
  94. module = wsgi:app
  95. master = true
  96. processes = 5
  97. # 接下来
  98. [uwsgi]
  99. module = wsgi:app
  100. master = true
  101. processes = 5
  102. socket = be.sock
  103. chmod-socket = 660
  104. vacuum = true
  105. die-on-term = true
  106. # 保存并关闭
  107. ```
  108. 六、创建 systemd 单元文件
  109. ```shell
  110. sudo nano /etc/systemd/system/be.service
  111. [Unit]
  112. Description=uWSGI instance to serve be
  113. After=network.target
  114. [Service]
  115. User=ubuntu
  116. Group=www-data
  117. WorkingDirectory=/home/ubuntu/be
  118. Environment="PATH=/home/ubuntu/be/bin"
  119. ExecStart=/home/ubuntu/be/bin/uwsgi --ini be.ini
  120. [Install]
  121. WantedBy=multi-user.target
  122. # 保存并关闭
  123. # 现在可以启动创建的 uWSGI 服务并启用它,以便它在启动时启动
  124. sudo systemctl start be
  125. sudo systemctl enable be
  126. # 查看状态
  127. sudo systemctl status be
  128. ```
  129. 七、将 Nginx 配置为代理请求
  130. ```shell
  131. sudo nano /etc/nginx/sites-available/be
  132. # 写入
  133. server {
  134. listen 80;
  135. server_name <公网IP>;
  136. location / {
  137. include uwsgi_params;
  138. uwsgi_pass unix:/home/ubuntu/be/be.sock;
  139. }
  140. }
  141. # 保存并关闭
  142. # 若要启用该配置,需将文件链接至 sites-enabled 目录
  143. sudo ln -s /etc/nginx/sites-available/be /etc/nginx/sites-enabled
  144. # 通过如下命令测试语法错误
  145. sudo nginx -t
  146. # 返回成功则重启 Nginx 进程以读取新配置
  147. sudo systemctl restart nginx
  148. # 再次调整防火墙,不再需要通过5000端口访问,可以删除该规则,最后可以允许访问Nginx服务器
  149. sudo ufw delete allow 5000
  150. sudo ufw allow 'Nginx Full'
  151. # 现在可以直接通过 IP 地址访问
  152. # 遇到错误使用如下方式检查
  153. sudo less /var/log/nginx/error.log # 检查Nginx错误日志
  154. sudo less /var/log/nginx/access.log # 检查Nginx访问日志
  155. sudo journalctl -u nginx # 检查Nginx进程日志
  156. sudo journalctl -u be # 检查你的Flask应用程序的uWSGI日志
  157. ```