Apache and Django Error forbidden and access to server - python

I want to deploy my Django project on my apache server but even if I follow django's example in deploying with apache I get a 403 Error. I am using:
django 1.6
apache 2.4.7
python 2.6.x
My wsgi.py is the one created by django-admin startproject
import os, sys
#root = os.path.join(os.path.dirname(__file__),'..')
# sys.path.insert(0, root)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rhombus.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
My VirutalServer conf file is the following
rhombus.conf
WSGIPythonPath /var/www/rhombus2
<VirtualHost *:80>
ServerAdmin webmast#rhombus.com
ServerName myrhombus.com
ServerAlias www.myrhombus.com
DocumentRoot /var/www/rhombus2
WSGIScriptAlias / /var/www/rhombus2/rhombus/wsgi.py
<Directory /var/www/rhombus2/rhombus>
<Files wsgi.py>
Require all granted
</Files>
Options Indexes FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
and I have placed rhombus2 inside /var/www. All foldters and files are at 755 with sudo chmod -R 755 /var/www/rhombus2. But even with that I get a Forbidden Error.
Among other things, when I remove the WSGIPythonPath, I don't get Import Error, which I probably should. I know it is not the secure way to have everything (code too) under document root. But I really need to have it working first, and then make it secure. Is there something wrong with my code?
EDIT: error.log
(13)Permission denied: [client 127.0.0.1:46669] AH00035: access to /favicon.ico denied (filesystem path '/home/avlahop/development/django') because search permissions are missing on a component of the path
Not sure why it's showing me this path, cause there isn't it anywhere in my rhombus.conf.
EDIT2: I also don't have any other site enabled. My hosts file has the following line
127.0.0.1 www.myrhombus.com

here is my working virtualhost
Listen 9090
<VirtualHost *:9090>
ServerName www.test.com
WSGIDaemonProcess project processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup project
WSGIScriptAlias / /srv/www/project/project/wsgi.py
Alias /static /srv/www/project/static/
Alias /favicon.ico /srv/www/project/static/favicon.ico
ErrorLog /var/log/apache2/project_error.log
LogLevel Info
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
and wsgi.py
import os, sys, site
from os.path import dirname, abspath, join
site.addsitedir('/home/user/virtualenvy/some_project/lib/python2.7/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "some_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I hope it helps You :)

Assume that Appache's username is www-data, run this command to check www-data's permission on your application folder: sudo su - www-data -s /bin/bash -c 'ls /var/www/rhombus2/rhombus/'
Since you did chmod 755 the folder, so I think it'll work, if not, check permisson of /var and /var/www
Also, paste content of wsgi.py might be helpful for solving problem.

Related

403 forbidden using a flask app with WSGI and Apache2.4

I'm trying to run a dash app on our website in a sub-directory. I'm getting a 403 forbidden when trying to access to website.
In the /etc/httpd/logs/ssl_error_logs i get the error:
[Tue Jan 03 13:53:42.973046 2023] [autoindex:error] [pid 50576] [client 134.94.71.5:60550] AH01276: Cannot serve directory /var/www/Typesetter/appname/: No matching DirectoryIndex (index.html,index.php,index.php) found, and server-generated directory index forbidden by Options directive
I think i might have something wrong with either the permissions or the virtualhost setup. But i can't figure out what is going wrong.
I want to run the dashboard on: our-group.our-uni.com/appname
The server is running on centos7.
I got a data folder with biochemical information to provide to the users in /var/www/html/appname/data/ from which i can download the data if i go to our-group.our-uni.com/appname/data/datapoint1.pt.
Since this works and i can also put a index.html/index.php page there without issues, i think my virtualhost setup pointing to WSGI is incorrect?
The error says cannot serve directory /var/www/Typesetter/appname/, but this directory is a symbolic link to /var/www/html/appname/.
The virtualhost file appname.conf located in /etc/httpd/conf.d/:
<VirtualHost *:80>
ServerName our-group.our-uni.com
DocumentRoot /var/www/html/appname
Errorlog /var/www/html/appname/appname-error.log
Alias /topenzyme /var/www/html/topenzmye
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appname-env
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
Options Indexes FollowSymLinks ExecCGI
Require all granted
</Directory>
</VirtualHost>
I also tried changing to <Directory /var/www/typesetter/appname>, but this also did not work.
The contents of testapp.wsgi in /var/www/html/appname/:
import sys, os
sys.path.insert(0, '/var/www/html/appname')
from testapp import app as application
The contents of testapp.py in /var/www/html/appname/:
from flask import Flask
app = Flask(__name__)
#app.route('/topenzyme/')
def main():
return 'Hello world'
if __name__ == '__main__':
app.run()
Permission setup:
-rwxr-xr-x. 1 apache apache 148 Jan 3 13:37 testapp.py
-rwxr-xr-x. 1 apache apache 100 Jan 3 13:38 testapp.wsgi
I also tried 777, but from what i've read 755 permissions should be sufficient?
If i do httpd -M i get wsgi_module (shared).
The wsgi module should be available.
I tried various virtualhost configs from other posts with similar issues.
I'm really unsure why this is not working.
Is there anything wrong with my configuration settings or could it be something else?
Found out the issue. I was miss-configuring my Apache setup.
As we are already hosting a website on the domain, i should not have tried to setup a virtualhost for the sub-directory. From what i understand now this only works for setting up (sub)domains. With this structure of setting it up the appname.conf file gets added the already existing virtualhost config.
Instead the /etc/httpd/conf.d/appname.conf is now:
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appnameenv
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
Options FollowSymLinks
AllowOverride All
Require all granted
allow from all
</Directory>
This corrently links the wsgi application to my sub-directory location i wanted (our-group.our-uni.com/appname)

408 Request Timeout Using Apache + mod_wsgi + Python Django in Windows

I have a Django app which is deployed in local network using Apache + mod_wsgi under Windows. When I run python manage.py runserver, everything works fine. But when I start the Apache Service, I cannot access the app. The only response I get from the access.log is the error code 408. Below is my httpd.conf:
LoadFile "c:/users/felix/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/felix/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/felix/appdata/local/programs/python/python37"
ServerName localhost
WSGIScriptAlias / "D:/dev/test_server/django/django/wsgi_windows.py"
Listen 8000
<VirtualHost *:8000>
WSGIPassAuthorization On
ErrorLog "logs/django.error.log"
CustomLog "logs/django.access.log" combined
Alias /static "D:/dev/test_server/staticfiles"
<Directory "D:/dev/test_server/staticfiles">
Require all granted
</Directory>
<Directory "D:/dev/test_server/django/django">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
And below is the wsgi_windows.py file:
# Activate the virtualenv
activate_this = 'D:/dev/test_server/.venv/Scripts/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))
import os # noqa
import sys # noqa
import site # noqa
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('D:/dev/test_server/.venv/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('D:/dev/test_server/django')
sys.path.append('D:/dev/test_server/django/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django.settings")
from django.core.wsgi import get_wsgi_application # noqa
application = get_wsgi_application()
I'd appreciate any ideas or hints on the issue.
start apache at command line with 'httpd' and look for error messages. If Apache has a problem at startup there is no message in the log files.
check error.log
You can place 'print('xyz') even in the settings.py and elsewhere and this way by checking error.log see how your app is setup and how far a request is processed. If your app get stuck somewhere like this you find the code where it is stuck
I have the same error after install scipy library and use it in some scrip in django. I found that some libraries like "numpy" and "scipy" only work in the Python main interpreter and you have to force the WSGI to run in the global app group to run it. Adding this line in my conf file work for me.
WSGIApplicationGroup %{GLOBAL}

Using Django w/ mod_wsgi and Apache

I've been trying to work out how I'll go about setting up a Django application on production, when it's ready for deployment. I'm using Django v1.11, and my EC2 is running Ubuntu 14.04. I have been attempting to refer to this guide as reference, however it is not specific to Ubuntu, so I've been experiencing a bit of difficulty in this regard. I've referred to several other resources, but much of what is out there seems to be outdated.
I have a host rule set up on my local machine, pointing www.example.com to my EC2 instance's public IP address.
I have a virtualenv set up which lives in /home/django/example.com/ENV. My Django project lives in /home/django/example.com directly. The project name is mysite, and was generated using django-admin startproject mysite, thus it has the default wsgi.py file inside the /home/django/example.com/mysite directory. The contents of wsgi.py look like:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I've tried adding VirtualHost rules such as the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory "/home/django/example.com/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Similarly, I've tried adding:
Include /etc/apache2/httpd.conf
to /etc/apache/apache2.conf and chucking the following:
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
WSGIPythonHome /home/django/example.com/ENV
WSGIPythonPath /home/django/example.com
<Directory /home/django/example.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
into httpd.conf.
In either case, I've restarted the Apache server directly afterwards.
I'm not getting any further than hitting "500 Internal Server Error" or hitting an "ERR_EMPTY_RESPONSE".
Anyone able to shed some light around a) where I'm going wrong, b) where I can refer to for up-to-date instructions, or c) how I can troubleshoot this?
After a lot of troubleshooting, and consulting with the resource mentioned in Graham's comment, here's what I established was required in my VirtualHost:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Alias /static /home/django/example.com/static
<Directory /home/django/example.com/static>
Require all granted
</Directory>
WSGIDaemonProcess mysite python-path=/home/django/example.com:/home/django/ENV/lib/python3.4/site-packages
WSGIProcessGroup mysite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory /home/django/example.com/mysite>
Require all granted
</Directory>
</VirtualHost>
and here are the contents of wsgi.py that I settled on:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os, sys
from django.core.wsgi import get_wsgi_application
path = '/home/django/example.com'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
And it is also worth noting (simply because it wasn't immediately obvious to me) that it is necessary to specify:
STATIC_ROOT = '/home/django/example.com/static'
in settings.py, and to run python manage.py collectstatic to collect static files from all applications into said STATIC_ROOT.
I hope this helps somebody else, in future!

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