Flask on WAMP only seeing directory listings - python

I am trying to publish a test python flask application to WAMP. I have gone through many tutorials, but face the issue of only seeing directory listings when I navigate to the app. I believe the wsgi module is setup correctly as the apache logs flag that the module has loaded, and as you can see from the screenshot below the server text also suggests the same. Has anyone else came across this, and resolved.
Here is my wsgi file:
import sys
sys.path.insert(0, 'C:/wamp64/www/flaskapp')
from webtool import app as application
Here is my init.py file:
from flask import Flask, request
app = Flask(__ name__)
#app.route('/')
def hello_world():
return "Hello World"
if __ name__ == '__ main__':
app.run()
The additional lines added to my httpd.conf
LoadModule wsgi_module "d:/program files/anaconda3/lib/site-
packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd"
LoadFile "d:/program files/anaconda3/python35.dll"
WSGIPythonHome "d:/program files/anaconda3"
Virtualhosts file:
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName flaskapp
WSGIScriptAlias /flaskapp c:/wamp64/www/flaskapp/flaskapp.wsgi
DocumentRoot c:/wamp64/www/flaskapp
<Directory c:/wamp64/www/flaskapp>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

It turned out to be my virtual hosts file. I cannot have two references to the same location (*:80) in the one file.
Updated file below:
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
WSGIScriptAlias /flaskapp c:/wamp64/www/flaskapp/flaskapp.wsgi
<Directory c:/wamp64/www/flaskapp>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Related

Multiple wsgi.mod on single apache

I have one django3 application on apache http://myapp.example.com
For now it works, but I want to put second django3 application on the same server with Anaconda.
Is it possible? if so , How should I set WSGIPythonPath and Alias /static/ for each application??
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName myapp.example.com
DocumentRoot "/var/www/html/myapp/current/"
ErrorLog ${APACHE_LOG_DIR}/myapp_error.log
CustomLog ${APACHE_LOG_DIR}/myapp_access.log combined
<Directory /var/www/html/myapp/current/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /var/www/html/myapp/current/myapp/wsgi.py
WSGIDaemonProcess py37 user=ubuntu group=ubuntu python-path=/home/ubuntu/anaconda3/envs/py37/lib/python3.7/site-packages
</VirtualHost>
LoadModule wsgi_module /home/ubuntu/anaconda3/envs/py37/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so
WSGIPythonHome /home/ubuntu/anaconda3/envs/py37/
WSGIPythonPath /var/www/html/myapp/current/
<Directory /var/www/html/myapp/current>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
Alias /static/ /var/www/html/myapp/current/static/
Alias /media/ /var/www/html/myapp/current/media/
<Directory /var/www/html/myapp/current/static>
Order allow,deny
Allow from all
</Directory>
simply clone the virtual host and replace the paths relevant for the new application, you may need to move the directory alias:
Alias /static/ /var/www/html/myapp/current/static/
Alias /media/ /var/www/html/myapp/current/media/
<Directory /var/www/html/myapp/current/static>
Order allow,deny
Allow from all
</Directory>
into the virtual host. I do not use the wsgi.py but a site.wsgi. Which runs the app.
Its content is:
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('{{PATH TO}/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('{PATH TO PROJECT}')
os.environ['DJANGO_SETTINGS_MODULE'] = '{PROJECT}.settings'
# Activate your virtual env
activate_env=os.path.expanduser("{PATH TO}/bin/activate_venv.py")
exec(open(activate_env).read(), dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
What works on my stage is(httpd.conf):
LoadModule wsgi_module {PATH TO}/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGISocketPrefix /var/run/wsgi
and the pyhton path/script is set in the virtual host instead in the main file:
WSGIDaemonProcess {GROUP} threads=1 python-home={PATH TO} python-path={PATH TO APP} user={USER}
WSGIProcessGroup {GROUP}
WSGIScriptAlias / {PATH TO FILE}/site.wsgi
you may need to comment the line that starts the application in your wsgi.py if you want to use the settings above

Troubles with running django app via wsgi on ubuntu 17

I need your help.
I have an error when trying to run on wsgi my django project.
I'm using Ubuntu 17, Apache2, Django 2.0, Python 3.6
When I running from manage.py, everything is working, but when via wsgi got the next error :
AH01276: Cannot serve directory /var/cardsite/cardsite/: No matching
DirectoryIndex
(index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found,
and server-generated directory index forbidden by Options directive
And don't know why, because I guess set everything correct. Bellow my configuration :
apache.conf
<VirtualHost *:80>
CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log
DocumentRoot /var/cardsite/cardsite/
Alias /static /var/cardsite/cardsite/static/
<Directory /var/cardsite/cardsite/static>
Require all granted
</Directory>
<Directory /var/cardsite/cardsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WSGIDaemonProcess cardsite python-path=/var/cardsite/ python-home=/var/venv_python36/
WSGIProcessGroup cardsite
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py
</VirtualHost>
wsgi.py
import os
import sys
PROJECT_DIR = '/var/cardsite'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cardsite.settings")
def execfile(filename):
globals = dict( __file__ = filename )
exec( open(filename).read(), globals )
activate_this = os.path.join( '/var/venv_python36/bin', 'activate_this.py' )
execfile( activate_this )
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
P.S. Permissions I have give on folder and all which inside.
P.S.S. Packages like "libapache2-mod-wsgi-py3" or "mod-wsgi" via pip3 also installed.
Thank you all for the any suggestion what's it can be
Don't set DocumentRoot to be a parent directory of your source code. If you were to take the WSGIScriptAlias out, people could download your source code. You should avoid the risk of that, even if WSGIScriptAlias currently intercepts everything under /. That DocumentRoot directory doesn't allow access may be part of the problem also.
Try:
<VirtualHost *:80>
CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log
DocumentRoot /var/cardsite/htdocs
<Directory /var/cardsite/htdocs>
Require all granted
</Directory>
Alias /static /var/cardsite/cardsite/static
<Directory /var/cardsite/cardsite/static>
Require all granted
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WSGIDaemonProcess cardsite python-path=/var/cardsite python-home=/var/venv_python36
WSGIProcessGroup cardsite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py
<Directory /var/cardsite/cardsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
The WSGIApplicationGroup has been added as always a good idea to have that, with only one WSGI application delegated to the daemon process group.
Make sure you create the directory:
/var/cardsite/htdocs
Finally, you are missing a ServerName directive. So if this isn't the default VirtualHost, your request may not even be getting handled by this VirtualHost.

Problems with deploying Flask application on Apache 2.4

I'm trying to deplay my Flask application on a RHEL7 Apache 2.4 server.
File structure is the following inside /var/www/html
/app
app.wsgi
/app
app.py
/templates
/static
In my /etc/httpd/conf/httpd.conf I have the following code to set up my project:
<VirtualHost *>
ServerName 10.65.112.75:443
WSGIDaemonProcess app user=apache group=apache threads=5 home=/var/www/html/app/app
WSGIScriptAlias / /var/html/app/app.wsgi
<Directory /var/www/html/app/app/>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Require all granted
</Directory>
Alias /static /var/www/html/app/app/static/
<Directory /var/www/html/app/app/static/>
Order deny,allow
Require all granted
</Directory>
And my app.wsgi contains the following:
#!/usr/bin/python
import sys
sys.path.insert(0, "/var/www/html/app/app/")
from app import app as application
The code for the project itself can be found in my github repository here.
I do not get any errors when trying to browse the server. It just doesnt do anything. Running my script from the terminal works, though.
Thanks for the help.
There are various things which aren't right.
You have:
WSGIScriptAlias / /var/html/app/app.wsgi
whereas it appears it should be:
WSGIScriptAlias / /var/www/html/app/app.wsgi
And:
<Directory /var/www/html/app/app/>
appears it should be:
<Directory /var/www/html/app>
Your VirtualHost definition also looks wrong. You have:
<VirtualHost *>
ServerName 10.65.112.75:443
If you really want this to be for HTTPS connections, you are missing all the SSL directives to add a SSL certificate.
ServerName would also usually be a fully qualified domain name and not an IP:PORT. The port number would usually be in the VirtualHost directive. For example, for HTTP, use:
<VirtualHost *:80>
ServerName my.host.name
where my.host.name is the full public host name for your machine which you use in the URL, not an IP address.

Deploying Flask app on a vps (Apache/Debian)

I'm trying to deploy a Flask app on a vps which OS is Debian and which is using Apache. I'm completely new to Apache so my problem may look simple but yet I was not able to find any solution on the web (I read a couple of tutorials and searched in this forum).
The server is running fine (it is already used for other purposes). When I try to access my app with www.myservername/web_app I get a 404 Not Found Error. Following http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/, here is what I've done:
I placed my application in /var/www/web_app/. In /var/www/web_app/app/init.py I created an instance of my app (named app) and configured it.
I placed the following .wsgi file in /var/www/web_app/:
activate_this = '/var/www/web_app/.pyenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this)
import sys
from app import app as application
Here is the /etc/apache2/sites-available/000-default.conf file (the site is enabled) :
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory /var/www/html/redmine>
RailsBaseURI /redmine
PassengerResolveSymlinksInDocumentRoot on
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess app user=www-data group=www-data threads=5
WSGIScriptAlias /web_app /var/www/web_app/app.wsgi
<Directory /var/www/web_app>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I also tried to replace /web_app with /var/www/web_app in the WSGIScriptAlias and also to access myservername/var/www/web_app. Moreover I tried to comment out the Rails/Passenger directives in the first Directory tag.
Of course I installed the wsgi module. I restarted the server before trying to access the page. Here is the access log:
... "GET /web_app HTTP/1.1" 404 508
And the error log :
[error] ... File does not exist : /var/www/web_app
Finally, my flask app - when using flask development server - works fine.
Thanks for your help!

Flask WSGI application hangs when import nltk

I followed the instructions here to create a onefile flask-app deployed to apache2 with mod-wsgi on ubuntu. That all works fine when using the original flask app. However, when adding import nltk to the flask app apache hangs (no 500).
I use python 2.7 and nltk 2.0.4
Others seem to have had similar problems with other packages. Setting
WSGIApplicationGroup %{GLOBAL}
in the VirtualHost configuration seemed to have helped. However, I still get the same behavior. Did anybody run into the same issue? Thanks for the help!
Here is the VirtualHost Configuration file:
<VirtualHost *:8080>
# ---- Configure VirtualHost Defaults ----
ServerAdmin jsmith#whoi.edu
DocumentRoot /home/bitnami/public_html/http
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/bitnami/public_html/http/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# ---- Configure WSGI Listener(s) ----
WSGIDaemonProcess flaskapp user=www-data group=www-data processes=1 threads=5
WSGIScriptAlias /flasktest1 /home/bitnami/public_html/wsgi/flasktest1.wsgi
<Directory /home/bitnami/public_html/http/flasktest1>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
# ---- Configure Logging ----
ErrorLog /home/bitnami/public_html/logs/error.log
LogLevel warn
CustomLog /home/bitnami/public_html/logs/access.log combined
Here is the modified flask code
#!/usr/bin/python
from flask import Flask
import nltk
app = Flask(__name__)
#app.route('/')
def home():
return """<html>
<h2>Hello from Test Application 1</h2>
</html>"""
#app.route('/<foo>')
def foo(foo):
return """<html>
<h2>Test Application 1</2>
<h3>/%s</h3>
</html>""" % foo
if __name__ == '__main__':
"Are we in the __main__ scope? Start test server."
app.run(host='0.0.0.0',port=5000,debug=True)
Where you have:
<Directory /home/bitnami/public_html/http/flasktest1>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
it should be:
<Directory /home/bitnami/public_html/http>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
as you are neither running your app in daemon mode or in the main interpreter because of the directives being in the wrong context.
That Directory directive then conflicts with one for same directory above so merge them.
If using mod_wsgi 3.0 or later count instead perhaps drop that second Directory block and use:
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias /flasktest1 /home/bitnami/public_html/wsgi/flasktest1.wsgi process-group=flaskapp application-group=%{GLOBAL}
Note that processes=1 has been dropped as that is the default and setting it implies other things you likely don't want. You also don't need to set user/group as it will automatically run as the Apache user anyway.

Categories

Resources