Apache and Flask: attempt to invoke directory as script - python

I have an Apache (ver. 2.2.15) server (running on Linux CentOS), where I have a lot of .cgi scripts located in /var/www/cgi-bin, aliased:
ScriptAlias /cgi-bin "/var/www/cgi-bin"
And it works fine when I enter mydomain/cgi-bin/something.cgi.
Now I want to have also Flask application running within Apache server on 80 port. The app is located in /var/www/cgi-bin/app. So, I created simple Flask app - a.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
and a.wsgi file:
import sys
import site
sys.path.insert(0, '/var/www/cgi-bin/app')
site.addsitedir('/var/www/cgi-bin/app:/<my_python_path>/Lib/site-packages')
from app import app as application
Also in /etc/httpd/conf/httpd.conf I have created required virtualhost:
<VirtualHost *:80>
ServerName app.<mydomain>:80
WSGIDaemonProcess app python-path=/var/www/cgi-bin/app:/<my_python_path>/Lib/site-packages user=user1 group=user1 threads=5
WSGIScriptAlias /cgi-bin/app /var/www/cgi-bin/app/a.wsgi
<Directory /var/www/cgi-bin/app>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/www/cgi-bin/app/logs/error.log
CustomLog /var/www/cgi-bin/app/logs/custom.log combined
</VirtualHost>
But everytime I try to open mydomain/cgi-bin/app/ in the error.log file I see this error:
(...) attempt to invoke directory as script: /var/www/cgi-bin/app/
Do you have an idea what have I done wrong here?

You should not put your Flask app under cgi-bin, in fact it should not be under the DocumentRoot at all. Move it somewhere else entirely - I like /srv/, but it's up to you - and change the WSGI alias appropriately.

Related

Apache2 and Flask issue

I try to load my app towards my apache2 server but i keep getting error 500.
File Structure:
/var/www/ApproveAndPost:
ApproveAndPost.wsgi
ApprovePost_Web
run.py
/var/www/ApproveAndPost/ApprovePost_Web:
forms.py
init.py
models.py
routes.py
services.py
static
templates
Files content:
/etc/apache2/sites-available/ApproveAndPost.conf
<VirtualHost *:80>
ServerName 192.168.170.67
ServerAdmin email#mywebsite.com
WSGIScriptAlias / /var/www/ApproveAndPost/ApproveAndPost.wsgi
<Directory /var/www/ApproveAndPost/ApprovePost_Web/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/ApproveAndPost/ApprovePost_Web/static
<Directory /var/www/ApproveAndPost/ApprovePost_Web/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
WSGI File:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/ApproveAndPost")
from ApproveAndPost import app as application
application.secret_key = 'DISISSECRETKEY'
Python Code:
run.py
from ApprovePost_Web import app
if __name__ == "__main__":
app.run(debug=True)
init.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = '#$%^&*'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
from ApprovePost_Web import routes
if __name__ == "__main__":
app.run(debug=True)
I think i am mixing up a couple of things ?
The problem is essentially that you are installing Flask, and possibly other required libraries, in a virtual environment but the python (wsgi interface) is running with the system python which does not have these extra libraries installed.

Target WSGI script cannot be loaded as Python module Flask

So, i've been working on this little project in apache2 using python and Flask microframework. I have this problem.
mod_wsgi (pid=11251): Target WSGI script '/var/www/FlaskApp/flaskr/wsgi.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/FlaskApp/flaskr/wsgi.wsgi'``
File "/var/www/FlaskApp/flaskr/wsgi.wsgi", line 8, in <module>
from flaskr import app as application
ImportError: cannot import name 'app'
This is what my wsgi.wsgi file looks like.
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,'/var/wwww/FlaskApp')
from flaskr import app as application
and this what my .conf files looks like:
<VirtualHost *>
ServerName (here goes my ip)
ServerAdmin "me"
WSGIDaemonProcess wsgi
WSGIScriptAlias / /var/www/FlaskApp/flaskr/wsgi.wsgi
<Directory /var/www/FlaskAppr/flaskr>
WSGIProcessGroup wsgi
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
Alias /static /var/www/FlaskApp/flaskr/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
so, far been roaming internet, got no answers. Flask local works ok.
Yet, i still get the 500 error whenever i try to run apache2
Btw, i am using a raspberry pi as server. Stretch version OS.
any ideas?

Why apache throws Target WSGI script not found or unable to stat on Flask app?

I want to setup my flask app for apache on my ubuntu server using wsgi. But after my setup, I get the following browser error:
Not Found
The requested URL / was not found on this server.
The apache error log throws:
Target WSGI script not found or unable to stat:
/var/www/html/appname/appname.wsgi
My wsgi file looks like this:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/appname/")
from IdeaHound import app as application
application.secret_key = 'here_the_key'
and my apache config file looks like this:
<VirtualHost *:80>
ServerName server_ip_here
WSGIScriptAlias / /var/www/html/appname/appname.wsgi
<Directory /var/www/html/appname/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel info
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The appname folder has the following structure:
appname
--application.py
--appname.wsgi
--LICENSE
--README.md
--requirements.txt
--db_manage.py
--appname
----frontend
----__inity__.py
----__init__.pyc
----models
----__pycache__
----socket_interface
--AppName
----__init__.py
----static
----templates
--instance
----config.py
What am I missing here to make the webserver run correctly with Flask?
Please read the following link of Digital Ocean. Do as describe in it. And your app will be up and running in just 5 mins.
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
Also do check the apache error logs for more information if it still throws an error.
/var/log/apache2/error.log

flask app only shows list of files with apache2

Im trying to host a flask app with an apache2 server. The server works but I'm only seeing a list of files, the wonderful "index of" page. My code is pretty simple. This is my hello.py file in /var/www/flask_dev:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0')
I also created an apache config file located in /etc/apache2/sites-available/flask_dev.conf:
ServerName example.com
<VirtualHost *:80>
ServerAdmin webmaster#localhost
WSGIDaemonProcess hello user=www-data group=www-data threads=5 python-path=/var/www/flask_dev
WSGIScriptAlias / /var/www/flask_dev/start.wsgi
<Directory /var/www/flask_dev>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And the needed wsgi file also located in /var/www/flask_dev/start.wsgi:
from hello import app as application
import sys
sys.stdout = sys.stderr
I'm not sure what I did wrong, I just followed a simple tutorial.
Thanks for your help :)
You probably did not install mod_wsgi module for Apache.
http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/
Apache needs to import the mod_wsgi module for it to work with python. Further instructions for installation can be found at.
https://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
Once installed, edit your httpd.conf with LoadModule wsgi_module modules/mod_wsgi.so
If you are on Windows, you will have to download the appropriate mod_wsgi.so for the python version and architecture. Rename the file to mod_wsgi.so if it has any python specific version naming and set conf to LoadModule.

Running django webapp on apache using mod_wsgi

I am new to working with apache and mod_wsgi. But I do have little experience in django, so from some tutorials I tried to run django app through apache webserver using mod_wsgi.
I created mysite in /var/www/
then in mysite/application I created application.wsgi ...
import os
import sys
sys.path.append('/var/www/mysite/application')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
and in /etc/httpd/sites-available I created file named mysite.conf ...
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin id#somewhere.com
ServerAlias mysite.com
DocumentRoot /var/www/mysite/
<Directory /var/www/mysite>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</Directory>
WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup mysite
</Virtualhost>
Then I ran a2ensite mysite.conf, didn't showed any error.
Then in /etc/httpd/hosts/ I added one line my-ipddress mysite.com
I gave permission chmod 777 to all the above files and to folder /var/www/mysite. Now when I open mysite.com on browser I see apahce's default page nothing from django.
I am using fedora 21.
You haven't put in anything in that configuration to serve your Django site via WSGI: you're missing the WSGIScriptAlias line (see the documentation:
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Note that you shouldn't really be putting things in /var/www; and also you shouldn't need to create your own WSGI file, Django creates one for you when you create a project.

Categories

Resources