Deploying PyAMF Django project on Apache with mod_wsgi - django

I have installed PyAMF, Django, Apache2, and mod_wsgi on a Mac OS X 10.6 machine. While developing the project I used Django development servier. I would like to continue to use http://localhost:8000/gateway or http://127.0.0.1:8000/gateway for the PyAMF gateway. I have my Flex project deployed on http://localhost and phpMyAdmin at http://localhost/phpmyadmin.They seem to work fine but I am not able to connect to the PyAMF gateway.
Here is my Apache setting:
<VirtualHost *:8000>
# Set this to 'warn' when you're done with debugging
LogLevel debug
CustomLog /var/log/apache2/pyamf-access.log combined
ErrorLog /var/log/apache2/pyamf-error.log
# PyAMF remoting gateway
WSGIScriptAlias /gateway /Library/WebServer/Documents/MyApp/django.wsgi
<Directory /Library/WebServer/Documents/MyApp>
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Here is the content of the django.wsgi script
import os
import sys
path = '/Library/WebServer/Documents'
if path not in sys.path:
sys.path.append(path)
sys.path.append('/Library/WebServer/Documents/MyApp')
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
os.environ['DJANGO_SETTINGS_MODULE'] = 'MyApp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
import posixpath
def application(environ, start_response):
# Wrapper to set SCRIPT_NAME to actual mount point.
environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME'])
if environ['SCRIPT_NAME'] == '/':
environ['SCRIPT_NAME'] = ''
return _application(environ, start_response)
I would appreciate any help.
Thanks

When you added separate VirtualHost on port 8000, did you also add to Apache configuration in appropriate place:
NameVirtualHost *:8000
Usually there would only be such a line for port 80.

Related

404 error when deploying django project with apache2

I tried to create a new django project on apache2, but I got 404 error.
Here are what I did.
-In /home/ubuntu/django directory, type
django-admin startproject proj
-Edit proj/proj/wsgi.py as follows.
import os
import sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
sys.path.append('/home/ubuntu/django/proj');
sys.path.append('/home/ubuntu/django/proj/proj');
application = get_wsgi_application()
-Edit ALLOWED_HOSTS in proj/proj/settings.py as follows.
ALLOWED_HOSTS = ['example.com']
-Edit /etc/apache2/conf-available/wsgi.conf as follows.
WSGIScriptAlias /proj /home/ubuntu/django/proj/proj/wsgi.py
WSGIPythonPath /home/ubuntu/django/proj
<Directory /home/ubuntu>
Require all granted
</Directory>
<Directory /home/ubuntu/proj/proj>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
-Restart apache2 as
/etc/init.d/apache2 restart
-Access to http://example.com/proj, and then got 404 error as follows.
Page not found (404)
Request Method: GET
Request URL: http://example.com/proj/
Using the URLconf defined in proj.urls, Django tried these URL patterns, in this order:
admin/
The empty path didn't match any of these.
My enviroment is as follows.
Ubuntu 18.04 LTS (AWS)
Apache/2.4.29
Python 3.6.9
Django 3.0.5
How can I solve this 404 error?
Do you have '/admin' in your urls.py file?
You may also need to change the apache2 settings from /home/ubuntu/proj/proj to /home/ubuntu/django/proj/proj

WSGI Django 403 Forbidden

When I run
sudo service apache2 restart
I get an error
unable to resolve host hostname
When I look at the error.txt it says it's my WSGI configuration.
Here is my WSGI script.
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
sys.executable = '/usr/lib/python2.7/dist-packages'
application = WSGIHandler()
django.setup()
EDIT1:
Here is my apache2 file. I did change the WSGI file to /user/app/app_settings/wsgi.py
<VirtualHost website:80>
ServerName website.com
ServerAlias www.website.com
ServerAdmin email#website.com
DocumentRoot /home/user/app/static
WSGIScriptAlias / /home/user/app/app_settings/wsgi.py
<Directory /home/user/app/>
Order allow,deny
Allow from all
Require all granted
</Directory>
#Alias /robots.txt /var/www/user/app/robots.txt
#Alias /favicon.ico /var/www/user/app/favicon.ico
#Alias /images /home/user/app/static/images
#Alias /static /home/user/app/static/
ErrorLog /var/www/website/error.log
#CustomLog /var/www/website/access.log combined
</VirtualHost>

Unable to run my Django Site (Version 1.6) on Apache Server

I am running a site that was previously running on django 1.4, but now I shifted it on django 1.6.
I am able to run my site on both version using python manage.py shell.
I am running it nicely on Apache when i keep django 1.4 virtual environment python path in Apache site-enabled, but it does not work when i keep django 1.6 virtual environment python path there.
For django 1.6 it throws error:
ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
What can be problem? Any changes required in wsgi.py file for django 1.6? Please help.
my virtualhost file is :
<VirtualHost *:80>
ServerAdmin vidya.sagar0911#gmail.com
DocumentRoot /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/
ServerName www.vidblog.com
<Directory /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo>
#Order deny,allow
#Allow from all
Options FollowSymLinks
AllowOverride None
</Directory>
Alias /media /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/vidyamedia/
Alias /static /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/static/
WSGIDaemonProcess www.demo.com user=www-data group=www-data processes=8 threads=75\
python-path=/home/vidya/workspace/djnago1.6/lib/python2.7/site-packages
WSGIProcessGroup www.vidblog.com
WSGIScriptAlias / /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/demo/wsgi.py
</VirtualHost>
This is the issue when you are migrating your old projects from django-1.x(<1.6) to django-1.6 you need to define DJANGO_SETTINGS_MODULE before you call get_wsgi_application
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Deploying django - "ImportError" for Apache VirtualHost and mod_wsgi

I'm deploying django on port 81 of my webserver. This is to get it off the development server, but I'm not ready to have it be served as the root on port 80.
The problem is that any time I try to access any page on port 81, I see the django error screen and it always says: ImportError at / No module named mysite.urls
httpd.conf
Listen 81
NameVirtualHost *:81
<VirtualHost *:81>
DocumentRoot /home/bill/Desktop/mysite
WSGIScriptAlias / /home/bill/Desktop/mysite/django.wsgi
<Directory /home/bill/Desktop/mysite>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
/home/bill/Desktop/mysite/django.wsgi
import os
import sys
sys.path.append('/home/bill/Desktop/mysite')
os.environ['PYTHON_EGG_CACHE'] = '/home/bill/Desktop/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
in mysite/settings.py
...
ROOT_URLCONF = 'mysite.urls'
...
I know for a fact that ROOT_URLCONF = 'mysite.urls' is getting loaded, because if I change it to anything else (like "test"), I get ImportError at / No module named test
Any thoughts about where this deployment has gone wrong? Thanks in advance!
Your deployment is fine. Your code needs to be corrected, in that it should only be importing (and subsequently using) urls.
(Although os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' is sort of redundant.)

Problem with Django setup using mod_wsgi and apache on centos 5.4

I am trying to setup apache with mod_wsgi to serve Django through a nginx proxy on CentOS 5.4. I want to start by configuring Apache with Wsgi to serve on a local port, but I get a 403 error when I run curl localhost:8080
# Httpd config:
Listen 127.0.0.1:8080
NameVirtualHost 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
ServerName staging.example.com
LogLevel warn
ErrorLog /home/django/project_name/log/apache_error.log
CustomLog /home/django/project_name/log/apache_access.log combined
DocumentRoot /home/django/project_name/django_project_dir/
WSGIDaemonProcess staging.example.com user=apache group=apache threads=25
WSGIProcessGroup staging.example.com
WSGIScriptAlias / /home/django/project_name/django_project_dir/django.wsgi
<Directory /home/django/project_name/django_project_dir>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
And my wsgi script:
#/home/django/project_name/django_project_dir/django.wsgi
ALLDIRS = ['/home/django/project_dir/virtualenv/lib/python2.4/site-packages']
import os
import sys
import site
prev_sys_path = list(sys.path)
for directory in ALLDIRS:
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
sys.path.append('/home/django/project_name/')
os.environ['PYTHON_EGG_CACHE'] = '/home/django/.python-eggs'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
What is the problem with this configuration?