Context
Python 2.7 64bit
Django 1.3
WAMP 2.2 (Apache 2.2.22) 64bit
mod_wsgi 3.4 64bit
Windows 7 64bit
Detail | Apache httpd.conf
...
Listen 192.168.20.11:8080
...
LoadModule wsgi_module modules/mod_wsgi.so
...
ServerName 192.168.20.11:8080
...
DocumentRoot "c:/wamp/www/"
...
<VirtualHost *:8080>
ServerName 192.168.20.11
ServerAdmin me#mine.com
DocumentRoot "C:/wamp/www/myapp"
<Directory "C:/wamp/www/mapp">
Order allow,deny
Allow from all
</Directory>
Alias /myapp/static "C:/wamp/www/myapp/static"
WSGIScriptAlias /test "C:/wamp/www/myapp/wsgi.py"
</VirtualHost>
Detail | wsgi.py
Generated by Django's startproject. Located at default /myapp/myapp/wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Problem
Cannot deploy Django app on WAMP given the context described above.
A "Hello World" test wsgi application works fine, so I have confirmed that mod_wsgi is active.
I get the error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, me#mine.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
when pointing to:
http://192.168.20.11:8080/myapp
The static folder works fine, I can access all static files within the directory by pointing to:
http://192.168.20.11:8080/myapp/static
Detail | Server error log
NB: I've removed the timestamps for legibility.
mod_wsgi (pid=5728, process='', application='192.168.20.11:8080|/myapp'): Loading WSGI script 'C:/wamp/www/myapp/myapp/wsgi.py'.
mod_wsgi (pid=5728): Exception occurred processing WSGI script
'C:/wamp/www/myapp/myapp/wsgi.py'.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 236, in call
self.load_middleware()
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "C:\Python27\lib\site-packages\django\conf\_init_.py", line 52, in getattr
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\_init_.py", line 47, in _setup
self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf\_init_.py", line 132, in init
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" %(self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myapp.settings' (Is it on sys.path?): No module named myapp.settings
File does not exist: C:/wamp/www/myapp/favicon.ico
I don't understand the function of middleware in this case and was under the impression it wasn't necessary to use middleware.
Regarding the system path, when I add:
WSGIPythonPath "c:/wamp/www/myapp"
to my VirtualHost in the httpd.conf file, the server refuses to start up.
Objective
To deploy a working Django app (that has been developed and tested locally using the Django development server) to a Microsoft SBS network without using IIS. I tried deploying on IIS and ran into difficulties early on so switched to WAMP.
I'm new to Python and Django and have never deployed a web app on any production server (I am an R programmer that has generally used programming for math and statistics).
Any advice that would help in achieving this objective, regardless of method, will be much appreciated!
This has nothing to do with middleware, that's just where the error was encountered. The error itself is the last line: "Could not import settings 'myapp.settings'". This is almost certainly because you haven't modified the PYTHONPATH - either in your wsgi or your Apache conf - to put "myapp" on the path.
See for instance the example Apache conf in the Django docs - there's a WSGIPythonPath directive there to add the app to the Python path, which you don't have.
Placing a WSGIPythonPath directive in the httpd.conf prevented the server from starting, but the problem was indeed related to the path not being declared.
Adding:
sys.path.append("c:/wamp/www/myapp")
to the wsgi.py file did the trick.
Thanks to Daniel Roseman for putting me on the right track.
Related
Please help me to set my home PC as a server (Windows 10 + Python + Django + Postgre SQL + Anaconda)
There are several instructions on how to set the server on the Internet, and surprisingly, many instructions differ from each other. I have absolutely no experience in setting a server. Probably, I’m making a stupid mistake somewhere that I can’t identify for 3 days. I am lost.
I believe that most useful instructions are these (they are complete and new):
https://www.codementor.io/aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e
https://ostrokach.gitlab.io/post/apache-django-anaconda/
I followed the instructions and successfully downloaded the necessary modules, installed Wamp, made the changes as displayed in the guide and started it. What I see now: the Wamp icon glows green. When I load a localhost, the page loads endlessly but does not load.
Wamp error log shows following: [Fri Oct 11 14:50:33.823752 2019]
[core:notice] [pid 1364:tid 808] AH00094: Command line:
'c:\wamp64\bin\apache\apache2.4.39\bin\httpd.exe -d
C:/wamp64/bin/apache/apache2.4.39' [Fri Oct 11 14:50:33.840681 2019]
[mpm_winnt:notice] [pid 1364:tid 808] AH00418: Parent: Created child
process 15052 [Fri Oct 11 14:50:34.981629 2019] [mpm_winnt:notice]
[pid 15052:tid 800] AH00354: Child: Starting 64 worker threads.
Project name: turiumasina
path D:/Users/PycharmProjects/turiumasina/
path to wsgi:D:/Users/PycharmProjects/turiumasina/turiumasina/wsgi_windows.py (I renamed wsgi to wsgi_windows, settings file is in turiumasina/turiumasina/)
I copied the output generated by the mod_wsgi-express command and pasted it at the end of C:\wamp64\bin\apache\apache\conf\httpd.conf
my httpd-vhosts.conf
ServerName localhost
WSGIPassAuthorization On
ErrorLog "logs/turiumasina.error.log"
CustomLog "logs/turiumasina.access.log" combined
WSGIScriptAlias / "D:/Users/PycharmProjects/turiumasina/turiumasina/wsgi_windows.py"
<Directory "D:/Users/PycharmProjects/turiumasina">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
Alias /static "D:/Users/PycharmProjects/turiumasina/static"
<Directory "D:/Users/PycharmProjects/turiumasina/static">
Require all granted
</Directory>
wsgi_windows.py
import os
import sys
import site
from django.core.wsgi import get_wsgi_application
site.addsitedir("C:/users/.conda/envs/turiumasina/Lib/site-packages")
sys.path.append('D:/Users/PycharmProjects/turiumasina')
sys.path.append('D:/Users/PycharmProjects/turiumasina/turiumasina')
os.environ['DJANGO_SETTINGS_MODULE'] = 'turiumasina.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "turiumasina.settings")
application = get_wsgi_application()
I generated the "static" folder, wrote a line in the settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
If I enter python manage.py runserver via pycharm, I can see my site through a browser (http://127.0.0.1:8000/). the Wamp icon glows green. When I load a localhost, the page loads endlessly but does not load.
I would like my site to be visible not only to me, but also to Internet users by entering my external IP.
If you need, I can send you the whole project and project settings
I would get rid of WAMP altogether, I don't see why you need it if you're not using PHP or MySql.
If you just need to play/debug the Django app over the local network just run ./manage.py runserver 0.0.0.0:8000 (or any other port). Make sure your Windows firewall is allowing incoming connections to the port.
The connect to it you need the computer's actual IP not '0.0.0.0:8000'. Type 'ipconfig' at the command prompt to see what your actual IP is an then connect to Your_actual_IP:8000
If you need something more solid than the build-in Django server (to run in non-debug mode) then you can install Waitress (https://docs.pylonsproject.org/projects/waitress/en/latest/).
Desired Behaviour
I want to serve content created by the file at:
/myapp/my_app.py
when accessing localhost.
Question
I know that if I add the following to test.py and run the file directly, the results will be accessible at localhost:8080:
from bottle import route, run
#route('/')
def hello():
return "Hello World!"
run(host='localhost', port=8080, debug=True)
But how do I trigger this file to run by default when accessing localhost?
Environment
Linux Mint 17
MongoDB
RockMongo (Apache2, PHP, MongoDB Driver)
What I've Tried
I installed mod_wsgi and created /var/www/html/myapp/app.wsgi with this content:
import bottle
import os
os.chdir(os.path.dirname(__file__))
#route('/')
def hello():
return "Hello World!"
application = bottle.default_app()
And then restarted Apache.
But going to localhost just shows a file directory.
I then created /etc/apache2/sites-enabled/mygreatapp.conf with this content:
<VirtualHost *>
ServerName google.com
WSGIScriptAlias / /var/www/html/myapp/app.wsgi
<Directory /var/www/html/myapp>
WSGIProcessGroup yourapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
But I still just see a file directory.
Official Instructions
http://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi
Further Information
Troubleshooting
See if mod_wsgi is loaded
apache2ctl -M
...
wsgi_module (shared)
Definitions
For my own reference, I'm trying to fully comprehend several elements that I think are involved in the process, feel free to correct if my understandings are wrong:
WSGI - A convention for how applications should communicate with servers.
It does not exist anywhere else other than as words in the PEP 3333 - Source
mod_wsgi - An Apache module that allows Python files to communicate with Apache.
Web Framework - See a list of them here: https://wiki.python.org/moin/WebFrameworks
Web Server - http://en.wikipedia.org/wiki/Web_server
Apache Server - http://en.wikipedia.org/wiki/Apache_HTTP_Server
I finally have "Hello World!" displaying in the browser at localhost.
I will paste the various required configs and files below.
I'm a newb so I don't know if this is definitive or best practise.
/var/www/html/myapp/app.wsgi
# view the effect of import method:
http://stackoverflow.com/a/2452639/1063287
import bottle
import os
os.chdir(os.path.dirname(__file__))
# note i am using bottle.route and not just route
#bottle.route('/')
def hello():
return "Hello World!"
application = bottle.default_app()
/etc/apache2/sites-available/000-default.conf (note: this is the default .conf file)
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
WSGIScriptAlias / /var/www/html/myapp/app.wsgi # just added this
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Viewing the Apache error log is what eventually led me to try a few different things:
sudo tail -100 /var/log/apache2/error.log
Where I saw things like:
Exception occurred processing WSGI script '/var/www/html/myapp/app.wsgi'.
Traceback (most recent call last):
File "/var/www/html/myapp/app.wsgi", line 8, in <module>
#route('/')
NameError: name 'route' is not defined
This was only after I had made the step of not using a new .conf file and just modifying the default one.
So for some reason it didn't like any new .conf files.
I have problems getting mod_wsgi to run in daemon mode on my Debian/Apache/Python2.6/Django setup. In my virtual host config file I have
<VirtualHost *:80>
ServerName mysite.com
WSGIDaemonProcess mysite.com processes=2 threads=15
WSGIProcessGroup mysite.com
WSGIScriptAlias / /path/to/mysite/wsgi/django.wsgi
<Directory /path/to/mysite/wsgi/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
set up. To test activation of daemon mode, I use this wsgi script:
import sys
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
def application(environ, start_response):
print >> sys.stderr, 'mod_wsgi.process_group = %s' % repr(environ['mod_wsgi.process_group'])
from django.core.handlers.wsgi import WSGIHandler
_application = WSGIHandler()
return _application(environ, start_response)
and the resulting log file always says:
mod_wsgi.process_group = ''
which, accroding to the documentation, indicates that daemon mode is not being used. I checked my setup multiple times, the versions of Apache, mod_wsgi and Python are matching and my setup is correct according to all the HOWTO's I've read out there. What could I be missing?
Edit: FYI my application is running fine in embedded mode, I just wanted to switch to daemon mode and found out it is not activated using the wsgi script above.
It turned out that a symlink wasn't set correctly so my config changes never loaded in Apache. Sorry for wasting your time, I thought I checked everything thoroughly before posting.
The line mod_wsgi.process_group = '' implies that you are still operating in embedded mode (as you note). mod_wsgi daemon mode will not work on Apache 1.x so I assume that you are running 2.x (as you also note).
You could try setting the apache
LogLevel info
so that there is more helpful information in the log file. I'm also assuming that you've forced apache to restart
# /etc/init.d/apache2 restart
this shouldn't be necessary in daemon mode so much, but if you were in embedded mode previously you will need to reload your config files.
After working with django's dev server for the past two months, the time finally came to move to apache + mod_wsgi.
The problem is when I go to my site (let's call it junux), to the URL mapped to the django app, things do not seem to work. When running the dev server on the server things work properly.
The bottom-line of the error is given to me in the apache error_log:
ImportError: Could not import settings 'junux_site.settings' (Is it on
sys.path?): No module named importlib
I'm aware this is similar to many other questions on the matter (there are so many that I won't even quote them here), but I still haven't found the answer. I have read quite a few guides on moving to production, including django's deployment docs, mod_wsgi's guides, some pycon presentation and have been googling the issue all day...
Lots of fun and exciting details below.
Any help will be appreciated.
Thanks in advance.
The configuration:
Apache 2.2.15 with mod_wsgi on CentOS 6
Python 2.7.3 compiled from source
The site uses a virtualenv
This is the error page apache returns:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (CentOS) Server at junux.net Port 80
The apache error_log reveals the following information:
mod_wsgi (pid=22502): Create interpreter 'junux.net|/dev'.
mod_wsgi (pid=22502): Exception occurred processing WSGI script '/var/www/junux_dev/junux_site/wsgi.py'.
Traceback (most recent call last):
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'junux_site.settings' (Is it on sys.path?): No module named importlib
The relevant wsgi.py:
import os
import sys
import site
# use our virtual environment
SITE_DIR = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(SITE_DIR)
site_packages = os.path.join(PROJECT_ROOT, 'venv/lib/python2.7/site-packages')
site.addsitedir(os.path.abspath(site_packages))
sys.path.insert(0, SITE_DIR)
sys.path.insert(1, PROJECT_ROOT)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "junux_site.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And httpd.conf:
(more stuff here from the default apache configuration file)
<VirtualHost *:80>
ServerName junux.net
ServerAlias junux.net
ServerAdmin admin#junux.net
WSGIScriptAlias /test /var/www/test/hello.py
WSGIScriptAlias /dev /var/www/junux_dev/junux_site/wsgi.py
<Directory /var/www/test >
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/junux_dev >
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
LogLevel info
There's a WSGIScriptAlias to /test to provide a sanity-check that mod_wsgi indeed works. It does. When opening that URL the (very simple) application works (a typical hello world).
I have set permissions to chmod o+r on my wsgi file and chmod o+rx on the entire /var/www/junux_dev dir, as instructed in the pycon-sydney-2010 presentation refered to from here.
What version of Python was mod_wsgi compiled against? Looks like you might have multiple Python installations on the system and your virtual environment is using Python 2.7, but your mod_wsgi is compiled against 2.6.
Am basing this guess on fact that importlib was only added in Python 2.7, so if mod_wsgi was compiled for 2.6 and using that base installation, then would fail to find importlib.
Run checks:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use
I am just starting to fiddle around with django. First I made a small app on my windows machine and verified it worked fine
Then, I zipped the entire project, and opened the zip on a linux machine.
The linux machine was installed with mod_wsgi and django 1.1.1, of course.
I created the following dirs:
/usr/local/bin/ROOT - contains only one file, django.wsgi
/usr/local/bin/ROOT/myapp - root dir of django app
Per the instructions here, I added to httpd.conf:
<VirtualHost *:80>
ServerName server
ServerAlias server
ServerAdmin webmaster#example.com
WSGIScriptAlias /myapp /usr/local/bin/ROOT/django.wsgi
<Directory /usr/local/bin/ROOT/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now per the instructions here I put in /usr/local/bin/ROOT/django.wsgi:
import os
import sys
path = '/usr/local/bin/ROOT'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If it is of any importance: there is nothing set on PYTHOHPATH before the application starts.
After that I did a graceful restart to apache, and went to server/myapp. I got a 500 error.
Lookin in the log I see:
[Sun Dec 05 12:24:17 2010] [error] [client XXXX] ImproperlyConfigured: Error importing middleware django.middleware.csrf: "No module named csrf"
What am I doing wrong? all other threads I found about this always either end up with a conclusion that it's an old version of django (but mine's 1.1.1) or that there are several apps running, but I have only one...
Help?
"django.middleware.csrf" is the package in Django 1.2.x
For Django 1.1.x CSRF settings read the appropriate docs Here
The package in 1.1.x was "django.contrib.csrf.middleware.CsrfMiddleware"
1.1.1 is an old version of Django (the current version is 1.2.3), and that's almost certainly the cause of your problem. There is no django.middleware.csrf in 1.1.1.