new relic integration with mod_wsgi

984 views
Skip to first unread message

virgil.balibanu

unread,
Jul 29, 2013, 4:30:32 AM7/29/13
to mod...@googlegroups.com
Hi,

I'm trying to install new relic on my demo server. It runs python+django on mod_wsgi on centos. In my djnago.wsgi file I try to do this:

import newrelic.agent
newrelic.agent.initialize('newrelic.ini')

import os
import sys

sys.path.append('/home/projects')
sys.path.append('/home/projects/sbo')

os.environ['DJANGO_SETTINGS_MODULE'] = 'sbo.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

But it keep failing. Internal server error. It fails even if a leave only the import. But newrelic is installed and in python2.7 which I use I can use the import.

Any ideea why it might fail?

Thanks,
Virgil Balibanu

Graham Dumpleton

unread,
Jul 29, 2013, 4:36:54 AM7/29/13
to mod...@googlegroups.com
What is the error in the Apache error logs?

At a guess it is because you aren't supplying an absolute path to the location of the newrelic.ini file.

The working directory of Apache isn't going to be the directory where your code is.

So use something like:

import os
import newrelic.agent
newrelic.agent.initialize(os.path.dirname(__file__), 'newrelic.ini'))

Presuming newrelic.ini is in the same directory as the wsgi.py file.

Graham

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To post to this group, send email to mod...@googlegroups.com.
Visit this group at http://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

virgil.balibanu

unread,
Jul 29, 2013, 4:59:45 AM7/29/13
to mod...@googlegroups.com
The problem seems to be the import:
ImportError: No module named newrelic.agent but if I try the import from python2.7 or I run python2.7 django.wsgi it works ok.

Graham Dumpleton

unread,
Jul 29, 2013, 8:33:11 AM7/29/13
to mod...@googlegroups.com
Is the newrelic module installed into a Python virtual environment or into a real Python installation?

That it is not found suggests a few possibilities.

1. The mod_wsgi is compiled against a different Python version.
2. You have a distinct Python 2.7 installation (system wide one) which is different to the one you are expecting it to use, and mod_wsgi is compiled against or using the other installation.
3. You are using a virtual environment but haven't told mod_wsgi how to use it.

I would suggest going through the following checklist.

1. Work out what version of mod_wsgi is compiled against. First up by looking at what libpythonX.Y.so it is using.


2. Work out what Python installation, version and location, mod_wsgi is using at run time.


3. Also validate whether mod_wsgi is running your application in embedded or daemon mode and whether that is what you expect.


Supply the results of that as well as what you have for the WSGI directives in your Apache configuration.

Graham

virgil.balibanu

unread,
Jul 29, 2013, 9:28:14 AM7/29/13
to mod...@googlegroups.com
There is something verry strange here. I print te python version in the django.wsgi file and it;s always python2.7 and I import newrelic and sometimes it works and sometimes it fails:
[Mon Jul 29 16:24:13 2013] [error] (2, 7, 2, 'final', 0)
[Mon Jul 29 16:24:13 2013] [error] [client 83.103.170.100] mod_wsgi (pid=24337): Target WSGI script '/home/projects/sbo/django.wsgi' cannot be$
[Mon Jul 29 16:24:13 2013] [error] [client 83.103.170.100] mod_wsgi (pid=24337): Exception occurred processing WSGI script '/home/projects/sbo$
[Mon Jul 29 16:24:13 2013] [error] [client 83.103.170.100] Traceback (most recent call last):
[Mon Jul 29 16:24:13 2013] [error] [client 83.103.170.100]   File "/home/projects/sbo/django.wsgi", line 6, in <module>
[Mon Jul 29 16:24:13 2013] [error] [client 83.103.170.100]     import newrelic
[Mon Jul 29 16:24:13 2013] [error] [client 83.103.170.100] ImportError: No module named newrelic

If I add the next line after the import newrelic it always fails saying it can not import newrelic.

The version of python mod_wsgi call is good and new relic is installed using pip-2.7 so it is installed for python2.7

Thanks,
Virgil


On Monday, July 29, 2013 11:30:32 AM UTC+3, virgil.balibanu wrote:

Graham Dumpleton

unread,
Jul 29, 2013, 4:48:38 PM7/29/13
to mod...@googlegroups.com
Can you please provide the outputs of the tests I asked you to do so I can check for myself and look for anything strange.

Thus the output from ldd (ensuring LD_LIBRARY_PATH is not set), sys.prefix and sys.version etc.

Also provide the WSGI directives from Apache configuration file as asked.

Provide the actual detail and you may get a quick answer. If you can't then it may take a long time as going on holiday in less than a day from now and replies will be very much delayed.

Graham


virgil.balibanu

unread,
Jul 30, 2013, 3:24:22 AM7/30/13
to mod...@googlegroups.com
1.
[root@vps701 sbo]# locate mod_wsgi.so
/usr/lib/httpd/modules/mod_wsgi.so
[root@vps701 sbo]# ldd /usr/lib/httpd/modules/mod_wsgi.so
    libpthread.so.0 => /lib/libpthread.so.0 (0xb7d30000)
    libdl.so.2 => /lib/libdl.so.2 (0xb7d2b000)
    libutil.so.1 => /lib/libutil.so.1 (0xb7d27000)
    libm.so.6 => /lib/libm.so.6 (0xb7cfe000)
    libc.so.6 => /lib/libc.so.6 (0xb7ba5000)
    /lib/ld-linux.so.2 (0xb7f29000)

It does not specify which python it is compiled for, but the site runs django and django is installed for python2.7 not 2.4(default) so it is ok

2
sys.version = '2.7.2 (default, Oct 20 2011, 14:51:08) \n[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)]'
sys.prefix = '/usr/local'

3.
mod_wsgi.process_group = ''
mod_wsgi.application_group = 'vps701.openvps.eu|'

In the apache configuration for mod_wsgi I have just this:
WSGIPassAuthorization On
WSGIScriptAlias / /home/projects/sbo/django.wsgi

Thanks,
Virgil

Graham Dumpleton

unread,
Jul 30, 2013, 4:04:59 AM7/30/13
to mod...@googlegroups.com
Please ensure you reply to the same message properly. You keep creating new messages which destroys the discussion thread.

On 30/07/2013, at 5:24 PM, virgil.balibanu <virgil....@gmail.com> wrote:

1.
[root@vps701 sbo]# locate mod_wsgi.so
/usr/lib/httpd/modules/mod_wsgi.so
[root@vps701 sbo]# ldd /usr/lib/httpd/modules/mod_wsgi.so
    libpthread.so.0 => /lib/libpthread.so.0 (0xb7d30000)
    libdl.so.2 => /lib/libdl.so.2 (0xb7d2b000)
    libutil.so.1 => /lib/libutil.so.1 (0xb7d27000)
    libm.so.6 => /lib/libm.so.6 (0xb7cfe000)
    libc.so.6 => /lib/libc.so.6 (0xb7ba5000)
    /lib/ld-linux.so.2 (0xb7f29000)

It does not specify which python it is compiled for, but the site runs django and django is installed for python2.7 not 2.4(default) so it is ok

And that is possibly part of your problem. It is a bad idea to be linking with a static Python library as it can cause various issue.

You should reinstall your Python version and this time use the --enable-shared option to the 'configure' script when building Python from source code. Make sure you start from fresh source directory or do a 'make distclean' before running configure.

See:


for details of this problem.

Now. The next problem likely is that you also have a Python 2.7 installed globally on your system under /usr. At run time, it is likely finding that Python installation and not using the one under /usr/local. To get around that you need to tell mod_wsgi where the Python installation is.

See:


In summary.

1. Reinstall your Python from fresh source code under /usr/local with --enable-shared used to configure script of Python configure script when installing it. So at least:

./configure --prefix=/usr/local --enable-shared
make
sudo make install

2. Rebuild mod_wsgi from fresh source code where --with-python gives the Python under /usr/local. When doing this, ensure that setting LD_RUN_PATH so knows to get shared library from /usr/local/lib.

./configure --with-python=/usr/local/bin/python2.7
LD_RUN_PATH=/usr/local/lib make 
sudo make install

3. Tell mod_wsgi at run time to use the Python under /usr/local.

WSGIPythonHome /usr/local

Graham

2
sys.version = '2.7.2 (default, Oct 20 2011, 14:51:08) \n[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)]'
sys.prefix = '/usr/local'

3.
mod_wsgi.process_group = ''
mod_wsgi.application_group = 'vps701.openvps.eu|'

In the apache configuration for mod_wsgi I have just this:
WSGIPassAuthorization On
WSGIScriptAlias / /home/projects/sbo/django.wsgi

Thanks,
Virgil

virgil.balibanu

unread,
Jul 30, 2013, 10:17:30 AM7/30/13
to mod...@googlegroups.com
Thanks, I tried to install on the live server instead of the demo, as i verified the ldd result on the live server has libpython2.7 and it worked.

Thanks for all the help,

Virgil

On Monday, July 29, 2013 11:30:32 AM UTC+3, virgil.balibanu wrote:

Aqeel Nazeer

unread,
Dec 17, 2013, 2:55:22 AM12/17/13
to mod...@googlegroups.com
This worked for me except the arguments passed has to be changed to this: 

newrelic.agent.initialize(os.path.join(os.path.dirname(__file__), "newrelic.ini"))

Graham Dumpleton

unread,
Dec 17, 2013, 3:02:04 AM12/17/13
to mod...@googlegroups.com
Yeah, the os.path.join() was missing. Whoops.
Reply all
Reply to author
Forward
0 new messages