mod_wsgi with Flask app: ImportError: No module named flask - apache

In CentOS 6.4, I created python virtual environment in /var/www/html/venv folder. Then after activating the virtual environment, I installed all necessary python libraries for my flask application. I checked, that Flask libraries were in /var/www/html/venv/lib/python2.7/site-packages folder. I've already installed and loaded mod_wsgi. Now in my flask app, which is in /var/www/html/truckman/wsgi folder, I created truckman.wsgi file with the following content:
activate_this = '/var/www/html/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
sys.path.insert(0, '/var/www/html/truckman/wsgi/')
from app import app as application
import config
application.config.from_object(config.Dev)
Also, in /etc/httpd/conf/httpd.conf I added:
<VirtualHost *>
WSGIScriptAlias / /var/www/html/truckman/wsgi/truckman.wsgi
<Directory /var/www/html/truckman/wsgi>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now, in /var/www/html/truckman/wsgi folder, I created run.py file with the following content:
from app import app as application
import config
application.config.from_object(config.Dev)
if __name__ == "__main__":
application.run(port=5001)
Now I tested my app with flask's development server; if I execute "python run.py", my app works as expected. I can browse to localhost:5001, and the app's initial page shows up.
Then I tested my app with mod_wsgi: First killed run.py process, and restarted httpd service, and after that browsed to localhost; but it returned: "500 Internal Server Error". In /etc/httpd/logs/error_log file I found the following error message: "ImportError: No module named flask". What's wrong with my settings?

Try adding the path to your python 2.7 folder in your virtual environment.
sys.path.insert(1, '/path/to/virtualenv/lib/python2.7')

The .conf should be something like this:
<VirtualHost *>
ServerName example.com
WSGIScriptAlias / /var/www/firstapp/hello.wsgi
WSGIDaemonProcess hello python-path=/var/www/firstapp:/var/www/firstapp/env/lib/python2.7/site-packages
<Directory /var/www/firstapp>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Preferably in /etc/apache2/sites-available/hello.conf, so this part can be the root of the problem.
You can check a working example code in: Hello world from flask.

Related

Error 500 while deploying my Flask app with mo_wsgi on Ubuntu VPS

I am currently trying to deploy a very basic Flask app on my ubuntu VPS with mod_wsgi.
I followed this youtube tutorial as precisely as I could : https://www.youtube.com/watch?v=w0QDAg85Oow
But i ended up with a 500 error. Here is a sample from the logs :
mod_wsgi (pid=8283): Failed to exec Python script file '/var/www/flask-app/app.wsgi'.
mod_wsgi (pid=8283): Exception occurred processing WSGI script '/var/www/flask-app/app.wsgi'.
Traceback (most recent call last):
File "/var/www/flask-app/app.wsgi", line 5, in <module>
with open(activate_this) as file_:
PermissionError: [Errno 13] Permission denied: '/root/.local/share/virtualenvs/flask-app-yYmzn1cG/bin/activate_this.py'
The app.wsgi file :
import sys
sys.path.insert(0, '/var/www/flask-app')
activate_this = '/root/.local/share/virtualenvs/flask-app-yYmzn1cG/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
from app import app as application
The python app.py :
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
The flask-app.conf :
<VirtualHost *:80>
WSGIDaemonProcess flaskapp user=www-data group=www-data threads=5
WSGIScriptAlias / /var/www/flask-app/app.wsgi
<Directory /var/www/flask-app>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/www/flask-app/logs/error.log
CustomLog /var/www/flask-app/logs/access.log combined
</VirtualHost>
Screenshot from apache2 both /etc/apache2/sites-available and /etc/apache2/sites-enabled :
screenshot
As we can see in the logs it seems to be a permission error.
What I understood from the video and the flask documentation (https://flask.palletsprojects.com/en/2.0.x/deploying/mod_wsgi/) is that the server runs my app with the "www-data" user as we told him in the flask-app.conf :
WSGIDaemonProcess flaskapp user=www-data group=www-data threads=5
What i've tried for the moment is to run from root :
$ chown -R www-data:www-data /root/.local/share/virtualenvs
Because i created my pipenv virtual env with root, i tried to give the ownership from these to the www-data user.
But the error still remains...
I just begin with VPS, apache and Flask and I can't find a solution for this problem.
Any idea ?
UPDATE
I tried to create a new user called "arnaud", and i copied the "flask-app-yYmzn1cG" folder (containing the virtualenv) in /home/arnaud/flask-app-venv.
I changed the path of the "activate_this.py" in the app.wsgi file to point the new /home/arnaud/flask-app-venv/flask-app-yYmzn1cG/bin/activate_this.py.
I changed "user" and "group" to "arnaud" in the flask-app.conf.
...and this worked. No error this time and my app is running.
So the problem was for the user "www-data" to access the pipenv generated virtual env in the /root/ (owned by root) folder.
Have you confirmed that mod_wsgi actually runs as the www-data user? If you start the application from a non-privileged user (and without sudo), mod_wsgi won't be able to switch to the www-data user.
I can't say for mod_wsgi, but IIRC uwsgi would just silently just run as the invoking user if it didn't have the required permission to run it as the configured user.

Firebase from Flask deployment server

in development mode with Flask, I am successfully able to read and write data to Firestore using the firebase admin module. However, once I deploy the app on Apache 2 using mod_wsgi, everything works fine (firebase app initialization doesn't throw any error) until I start getting a document, where the code gets stuck and the page loads forever.
Flask function where the problem occurs in __init__.py:
#app.route('/users/<user>')
def login(user=None):
if not user:
abort(404)
userRef = db.collection('users').document(user)
print("1") # This runs.
userDoc = userRef.get() # The code gets stuck here. No error message is ever returned or stored in the error log.
print("2") # This never runs.
flaskapp.wsgi
#!/usr/local/bin/python3.8
import sys
import os
import logging
logging.basicConfig(stream=sys.stderr)
os.chdir("/var/www/html/example.com/FlaskApp/")
sys.path.insert(0,"/var/www/html/example.com/FlaskApp/")
from __init__ import app as application
example.com.conf
LoadModule wsgi_module "/home/username/.local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
WSGIPythonHome "/usr/local"
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin username#example.com
ServerName example.com
ServerAlias www.example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.com/public_html
WSGIScriptAlias / /var/www/html/example.com/FlaskApp/flaskapp.wsgi
<Directory /var/www/html/example.com/FlaskApp/>
Require all granted
</Directory>
Alias /static /var/www/html/example.com/FlaskApp/static
<Directory /var/www/html/example.com/FlaskApp/static/>
Require all granted
</Directory>
# Log file locations
LogLevel warn
ErrorLog /var/www/html/example.com/log/error.log
CustomLog /var/www/html/example.com/log/access.log combined
</VirtualHost>
Do you know how to make Firebase get() works in a deployed Flask app? Thank you for your help!
Seems like WSGI needs to be forced to use the main Python interpreter. Therefore, you'll need to add application-group=%{GLOBAL} to the end of your ``WSGIScriptAlias line on example.com.conf, so that the line now reads:
WSGIScriptAlias / /var/www/html/example.com/FlaskApp/flaskapp.wsgi application-group=%{GLOBAL}
After that, restart Apache with the terminal command systemctl restart apache and your problem should be solved.

Deploying Multiple Flask Apps On Server

I am attempting to deploy a 2nd flask application to an apache webserver. I have added a 2nd listener to the httpd.conf file for 127.0.0.1:8868 because I am accessing the api through a gateway located on my machine. The first app I deployed was set up in a very similar fashion and works perfectly. The flask app works fine when launched from the .py file. I have attached excerpts from the httdpd.conf file, httpd-vhosts.conf file, and the web.wsgi file. It doesn't record any errors in the apache errors log and or the access logs. It will record it in the access and error logs if I have an error in the .wsgi file. Having both listeners enabled also seems to interfere with the first application listening on port 5000.
httpd listeners:
Listen 127.0.0.1:5000
Listen 127.0.0.1:8868
httpd-vhosts.conf:
<VirtualHost *:5000>
WSGIScriptAlias / C:/Users/me/web_apps/task_manager/app/index/web.wsgi
<Directory C:/Users/me/web_apps/task_manager>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8868>
WSGIScriptAlias / C:/Users/me/web_apps/template_autoupdate/app/index/web.wsgi
<Directory C:/Users/me/web_apps/template_autoupdate>
Require all granted
</Directory>
</VirtualHost>
web.wsgi
import sys
sys.path.insert(0, "C:/Users/me/web_apps/template_autoupdate/app")
from autoupdater import app as application

run flask on apache on windows

Here's the issue.
I am trying to deploy my flask app in apache2.4 Server using mod_wsgi.After configuration,my apache server start to run on my computer.But when I visit http://127.0.0.1:5000/ the page doesn't display as my wish.
Here's my flask code.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run(port=5000)
And here's my virtual host config.
<VirtualHost *:5000>
ServerAdmin example#company.com
DocumentRoot C:\flask
WSGIScriptAlias / C:\flask\test.wsgi
<Directory "C:\flask">
Require all granted
Require host ip
Allow from all
</Directory>
</VirtualHost>
My wsgi code.
import sys
#Expand Python classes path with your app's path
sys.path.insert(0, "C:/flask")
from test import app as application
#Put logging code (and imports) here ...
#Initialize WSGI app object
application = app
The page is like this:
It says 'Internal Server Error'.
Thank everyone!
remove the code
#Initialize WSGI app object
application = app
maybe it works
first remove last line from test.wsgi file.
(i.e application = app)
then check http://127.0.0.1:5000/, if it works then good,
if not then, add the following lines at the bottom of httpd.conf file.
WSGIScriptAlias / C:\flask\test.wsgi
<Directory C:\flask>
Require all granted
</Directory>
then check again http://127.0.0.1:5000/. it will work definitely.

Writing a mod-wsgi script on Amazon linux

I am using httpd2.4 with mod-wsgi installed on Amazon linux.
My wsgi script looks like this:
/projects/mv2/test/test.wsgi
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
from test import *
/projects/mv2/test/test.py
from flask import Flask
app = Flask(__name__)
#app.route('/test')
def hello_world():
return 'Hello, World!'
Apache conf file
<VirtualHost *:80>
ServerName test-algo.com
WSGIDaemonProcess algos_app user=mv2 group=mv2 threads=1
WSGIScriptAlias / /projects/mv2/test/test.wsgi
<Directory /projects/mv2/test/test>
WSGIProcessGroup algos_app
WSGIApplicationGroup %{GLOBAL}
Options MultiViews FollowSymLinks
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
When I hit the url http://test-algo.com/test, I get a 403 response and the following the httpd error file
[authz_core:error] [pid 27555] [client 153.156.225.142:65083] AH01630: client denied by server configuration: /projects/mv2/test/test.wgi
I am not able to find what is wrong with the wsgi script.
The Directory blog should start with:
<Directory /projects/mv2/test>
You have an extra test at end of path.
That would cause the 403 error.
The WSGI script should also use:
from test import app as application
The name of the WSGI entry point is expected to be application not app as your Flask file uses.
If don't fix this you will get a different error after fixing the first.