Django: Apache Virtual Host giving 500 server error - django

I don't know why I'm getting this error:
500 - Internal Server Error
In my httpd.conf I put the following code:
<VirtualHost *:80>
WSGIScriptAlias / "/var/django/projects/blunne/blunne/wsgi.py"
ServerName teste.blunne.com
Alias /static "/var/django/projects/blunne/static/"
<Directory "/var/django/projects/blunne/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
What's wrong with this code?
blunne is the name of my application, it works fine on development server.
I tried to change the paths, put the code without quotation, make a custom wsgi in the path: "/var/django/projects/blunne.wsgi" with the following code
import os, sys
sys.path.append(os.sep.join(os.path.abspath('/var/django/projects/blunne/').split(os.sep)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blunne.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
But always the same error... If i delete the virtualhost Apache gets normal
.
EDIT: In my localhost with wamp (Windows 7), I can run django with the following:
.
FILE: httpd-vhosts.conf
# httpd.conf includes conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
WSGIScriptAlias / "C:/Django/projects/blunne.wsgi"
ServerName beta.blunne.com
Alias /static "C:/Django/projects/blunne/static/"
<Directory "C:/Django/projects/blunne/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
FILE: C:/Django/projects/blunne.wsgi
import os, sys
#sys.path.append(os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))
sys.path.append(os.sep.join(os.path.abspath('C:/Django/projects/blunne/').split(os.sep)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blunne.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Then I configured hosts to
127.0.0.1 beta.blunne.com
I don't know what to do.

Related

End of script output before headers: wsgi.py

I am trying to install my django project with Apache, mod_wsgi and python3. but Apache still gives this error:
Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
Traceback (most recent call last):
File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
assert tlock is not None
AssertionError:
End of script output before headers: wsgi.py
I lost two days trying to fix this problem, I know that this error can be produced by several reasons but I not find where the problem.
Here the wsgi.py content:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import site, sys
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if path not in sys.path:
sys.path.append(path)
sys.path.append('/var/www/myproject/myproject_env/bin/python3.4/dist-packages')
site.addsitedir('/var/www/myproject/myproject_env/bin/python3.4/dist-packages')
os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
The Apache configuration is as follows:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www
Alias /static/ /var/www/myproject/static/
Alias /static/admin/ /var/www/myproject/static/admin/
Alias /uploads/ /var/www/myproject/uploads/
WSGIDaemonProcess myproject lang='fr_FR.UTF-8' locale='fr_FR.UTF-8' python-path=/var/www/myproject:/var/www/myproject/myproject_env/bin/python3.4/dist-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/myproject/myproject/wsgi.py
WSGIApplicationGroup %{GLOBAL}
<Directory "/var/www/myproject/myproject/">
Require all granted
</Directory>
<Directory "/var/www/myproject/myproject/wsgi.py">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
<Directory /static/admin/>
Require all granted
</Directory>
<Location "/uploads/">
SetHandler None
</Location>
ErrorLog /var/log/apache2/myproject.log
CustomLog /var/log/apache2/myproject.access.log combined
</VirtualHost>
Please anyone helps me fix this?
Solved: I changed the location of my Django project to another folder in a new linux account. I think (and I'm not really sure) the error occurred because /var/www contains another python project using cgi-bin, this maybe creates conflict with my Django project.

Deploy a Django site and a PHP site on the same server with Apache and mod_wsgi

I currently have a Django site working at cinepass.com.ec , I would like to deploy an additional PHP site to the same server at mobile.cinepass.com.ec
My current httpd.conf (from DjangoFoo) :
<Directory "/home/ec2-user/cinepass/media">
Order deny,allow
Allow from all
</Directory>
<Directory "/home/ec2-user/cinepass/cinepass">
AllowOverride All
Order deny,allow
Allow from all
</Directory>
Alias /media/ /home/ec2-user/cinepass/media/
ServerAdmin smansfield#palapa.com.ec
ErrorLog "logs/cinepass.com-error_log"
CustomLog "logs/cinepass.com-access_log" common
# mod_wsgi configuration is here
# we are running as user/group 'deamon', if you don't have those you need to change or create.
WSGIDaemonProcess cinepass python-path=/home/ec2-user/cinepass:/home/ec2-user/cinepass/venv/lib/python2.6/site-packages user=daemon group=daemon processes=2 threads=25
WSGIProcessGroup cinepass
# this is our WSGI file.
WSGIScriptAlias / /home/ec2-user/cinepass/cinepass/wsgi.py
My current wsgi.py :
import os, sys
sys.path.append('/home/')
sys.path.append('/home/ec2-user/cinepass/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cinepass.settings_production.py")
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
How would I edit my Apache configuration so that I can also run a php site at mobile.cinepass.com.ec?
Using apache´s virtualhosts, here I put an example of something similar in a server of mine, in which I have a djangp app in the main domain and a joomla in a subdomain. Both files are located in /etc/apache2/sites-enabled
Joomla´s apache conf file (named /etc/apache2/sites-enabled/manual.domain.com):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin dsanabria#domain.com
ServerName manual.domain.com
DocumentRoot "/home/ubuntu/manual/"
<Directory /home/ubuntu/manual/>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/manual.domain-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/manual.domain-access.log combined
</VirtualHost>
And the django app (named /etc/apache2/sites-enabled/www.domain.co):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin diego#diegue.us
ServerName domain.co
ServerAlias machete.anotherdomain.com
Alias /admin/media/ /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/
Alias /media/ /home/ubuntu/webapps/machete/machete/media/
Alias /static/ /home/ubuntu/webapps/machete/machete/collected/
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/django/contrib/admin/media/ >
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/collected/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptReloading On
WSGIDaemonProcess machete python-path=/home/ubuntu/webapps/machete/lib/python2.7/site-packages
WSGIProcessGroup machete
WSGIApplicationGroup machete
WSGIPassAuthorization On
WSGIScriptAlias / /home/ubuntu/webapps/machete/machete/machete/wsgi.py
ErrorLog /var/log/apache2/machete-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/machete-access.log combined
</VirtualHost>
The first, tells to apache, that if the user gets to manual.domain.com, just response with a php application (joomla). The second file says to apache, that if the user calls the server with www.domain.com response with a python wsgy, (django).
This is in a ubuntu server, redhat/centos/fedora locates the folder sites-enabled in another location that I can´t remember, but anyway you can use virtualhosts.
Generraly, I avoid to mess with the httpd.conf file and prefer use virtualhosts.

Django Deployment on Apache's virtualhost

I have a wordpress (/var/www/cb), which I wish to run as root (www.cb.com) and one Django app (/vc/cb/cb) as a subdirectory (cb.com/launch).
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin info#cb.com
ServerName cb.com
DocumentRoot /var/www/cb
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/cb>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo Indexes
Order allow,deny
allow from all
</Directory>
WSGIScriptAlias /launch /vc/cb/cb/wsgi.py
<Directory "/vc/cb/cb">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
</IfModule>
A similar question has been asked here, however that was pre-Django-1.4, where you had to create your wsgi file manually.
In the documentation it says the wsgi.py is created automatically for you. The main difference I see is the file extension, in the link above they refer to the wsgi file as xx.wsgi while django 1.4 documentation points to wsgi.py. I don't know if this is an issue, but I get a 404 when I do a https://cb.com/launch
The content of wsgi.py:
import os
import sys
sys.path.append('/vc/cb')
sys.path.append('/vc/cb/cb')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cb.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
What am I missing?

Django + Apache + mod_wsgi permission denied

I finished the tutorial on Django's site about using mod_wsgi (here), and having substituted my paths as appropriate, results in a big fat "Permission denied." when I try to access /. Here is the stuff I added to httpd.conf (mod_wsgi is enabled earlier in the conf file):
# Django configuration
WSGIScriptAlias / /usr/local/django/billing/apache/django.wsgi
<Directory /usr/local/django/billing/apache/django.wsgi>
Order allow,deny
Allow from all
</Directory>
AliasMatch ^/([^/]*\.css) /usr/local/wsgi/static/styles/$1
Alias /media/ /usr/local/django/billing/media/
Alias /static/ /usr/local/django/billing/static/
<Directory /usr/local/django/billing/static>
Order deny,allow
Allow from all
</Directory>
<Directory /usr/local/django/billing/media>
Order deny,allow
Allow from all
</Directory>
Edit #1:
I've gone through the slides multiple times, from the start: still no joy. Even after opening up the path to the script, chmod'ing every relevant directory to be readable, and chmod'ing the .wsgi script, I still get permission denied. If I change the directory path from /usr/local/django/billing/apache/django.wsgi to have the django.wsgi truncated, the server returns a configuration error, despite that being how it's configured in the slides.
Same configuration, same environment... but everything was working except a simple call to Popen() in one of my django/python routines...
"Permission denied"
Turned out to be SELINUX (enforcing mode) blocking apache.
You can make SELINUX happy with your application by running the following commands:
# semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/your/app(/.*)?'
# restorecon -R -v /path/to/your/app
I had the same problem with permission denied.
https://serverfault.com/questions/357804/apache2-mod-wsgi-django-named-virtual-servers
The specific error is described in:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
I had the same issue,Sometimes this happends if the WSGI application is located outside of any directories already configured to be accessible to Apache, particularly when it is on your home directory, its good to specify user=username directive.
/etc/apahe2/sites-avaliable/myvhost [section]
WSGIDaemonProcess localhost python-path=/home/hemanth/ecm:/home/env/lib/python2.7/site-packages user=hemanth
WSGIProcessGroup localhost
/etc/apahe2/sites-avaliable/myvhost [full]
<VirtualHost *:80>
ServerAdmin xy#gmail.om
ServerName localhost
ServerAlias localhost
DocumentRoot /home/hemanth/ecm
<Directory /home/hemanth/ecm>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /home/hemanth/ecm/index.wsgi
WSGIDaemonProcess localhost python-path=/home/hemanth/ecm:/home/env/lib/python2.7/site-packages user=hemanth
WSGIProcessGroup localhost
Alias /static/ /home/hemanth/ecm/static/
Alias /media/ /home/hemanth/ecm/media/
<Directory /home/hemanth/ecm/media/>
Order allow,deny
Allow from all
</Directory>
<Location "/static/">
Options -Indexes
</Location>
ErrorLog /home/hemanth/ecm/error.log
</VirtualHost>
index.wsgi
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/hemanth/env/local/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/home/hemanth/ecm')
sys.path.append('/home/hemanth/ecm/ecm')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ecm.settings")
# Activate your virtual env
activate_env="/home/hemanth/env/bin/activate_this.py"
execfile(activate_env, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I got it working after installing flask(in venv) and setting the WSGISocketPrefix.
I am deploying on centos6.8 running python3.6 in venv.
The project folder referenced here is the place where the actual django code is stored. The project public folder references here is accessed by the apache and contains simlinks to relevant resources along with the .htaccess and the .wsgi files relevant for the execution
The socket prefix may vary depending on the os and the apache configuration.
The permissions may vary depending on the apache version if you have issues you can change:
Order allow,deny
Allow from all
to
Require all granted
here is my mod_wsgi configuration (httpd.conf)
LoadModule wsgi_module **path to venv**/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGIPythonHome **path to venv**
WSGIDaemonProcess flask user=**user name** group=**user group**
WSGIProcessGroup flask
WSGISocketPrefix /var/run/wsgi
<Directory **path-to-project-dir**>
Options ExecCGI MultiViews Indexes
MultiViewsMatch Handlers
AddHandler wsgi-script .py
AddHandler wsgi-script .wsgi
DirectoryIndex index.html index.php index.py app.wsgi
Order allow,deny
Allow from all
</Directory>
here is the virtual host (httpd.conf)
<VirtualHost *:80>
DocumentRoot **project public folder**
ServerName **www.project.com**
ServerAlias **project.com**
WSGIScriptAlias / **project public folder**/site.wsgi
Alias /media/ **project public folder**/media/
<Directory **project public folder**/media>
Order allow,deny
Allow from all
</Directory>
Alias /static/ **project public folder**/static/
<Directory **project public folder**/static>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
here is the site.wsgi file
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('**path to venv**/lib/python3.6/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('**path to project folder containing manage.py**')
sys.path.append('**path to project folder containing settings.py**')
os.environ['DJANGO_SETTINGS_MODULE'] = '**project name**.settings'
# Activate your virtual env
#activate_venv.py is an empty python file which will activate
#the virtual environment when executed. Create it manually
activate_env=os.path.expanduser("**path to venv**/bin/activate_venv.py")
exec(open(activate_env).read(), dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I had to move following two lines from the wsgi.py to "init.py" in the same folder to solve "Applications not ready error"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Here is a dump of the packages in the virtual environment
Click==7.0
Django==2.2.1
django-debug-toolbar==1.11
django-redis==4.10.0
django-tastypie==0.14.2
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
mod-wsgi==4.6.5
mysqlclient==1.4.2.post1
Pillow==6.0.0
pip==19.1.1
python-dateutil==2.8.0
python-mimeparse==1.6.0
pytz==2019.1
redis==3.2.1
setuptools==41.0.1
simplejson==3.16.0
six==1.12.0
sqlparse==0.3.0
Werkzeug==0.15.4

No module named registro.forms in my own local django server [edited]

im setting Django Server in my Ubuntu machine, apache/wsgi, but im getting a error in my view:
was No module named registro.forms now after some code in my wsgi is No module name forms
#registro.views
from registro.forms import ComercioForm
In my laptop is running but not in my server machine
django is running
all database table are syncd
WSGI
#path /srv/www/project/apache/django.wsgi
import is, sys
sys.path.insert(0,'/srv/www')
sys.path.insert(0,'/srv/www/project')
sys.path.insert(0,'/srv/www/') #testing
sys.path.insert(0,'/srv/www/project/') #testing
os.environ['DJANGO_SETTINGS_MODULE']='project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
httpd.conf
#path /srv/www/project/apache/
Alias /media/ "/srv/www/project/public/admin_tools"
<Directory "/srv/www/project/public/admin_tools">
Allow from all
</Directory>
WSGIScriptAlias "/srv/www/project/apache/django.wsgi"
<Directory "/srv/www/project/apache">
Allow from all
</Directory>
project
#path /etc/apache2/sites-available
<VirtualHost *:80>
ServerName project
DocumentRoot /srv/www/project
<Directory /srv/www/project>
Order allow,deny
Allow from all
</Directory>
WSGIDeamonProcess project processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup project
WSGIScriptAlias / /srv/www/project/apache/django.wsgi
</VirtualHost>
Any idea?
thanks
You probably don't have the "registro"-application in your Django-Path. For an example how to fix it, see here
.