Problems with deploying in flask wsgi python2 on apache2 - python

I'm trying to deploy my flask on apache2. File structure is following inside /var/www/html/
parsers/
app.wsgi
app/
__init__.py
init.py
static/
templates/
My app.wsgi file
#!/usr/bin/python
import sys
import logging
sys.path.insert(0, '/var/www/html/parsers/')
from init import app as application
logging.basicConfig(stream=sys.stderr)
My parsers.conf file in /etc/apache2/site/sites-available/
<VirtualHost *:80>
ServerName 143.107.183.175:23580
WSGIScriptAlias / /var/www/html/parsers/app.wsgi
<Directory /var/www/html/parsers>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I'm getting the page
Index of/
What are the problems?

Related

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.

webapp2 mod_wsgi apache centos 500 internal server error; The wsgi script cannot be loaded as Python module

I'm using webapp2 to develop my app and I want to deploy it on apache using mod_wsgi but I'm getting 500 Internal server Error along with import webapp2 no module found error in error_log.
I installed WebOb, Paste, webapp2 on to the system as well as in virtualenv and tested on python environment
>> import webapp2
and it's working fine.
Error is:
> mod_wsgi (pid=15727): Target WSGI script '/var/www/html/app/main.py'
> cannot be loaded as Python module.
> mod_wsgi (pid=15727): Exception occurred processing WSGI script '/var/www/html/app/main.py'.Traceback (most recent call last):File
> "/var/www/html/app/main.py", line 20, in <module>
> import webapp2
My httpd.conf file configuration is below
WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /var/www/html/app/env/lib/python2.7/site-packages
<VirtualHost 10.10.10.9>
ServerName prep2016.msitprogram.net
ServerAlias prep2016.msitprogram.net
ServerAdmin sirimala.sreenath#gmail.com
# DocumentRoot /var/www/html/app
#WSGIDaemonProcess app python-path=/var/www/html/app:/var/www/html/app/env/lib/python2.7/site-packages
WSGIDaemonProcess prep2016.msitprogram.net processes=2 threads=15
WSGIScriptAlias / /var/www/html/app/main.py
<Directory /var/www/html/app/>
# options +ExecCGI
# DirectoryIndex main.py
# Order allow,deny
# Allow from all
# </Directory>
# DocumentRoot /var/www/html/app
# WSGIDaemonProcess prep2016.msitprogram.net processes=2 threads=15
#WSGIDaemonProcess prep2016.msitprogram.net user=apache group=apache processes=2 threads=15
WSGIProcessGroup prep2016.msitprogram.net
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
# WSGIScriptAlias / /var/www/html/app/main.py
# WSGIPythonPath /var/www/html/app
# <Directory /var/www/html/app>
# <Files main.py>
Order allow,deny
Allow from all
# Require all granted
# </Files>
</Directory>
</VirtualHost>
This is my main.py file content
#!/usr/bin/env python
import os
import webapp2
import jinja2
import logging
import json
import datetime
import jwt.api_jwt
import os, sys
ABSPATH = os.path.dirname (__ file__)
sys.path.append (ABSPATH)
os.chdir (ABSPATH)
sys.path.append('/var/www/html/app/env/lib/python2.7/site-packages')
#site.addsitedir('/var/www/html/app/env/lib/python2.7/site-packages')
class MainPage(Webapp2.RequestHandler):
def get(self):
self.response.out.write("hello world")
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
def main():
from paste import httpserver
httpserver.serve(app, host='prep2016.msitprogram.net', port='8080')
if __name__ == '__main__':
main()

Apache and Flask: attempt to invoke directory as script

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.

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.

client denied by server configuration

I am trying to setup django project by apache and mod_wsgi file. But I am getting this error client denied by server configuration: /home/ghrix/production . I have google this errro and found lot of solutions but nothing worked for me.
My code are as follows :
production.wsgi
import os
import sys
sys.path = ['/home/ghrix/myproject/'] + sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
production.conf file :
<VirtualHost *:80>
WSGIScriptAlias / /home/ghrix/production.wsgi
ServerName firstweb.com
ServerAlias firstweb.com
Alias /static/ /home/ghrix/myproject/static/
<Directory /home/ghrix/myproject/ >
Options Indexes FollowSymLinks
WSGIProcessGroup production
WSGIApplicationGroup %{GLOBAL}
Require all denied
</Directory>
</VirtualHost>
I solved the problem by making two changes :
1) Require all denied -> change this to -> Require all granted
2) If you look at the project created by you there is already a wsgi.py file in you project directory (same dir where your settings are placed by default) , So you do not have to create seperate wsgi file as I have created initially. Just point to that wsgi.py in your conf file inside apache2 as
WSGIScriptAlias / /<path to my-project wsgi.py file>
That's it you error is resolved. But still if you are getting one more error after this your settings module is not find then you have to edit your wsgi file and add two lines of code :
import sys
sys.path = ['<path to my project>'] + sys.path
That's it your project will run smoothly now.
So complete wsgi and conf files will look like this :
wsgi.py file :
import os
import sys
sys.path = ['<path to my project>'] + sys.path
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
and conf file :
<VirtualHost *:80>
WSGIScriptAlias / /<path to my project>/wsgi.py
ServerName secondweb.com
ServerAlias secondweb.com
Alias /static/ /<path to my project>/static/
<Directory /<path to my project>/ >
Options Indexes FollowSymLinks
Require all granted
</Directory>
</VirtualHost>

Categories

Resources