2014-07-26 201 views
2

我在教自己如何設置一個Ubuntu服務器來運行我的Django應用程序。我想使用Nginx + uwsgi。我知道這個問題對專家來說可能非常容易,但是我花了6天的時間在網上找到它,但是沒有得到它(在任何情況下,請原諒我,如果與答案有任何關聯的話)。我跟着很多教程和帖子,但我沒有找到解決方案。uwsgi ImportError:沒有名爲os的模塊

我形容我下面的文件結構:

我的Django項目位於在/ usr /本地/項目/ MyProject的

我的virtualenv是/root/.virtualenvs/myproject

我uwsgi配置文件myproject.ini是在/ etc/uwsgi /應用程序可用/和正確的符號在/ etc/uwsgi /啓用應用程序-/

[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的配置文件的Myproj鏈接ect.conf是在/ etc/nginx的/網站可用/和正確的符號鏈接中的/ etc/

# 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/; 
    } 
} 

當我嘗試進入/ nginx的/啓用的站點 - 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) 

我需要你們的幫助,因爲我無法找到儘管是很簡單的posibility的解決方案。

如果您需要了解其他信息,請告訴我,我會盡快更新我的問題。

+0

您是否使用virtaulenv創建虛擬環境/ usr/local/projects/myproject?我看到你在/ root中有一些東西 - 我的建議是你不要使用/ root進行開發,因爲進程無法訪問內容,除非它們以root身份運行。通常你會想在你的服務器上爲一個開發項目創建一個用戶,並將你的源文件放在裏面,比如/ home/myproject/...等等。 – kchan

回答

2

我終於找到了解決辦法。

我跟隨kchan的建議,不要將任何內容放在/ root /目錄中。基本上,我在myproject.conf文件和我的myproject.ini文件中做了一些小改動。我創建了一個用戶和結構都象下面這樣:

在uwsgi配置文件myproject.ini的/ etc/uwsgi /應用程序可用/和正確的符號在/ etc/uwsgi /啓用應用程序-/

[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的/網站可用/和正確的符號鏈接中的/ etc/nginx的/啓用的站點 -/

# 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文件中設置我的數據庫配置。希望能幫助別人。

相關問題