Django with Apache and mod_wsgi on Windows 2008 Server - django

I'm experiencing a problem with deploying a site with Apache 2.2, Django 1.5.4, Python 3.3.2 and mod_wsgi 3.4
Here is my train of thought.
After installing an Apache, I put "Hello world" index.html in htdocs directory to check if I set it up properly. No problem here.
Than I installed Python, Django (import django - no errors) and mod_wsgi.
To check on wsgi I did httpd.exe -e debug and got this in return:
...
[debug] mod_so.c(246): loaded module wsgi_module
Than I added following to httpd.conf:
WSGIScriptAlias / C:/Program Files (x86)/Apache2.2/htdocs/my_project/my_site/apache/django.wsgi
<Directory C:/Program Files (x86)/Apache2.2/htdocs/my_project/my_site/apache/>
Order deny,allow
Allow from all
</Directory>
At this point I can't start an Apache no more - The requested operation has failed!
And now httpd.exe -e debug returns Syntax error on line 496 of C:/Program Files (x86)/Apache2.2/conf/httpd.conf: Invalid option to WSGI alias definition.
Please, let me know if addition information required.
This is my first experience and your assistance is greatly appreciated.

When you have spaces in path names in the Apache configuration, they must be quoted.
WSGIScriptAlias / "C:/Program Files (x86)/Apache2.2/htdocs/my_project/my_site/apache/django.wsgi"
<Directory "C:/Program Files (x86)/Apache2.2/htdocs/my_project/my_site/apache/">
Order deny,allow
Allow from all
</Directory>

Related

Apache, Django and wsgi, No module named 'encodings'

I've looked through many stack overflow questions with the same issues, but none of them solved my issue. I'm trying to deploy Django on AWS - Ubuntu 18.04 using Apache and WSGI.
When running my website with Django's servers on port 8000, everything works fine. Then when I attempt to run on apache I get the following error in my logs.
Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
Command line: '/usr/sbin/apache2'
(2)No such file or directory: mod_wsgi: Unable to stat Python home /var/www/html/projects/venv
Python interpreter may not be able to be initialized correctly. Verify the supplied path
and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
As it says clearly mod_wsgi and Python 3 are configured. I am using a virtual environment. My project is located at:
/var/www/html/project/venv/mysite
My Apache conf file is configured as:
<Directory /var/www/html/projects/venv/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-path=/var/www/html/projects/venv/mysite python-home =/var/www/html/projects/venv
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/html/projects/venv/mysite/mysite/wsgi.py
Alias /static /var/www/html/projects/venv/mysite/polls/static
<Directory /var/www/html/projects/venv/mysite/polls/static>
Require all granted
</Directory>
I'm not sure where to go from here. I don't fully understand what the error means. I have Python installed in my Venv. There is a file pyenv.conf which reads
home = /usr
implementation = CPython
version_info = 3.6.9.final.0
virtualenv = 20.0.20
include-system-site-packages = false
base-prefix = /usr
base-exec-prefix = /usr
base-executable = /usr/bin/python3
I'm wondering if this base-executable path being different than my python path in the apache config file is causing problems, but I'm not sure exactly what I need to do from here (or if this is even part of the problem. I believe permissions should not be the issue since I added all of my files within the var/www/html folder.
Update: I added the recommended permissions to all files in the chain and that didn't change anything.
Any advice is appreciated.

Django - Apache2 - Postgresql: error with _psycopg

Trying to deploy a django project on apache (2.2) on Debian 7.
Using Django 1.8 and a virtual environment with python3.4.
Getting the following error:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named _psycop
I have tried the suggestions pointed out here, and here.
Here is my apache conf.
(following lines are nested within VirtualHost scope)
#WSGI configuration for ds_monitor django project
Alias /static /home/pkaramol/Applications/timeds/ds_monitor/static_root
<Directory /home/pkaramol/Applications/timeds/ds_monitor/static_root>
Allow from all
</Directory>
<Directory /home/pkaramol/Applications/timeds/ds_monitor/ds_monitor>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
WSGIDaemonProcess ds_monitor python-path=/home/pkaramol/Applications/timeds/ds_monitor:/home/pkaramol/Applications/timeds/venv_p34dj18/lib/python3.4/site-packages
WSGIProcessGroup ds_monitor
WSGIScriptAlias /ds_monitor /home/pkaramol/Applications/timeds/ds_monitor/ds_monitor/wsgi.py
and this outside VirtualHost scope (to enable production - time virtual environment)
WSGIPythonPath /home/pkaramol/Applications/timeds/ds_monitor:/home/pkaramol/Applications/timeds/venv_p34dj18/lib/python3.4/site-packages
Another issue is the following line present in apache's log stack trace
File "/usr/lib/python3.2/importlib/_bootstrap.py", line 821, in _gcd_import
loader.load_module(name)
Why is it using system's python 3.2?
Have I done s.th wrong in terms of pointing the virtualenv path correctly in wsgi confs?
In any case, the psycopg2 module is also installed in the system (not only in virtualenv)
Seems very weird but the whole issue most likely had to do with the python version that mod_wsgi was compiled against.
I had downloaded / configured / installed version 4.5.2 of mod_wsgi from here but forgot to configure with the appropriate python version.
So I run through the whole process again (starting from unzipping the original tarball) and configuring as follows:
./configure --with-python=/usr/bin/python3.5
Restarted apache and problem solved.

Django (1.6) + Apache2

First time I setup Django and Apache, and I'm having a tough time. It's seems easy by following tutorials, but it doesn't seem to work.
Basically, the steps I followed (on Debian):
Install Django (and some libraries)
Install Apache2
Install mod_wsgi
Then I've put my Django app in /root/, so the path is something like :
root/
.Projet/
.myprojet/
.site/
. .#here the models.py, views.py, etc of my site
.myprojet/
. .#here the settings.py, wsgi.py
. .static/
. .#static files in folders
.templates
. .#my templates
.database.sql
.manage.py
Then in /etc/apache2 I created the file httpd.conf, with this inside :
WSGIScriptAlias / /root/Projet/myprojet/myprojet/wsgi.py
WSGIPythonPath /root/Projet/myprojet
<Directory /root/Projet/myprojet/myprojet>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I do a2enmod wsgi (it tells me "Module wsgi already enabled").
Then service apache2 restart (successful, but tells me I have no VirtualHosts).
When I go to my website (using the IP adress), just the Apache "It works" display.
Thank you for your help ^^
You shouldn't put your Apache configuration file in /etc/apache2/. The fact that you mention a2enmod implies that you are on a Debian-based distro like Ubuntu. For those systems, you need to give that file a name related to the app - call it django if you like - and put it in /etc/apache2/sites-available. Then you can run a2ensite django to link it to sites-enabled, restart Apache, and it should work.

Apache is not serving static files from Django app

I don't know what's wrong in my virtualhost for django project but the simply question is no matter what modification I do over this file stills output the same in error log from apache and not load any css or js files, what I can see is that Apache is looking for static and media file in the root web folder: /var/www
[Fri May 30 00:58:08 2014] [error] [client 192.168.1.145] File does not exist: /var/www/static, referer: http://192.168.1.143/dgp/login/
I set up virtual host file as follows:
WSGIPythonPath /var/www/dgp_python2_7/bin/python2.7:/var/www/dgp_python2_7/lib/python2.7/site-packages
WSGIScriptAlias /dgp /var/www/dgp/dgp/dgp/wsgi.py
<VirtualHost 127.0.0.1:80>
ServerName www.dgp.dev
ServerAlias dgp.dev
AliasMatch ^/([^/]*\.css) /var/www/dgp/dgp/static/$1
Alias /media/ /var/www/dgp/dgp/media/
Alias /static/ /var/www/dgp/dgp/static/
Alias /images/ /var/www/dgp/dgp/images/
<Directory /var/www/dgp/dgp/static/>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/dgp/dgp/media/>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/www/dgp/dgp/error.log
CustomLog /var/www/dgp/dgp/access.log combined
</VirtualHost>
And in settings.py STATIC_ROOT with '/var/www/dgp/dgp/static/' where is located all the css content.
How can I tell apache or Django to looking for the proper directory '/var/www/dgp/dgp/static/'? It's driving me crazy, I don't understand how something so elemental in development it's so complex for production.
Regards!
Edit with the solution
The really problem was that I didn't disable the default site for Debian Apache (that is the version I'm working for) and has another method for stablish virtualhost, at beginning we have to disable default site with the follow command: a2dissite defaultand everything works now like a charm!
You can tell where your static files are being looked for in your project's rendered html. Just view the source in your browser and look for a stylesheet or javascript include, what is the full path to the file?
My guess, you have to run Django's collect static script, which will collect all the static scripts in all of your project's app and put them into one location. This is a core part of deploying Django projects and unavoidable if you use multiple "apps" in your project.
in your terminal go to the Django projects root folder and type this:
python manage.py collectstatic
Read more at https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

Configuring Apache 2 with mod_wsgi for Django - no httpd.conf file

I am using Apache 2 on raspbian os. I read somewhere that in the newest versions of Apache there is no httpd.conf file, is this true? [Edit] (This is true)
My apache server is running (I can see the default web page), and mod_wsgi is successfully installed. So I began the instructions here for using django 1.5 with apache. However it tells me I need to add the following lines to the httpd.conf file:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
However, there is no httpd.conf file present in my version of apache. I have checked /etc/httpd/confand /etc/apache2 for the http.conf file. /etc/apache2 is where apache is though.
In short, where should I add the required settings so that apache recognizes mod_wsgi.
Possibly, many distros use
/etc/apache2/apache2.conf
Instead. However you shouldn't really edit that and it affects apache2 globally, i.e. all you sites (virtual-hosts). If you have only the one site better edit the contents of:
/etc/apache2/sites-available/default
The Ubuntu Apache2 configuration documentation is a good getting started guide: https://help.ubuntu.com/14.04/serverguide/httpd.html#http-configuration
Here is tutorial configuring Apache2 with mod_wsgi for Django on Ubuntu 11.04 wihtout httpd.conf: http://blog.madspace.me/configure-python-django-with-apache/, and one on 12.04: http://www.lennu.net/2012/05/14/django-deployement-installation-to-ubuntu-12-dot-04-server/