mail_admins causing connection refused for Django/Apache2?

时间:2016-07-11 19:07:02

标签: python django apache apache2

I'm running Django with an Apache2 server on an Ubuntu backend. On load of my site, I get a 500 internal server error. The Apache log looks like:

AH00094: Command line: '/usr/sbin/apache2'    
mod_wsgi (pid=9568): Exception occurred processing WSGI script '/home/ubuntu/project/project/wsgi.py'.
Traceback (most recent call last):
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site- packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 216, in get_response
    'request': request
File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
    self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
    self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
    self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1321, in       callHandlers
    hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
    self.emit(record)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/utils/log.py", line 117, in emit
    self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/utils/log.py", line 120, in send_mail
     mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 97, in mail_admins
    mail.send(fail_silently=fail_silently)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send
    return self.get_connection(fail_silently).send_messages([self])
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
    new_conn_created = self.open()
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 58, in open
    self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 111] Connection refused

This looks to me to be an error when the mail_admins method is called. I can post the contents of wsgi.py or other files if that is needed. My application involves no explicit email usage, so I'm not sure how this is happening. Any ideas? Thanks.

1 个答案:

答案 0 :(得分:1)

Django has a special logger for sending an email to admins when a 500-error occurs. Check out https://docs.djangoproject.com/en/1.9/topics/logging/#django.utils.log.AdminEmailHandler for more info.

It seems that an exception is thrown and django tries to propagate it by sending you an email, but the email cannot be send so you get another exception! Probably the cause of the real exception is before the exception that is caused by unable to send the email. However, I propose to fix your email configuration so that django will be able to send you these emails when a 500-error occurs, it is a nice feature (and a first step to a full exception tracker like Sentry). If, for some reason you are unable to send emails from that server then I recommend at least to configure your email backend (https://docs.djangoproject.com/en/1.9/topics/email/#email-backends) to either console (EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend') or file

EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location

so that the mail backend will work.