自从我来自 Windows 背景以来,尝试部署到 aws EC2 ubuntu 服务器对我来说非常困难。我在尝试将 django 应用程序绑定到 Gunicorn 时遇到错误。我运行的命令是
sudo gunicorn --bind 0.0.0.0:8000 logistics.wsgi:application
错误日志如下所示:
(venv) ubuntu@ip-172-31-18-196:/var/www/html$ sudo gunicorn --bind 0.0.0.0:8000 logistics.wsgi:application
[2021-09-08 11:21:00 +0000] [29379] [INFO] Starting gunicorn 20.1.0
[2021-09-08 11:21:00 +0000] [29379] [INFO] Listening at: http://0.0.0.0:8000 (29379)
[2021-09-08 11:21:00 +0000] [29379] [INFO] Using worker: sync
[2021-09-08 11:21:00 +0000] [29382] [INFO] Booting worker with pid: 29382
[2021-09-08 11:21:00 +0000] [29382] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.5/dist-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/var/www/html/logistics/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 2, in <module>
from django.core.handlers.wsgi import WSGIHandler
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 3, in <module>
from django.conf import settings
File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 19, in <module>
from django.utils.deprecation import RemovedInDjango40Warning
File "/usr/local/lib/python3.5/dist-packages/django/utils/deprecation.py", line 5, in <module>
from asgiref.sync import sync_to_async
File "/usr/local/lib/python3.5/dist-packages/asgiref/sync.py", line 115
launch_map: "Dict[asyncio.Task[object], threading.Thread]" = {}
^
SyntaxError: invalid syntax
[2021-09-08 11:21:00 +0000] [29382] [INFO] Worker exiting (pid: 29382)
[2021-09-08 11:21:00 +0000] [29379] [INFO] Shutting down: Master
[2021-09-08 11:21:00 +0000] [29379] [INFO] Reason: Worker failed to boot.
当我运行
gunicorn --bind 0.0.0.0:8000 logistics.wsgi:application
(即没有 sudo
)时,我收到另一个错误:
(venv) ubuntu@ip-172-31-18-196:/var/www/html$ gunicorn --bind 0.0.0.0:8000 logistics.wsgi:application
Traceback (most recent call last):
File "/home/ubuntu/.local/bin/gunicorn", line 7, in <module>
from gunicorn.app.wsgiapp import run
ModuleNotFoundError: No module named 'gunicorn'
但是我已经使用命令
pip3 install gunicorn --user
安装了gunicorn。我在最后添加--user
的原因是在激活的虚拟环境中运行pip3 install gunicorn
会抛出权限错误,如下所示:
(venv) ubuntu@ip-172-31-18-196:/var/www/html$ pip3 install gunicorn
Collecting gunicorn
Using cached https://files.pythonhosted.org/packages/e4/dd/5b190393e6066286773a67dfcc2f9492058e9b57c4867a95f1ba5caf0a83/gunicorn-20.1.0-py3-none-any.whl
Requirement already satisfied: setuptools>=3.0 in ./venv/lib/python3.6/site-packages (from gunicorn) (40.6.2)
Installing collected packages: gunicorn
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/var/www/html/venv/lib/python3.6/site-packages/gunicorn-20.1.0.dist-info'
Consider using the `--user` option or check the permissions.
You are using pip version 18.1, however version 21.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
再次,我已经从
python3.5
升级到 python3.6
,这样当我在终端上运行 python3 时,我会得到以下输出
(venv) ubuntu@ip-172-31-18-196:/var/www/html$ python3 --version
Python 3.6.13
但是,我不知道为什么错误日志引用 python3.5 而不是 python3.6,如下所示:
File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py"
每当我运行 sudo gunicorn --bind 0.0.0.0:8000 logistics.wsgi:application
请我想知道为什么我会收到该错误
您必须以
Ubuntu
用户身份登录,并且 NOT sudo su/root
第 1 阶段:将 Gunicorn 绑定到 Django 应用程序并检查上游的 Gunicorn 是否工作正常。 请注意,如果没有其他阶段,部署是不完整的
sudo apt-get update
sudo apt-get upgrade
可选 - 如果显示弹出窗口/选项,则只需选择 pkg 维护者版本。
python3 -m venv env
sudo apt-get install python3-venv
source env/bin/activate
pip3 install django
git clone <your-repo-url>
pip3 install gunicorn
sudo apt-get install -y nginx
cd
到您的项目目录,其中存储 settings.py
、db.sqlite3
以及项目的所有这些文件。
pip3 install -r requirements.txt
gunicorn --bind 0.0.0.0:8000 <project_name>.wsgi:application
django-admin startproject <project_name>
命令创建的主应用程序名称
你会看到
[2021-09-08 15:20:17 +0000] [12789] [INFO] Starting gunicorn 20.1.0
[2021-09-08 15:20:17 +0000] [12789] [INFO] Listening at: http://0.0.0.0:8000 (12789)
[2021-09-08 15:20:17 +0000] [12789] [INFO] Using worker: sync
[2021-09-08 15:20:17 +0000] [12791] [INFO] Booting worker with pid: 12791
这意味着您已成功绑定 Gunicorn 来运行 Django 应用程序,并且您的 Django 应用程序现在已准备好与网络服务器(在我们的例子中为 NGINX)链接。这标志着第一阶段的完成。 要测试第 1 阶段是否成功,您可以使用端口
:8000
输入您的 IP 并查看您的应用程序运行(确保您的 aws 安全性允许端口 8000,否则您将看到 404),但上面使用 pid 启动工作程序确认它正在运行.
第 2 阶段:设置
supervisor
,以便您的 Gunicors 在重新启动时和首次启动后自动启动您的 Django 应用程序。
sudo apt-get install -y supervisor
cd /etc/supervisor/conf.d/
sudo touch gunicorn.conf
sudo nano gunicorn.conf
这将打开一个编辑器,您必须在其中输入gunicorn的脚本(我们在第1阶段中进行的绑定,但现在我们告诉主管每次服务器/实例启动时都绑定gunicorn)
[program:gunicorn]
directory = /home/ubuntu/<path to manage.py>
command = /home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/<App Directory>/app.sock <App Name>.wsgi:application
autostart = true
autorestart = true
stderr_logfile = /var/log/gunicorn/gunicorn.err.log
stdout_logfile = /var/log/gunicorn/gunicorn.out.log
[group:guni]
programs:gunicorn
sudo mkdir /var/log/gunicorn
,这里我们为gunicorn 输出和错误日志创建日志文件夹。
sudo supervisorctl reread
如果您在这里看到
guni:available
,则表示您的主管已准备就绪,并且到目前为止您已经正确完成了所有事情。
sudo supervisorctl update
如果您看到
guni: added process group
,这是您已正确完成这些步骤的另一个标记。
sudo supervisorctl status
如果您看到
guni:gunicorn RUNNING
,这是第三个也是最后一个指示器,表明您的 Gunicorn 现在已正确设置。
完成所有这些步骤后,确认您的 Gunicorn 现在正在与项目目录中自动创建的 app.sock
文件进行双向通信。
第 3 阶段:将 Gunicorn 上游服务器链接到 NGINX 的最后一步
cd /etc/nginx/sites-available
sudo touch django.conf
sudo nano django.conf
这将打开您的 Nano 编辑器,您必须在其中输入这些准确的服务器设置。
server {
listen 80;
server_name <ipaddress or domain name> ;
#server_name 192.168.0.1 yourdomain.com your_alternate_domain.com; this is how you can add multiple hosts. Do not add any comma just separate it with spaces
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/Appdir/app.sock;
}
}
sudo nginx -t
(这将测试您的配置文件的语法)
sudo ln django.conf /etc/nginx/sites-enabled/
<---- this is a very crucial step make sure there is not typing mistake here, it creates a symlink
sudo nginx -t
sudo service nginx restart
现在,您已将 Nginx 链接到 app.sock 上的 Gunicorn 上游,因此请打开浏览器并输入实例的 IP 地址,您将看到您的应用程序正在运行。
如果您可以看到没有任何 CSS 的网站,那么您已经正确执行了所有操作,一旦您确认一切正常,我将重新编辑有关如何在 Nginx 中提供 Django 应用程序的静态文件的答案。
@Ruchit Micro 感谢您的整个解释。如果按照步骤进行操作,这会简化 AWS 上的部署。请问,关于nginx使用静态文件服务的问题,可以上传那部分吗?