Skip to main content
 首页 » 编程设计

python之在 apache 服务器上运行多个 flask 应用程序

2024年11月24日32itcoder

我在 var/www/html 中有多个 flask 应用程序,例如 var/www/html/website、var/www/html/summary,var/www/html/sentiment

一旦我成功运行所有应用程序。

然后我又添加了一个应用程序,我为其创建了 conf 文件并重新启动了服务器。

之后所有应用程序都停止工作,只有 var/www/html/sentiment 打开。

我检查了其他应用程序的 python、wsgi 和 conf 文件中的代码与 sentiment 中的代码相同。

情感应用代码

flask 应用程序

from flask import Flask 
app = Flask(__name__) 
 
@app.route('/sentiment') 
def hello_world(): 
  return 'Hello from Sentiment!' 
 
if __name__ == '__main__': 
  app.run() 
 
 

flask 应用程序.wsgi

import sys 
sys.path.insert(0, '/var/www/html/sentiment/') 
 
from flaskapp import app as application 

conf 文件 -/etc/apache2/sites-available/sentiment.conf

<VirtualHost *:80> 
                ServerName IP 
                ServerAdmin admin@mywebsite.com 
                WSGIScriptAlias / /var/www/html/sentiment/flaskapp.wsgi 
                <Directory /var/www/html/sentiment/> 
                        Order allow,deny 
                        Allow from all 
                </Directory> 
                ErrorLog ${APACHE_LOG_DIR}/error.log 
                LogLevel warn 
                CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost> 
 

应用程序代码摘要 flaskapp.y

from flask import Flask 
app = Flask(__name__) 
 
@app.route('/summary') 
def hello_world(): 
  return 'Hello from Summary!' 
 
if __name__ == '__main__': 
  app.run() 

flask 应用程序.wsgi

import sys 
sys.path.insert(0, '/var/www/html/summary/') 
 
from flaskapp import app as application 

/etc/apache2/sites-available/summary.conf

<VirtualHost *:80> 
                ServerName IP 
                ServerAdmin admin@mywebsite.com 
                WSGIScriptAlias / /var/www/html/summary/flaskapp.wsgi 
                <Directory /var/www/html/summary/> 
                        Order allow,deny 
                        Allow from all 
                </Directory> 
                ErrorLog ${APACHE_LOG_DIR}/error.log 
                LogLevel warn 
                CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost> 

当我打开 ip/sentiment 时它仍然有效,但 ip/summary 给出

在服务器上找不到请求的 URL。如果您手动输入 URL,请检查您的拼写并重试。

对此有什么建议吗?

请您参考如下方法:

问题出在您的 WSGIScriptAlias 名称上。

/ 指向目录中的所有内容。

为所有应用程序提供唯一的别名。

示例:

将 / 更改为 /app1 for WSGIScriptAlias

为 WSGIScriptAlias 将 / 更改为 /app2

在两个文件中。

然后重启服务器。