How to properly configure a bunch of python 3.4 + django + apache 2.2 + virtualenv ?

Python 2.7 and python 3.2 are installed, python 3.4 is installed in the virtual environment. By 127.0.0.1:8000 site works.

How now to open access from the outside to the site?

Now I'm trying to run a clean django , i.e. test page. I do this: I launch Apache in a virtual environment, it runs without errors, writes only:

Could not reliably determine the server's fully qualified domain name using 127.0.1.1 for ServerName

I run the django runserver (uses python 3.4 ) - the django test page opens, and externally, error 500.

Apache config:

 <VirtualHost *:80> ServerName myproject ServerAdmin mail@list.ru WSGIScriptAlias / /var/www/myproject/src/myproject/myproject/django.wsgi WSGIDaemonProcess myproject processes=1 maximum-requests=1 threads=1 WSGIProcessGroup myproject LogLevel debug ErrorLog /var/www/myproject/logs/myproject.error.log CustomLog /var/www/myproject/logs/myproject.access.log combined ServerSignature On <Directory "/var/www/myproject/src"> Order allow,deny Allow from all </Directory> </VirtualHost> 

Project structure:

 /var/www/myproject ..../static_content ..../src ......../myproject ............manage.py ............/myproject ...............django.wsgi (использую сейчас) ...............settings.py ...............urls.py ...............wsgi.py (был изначально) .../logs .../env ......../bin .............activate .............activate.csh .............activate.fish .............django-admin .............django-admin.py .............easy_install .............easy_install-3.4 .............pip .............pip3 .............pip3.4 .............python -> python3.4 .............python3 -> python3.4 .............python3.4 -> /usr/local/opt/python-3.4.3/bin/python3.4 ......../include ......../lib ......../docs 

django.wsgi:

 # -*- coding: utf-8 -*- import os, sys, site from django.core.wsgi import get_wsgi_application sys.path.insert(0, os.path.dirname(__file__)) site.addsitedir('/var/www/myproject/env/lib/python3.4/sitepackages') os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from django.core.handlers.wsgi import WSGIHandler application = WSGIHandler() 

wsgi.py (currently not used, it was originally, it didn’t work with it either):

 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") application = get_wsgi_application() 

Apache logs write: [notice] Apache/2.2.22 (Debian) PHP/5.4.36-0+deb7u1 mod_wsgi/3.3 Python/3.2.3 configured -- resuming normal operations

In myproject.error.log:

 [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Create interpreter 'myproject|'. [Tue Jul 28 19:04:22 2015] [info] [client ::1] mod_wsgi (pid=3770, process='myproject', application='myproject|'): Loading WSGI script '/var/www/myproject/src/myproject/myproject/django.wsgi'. [Tue Jul 28 19:04:22 2015] [error] [client ::1] mod_wsgi (pid=3770): Target WSGI script '/var/www/myproject/src/myproject/myproject/django.wsgi' cannot be loaded as Python module. [Tue Jul 28 19:04:22 2015] [error] [client ::1] mod_wsgi (pid=3770): Exception occurred processing WSGI script '/var/www/myproject/src/myproject/myproject/django.wsgi'. [Tue Jul 28 19:04:22 2015] [error] [client ::1] Traceback (most recent call last): [Tue Jul 28 19:04:22 2015] [error] [client ::1] File "/var/www/myproject/src/myproject/myproject/django.wsgi", line 7, in <module> [Tue Jul 28 19:04:22 2015] [error] [client ::1] from django.core.wsgi import get_wsgi_application [Tue Jul 28 19:04:22 2015] [error] [client ::1] ImportError: No module named django.core.wsgi [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Maximum requests reached 'myproject'. [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Shutdown requested 'myproject'. [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Stopping process 'myproject'. [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Destroying interpreters. [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Destroy interpreter 'myproject|'. [Tue Jul 28 19:04:22 2015] [debug] mod_deflate.c(700): [client ::1] Zlib: Compressed 614 to 376 : URL / [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Cleanup interpreter ''. [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Terminating Python. [Tue Jul 28 19:04:22 2015] [info] mod_wsgi (pid=3770): Python has shutdown. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Attach interpreter ''. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Create interpreter 'myproject|'. [Tue Jul 28 19:04:23 2015] [info] [client ::1] mod_wsgi (pid=3854, process='myproject', application='myproject|'): Loading WSGI script '/var/www/myproject/src/myproject/myproject/django.wsgi'. [Tue Jul 28 19:04:23 2015] [error] [client ::1] mod_wsgi (pid=3854): Target WSGI script '/var/www/myproject/src/myproject/myproject/django.wsgi' cannot be loaded as Python module. [Tue Jul 28 19:04:23 2015] [error] [client ::1] mod_wsgi (pid=3854): Exception occurred processing WSGI script '/var/www/myproject/src/myproject/myproject/django.wsgi'. [Tue Jul 28 19:04:23 2015] [error] [client ::1] Traceback (most recent call last): [Tue Jul 28 19:04:23 2015] [error] [client ::1] File "/var/www/myproject/src/myproject/myproject/django.wsgi", line 7, in <module> [Tue Jul 28 19:04:23 2015] [error] [client ::1] from django.core.wsgi import get_wsgi_application [Tue Jul 28 19:04:23 2015] [error] [client ::1] ImportError: No module named django.core.wsgi [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Maximum requests reached 'myproject'. [Tue Jul 28 19:04:23 2015] [debug] mod_deflate.c(700): [client ::1] Zlib: Compressed 614 to 376 : URL /favicon.ico [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Shutdown requested 'myproject'. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Stopping process 'myproject'. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Destroying interpreters. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Destroy interpreter 'myproject|'. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Cleanup interpreter ''. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Terminating Python. [Tue Jul 28 19:04:23 2015] [info] mod_wsgi (pid=3854): Python has shutdown. [Tue Jul 28 19:04:24 2015] [info] mod_wsgi (pid=3860): Attach interpreter ''. 
  • You don’t need to launch Apache in virtualenv, normal configuration means launching a separate server (gunicorn, uwsgi, something else) in virtualenv, and Apache / nginx just redirect requests to it - andreymal
  • @andreymal thanks for the comment, now I launch Apache outside virtualenv - Emm

2 answers 2

At a minimum, if you want to do everything on python3, then you are doing the environment wrong. In python 3, the environment is installed through the internal module venv, and not through third-party virtualenv scripts (this is all described in the documentation ). Virtualenv no longer works with it, the python parameter is needed if you have several second pythons.

Additionally, you can read the answer here.

  • Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - Denis
  • @Denis Complemented - FeroxTL

ImportError: No module named django.core.wsgi

It is probably worth adding the following directive to the apache configuration:

 WSGIPythonHome /var/www/myproject/env 

It should be added to the “global” server configuration (outside the virtualhost blocks) - this is the context for its use .

ps based on this answer:.

  • Added directive, gives error: WSGIPythonHome cannot occur within <VirtualHost> section Action 'configtest' failed. - Emm
  • added a reference to the response about the context. - aleksandr barakin
  • Now it gives an error: Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named encodings - Emm
  • there is probably no such module in the environment you created. If you do not know how to deal with this - ask a new question about this. - aleksandr barakin