使用mod_fcgid和systemctl在Apache2上部署Flask应用程序时出现问题(“单元进入失败状态。”)

时间:2019-05-31 13:04:38

标签: python flask apache2 systemctl mod-fcgid

我正在尝试使用mod_fcgid和flup在服务器上的Apache 2.4上部署Flask应用程序。

我的Flask应用程序是一个大型Python3.6应用程序。 我使用systemctl在服务器上将其注册为服务(bot.service),并为Apache创建了虚拟主机配置。重新启动apache和systemctl之后,我启动bot.service并询问其状态(sudo systemctl status bot)并得到以下错误消息:

May 31 11:18:06 vlg00086 systemd[1]: bot.service: Unit entered failed state.
May 31 11:18:06 vlg00086 systemd[1]: bot.service: Failed with result 'exit-code'.
May 31 11:18:06 vlg00086 systemd[1]: bot.service: Service RestartSec=100ms expired, scheduling restart.
May 31 11:18:06 vlg00086 systemd[1]: Stopped Chatbot_classification_system.
May 31 11:18:06 vlg00086 systemd[1]: bot.service: Start request repeated too quickly.
May 31 11:18:06 vlg00086 systemd[1]: Failed to start Chatbot_classification_system.
May 31 11:18:06 vlg00086 systemd[1]: bot.service: Unit entered failed state.
May 31 11:18:06 vlg00086 systemd[1]: bot.service: Failed with result 'start-limit'.

关于我的申请:

我的应用程序位于:/ folder_name / data_folder / local_html / Chatbot_classification_system /  并具有以下结构:

Chatbot_classification_system/
    bot/
        module1/…
        module2/…
        ...
        module…/…
        templates/…
        __init__.py
        __main__.py
        bot.fcgi
        config.py
        forms.py
        … (other .py modules)
    lib/…
    templates/…
    tests/…
    wwww/
        .htaccess
    __init__.py
    bot.service
    bot_exe.sh
    config.json
    globals.py
    requieremnts.txt
setup.py

它与pip一起安装在python3.6虚拟环境中。


在内置烧瓶本地主机上运行:

它可以通过

在内置的Flask localhost上运行
python3.6 __main__.py.

在执行了某些程序特定的步骤(创建用户ID等)之后,应用程序将用户重定向到flask本地主机,并显示html页面-带有问题的表单。


在服务器上运行:

我创建了一个bot.fcgi文件。

我不使用bot。 main .py导入应用程序语句,而只是从 main.py 文件中复制并粘贴我需要的代码,因为在app.run之前还有很多其他步骤,我不想在导入中迷路。

这就是我的bot.fcgi的样子:

#!/folder_name/data_folder/local_html/pythons/python3.6.3/bin/python3.6
import sys, os
print("1")
from flup.server.fcgi import WSGIServer
print("2")
import pandas as pd
import json
from flask import Flask, render_template, flash, request
from bot.forms import ReusableForm
import re
from werkzeug.datastructures import ImmutableMultiDict
# …
# (other imports)

print("test imports done...")

list_of_meta_labels = {...}
# (… programm specific code)

#--------------------------------------------------------------------------
# App configuration:
#--------------------------------------------------------------------------

app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = '...'
app.config['EXPLAIN_TEMPLATE_LOADING'] = True
print("app configured...")

# … (Programm specific code)


def submit(name):
    # ...

def try_strip(inp):
    # ...

def read_conf(): READ CONFIGURATION FILE # get Environment variables
    # …

@app.route("/", methods=['GET', 'POST'])  # URL to trigger our function

def support_form(test=None):
    # … (programm specific steps)

    form = ReusableForm(request.form)
    # … (programm specific steps) 
    #------------------------------------------------------------------------
    # Update "form" with the answer and send it the response page:
    #------------------------------------------------------------------------
    parameters = request.form.to_dict()
    parameters['answer'] = answer_html
    form = ImmutableMultiDict(parameters)
    return render_template('success_landing_page.html', title='Index',    
                           form=form)


if __name__ == '__main__':
    app.debug = True
    WSGIServer(app).run()


我的bot.service文件:

[Unit]
Description=Chatbot_classification_system

[Service]
ExecStart= /folder_name/data_folder/local_html/Chatbot_classification_system/bot/bot.fcgi
Restart=always
Group=dlm_nagios
Environment=NODE_ENV=production
WorkingDirectory /folder_name/data_folder/Chatbot_classification_system/

[Install]
WantedBy=multi-user.target

Apache配置:

/etc/apache2/vhosts.d/50006.conf文件:

LoadModule fcgid_module ./usr/lib64/apache2/mod_fcgid.so

<VirtualHost *:50006>

    ServerRoot /folder_name/data_folder/local_html/Chatbot_classification_system
    DocumentRoot /folder_name/data_folder/local_html/Chatbot_classification_system/templates

    AddHandler fcgid-script .fcgi
   ScriptAlias / /folder_name/data_folder/local_html/Chatbot_classification_system/bot/bot.fcgi/

    <Location />
        SetHandler fcgid-script
    </Location>
</VirtualHost>

我重新启动一切:

service httpd restart
sudo systemctl daemon-reload
sudo systemctl start bot

输出

输出

sudo systemctl status bot

* bot.service - Chatbot_classification_system
   Loaded: loaded (/etc/systemd/system/bot.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Fri 2019-05-31 12:13:59 UTC; 29min ago
  Process: 20899 ExecStart=/folder_name/data_folder/local_html/Chatbot_classification_system/bot.fcgi (code=exited, status=203/EXEC)
 Main PID: 20899 (code=exited, status=203/EXEC)

May 31 12:13:59 vlg00086 systemd[1]: bot.service: Main process exited, code=exited, status=203/EXEC
May 31 12:13:59 vlg00086 systemd[1]: bot.service: Unit entered failed state.
May 31 12:13:59 vlg00086 systemd[1]: bot.service: Failed with result 'exit-code'.
May 31 12:13:59 vlg00086 systemd[1]: bot.service: Service RestartSec=100ms expired, scheduling restart.
May 31 12:13:59 vlg00086 systemd[1]: Stopped Chatbot_classification_system.
May 31 12:13:59 vlg00086 systemd[1]: bot.service: Start request repeated too quickly.
May 31 12:13:59 vlg00086 systemd[1]: Failed to start Chatbot_classification_system.
May 31 12:13:59 vlg00086 systemd[1]: bot.service: Unit entered failed state.
May 31 12:13:59 vlg00086 systemd[1]: bot.service: Failed with result 'start-limit'.

的输出
sudo apache2ctl -S 

VirtualHost configuration:
*:50006                server_name.sss.sss.corp (<it is our Server name>) (/etc/apache2/vhosts.d/50006.conf:3)
ServerRoot: "/folder_name/data_folder/local_html/Chatbot_classification_system"
Main DocumentRoot: "/folder_name/data_folder/local_html/Chatbot_classification_system"
Main ErrorLog: "/var/log/apache2/error_log"
Mutex ssl-cache: using_defaults
Mutex default: dir="/run/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex fcgid-pipe: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex fcgid-proctbl: using_defaults
Mutex ssl-stapling: using_defaults
PidFile: "/var/run/httpd.pid"
Define: SYSCONFIG
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="wwwrun" id=30
Group: name="www" id=8

我尝试使用

运行我的应用程序
§ python3.6 bot.fcgi

当然,通过这种方式,应用程序不会获得任何服务器配置信息,但可以检查代码本身是否启动且没有错误。

我看到“ 1”,“ 2”,“测试导入已完成...”和“应用程序已配置...”打印语句以及以下消息:

WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 301 MOVED PERMANENTLY
Content-Type: text/html; charset=utf-8
Content-Length: 241
Location: http://localhost/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="http://localhost/">http://localhost/</a>.  If not click the link.

我猜这意味着程序启动时没有错误,然后没有找到服务器配置(这是正常的,因为它在apache中),并显示404错误消息。 看来问题出在我的Apache配置中。

如果有人可以帮助我,我将非常高兴!

0 个答案:

没有答案