#python #django #docker #nginx #gunicorn
Вопрос:
Я следовал учебнику по настройке приложения Django, но у меня возникли проблемы с применением тех же инструкций к моему собственному приложению.
Моя структура папок выглядит следующим образом:
├── project_name
│ ├── app_name
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── exceptions.py
│ │ ├── forms.py
│ │ ├── functions.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ ├── models.py
│ │ ├── __pycache__
│ │ ├── report_functions.py
│ │ ├── static
│ │ ├── templates
│ │ ├── tests.py
│ │ ├── upload_file.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── project_name
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── db.sqlite3
│ ├── manage.py
│ └── static
│ ├── admin
│ └── app_name
├── Dockerfile
├── nginx.default
├── pip_cache
├── requirements.txt
├── start-server.sh
└── venv
Докерфайл
FROM python:3.7-buster
RUN apt-get update amp;amp; apt-get install nginx vim -y --no-install-recommends
COPY nginx.default /etc/nginx/sites-available/default
RUN ln -sf /dev/stdout /var/log/nginx/access.log
amp;amp; ln -sf /dev/stderr /var/log/nginx/error.log
RUN mkdir -p /opt/app
RUN mkdir -p /opt/app/pip_cache
RUN mkdir -p /opt/app/project_name
COPY requirements.txt start-server.sh /opt/app/
COPY pip_cache /opt/app/pip_cache/
COPY project_name /opt/app/project_name/
WORKDIR /opt/app
RUN pip install -r requirements.txt --cache-dir /opt/app/pip_cache
RUN chown -R www-data:www-data /opt/app
EXPOSE 8020
STOPSIGNAL SIGTERM
CMD ["/opt/app/start-server.sh"]
start-server.sh
#!/usr/bin/env bash
# start-server.sh
if [ -n "$DJANGO_SUPERUSER_USERNAME" ] amp;amp; [ -n "$DJANGO_SUPERUSER_PASSWORD" ] ; then
(python3 manage.py createsuperuser --no-input)
fi
(cd project_name; gunicorn project_name.wsgi --user www-data --bind 0.0.0.0:8010 --workers 3) amp;
nginx -g "daemon off;"
project_name/project_name/wsgi.py
"""
WSGI config for creativ_ceutical project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'creativ_ceutical.settings')
application = get_wsgi_application()
Когда я запускаю start-server.sh сценарий, gunicorn не может породить рабочих из-за ModuleNotFoundError, в котором говорится «нет модуля с именем «имя_проекта»». Похоже, это связано с файлом wsgi, но я не понимаю, почему, поскольку в папке имя_проекта есть __init__.py
файл, и я добавил его каталог os.sys.path
. Полная обратная связь-это:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
nginx: [emerg] bind() to [::]:80 failed (98: Unknown error)
[2021-10-06 17:17:17 0100] [9703] [INFO] Starting gunicorn 20.1.0
[2021-10-06 17:17:17 0100] [9703] [INFO] Listening at: http://0.0.0.0:8010 (9703)
[2021-10-06 17:17:17 0100] [9703] [INFO] Using worker: sync
[2021-10-06 17:17:17 0100] [9704] [INFO] Booting worker with pid: 9704
[2021-10-06 17:17:17 0100] [9704] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 384, in import_app
mod = importlib.import_module(module)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'project_name'
[2021-10-06 17:17:17 0100] [9704] [INFO] Worker exiting (pid: 9704)
[2021-10-06 17:17:17 0100] [9703] [INFO] Shutting down: Master
[2021-10-06 17:17:17 0100] [9703] [INFO] Reason: Worker failed to boot.
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
nginx: [emerg] bind() to [::]:80 failed (98: Unknown error)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
nginx: [emerg] bind() to [::]:80 failed (98: Unknown error)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
nginx: [emerg] bind() to [::]:80 failed (98: Unknown error)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
nginx: [emerg] bind() to [::]:80 failed (98: Unknown error)
nginx: [emerg] still could not bind()
Комментарии:
1. вы настроили
gunicorn
?2. почтовый Докерфайл
3. @DefSoudani Я добавил файл Dockerfile :), Сабил, я не уверен. Учебник, которому я следовал ( semaphoreci.com/community/tutorials/… ) ничего не делает с настройкой gunicorn, поэтому я предполагаю, что я этого не сделал