Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core:error] [pid 73214] [client ipaddress:62824] Script timed out before returning headers: myapp.wsgi #880

Open
shivprasadmj opened this issue Mar 22, 2024 · 3 comments

Comments

@shivprasadmj
Copy link

shivprasadmj commented Mar 22, 2024

Hi everyone,

I'm currently trying to deploy a Flask application on an Apache/2.4.6 server running on CentOS, and I've encountered a problem. Despite following all the necessary steps for deployment, whenever I try to access the app through its URL, I receive an error message stating: "Script timed out before returning headers: myapp.wsgi".

Here's a brief overview of my setup:

Virtual Environment Location: My Python virtual environment is located at /var/www/html/flaskapps/myapp/venv.

Apache Configuration: In my Apache .conf file, I've set up the following directives:

WSGIDaemonProcess your_application_name python-home=/var/www/html/flaskapps/myapp/venv python-path=/var/www/html/flaskapps/myapp
WSGIScriptAlias / /var/www/html/flaskapps/myapp/myapp.wsgi

WSGI File Content:
In my myapp.wsgi file, I've included the Python path to my application and the site-packages directory inside the virtual environment. Additionally, I've attempted to activate the virtual environment by executing the activate_this.py script. Here is the content of myapp.wsgi:

# myapp.wsgi
import sys

sys.path.insert(0, '/var/www/html/flaskapps/myapp')

sys.path.insert(0, '/var/www/html/flaskapps/myapp/venv/lib/python3.9/site-packages/')

activate_this = '/var/www/html/flaskapps/myapp/venv/bin/activate_this.py'
with open(activate_this) as file_:
     exec(file_.read(), dict(__file__=activate_this))

from myapp_app import app as application

I have double-checked all the paths specified in the .conf file, and they appear to be correct. However, I'm still facing the timeout issue when trying to access the application.

Also, I went through the documentation: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html to find out the solution for the issue.

Has anyone faced a similar issue or have any suggestions on what might be going wrong? Any advice or guidance would be greatly appreciated.

Thank you!

@GrahamDumpleton
Copy link
Owner

It is possibly because you are not forcing the use of the main Python interpreter context by using WSGIApplicationGroup %{GLOBAL} directive or application-group=%{GLOBAL} option to WSGIScriptAlias.

Various third party Python packages will not work in Python sub interpreters. This includes very popular packages such as numpy and any thing that uses it.

There is additional details as to why in:

In short though, your application code could be deadlocking due to the problems with those third party packages and so the request never returns.

You also seem to be missing the correct directives to have your application run in daemon mode as well.

Can you ensure that you provide a more complete copy of the mod_wsgi configuration you are using so I can review it?

At least try with:

WSGIDaemonProcess your_application_name python-home=/var/www/html/flaskapps/myapp/venv python-path=/var/www/html/flaskapps/myapp
WSGIScriptAlias / /var/www/html/flaskapps/myapp/myapp.wsgi process-group=your_application_name application-group="%{GLOBAL}"

@shivprasadmj
Copy link
Author

Thank you very much for your suggestion.

I've already implemented the configuration as you recommended.

Below are the detailed .conf file configurations I'm currently using:

<VirtualHost *:80>
    ServerName domainname
    # Redirect all HTTP traffic to HTTPS
    Redirect permanent / https://domainname/
</VirtualHost>

<VirtualHost *:443>
    ServerName domainname
    SSLEngine on
    SSLCertificateFile /var/www/html/flaskapps/ssl/domainname.crt
    SSLCertificateKeyFile /var/www/html/flaskapps/ssl/domainname.key
    SSLCACertificateFile /var/www/html/flaskapps/ssl/domainname.ca-bundle

    WSGIProcessGroup dashboard
    WSGIScriptAlias / /var/www/html/flaskapps/myapp/myapp.wsgi
    WSGIDaemonProcess dashboard python-home=/var/www/html/flaskapps/myapp/venv python-path=/var/www/html/flaskapps/myapp/:/var/www/html/flaskapps/myapp/venv/lib/python3.9/site-packages/ threads=15 processes=3 user=apache group=apache
    WSGIApplicationGroup %{GLOBAL}

    <Directory /var/www/html/flaskapps/myapp >
        Require all granted
    </Directory>

    ErrorLog /var/www/html/flaskapps/log/myapp_error.log
    CustomLog /var/www/html/flaskapps/log/myapp_access.log combined
</VirtualHost>

Could you please let me know if you see any issues with these configurations?

Thank you.

@GrahamDumpleton
Copy link
Owner

You should not need to add /var/www/html/flaskapps/myapp/venv/lib/python3.9/site-packages/ to python-path, that is what python-home does when you give it the root of the virtual environment. The Flask docs has been wrong in some places in saying you need to add site-packages directory to the path.

Add:

WSGIRestrictedEmbedded On

outside of all VirtualHost sections if only using daemon mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants