apache2 with django - can't write to log file
NickName:Ofek Agmon Ask DateTime:2019-01-09T20:27:38

apache2 with django - can't write to log file

I have django running on apache2 server.

my settings.py looks like this:

WSGI_APPLICATION = 'my_app.wsgi.application'
...

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'verbose': {
        'format': '%(levelname)s %(asctime)s %(module)s: %(message)s'
    }
},
'handlers': {
    'file': {
        'level': 'INFO',
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': '../django.log',
        'formatter': 'verbose',
        'maxBytes': 1024 * 1024 * 5,  # 5 MB
        'backupCount': 5,
    },
},
'loggers': {
    'my_app': {
        'handlers': ['file'],
        'level': 'INFO',
        'propagate': True,
    },
},

}

My idea was basically to create a log that all the components of my django app will write to, with level INFO.

The issue I am facing is, when running the server, the log is created with root permissions:

 ll ../django.log 
 -rw-r--r-- 1 root root 0 Jan  9 10:17 ../django.log

And so, what happens when I try to log to it is:

/var/log/apache2/error.log

[Wed Jan 09 11:37:43.677755 2019] [:error] [pid 1457:tid 140554321598208] [remote 192.168.254.52:60257] ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '../django.log'

I did find those issues: Permission Denied when writing log file and Django: Setup an efficient logging system for a website in production.

and if I change the permissions of the file to be owned by www-data and not root, it works.

My question is - where is the right place to set this in production? should it be changing it manually? maybe somewhere in the settings.py or the apache2-config?

I am looking for the best practice of django logging.

EDIT: ps aux | grep apache2 shows:

root      1444  0.0  0.0  97916  7452 ?        Ss   13:22   0:00 /usr/sbin/apache2 -k start

ps aux | grep wsgi shows:

www-data  1447  0.0  0.2 510528 23692 ?        Sl   13:22   0:00 (wsgi:name -k start

Thanks!

Copyright Notice:Content Author:「Ofek Agmon」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54110184/apache2-with-django-cant-write-to-log-file

More about “apache2 with django - can't write to log file” related questions

Host a Django project on Apache2 locally

I have finished a Django project and want to deploy it over Apache2 locally over LAN. I have figured out how to connect Django with Apache2 using mod_wsgi and it worked great because I was able to ...

Show Detail

how to run Django application in apache2

Hi All i am not able to run a django application in apache2 webserver. I have went through all the document but it still did not work for me.This is my Apache2's httpd.conf file <Location "/mys...

Show Detail

Django and apache2 restart

I have the following issue as I'm trying to setup Django with apache: $ sudo /etc/init.d/apache2 restart * Restarting web server apache2 [Wed Mar 07 03:21:17 2012] [warn] NameVirtualHost *:80 has...

Show Detail

django directory apache2 permissions

Hey I just got apache2 working with mod_wsgi on my django_project directory, which is pretty kool. However I can only make it work if I set the permissions on my django_project to chmod -R 777

Show Detail

Django on Apache2, django-wkhtmltopdf without celery run shell subprocess

In the past in order to make any os binary calls from Django over WSGI on Apache2 with a Debian server, I've had to use celeryd to queue the message, open a subprocess (Apache2 would not create a new

Show Detail

404 error when deploying django project with apache2

I tried to create a new django project on apache2, but I got 404 error. Here are what I did. -In /home/ubuntu/django directory, type django-admin startproject proj -Edit proj/proj/wsgi.py as fol...

Show Detail

django mod wsgi apache2 configuration

I'm using apache2.4 on ubuntu 14.04, I started new project from django documentation, then I checked that app runs using python runserver, it works great, but in my server apache2 i see 500 error. My

Show Detail

Apache2 not loading Django settings.py file properly

I am currently facing an issue where making any changes in settings.py file is not reflecting properly in my project. I have tried deleting the .conf file, adding it again, enabling it and reloadin...

Show Detail

apache2 with django - can't write to log file

I have django running on apache2 server. my settings.py looks like this: WSGI_APPLICATION = 'my_app.wsgi.application' ... LOGGING = { 'version': 1, 'disable_existing_loggers': False,

Show Detail

Deploy django app on ubuntu apache2

I want to run my Django app created in virtualenv on ubuntu with python3. Folder structure in virtualenv folder: -bin -include -lib -myapp -share pip-selfcheck.json The myapp folder contains..

Show Detail