Deploying Flask app on a vps (Apache/Debian) - python

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!

Related

Serving multiple Django projects in different virtualenv at the same IP (Apache)

I'm trying to serve two Django projects on different virtualenv at the same IP adress on Apache.
My first site is at http://myip/site-1
and the seccond: http://myip/site-2
When I run http://myip/site-1, Apache serves it without issues, but when I run the second (http://myip/site-2) it raises the following:
The requested URL /site-2/ was not found on this server.
Because it searches in the first site's document root.
Here is my apache.conf
<VirtualHost *:80>
ServerName site-1.example.com
DocumentRoot /home/venv/site-1
# adjust the following line to match your Python path
WSGIDaemonProcess site-1.example.com processes=2 threads=15 display-name=%{GROUP} python-home=/home/venv/lib/python2.7
WSGIProcessGroup site-1.example.com
WSGIScriptAlias / /home/venv/site-1/site-1/wsgi.py
<directory /home/venv/site-1>
AllowOverride all
Require all granted
Options FollowSymlinks
</directory>
Alias /static/ /home/venv/site-1/static_root/
<Directory /home/venv/site-1/static_root>
AllowOverride all
Require all granted
Options FollowSymlinks
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site-2.example.com
DocumentRoot /home/venv_mob/site-2
# adjust the following line to match your Python path
WSGIDaemonProcess site-2.example.com processes=2 threads=15 display-name=%{GROUP} python-home=/home/venv_mob/lib/python2.7
WSGIProcessGroup site-2.example.com
WSGIScriptAlias / /home/venv_mob/site-2/site-2/wsgi.py
<directory /home/venv_mob/site-2>
AllowOverride all
Require all granted
Options FollowSymlinks
</directory>
Alias /static/ /home/venv_mob/site-2/static_root/
<Directory /home/venv_mob/site-2/static_root>
AllowOverride all
Require all granted
Options FollowSymlinks
</Directory>
</VirtualHost>
I have tried many solutions that I found on the web but the problem remains the same.
Any ideas ?
You have to connect to http://site-1.example.com (using terms from your example), not http://myip/site-1. And http://site-2.example.com, not http://myip/site-2.
Generally speaking you can have name-based virtual hosts for which you need to configure DNS (mapping site-1.example.com and site-2.example.com to your IP address) or you can run different sites on different ports.

Flask App Getting AH01630: client denied by server configuration error in Apache

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>

Flask on WAMP only seeing directory listings

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>

EC2: Deploying a django project with a apache server

I have set up an ec2 instance and I'm able to connect to it via ssh. I've installed apache, django, git and all the necessary dependencies.
I've managed to clone a git repository which is located in /home/ubuntu/.
I've also set up the configuration file in /etc/apache2/sites-enabled/hyper-blog.conf.
This is my hyper-blog.conf file:
<VirtualHost *:80>
ServerName ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAlias ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAdmin webmaster#localhost
DocumentRoot /home/ubuntu/hyper-blog/
AddDefaultCharset UTF-8
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
alias /static/ /home/ubuntu/hyper-blog/static/
alias /media/ /home/ubuntu/hyper-blog/media/
<Directory "/home/ubuntu/hyper-blog/static">
Require all granted
Options -Indexes
</Directory>
<Directory "/home/ubuntu/hyper-blog/media">
Require all granted
Options -Indexes
</Directory>
<Directory /home/ubuntu/hyper-blog/hyper-blog>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.hyperiondev.com%{REQUEST_URI}
WSGIDaemonProcess hyper-blog python-path=/home/ubuntu/hyper-blog:/usr/local/lib/python3.4/dist-packages
WSGIProcessGroup hyper-blog
WSGIScriptAlias / /home/ubuntu/hyper-blog/hyper-blog/wsgi.py
The problem is when I go to my public domain: ec2-52-11-240-28.us-west-2.compute.amazonaws.com it takes a while to load and eventually does not render anything.
I need to know how to get my application up and running. I've tried this documentation on digital ocean https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04,
but still doesn't work.
How can I get my application working without installing virtualenv?

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