我正在尝试使用 nginx 在 Ubuntu 16.04 系统上部署 django 网站,但遇到了问题。
错误
我从 nginxerror.log
文件中获取错误:
connect() to unix:/home/teddycrepineau/contoursandcolors/contoursandcolors.sock failed (13: Permission denied) while connecting to upstream, client: 64.125.191.37, server: 173.255.210.63, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/teddycrepineau/contoursandcolors/contoursandcolors.sock:", host: "173.255.210.63", referrer: "http://173.255.210.63/"
状态uwgsi
:
$ uwsgi status
uwsgi[25361]: thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi[25361]: uwsgi socket 0 bound to UNIX address /home/teddycrepineau/contoursandcolors/contoursandcolors.sock fd 3
uwsgi[25361]: Python version: 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
uwsgi[25361]: !!! Python Home is not a directory: /home/teddycrepineau/Env/contoursandcolors !!!
uwsgi[25361]: Set PythonHome to /home/teddycrepineau/Env/contoursandcolors
uwsgi[25361]: Fatal Python error: Py_Initialize: Unable to get the locale encoding
uwsgi[25361]: ImportError: No module named 'encodings'
uwsgi[25361]: Current thread 0x00007f0ec8429700 (most recent call first):
uwsgi[25361]: Thu Apr 12 13:31:57 2018 - [emperor] curse the uwsgi instance contoursandcolors.ini (pid: 4955)
uwsgi[25361]: Thu Apr 12 13:32:00 2018 - [emperor] removed uwsgi instance contoursandcolors.ini
设置
- 使用 Python 3
- Ubuntu 16.04
- 使用 Virtualenvwrapper 和 virtualenv
Virtualenvwrapper 设置
echo "export WORKON_HOME=~/Env" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
/etc/uwsgi/sites/contoursandcolors.ini
:
[uwsgi]
project = contoursandcolors
uid = teddycrepineau
base = /home/%(uid)
chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 2
socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true
/etc/nginx/sites-available/contoursandcolor
:
server {
listen 80;
server_name 173.255.210.63;
location = /favicon.io { access_log off; log_not_found off; }
location /static/ {
root /home/teddycrepineau/contoursandcolors;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/teddycrepineau/contoursandcolors/contoursandcolors.sock;
}
}
/etc/systemd/system/uwsgi.service
:
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown teddycrepineau:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
我尝试过的方法
- 将默认 Python 版本从 2.7 更改为 3.5
- 将套接字文件位置从项目更改为运行(出现文件未找到错误)
- 我查看了不少 StackExchange 帖子,但到目前为止都没有解决我的问题
据我所知,我的问题可能与虚拟环境位置设置有关。我应该怎么办?
编辑
我最终使用了 gunicorn(而不是 uWSGI)。它工作得很好,而且设置起来也相当容易。以下是教程的链接:https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04