httpd and mod_wsgi ran with wrong python version - python

I am deploying a Django project with httpd on RHEL7. And changed the system default python version to python 3.4. But after I restarted the httpd, it still used python 2.7.
I searched a lot and it is mostly because mod_wsgi was built under the wrong python version. However, I checked that mod_wsgi was installed under python 3.4.
May I know how to make it run under the correct python version? Not too sure if httpd has a default python version. Thanks very much for your help!
Error Stack trace: No module named embed

I just find that it was due to the fact that I didn't Load 'wsgi_module' to the httpd conf file. I just followed the instruction https://github.com/GrahamDumpleton/mod_wsgi to add LoadModule into httpd.conf file. It solves my problem and now runs in the correct python version!
LoadModule wsgi_module "/usr/lib64/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so"
WSGIPythonHome "/usr"
But I think it is strange that it will show module wsgi_module is already loaded,skipping. If I comment these lines, it will again run in python2.7

Related

Apache2 on Ubuntu gives Py_Initialize ImportError: No module named encodings

I'm working on Ubuntu with Apache2, Python 3.6.1. I followed the directions here to set it up with Django and mod_wsgi: How To Serve Django Applications with Apache and mod_wsgi on Ubuntu 14.04
I've looked at a lot of similar questions and still can't get it to work. The Apache error log spews the "ImportError: No module named 'encodings' almost constantly.
Here are the relevant settings in my Apache2 files:
In 000-default.conf:
WSGIDaemonProcess myproject python-home=/home/user/myproject/myprojectenv/ python-path=/home/user/myproject/myprojectenv/bin/python
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myprojectenv/myproject/myproject/wsgi.py
In apache2.conf:
WSGIPythonPath /home/user/myproject/myprojectenv
Note that, when I check sys.prefix it equals what I have in the WSGIPythonPath above.
Yes, I've activated the virtual environment.
For my environment variables, I have:
PATH = /home/user/myproject/myprojectenv/bin:$PATH
PYTHONPATH = /home/user/myproject/myprojectenv/bin:$PYTHONPATH
PYTHONHOME = not set
I've tried various combinations of the environment variables being set and not being set. None that I've tried have worked.
When I followed the steps from the link above, I'm not sure that a new mod_wsgi was built that is for Python 3.6.1. I had tried (on another copy of my VM) to explicitly build a Python shared library and to build mod_wsgi with that. It failed miserably. I hesitate to go that route again unless I'm sure that's what I need to do.
Any help is greatly appreciated.
I did get passed this error. As #Alasdair pointed out, I did need to build Python with a shared library and then build mod_wsgi with that version of Python. I followed the directions here: Compiling mod_wsgi for a custom Python Build
The only difference is that I needed to add 'sudo' before 'make install' so the permissions were good for putting the module in the /usr/bin/apache2/modules directory.
I also needed to be sure LD_LIBRARY_PATH recognized the python library (I received a "error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory" error). That error was cleaned up by using 'ldconfig'.
I'm still having an Apache issue, which I will write up separately, but this got me passed the 'encodings' problem.

python, trac, apache, mod_wsgi on windows

I'm using trac 1.2 on windows. Their documentation says it requires python2, so I pip install'd it with python2 x64, and spent a lot of time configuring some trac environments. Now I want to run trac through apache httpd so I can set it up with microsoft active directory, so I figured, okay, let's try mod_wsgi. I read this mod_wsgi documentation which mentions that python2 is built with VC9, and that apache, mod_wsgi, python binaries need to be consistent architecture throughout. So that means, for me, VC9, x64 for all. I installed the VC9 runtime from here. For mod_wsgi, I pip installd it using the mod_wsgi‑4.6.4+ap24vc9‑cp27‑cp27m‑win_amd64.whl file from here to avoid having to install VS2008's entire toolchain. Now I guess I need apache x64 VC9. The apache binary closest to VC9 I could find was VC10 here which says it loads VC9 modules, so I went ahead and tried it. I ran mod_wsgi-express module-config which gave me
LoadModule wsgi_module "c:/python27/lib/site-packages/mod_wsgi/server/mod_wsgiNone"
WSGIPythonHome "c:/python27"
The first line wasn't right, since httpd complained it couldn't load the module, so I changed it to
LoadModule wsgi_module "c:/python27/lib/site-packages/mod_wsgi/server/mod_wsgi.pyd"
I verified that that module exists at the specified path, and ran httpd which still error'd out with
httpd: Syntax error on line 179 of C:/Apache24/conf/httpd.conf: Cannot load c:/python27/lib/site-packages/mod_wsgi/server/mod_wsgi.pyd into server: The specified module could not be found.
So I guess the VC10 x64 apache binary I downloaded isn't backwards compatible with VC9 x64 mod_wsgi.
Does this mean I now need to build my own VC9 x64 apache binary? There must be a simpler way to achieve running trac 1.2 within an apache web server, right? Unfortunately, it's not as obvious to me. Any help is appreciated. Also, let me know if I need to clarify any other detail.

BitNami DjangoStack not 'self contained', cannot run application

I installed the Windows (32bit) BitNami DjangoStack 1.4.1.0 and elected not to install the python that came with it as I already have Python 2.7 installed.
I got the error below and also an internal server error when I clicked on the button in the introduction web page (so Appache is running ok)
enter code here
[error] mod_wsgi (pid=3990): Target WSGI script '/opt/bitnami/apps/django/scripts/django.wsgi' cannot be loaded as Python module
I found a similar question but that related to installing on Amazon. Also following through the answers it appeared that the problem is that the wsgi had been compiled against the wrong python version.
I then uninstalled BitNami and re-installed using the python version that was provided - still the same message.
I don't see how the wrong python version can be the problem if the BitNami package is self contained, i.e. comes with all the bits needed to run.
In case my previously installed version was causing the problem I remove it from the PATH environment variable.
Also the documentation for Windows seems wrong. django.conf and django.wsgi are not where the docs say they are (\Users\user\BitNami SDjangoStack projects\apps). In fact the only thing in that directory is the new project I selected on installation - no apps directory. I found the files in the BitNami directories.
Also when I typed python in the BitNami console I got python 2.7.3, not 2.7.2 which is listed in the installation guide.
I thought this package would be wonderful to help me get a production Django site running. It is now ready in the development phase to go live for testing.
What do you exactly mean with not installing Python? BitNami DjangoStack is self contained in the sense that it includes its own Python and its dependecies. Also you mentioned that you are running it on Windows but the path /opt/bitnami is the default path when installing as root in Linux. What exact options did you select during the installation?

Ubuntu: Python version, virtualenvwrapper and django

i have a problem with virtualenvs and django. Apparently i just dont get how they are supposed to work.
I installed python 2.7.3 ucs2 in /opt/bin/python and it works just fine
I installed virtualenv and virtualenvwrapper and created virtual environment named py273ucs2 so when i type workon py273ucs2 i get correct virtual environment.
when i create python script with
#!/home/alan/python/virtualenvs/py273ucs2/bin/python
import sys
print sys.maxunicode
i get 65535 which is correct for ucs2 python.
at same time i have no idea how to use correct virtualenvironment for my django app so i could use ucs2 python not ucs4 one. I tried creating one test project with virtualenvwrapper.django thinking that its wsgi file or manage.py would give me hints about how to start wsgi script with correct environment - no such luck.
I found this site:
http://www.saltycrane.com/blog/2009/05/notes-using-pip-and-virtualenv-django/
which basically (in addition to other things that i already did) tells me to add this into my wsgi file :
site.addsitedir('/srv/python-environments/saltycrane/lib/python2.5/site-packages')
which in my case is:
site.addsitedir('/home/alan/python/virtualenvs/py273ucs2/lib/python2.7/site-packages')
Tried - no luck.
Then there is such page:
http://www.gyford.com/phil/writing/2011/06/27/virtualenv-webfaction.php
In addition to last page it suggests i add:
activate_this = os.path.expanduser("~/.virtualenvs/my_env_name/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
which in my case is:
activate_this = os.path.expanduser("/home/alan/python/virtualenvs/py273ucs2/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
again no luck.. view
def base(request):
return HttpResponse(sys.version+' maxunicode ' +str(sys.maxunicode))
prints out python version 2.7.2 and maxunicode 1114111 not 65535.
Then there is this message here at stackoverflow:
How does django work with virtualenv?
which suggests i add params in httd.conf - also tried and also not working.
So honestly - im not getting this. Perhaps the version is supposed to remain 2.7.2 not become 2.7.3... then whats the point of virtualenvs... Or im doing something wrong and not getting something from those examples. I also checked wsgi wiki about virtualevns:
http://code.google.com/p/modwsgi/wiki/VirtualEnvironments
and modified my httpd.conf to:
WSGIPythonHome /home/alan/python/virtualenvs/py273ucs2
WSGIPythonPath /home/alan/python/virtualenvs/py273ucs2/lib/python2.7/site-packages
WSGIDaemonProcess printer user=alan group=alan processes=5 threads=5 python-path=/home/alan/python/virtualenvs/py273ucs2/lib/python2.7/site-packages
WSGIProcessGroup printer
WSGIScriptAlias / /home/alan/projects/printer/printer.wsgi
Nothing. Still see python 2.7.2 in view...
could someone help me out please :)
Alan
Edit1: answer to comments:
1)
I have to install/compile new mod_wsgi if i want to use virtualenvironment? why?
2)
if i do:
alan#alan:~$ workon py273ucs2
(py273ucs2)alan#alan:~$ cd projects/printer/
(py273ucs2)alan#alan:~/projects/printer$ python manage.py runserver
Validating models...
0 errors found
Django version 1.3, using settings 'printer.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[08/Jul/2012 05:01:00] "GET / HTTP/1.1" 200 68
then my view shows me:
2.7.3 (default, May 16 2012, 10:58:59) [GCC 4.6.1] maxunicode 65535
Edit2: Thanks to the help of Graham Dumpleton at modwsgi google group i managed to install mod_wsgi for ucs2 python (https://groups.google.com/forum/?fromgroups#!topic/modwsgi/F5Wn4uWrQAg).
But when i start this site i still get
2.7.2+ (default, Oct 4 2011, 20:41:12) [GCC 4.6.1] maxunicode 1114111
not
2.7.3 (default, Jul 9 2012, 13:32:29) [GCC 4.6.1] maxunicode 65535
Btw apache error log prints out this:
[Mon Jul 09 14:13:19 2012] [warn] mod_wsgi: Compiled for Python/2.7.3.
[Mon Jul 09 14:13:19 2012] [warn] mod_wsgi: Runtime using Python/2.7.2+.
Final edit:
There were several problems with how the mod_wsgi was supposed to be installed. You can find out all necessary info when you follow this link : https://groups.google.com/forum/?fromgroups#!topic/modwsgi/F5Wn4uWrQAg.
But after those problems the point was that the way Ubuntu python installation is made it conflicts with other pythons (not the best explanation probably but you will find very good explanation by Graham Dumpleton if you follow the link above). Virtual python and mod_wsgi started all working when i removed libapache2-mod-python package. It caused other problems, but those problems are caused by the way ubuntu and its python package are set up.
So if you are ubuntu user and happen to find same problem, then this page and Graham Dupmpletons Huge efforts will probably help you too.
Duplicate of:
https://groups.google.com/forum/?fromgroups#!topic/modwsgi/F5Wn4uWrQAg
Issue documented at:
http://code.google.com/p/modwsgi/wiki/InstallationIssues#Mixing_32_Bit_And_64_Bit_Packages
Use '--enable-shared' when configuring/installing Python.
You can't install a virtualenv of a python version you don't have already installed.
The point of virtualenv is to isolate your packages. If you create a virtualenv with --no-site-packages. It won't use the packages already installed on the system. In other words, it means you can install multiple version of each package for one pythone binary.
Having virtualenv also make it easier for development since you don't require root access to install packages using python setup.py or pip install.
It's not really clear from what you're posting, why it isn't working.
to activate a virtualenv. You have to do
source path/to/env/bin/activate
When running scripts from config, pointing out where python is located should be enough. If you do add_site_package, it is probably only adding to the python by default and not the one you wanted.
If nothing works, you might be able to do that. It should solve your problem if nothing requires the other python executable.
using which python, you should be able to know where is located the current python executable.
You could replace the symlink for the default python to your python2.7.3ucs. But I can't promise it's not going to break something somewhere. Since some programs may depend on that particular python2.7.2
edit
Apparently, your problem is definitely not with virtualenv. But with mod_wsgi. Here is a site that describe how to compile mod_wsgi for your desired python version.
http://technomilk.wordpress.com/2011/08/10/running-our-django-site-with-mod_wsgi-and-virtualenv-part-2/
this line:
$ ./configure --with-python=/home/isigma/.pythonbrew/pythons/Python-2.7.2/bin/python
This is where you put the path to your python version and then follow the steps. And hopefully it will work like butter.
edit 2
If nothing else work, it might mean you have to check for alternatives to mod_wsgi.
Here is a good one.
http://projects.unbit.it/uwsgi/wiki/Example
edit 3
Create a new virtualenv like that
virtualenv --no-site-packages --python=/opt/bin/python

mod_wsgi on archlinux with python 3.2

I actually had mod_wsgi working with python3.1, but after updating some software... it no longer works.
I followed these instructions for python3.1 modified slightly for 3.2:
https://wiki.archlinux.org/index.php/Mod_wsgi
and while it seems to build/install ok, trying to start apache give me the error :
Cannot load /etc/httpd/modules/mod_wsgi.so into server: /etc/httpd/modules/mod_wsgi.so: undefined symbol: PyCObject_FromVoidPtr
just as this guy got:
Python 3.1.1 with --enable-shared : will not build any extensions
Any help would be appreciated.
I am a linux noob.
The released versions of mod_wsgi cannot be used with Python 3.2. You must currently use source code directly from subversion repository for mod_wsgi. See:
http://code.google.com/p/modwsgi/source/checkout

Categories

Resources