Setup logging of Celery in Django correctly - how? - django

Setting up celery on our production server seems to be more complicated than it should be - and the problem is logging.
Our setup is like this:
Server is Ubuntu 20.04.5 LTS
We run Django 3.2.8 in it's own user and group named "django"
The Celery process also has it's own user and group named "celery".
Celery is set up as a service and logs to /var/log/celery
In celery.py we have set djangoConf pointing to the main settings.py
On startup Celery tries to touch a django-logfile and - missing the permissions to do so - returns an error:
Dec 16 10:57:27 h2989788 celery[128418]: Usage: celery [OPTIONS] COMMAND [ARGS]...
Dec 16 10:57:27 h2989788 celery[128418]: Try 'celery --help' for help.
Dec 16 10:57:27 h2989788 celery[128418]: Error: Invalid value for '-A' / '--app':
Dec 16 10:57:27 h2989788 celery[128418]: Unable to load celery application.
Dec 16 10:57:27 h2989788 celery[128418]: While trying to load the module theApp the following error occurred:
Dec 16 10:57:27 h2989788 celery[128418]: Traceback (most recent call last):
Dec 16 10:57:27 h2989788 celery[128418]: File "/usr/lib/python3.8/logging/config.py", line 563, in configure
Dec 16 10:57:27 h2989788 celery[128418]: handler = self.configure_handler(handlers[name])
Dec 16 10:57:27 h2989788 celery[128418]: File "/usr/lib/python3.8/logging/config.py", line 744, in configure_handler
Dec 16 10:57:27 h2989788 celery[128418]: result = factory(**kwargs)
Dec 16 10:57:27 h2989788 celery[128418]: File "/usr/lib/python3.8/logging/handlers.py", line 148, in __init__
Dec 16 10:57:27 h2989788 celery[128418]: BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
Dec 16 10:57:27 h2989788 celery[128418]: File "/usr/lib/python3.8/logging/handlers.py", line 55, in __init__
Dec 16 10:57:27 h2989788 celery[128418]: logging.FileHandler.__init__(self, filename, mode, encoding, delay)
Dec 16 10:57:27 h2989788 celery[128418]: File "/usr/lib/python3.8/logging/__init__.py", line 1147, in __init__
Dec 16 10:57:27 h2989788 celery[128418]: StreamHandler.__init__(self, self._open())
Dec 16 10:57:27 h2989788 celery[128418]: File "/usr/lib/python3.8/logging/__init__.py", line 1176, in _open
Dec 16 10:57:27 h2989788 celery[128418]: return open(self.baseFilename, self.mode, encoding=self.encoding)
Dec 16 10:57:27 h2989788 celery[128418]: PermissionError: [Errno 13] Permission denied: '/home/django/FLY/logs/myrate.log'
Dec 16 10:57:27 h2989788 celery[128418]: The above exception was the direct cause of the following exception:
Dec 16 10:57:27 h2989788 celery[128418]: Traceback (most recent call last):
Dec 16 10:57:27 h2989788 celery[128418]: File "/home/django/env/lib/python3.8/site-packages/kombu/utils/imports.py", line 56, in symbol_by_name
Dec 16 10:57:27 h2989788 celery[128418]: module = imp(module_name, package=package, **kwargs)
Dec 16 10:57:27 h2989788 celery[128418]: File "/home/django/env/lib/python3.8/site-packages/celery/utils/imports.py", line 105, in import_from_cwd
Dec 16 10:57:27 h2989788 celery[128418]: return imp(module, package=package)
Dec 16 10:57:27 h2989788 celery[128418]: File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
Dec 16 10:57:27 h2989788 celery[128418]: return _bootstrap._gcd_import(name[level:], package, level)
Dec 16 10:57:27 h2989788 celery[128418]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
Dec 16 10:57:27 h2989788 celery[128418]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
Dec 16 10:57:27 h2989788 celery[128418]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
Dec 16 10:57:27 h2989788 celery[128418]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
Dec 16 10:57:27 h2989788 celery[128418]: File "<frozen importlib._bootstrap_external>", line 848, in exec_module
Dec 16 10:57:27 h2989788 celery[128418]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
Dec 16 10:57:27 h2989788 systemd[1]: celery.service: Control process exited, code=exited, status=2/INVALIDARGUMENT
Dec 16 10:57:27 h2989788 celery[128418]: File "/home/django/FLY/theApp/__init__.py", line 1, in <module>
Dec 16 10:57:27 h2989788 celery[128418]: from .celery import app as celery_app
Dec 16 10:57:27 h2989788 celery[128418]: File "/home/django/FLY/theApp/celery.py", line 18, in <module>
Dec 16 10:57:27 h2989788 celery[128418]: configurations.setup()
Dec 16 10:57:27 h2989788 celery[128418]: File "/home/django/env/lib/python3.8/site-packages/configurations/__init__.py", line 31, in setup
Workaround: change the permissions of the log file, which belongs to user django, so that user celery can write to it. But as we're using a rotating logger, this only helps until the next log-file is created.
Solutions we thought of:
Changing the access-rights of the file every time a new log-file is created: probably the worst way of handling this
Using the same user for the django- and celery-processes: smells like the last resort
Creating a specific run-configuration in settings.py overwriting the logging handlers like so:
class CeleryProd(Prod):
def __init__(self) -> None:
super().__init__()
self.LOGGING['handlers']={}
But somehow every of these solutions seems like a workaround to me - even the last one seems a bit clumsy. Mustn't there be a way of doing it right?
How have you done it?

Related

Django Google App Engine: 502 Bad Gateway, already installed package not recognized

I'm deploying Django in Google App Engine.
I get 502 Bad Gateway and in the log I get the following error:
2021-03-08 12:08:18 default[20210308t130512] Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker worker.init_process() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/gthread.py", line 92, in init_process super().init_process() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/base.py", line 119, in init_process self.load_wsgi() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi self.wsgi = self.app.wsgi() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 49, in load return self.load_wsgiapp() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp return util.import_app(self.app_uri) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/util.py", line 358, in import_app mod = importlib.import_module(module) File "/opt/python3.9/lib/python3.9/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File "", line 1007, in _find_and_load File "", line 986, in _find_and_load_unlocked File "", line 680, in _load_unlocked File "", line 790, in exec_module File "", line 228, in _call_with_frames_removed File "/srv/main.py", line 1, in from django_project.wsgi import application File "/srv/django_project/wsgi.py", line 16, in application = get_wsgi_application() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application django.setup(set_prefix=False) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/init.py", line 19, in setup configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/conf/init.py", line 82, in getattr self._setup(name) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/conf/init.py", line 69, in _setup self._wrapped = Settings(settings_module) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/conf/init.py", line 170, in init mod = importlib.import_module(self.SETTINGS_MODULE) File "/opt/python3.9/lib/python3.9/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/srv/django_project/settings.py", line 84, in import pymysql # noqa: 402 ModuleNotFoundError: No module named 'pymysql'
The problem is that I already installed pymysql, in fact if I run pip3 install pymysql, I get Requirement already satisfied: ...
Why is that?
Thanks in advance!
Edit:
Here's requirements.txt:
asgiref==3.3.1
attrs==20.3.0
Automat==20.2.0
certifi==2020.12.5
cffi==1.14.4
chardet==4.0.0
constantly==15.1.0
cryptography==3.4.1
cssselect==1.1.0
Django==3.1.6
django-phonenumber-field==5.0.0
django-widget-tweaks==1.4.8
hyperlink==21.0.0
idna==2.10
incremental==17.5.0
instaloader==4.6.1
itemadapter==0.2.0
itemloaders==1.0.4
jmespath==0.10.0
jsonfield==3.1.0
lxml==4.6.2
parsel==1.6.0
phonenumberslite==8.12.18
progress==1.5
Protego==0.1.16
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
PyDispatcher==2.0.5
PyHamcrest==2.0.2
pyOpenSSL==20.0.1
pytz==2021.1
queuelib==1.5.0
requests==2.25.1
schedule==0.6.0
Scrapy==2.4.1
semantic-version==2.8.5
service-identity==18.1.0
setuptools-rust==0.11.6
six==1.15.0
sqlparse==0.4.1
toml==0.10.2
urllib3==1.26.3
w3lib==1.22.0
zope.interface==5.2.0
If you run pip3 install pymysql in your local computer, this does not mean that when you deploy the app this module is packaged. In fact GAE attempts to install everything at build time using your requirements.txt file so it doesn't matter if you installed everything in your PC since GAE will not use what you have in local (talking about packages installed with pip).
Checking your requirements.txt file I do not see that the package PyMySQL is added. You should add it to that file and attempt to deploy again.
"pymysql" package is missing (not installed) according to the message below from your error:
ModuleNotFoundError: No module named 'pymysql'
So, you need to run this command below to install "pymysql":
pip install pymysql

Django & uWSGI ImproperlyConfigured Problem(SQLite 3.8.3 or later is required)

I have already read some articles about "SQLite 3.8.3 or later is required" problem, commonly about old sqlite3 version and might be solved by installing a latest version with LD_LIBRARY_PATH=new/installed/sqlite3.
Well, it did solve my problem partly. When I finished the above procedure, I did run python3 manage.py runserver successfully. BUT, when I was going to move the project to uwsgi, I got django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
Following my uWSGI shell cmd:
uwsgi --http :8000 --pythonpath /mnt/datasource/<privacy_hidden>/venv/bin/python3 --virtualenv=/mnt/datasource/app-repos-management/back/venv --wsgi-file delivery/wsgi.py --master --processes 4 --threads 4
Relative Output Is:
*** Starting uWSGI 2.0.18 (64bit) on [Mon Mar 2 16:55:39 2020] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 02 March 2020 08:43:11
os: Linux-3.10.0-1062.4.3.el7.x86_64 #1 SMP Wed Nov 13 23:58:53 UTC 2019
nodename: localhost.localdomain
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /mnt/datasource/<privacy_hidden>
detected binary path: /home/user/.local/bin/uwsgi
your processes number limit is 31049
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:45914 (port auto-assigned) fd 3
Python version: 3.6.8 (default, Aug 7 2019, 17:28:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
PEP 405 virtualenv detected: /mnt/datasource/<privacy_hidden>/venv
Set PythonHome to /mnt/datasource/<privacy_hidden>/venv
Python main interpreter initialized at 0x19e8e70
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 521440 bytes (509 KB) for 16 cores
*** Operational MODE: preforking+threaded ***
added /mnt/datasource/<privacy_hidden>/venv/bin/python3 to pythonpath.
Traceback (most recent call last):
File "delivery/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/models/base.py", line 121, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/models/base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/models/options.py", line 208, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/utils.py", line 207, in __getitem__
backend = load_backend(db['ENGINE'])
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 68, in <module>
check_sqlite_version()
File "/mnt/datasource/<privacy_hidden>/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 65, in check_sqlite_version
raise ImproperlyConfigured('SQLite 3.8.3 or later is required (found %s).' % Database.sqlite_version)
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 81912)
spawned uWSGI worker 1 (pid: 81914, cores: 4)
spawned uWSGI worker 2 (pid: 81915, cores: 4)
spawned uWSGI worker 3 (pid: 81919, cores: 4)
spawned uWSGI worker 4 (pid: 81923, cores: 4)
spawned uWSGI http 1 (pid: 81927)
btw, my sqlite3.sql_version test is '3.31.1', Any Suggestion will be cool🙏.
After upgraded the SQLite3, replace the system default SQLite3 by the newer one.
mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
echo "/usr/local/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
My operation system is CentOS7.

Daemonizing Celery--No module named 'celery' error

I'm trying to daemonize Celery 4.3.0 on Ubuntu 18 following the official documentation. This is for a Django project to off-load intensive tasks.
When the server is running the celery.service should make the Celery worker available to process tasks. However, Apache2 won't even run. If I tail the Apache log I see:
[Sun Sep 29 07:42:07.621273 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Sun Sep 29 07:42:07.621279 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
[Sun Sep 29 07:42:07.621285 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
[Sun Sep 29 07:42:07.621291 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap_external>", line 678, in exec_module
[Sun Sep 29 07:42:07.621297 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Sun Sep 29 07:42:07.621303 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "/var/www/html/examgap/examgap/__init__.py", line 5, in <module>
[Sun Sep 29 07:42:07.621307 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] from .celery import app as celery_app
[Sun Sep 29 07:42:07.621313 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "/var/www/html/examgap/examgap/celery.py", line 5, in <module>
[Sun Sep 29 07:42:07.621317 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] from celery import Celery
[Sun Sep 29 07:42:07.621333 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] ModuleNotFoundError: No module named 'celery'
Celery is definitely installed for Python3. I can run the Celery worker for my app standalone using celery -A examgap worker -l warning--just not as a daemon.
I can also import celery if I launch a Python3 shell.
I've looked at several existing SO questions like this one and think I've eliminated some possibilities. For example, the daemon user is ubuntu and this has rwx permissions for my project folder.
Still I think the problem lies with my celery.service file and the command that starts the daemon either not accessing the right folder or using the right permissions.
Here's my /etc/systemd/system/celery.service:
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=ubuntu
Group=ubuntu
EnvironmentFile=/etc/conf.d/celery
WorkingDirectory=/opt/celery
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} --pidfile=${CELERYD_PID_FILE}' ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
[Install]
WantedBy=multi-user.target
Thanks in advance.
Edit: Here's my /etc/conf.d/celery too:
# Name of nodes to start
# here we have a single node
CELERYD_NODES="eg1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the 'celery' command:
#CELERY_BIN="/usr/local/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"
#Think I installed Celery only for user ubuntu
CELERY_BIN="/home/ubuntu/.local/bin/celery"
#CELERYD_CHDIR="/var/www/html/examgap/"
# App instance to use
# comment out this line if you don't use an app
CELERY_APP="examgap"
# or fully qualified:
# CELERY_APP="examgap.tasks:app"
# How to call manage.py
CELERYD_MULTI="multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# - %n will be replaced with the first part of the nodename.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
Edit 2: Changed the WorkingDirectory to my project directory and now the celery service is running. Yet the ModuleNotFoundError: No module named 'celery' persists.
[Sun Sep 29 07:42:07.621313 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "/var/www/html/examgap/examgap/celery.py", line 5, in <module>
Seems like you have your own celery.py, if so you should rename it to avoid import problems
Here I let some links that look excellent. Just now I'm doing the same.
https://realpython.com/asynchronous-tasks-with-django-and-celery/
(recommended)
http://blog.fixter.org/hospedando-django-celery-rabbitmq-en-aws-parte-1/
(i think it's tricky way)
https://dev.to/idrisrampurawala/deploying-django-with-celery-and-redis-on-ubuntu-3fo6 (level pro)
I hope this work for you.

Ubuntu / Django / Logging / PermissionError: [Errno 13] Permission denied

Ubuntu 18.04.1
using the Bitnami Django Stack: bitnami-djangostack-2.1.5-1
I get this error when starting apache service:
[16:21:20.028270 2019] [wsgi:error] Traceback (most recent call last):
[16:21:20.028373 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 562, in configure
[16:21:20.028395 2019] [wsgi:error] handler = self.configure_handler(handlers[name])
[16:21:20.028421 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 735, in configure_handler
[16:21:20.028438 2019] [wsgi:error] result = factory(**kwargs)
[16:21:20.028463 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/handlers.py", line 147, in __init__
[16:21:20.028479 2019] [wsgi:error] BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
[16:21:20.028504 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/handlers.py", line 54, in __init__
[16:21:20.028520 2019] [wsgi:error] logging.FileHandler.__init__(self, filename, mode, encoding, delay)
[16:21:20.028545 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/__init__.py", line 1092, in __init__
[16:21:20.028562 2019] [wsgi:error] StreamHandler.__init__(self, self._open())
[16:21:20.028601 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/__init__.py", line 1121, in _open
[16:21:20.028618 2019] [wsgi:error] return open(self.baseFilename, self.mode, encoding=self.encoding)
[16:21:20.028666 2019] [wsgi:error] PermissionError: [Errno 13] Permission denied: '/opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/static/logs/ca.myproject.app.cleaner.log'
[16:21:20.028701 2019] [wsgi:error]
[16:21:20.028716 2019] [wsgi:error] The above exception was the direct cause of the following exception:
[16:21:20.028729 2019] [wsgi:error]
[16:21:20.028753 2019] [wsgi:error] Traceback (most recent call last):
[16:21:20.028832 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/myproject/wsgi.py", line 19, in <module>
[16:21:20.028851 2019] [wsgi:error] application = get_wsgi_application()
[16:21:20.028886 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/core/wsgi.py", line 12, in get_wsgi_application
[16:21:20.028903 2019] [wsgi:error] django.setup(set_prefix=False)
[16:21:20.028928 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/__init__.py", line 19, in setup
[16:21:20.028944 2019] [wsgi:error] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[16:21:20.028968 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/utils/log.py", line 76, in configure_logging
[16:21:20.028984 2019] [wsgi:error] logging_config_func(logging_settings)
[16:21:20.029008 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 799, in dictConfig
[16:21:20.029023 2019] [wsgi:error] dictConfigClass(config).configure()
[16:21:20.029046 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 570, in configure
[16:21:20.029062 2019] [wsgi:error] '%r' % name) from e
[16:21:20.029102 2019] [wsgi:error] ValueError: Unable to configure handler 'cleaner_file'
How do I fix it?
Django is unable to create the log file in the directory /opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/static/logs/
In my project settings.py the file path is specified as an absolute path, so Django should be able to write a log file to the folder.
Short fix:
chmod -R 777 $(pwd)/static/logs that will fix the issue, but after a reboot the error is back :(
Thanks for any help
Once the log hit the max size new file will be created with root permissions, see below:
-rw-rw-r-- 1 daemon daemon 0 Jun 5 13:54 my_project.debug.log
-rw-rw-r-- 1 daemon daemon 15319 Jun 12 17:52 my_project.app.log
**-rw-r--r-- 1 root root 159042 Jun 12 17:52 my_project.ws.log** --> HERE
-rw-rw-r-- 1 daemon daemon 15700891 Jun 12 17:52 my_project.ws.log.1
-rw-rw-r-- 1 daemon daemon 15711651 Jun 12 16:53 my_project.ws.log.2

mod_wsgi + python 2.7.5 import error

I have installed and probably right configurated apache2, mod_wsgi and python 2.7.5. The problem is that 127.0.0.1 is not opening (loading is just freezed) and I see following log in error.log:
[Wed Jun 19 14:49:11 2013] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.4 Python/2.7.3 configured -- resuming normal operations
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site.py", line 548, in <module>
main()
File "/usr/local/lib/python2.7/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/lib/python2.7/site.py", line 266, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/lib/python2.7/site.py", line 241, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/lib/python2.7/site.py", line 231, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/lib/python2.7/sysconfig.py", line 516, in get_config_var
return get_config_vars().get(name)
File "/usr/local/lib/python2.7/sysconfig.py", line 449, in get_config_vars
import re
File "/usr/local/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/local/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/local/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/local/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
Tried to restart apache, but it's not working. Thanks for any help!
Just found solution. I checked 'sys.prefix' in my python's interpretator and it printed '/usr/local', but I had not set WSGIPythonHome in apache config (as written in mod_wsgi docs for this case). And just for experiment I set 'WSGIPythonHome /usr'. It's strange, but it resolves my problem, everything is fine now!