基于Flask+Apache+WSGI等模块配置Deep Learning应用功能网站(Ubuntu 22.04服务器)

首先预设你已经在本地搭建好了网站,只需要放到服务器上,然后配置Apache / Nginx等反向代理软件,即可用服务器ip地址/域名进行访问。

因为操作不规范/坑太多,而反反复复配了至少10遍。接下来的教程主要是总结规范流程,讲解遇到常见的bug如何解决。

注意: 在服务器上建议装python env的虚拟环境,不建议装conda的虚拟环境,因为如何在wsgi后缀名的文件里激活conda的虚拟环境,本人还没有找到正确的教程 😦。

接下来进入正题:

  1. 服务器上建deep learning应用功能所需的虚拟环境:
bash 复制代码
python3 -m venv pytorch_env

激活虚拟环境

bash 复制代码
source pytorch_env/bin/activate

按需求装自己环境需要的软件包,例如我的环境需要:

bash 复制代码
pip install Flask
pip install Werkzeug
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
pip install transformers
  1. 在虚拟环境中装以下两个包, 如果可以直接装mod_wsgi, 则直接跳过mod_wsgi-httpd, 如果有报错,则需要先装第一个:
bash 复制代码
pip install mod_wsgi-httpd
pip install mod_wsgi

mod_wsgi主要是wsgi和apache的中间件(官网原话:The mod_wsgi package implements a simple to use Apache module which can host any Python web application which supports the Python WSGI specification)。

  1. 全局安装libapache2-mod-wsgi-py3模块
powershell 复制代码
 sudo apt-get install libapache2-mod-wsgi-py3

mod_wsgi 适配器, 为应用提供接口:Python 3 WSGI adapter module for Apache.

  1. 相信Apache你已经提前下载好了,并已经按照官方教程配置好了,可以开始使用。我们下面主要是讲如何配置应用的,在网站原目录下建一个"app.wsgi"文件:
python 复制代码
#!/usr/bin/python
import sys
import logging
import os
logging.basicConfig(stream=sys.stderr)
#插入网站的根目录
sys.path.insert(0,"/var/www/html/ddi-website")

#激活虚拟环境
activate_this = "/var/www/html/ddi-website/pytorch_env/bin/activate_this.py"
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

path = os.path.join(os.path.dirname(__file__), os.pardir)
if path not in sys.path:
    sys.path.append(path)

from app import app as application

本人设置的路由是app, 所以建的是app.wsgi, 最后也是import app。大家根据自己建立的flask应用灵活调整。

  1. 现在python建的虚拟环境已经没有activate_this.py这个文件了,但是还有activate文件。我试着用可用activate激活,还是会在deactivate这行内部文件代码报错。于是在网上找到了activate_this.py的原始版本,并加在了""/var/www/html/ddi-website/pytorch_env/bin/"目录下:
python 复制代码
"""By using execfile(this_file, dict(__file__=this_file)) you will
activate this virtualenv environment.
This can be used when you must use an existing Python interpreter, not
the virtualenv bin/python
"""

try:
    __file__
except NameError:
    raise AssertionError(
        "You must run this like execfile('path/to/active_this.py', dict(__file__='path/to/activate_this.py'))")
import sys
import os

base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

直接复制到activate_this.py里即可,不需要任何改动。

  1. 接下来添加"站点名字.conf"后缀文件,例如"ddi-website.conf"在目录/etc/apache2/sites-available/下:
python 复制代码
```python

```python
<VirtualHost *:80>
  ServerName 176.22.88.44
  ServerAdmin hahahah@qq.email.com
   
  WSGIDaemonProcess ptb-ddi-website python-path=/var/www/html/ddi-website/pytorch_env/lib/python3.10/site-packages user=www-data group=www-data threads=5 
  WSGIScriptAlias / /var/www/html/ddi-website/app.wsgi

  <Directory /var/www/html/ddi-website>
    WSGIProcessGroup ddi-website
    WSGIApplicationGroup %{GLOBAL}
    Order allow,deny
    Allow from all
  </Directory>
 
  Alias /static /var/www/html/ddi-website/static
  
  <Directory /var/www/html/ddi-website/static/>
    Order allow,deny
    Allow from all
  </Directory>

  ErrorLog /var/www/html/ddi-website/logs/error.log
  CustomLog /var/www/html/ddi-website/logs/access.log combined

</VirtualHost>

然后需要在自己网站目录下新建一个logs文件夹,用来存储错误日志和访问日志。

保存好后,接着执行下面的命令。

  1. 启动站点和服务
bash 复制代码
sudo a2ensite ddi-website.conf 
sudo systemctl reload apache2
sudo a2enmod wsgi
sudo apachectl configtest
sudo apachectl restart

然后访问"http:服务器ip:端口80/ddi-website"即可大功告成!

相关推荐
爬山算法6 小时前
Netty(10)Netty的粘包和拆包问题是什么?如何解决它们?
服务器·网络·tcp/ip
Sleepy MargulisItG6 小时前
【Linux网络编程】应用层协议:HTTP协议
linux·服务器·网络·http
logic_56 小时前
静态路由配置
运维·服务器·网络
suzhou_speeder7 小时前
企业数字化网络稳定运行与智能化管理解决方案
运维·服务器·网络·交换机·poe·poe交换机
RisunJan8 小时前
Linux命令-grpck命令(验证和修复组配置文件(`/etc/group` 和 `/etc/gshadow`)完整性的工具)
linux·运维·服务器
Xの哲學9 小时前
Linux VxLAN深度解析: 从数据平面到内核实现的全面剖析
linux·服务器·算法·架构·边缘计算
LRX_19892710 小时前
华为设备配置练习(七)VRRP 配置
服务器·网络·华为
林疏safe10 小时前
灯塔部署云服务器docker 部署方式,以及忘记密码如何查找
运维·服务器·docker
牛奶咖啡1311 小时前
Linux常见系统故障案例说明并修复解决(下)
linux·服务器·文件系统挂载异常分析并修复·持久化挂载分区文件丢失故障修复·分析系统进程cpu占用率过高
zclinux_12 小时前
【Linux】虚拟化的内存气泡
linux·运维·服务器