基于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"即可大功告成!

相关推荐
saynaihe5 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
小蜜蜂爱编程5 小时前
ubuntu透网方案
运维·服务器·ubuntu
头发那是一根不剩了6 小时前
nginx:SSL_CTX_use_PrivateKey failed
运维·服务器
景彡先生9 小时前
C++编译期计算:常量表达式(constexpr)全解析
服务器·c++
阿巴~阿巴~9 小时前
理解Linux文件系统:从物理存储到统一接口
linux·运维·服务器
java1234_小锋9 小时前
【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 架构搭建
python·自然语言处理·flask
tan77º9 小时前
【Linux网络编程】应用层自定义协议与序列化
linux·运维·服务器·网络·c++·tcp/ip
wanhengidc9 小时前
在徐州网络中服务器租用与托管的优势
运维·服务器·网络
音程11 小时前
(已解决) 如何使用密钥连接远程服务器jupyter notebook从而在本地浏览器上打开
运维·服务器·python·jupyter
Q_Q51100828515 小时前
python的小学课外综合管理系统
开发语言·spring boot·python·django·flask·node.js