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.
Related
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.
Getting 403 Forbidden when attempting to send requests to my Flask app.
Finding a lot of examples of this error online but none of the solutions work for me so far.
Error from the apache error log file:
AH01630: client denied by server configuration: /opt/MyTinyURL/webtool.wsgi
Here is my VirtualHost.
<VirtualHost *:80>
ServerName MY_SERVER_NAME
DocumentRoot /opt/MyTinyURL
WSGIDaemonProcess webtool user=www-data group=www-data threads=5 home=/opt/MyTinyURL/
WSGIScriptAlias / "/opt/MyTinyURL/webtool.wsgi"
<Directory "/opt/MyTinyUrl">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride None
Require all granted
WSGIProcessGroup webtool
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
</Directory>
</VirtualHost>
UNIX file path names are case sensitive. The path used with WSGIScriptAlias doesn't match that used in the Directory directive.
Cleaning up a few other issues with things that aren't needed, try using:
<VirtualHost *:80>
ServerName MY_SERVER_NAME
# XXX Bad practice to set DocumentRoot to be directory
# where your code is. Comment out WSGIScriptAlias and
# people could download your source code.
# DocumentRoot /opt/MyTinyURL
# XXX Don't need user/group as the default to Apache user.
WSGIDaemonProcess webtool threads=5 home=/opt/MyTinyURL/
WSGIScriptAlias / "/opt/MyTinyURL/webtool.wsgi"
# XXX You had MyTinyUrl and not MyTinyURL.
<Directory "/opt/MyTinyURL">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride None
Require all granted
WSGIProcessGroup webtool
WSGIApplicationGroup %{GLOBAL}
# XXX Script reloading is the default option.
# WSGIScriptReloading On
</Directory>
</VirtualHost>
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>
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!
I'm having an issue configuring mod_wsgi with Apache 2.2. I need mod_wsgi to handle everything under root directory of a virtual server except certain paths. For those paths I want ordinary Apache directory listing to work. The following virtual server config half works. mod_wsgi does not kick in for paths directly to files but does for directories. I can't figure this out. Looked at docs here - https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide. It shows using aliases in the way I have to disable mod_wsgi but does not mention this problem. (Note not that it matters but the foo.wsgi end point is just the hello world example).
<VirtualHost *:80>
ServerAdmin webmaster#test.wsgi.sethandler.localdomain
ServerName test.wsgi.sethandler.localdomain
DocumentRoot /var/www/test.wsgi.sethandler.localdomain
<Directory /var/www/test.wsgi.sethandler.localdomain>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo Options Indexes
Order allow,deny
allow from all
</Directory>
Alias /static /var/www/test.wsgi.sethandler.localdomain/static
WSGIScriptAlias / /var/www/test.wsgi.sethandler.localdomain/cgi-bin/foo.wsgi
<Directory /var/www/test.wsgi.sethandler.localdomain/cgi-bin>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I've tried disabling any handler with the following but it does not work:
<Location "/static">
SetHandler None
</Location>
<LocationMatch ^/static>
SetHandler None
</LocationMatch>
Have you tried putting and end slash after static?
Alias /static/ /var/www/test.wsgi.sethandler.localdomain/static/
<Directory /var/www/test.wsgi.sethandler.localdomain/static/>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/test.wsgi.sethandler.localdomain/cgi-bin/foo.wsgi