Problems with deploying Flask app on Apache2 with mod_fcgid and systemctl ("Unit entered failed state.")

315 views Asked by At

I am trying to deploy a Flask app on Apache 2.4 on the server using mod_fcgid and flup.

My Flask app is a big Python3.6 application. I registered it as a service (bot.service) on the server with systemctl and created a virtual host configuration for Apache. After restarting apache and systemctl I start my bot.service and ask for its status (sudo systemctl status bot) and get the following error message:

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'.

ABOUT MY APPLICATION:

My application is located here: /folder_name/data_folder/local_html/Chatbot_classification_system/ and has the following structure:

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

It is installed with pip in a python3.6 virtual environment.


RUNNING ON THE IN-BUILT FLASK LOCALHOST:

It can run on the in-built Flask localhost with

python3.6 __main__.py.

After performing some program specific steps (creating a user id, etc.) the application redirects the user to the flask localhost and shows a html page - form with questions.


RUNNING ON THE SERVER:

I created a bot.fcgi file.

I do not use from bot.main.py import app statement, but simply copy-past the Code I need from the main.py file, because there are many additional steps before app.run and I do not want to get lost in imports.

That is how my bot.fcgi looks like:

#!/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()


My bot.service file:

[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 configuration:

/etc/apache2/vhosts.d/50006.conf file:

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>

I restart everything:

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

Outputs

Output of

sudo systemctl status bot

is

* 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'.

Output of

sudo apache2ctl -S 

is

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

I tried to run my application with

§ python3.6 bot.fcgi

Of course, this way the app does not get any Server configuration information, but it allows to check that the code itself starts without errors.

I see "1", "2", "test imports done..." and "app configured..." print statements and the following message:

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.

I guess it means that the programm starts without errors and then does not find server configuration (which is normal because it is in apache) and shows 404 error message. It seems that the problem is somewhere in my Apache configuration.

I would be really happy if somebody could help me!

0

There are 0 answers