我自学了如何设置Ubuntu服务器来运行我的Django应用程序。我想使用Nginx + uwsgi。我知道这个问题对于专家来说非常容易,但我花了6天的时间在互联网上寻找它而没有得到它(无论如何,如果与答案有任何联系,请原谅我)。我已经关注了很多教程和帖子,但我没有找到解决方案。
我在下面描述了我的文件结构:
我的django项目位于/ usr / local / projects / myproject
我的virtualenv位于/root/.virtualenvs/myproject
我的uwsgi配置文件myproject.ini位于/ etc / uwsgi / apps-available /并正确地符号链接在/ etc / uwsgi / apps-enabled /
[uwsgi]
plugins = python
socket = /tmp/myproject.sock
chmod-socket = 644
uid = www-data
gid = www-data
master = true
enable-threads = true
processes = 2
no-site=true
virtualenv = /root/.virtualenvs/myproject
chdir = /usr/local/projects/myproject
module = myproject.wsgi:application
pidfile = /usr/local/projects/myproject/myproject.pid
logto = /var/log/uwsgi/myproject_uwsgi.log
vacuum = true
我的nginx配置文件myproject.conf位于/ etc / nginx / sites-available /并正确地符号链接在/ etc / nginx / sites-enabled /
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/myproject.sock; # for a file socket
}
server {
listen 80;
server_name dev.myproject.com www.dev.myproject.com;
access_log /var/log/nginx/myproject_access.log;
error_log /var/log/nginx/myproject_error.log;
location / {
uwsgi_pass unix:///tmp/myproject.sock;
include /etc/nginx/uwsgi_params;
uwsgi_param UWSGI_SCRIPT myproject.wsgi;
}
location /media/ {
alias /usr/local/projects/myproject/media/;
}
location /static/ {
alias /usr/local/projects/myproject/static/;
}
}
当我尝试访问dev.myproject.com时,我收到内部服务器错误。然后我看看我的uwsgi日志:
Traceback (most recent call last):
File "./myproject/wsgi.py", line 9, in <module>
import os
ImportError: No module named os
Sat Jul 26 17:39:16 2014 - unable to load app 0 (mountpoint='') (callable not found or import error)
Sat Jul 26 17:39:16 2014 - --- no python application found, check your startup logs for errors ---
[pid: 8559|app: -1|req: -1/8] 79.148.138.10 () {40 vars in 685 bytes} [Sat Jul 26 17:39:16 2014] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 1 headers in 57 bytes (0 switches on core 0)
我需要你的帮助,因为尽管非常简单,但我无法找到解决方案。
如果您需要了解其他信息,请告诉我,我会尽快更新我的问题。
答案 0 :(得分:2)
我终于找到了解决方案。
我遵循了kchan关于不将任何内容放在/ root /目录中的建议。基本上,我在myproject.conf文件和myproject.ini文件中做了一些小改动。我创建了一个用户并构建了如下所示的内容:
uwsgi配置文件myproject.ini在/ etc / uwsgi / apps-available /中正确地符号链接在/ etc / uwsgi / apps-enabled /[uwsgi]
plugins = python
socket = /tmp/myproject.sock
chmod-socket = 644
uid = www-data
gid = www-data
master = true
enable-threads = true
processes = 2
virtualenv = /home/user/.virtualenvs/myproject
chdir = /home/user/projects/myproject
module = myproject.wsgi:application
pidfile = /home/user/projects/myproject/myproject.pid
daemonize = /var/log/uwsgi/myproject_uwsgi.log
vacuum = true
nginx配置文件myproject.conf在/ etc / nginx / sites-available /中正确地符号链接在/ etc / nginx / sites-enabled /
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/myproject.sock; # for a file socket
}
server {
listen 80;
server_name dev.myproject.com www.dev.myproject.com;
access_log /var/log/nginx/myproject_access.log;
error_log /var/log/nginx/myproject_error.log;
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
}
location /media/ {
alias /home/user/projects/myproject/media/;
}
location /static/ {
alias /home/user/projects/myproject/static/;
}
}
我必须说我认为真正的问题是尝试在我的virtualenv的postactivate文件中设置我的数据库配置。希望能帮助别人。