Problems with deploying Flask application on Apache 2.4 - python

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.

Related

Using Apache XAMPP on Windows 10 to create 1 Django website and one normal website

I have created a Django protect that works perfectly fine on windows Apache with Xampp. However, if I try to create a virtual host for a non-Django website, it doesn't work.
If I then put my Django website into a virtual host it doesn't work, but then my non-Django website does work.
By doesn't work I mean it takes me to this https://i.stack.imgur.com/DS0a5.png
Here is all my code for my Django website inside a virtual host and my other non-project in a virtual host.
#Django Website
<VirtualHost *:443 _default_:443 neostorm.us.to:443>
ServerName neostorm.us.to
ServerAlias neostorm.us.to
Alias /static "C:/xampp/htdocs/neostorm/static"
<Directory "C:/xampp/htdocs/neostorm/static">
Require all granted
</Directory>
WSGIScriptAlias / "C:/xampp/htdocs/neostorm/neostorm/wsgi_windows.py" application-group=neostorm
<Directory "C:/xampp/htdocs/neostorm/neostorm">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
ErrorLog "C:\xampp\apache\logs\neostorm_error.log"
CustomLog "C:\xampp\apache\logs\neostorm_custom.log" common
</VirtualHost>
#Non Django Website
<VirtualHost *:443 mail.neostorm.us.to:443>
ServerName mail.neostorm.us.to
DocumentRoot "C:/xampp/htdocs/webmail"
<Directory "C:/xampp/htdocs/webmail">
Require all granted
</Directory>
</VirtualHost>
Any help would be appreciated.
The problem was there was a virtual host inside the httpd-ssl.conf file. Simply delete the virtual host inside that file and create your virtual hosts inside your virtual host file or where ever you want it.
The reason you may see your index of files is because there is no index.html to open.
This is an example of a virtual host
<VirtualHost *:443>
ServerName example.com
DocumentRoot "C:/xampp/htdocs/example"
<Directory "C:/xampp/htdocs/neostorm/webmail">
AllowOverride All
Require all granted
Options +Indexes
</Directory>
SSLEngine on
SSLCertificateFile "conf/example/example.com-chain.pem"
SSLCertificateKeyFile "conf/example/example.com-key.pem"
</VirtualHost>
If there is no index file inside that document root, you will see the index/ page.
Just a footnote, Options +Indexes tells your web server to show an index page like seen in the photo. Use Options -Indexes to prevent showing the index/ page, this is beneficial if you have a static folder.

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>

Issue when setting-up wsgi as an alias path in apache

NOTE: This question is different from 'Add a prefix to all Flask routes' as I am trying to resolve this at apache level. Additional, the suggested fix for flask routes did not work!
Following on from this post, I'm trying to set up apache to serve PHP files by default, but point a given alias (i.e. /flaskapp) to a wsgi path. The wsgi file in turn routes requests to a python flask app.
Here's the apache config that I'm trying (under 000-default.conf):
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
Alias / /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
WSGIScriptAlias /flaskapp "/var/www/flaskapp/deploy.wsgi"
<Directory /var/www/flaskapp>
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
After doing a service apache2 restart I find that requests to http://myip/flaskapp result in a 404 error. Everything else works fine.
Things I've tried so far:
Double checking all the file and folder paths (no issues found)
Using the wsgi part of the above code to set up the wsgi app as a standalone virtualhost (works fine)
Adding app.config['APPLICATION_ROOT'] = '/flaskapp' to my app.py file, as suggested the question 'Add a prefix to all Flask routes' (Didn't have any effect)
Where could I be going wrong?
Instead of:
Alias / /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
use:
DocumentRoot /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
Using '/' with Alias takes precedence over everything else including mod_wsgi's ability to intercept requests at a sub URL. So for stuff at root of the site you need to use DocumentRoot directive.

Deploy cherrypy on apache with mod-wsgi

I installed mod-wsgi and have a cherrypy helloworld code I want to deploy it to apache because I want to use multithreading and measure performance of cherrypy.
What apache configs I have to do including virtualhost and what to do with cherrypy code.
try this...
<VirtualHost *>
ServerName example.com
WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5
WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi
<Directory /var/www/yourapplication>
WSGIProcessGroup yourapplication
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
http://flask.pocoo.org/docs/deploying/mod_wsgi/#configuring-apache
your yourapplication.wsgi file will refer to your webapp.py file.
Hope this helps!

Howto configure Apache WSGI for multiple separate Django instances?

I have an apache instance where I have the following
WSGIPythonPath /production/somelocation/django12/lib/python2.4/site-packages/
<VirtualHost 192.168.1.1:443>
WSGIScriptAlias / /opt/project.wsgi
.....
My Django 1.5 app apache config looks like,
WSGIPythonPath /production/somelocation/django15/lib/python2.7/site-packages/
<VirtualHost 192.168.1.2:443>
....
WSGIScriptAlias / /opt/project2.wsgi
My /opt/project.wsgi looks like
import os
import sys
# django1.2 virtualenv
import site
site.addsitedir("/production/somelocation/django12/lib/python2.4/site-packages")
.....
However when I go to the site I still get my default django (1.5) instance. What am I missing ?
The other answers mention setting the python path, however using WSGIPythonPath or WSGIPythonHome are not correct. The WSGIPythonPath / WSGIPythonHome can only be set server-wide, so no different paths per virtualhost.
You would want to use the WSGIDaemonProcess python-path and home arguments to set the python path and your apps home directory per virtualhost.
Also, within your code there is no need to adjust python paths; just make sure your virtualhost config is correct.
You may need to set WSGIPythonHome since you have different Django installations.
WSGIPythonPath is used to define additional directories, but this option do not set default python installation. So probably, your default python directory also includes django (1.5) and recognize this version as the default django version. I do not know your python and django installation and configuration but this might be the reason.
Additional info for WSGIPythonHome
This is how I do with Pyramid:
<VirtualHost *:80>
Servername hackintosh
DocumentRoot "/Library/WebServer/Documents"
</VirtualHost>
<VirtualHost *:80>
ServerName modwebsocket.local
ErrorLog "/PythonProjects/MOD_WEBSOCKET/logs/error_log"
CustomLog "/PythonProjects/MOD_WEBSOCKET/logs/access_log" common
WSGIDaemonProcess pyramid-modwebsocket user=apero group=staff threads=4 python-path=/PythonProjects/MOD_WEBSOCKET/lib/python2.7/site-packages
WSGIProcessGroup pyramid-modwebsocket
WSGIScriptAlias / /PythonProjects/MOD_WEBSOCKET/wsgi/pyramid.wsgi
<Directory "/PythonProjects/MOD_WEBSOCKET/wsgi">
WSGIProcessGroup pyramid-modwebsocket
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName ai.local
ErrorLog "/PythonProjects/AI/logs/error_log"
CustomLog "/PythonProjects/AI/logs/access_log" common
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIDaemonProcess pyramid-ai user=apero group=staff threads=4 python-path=/PythonProjects/AI/lib/python2.7/site-packages
WSGIProcessGroup pyramid-wizard
WSGIScriptAlias / /PythonProjects/AI/wsgi/pyramid.wsgi
<Directory "/PythonProjects/AI/wsgi">
WSGIProcessGroup pyramid-ai
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This topic and the typical causes are detailed in:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
There is not enough information in your question to properly evaluate which of the problems you are encountering.

Categories

Resources