2015-05-21 80 views
1

我有一個網上商店,其中包含主頁面和商店頁面。現在這個主頁和商店頁面實際上是兩個不同的項目,所以爲了讓他們在線,我必須運行兩個django實例。無法在子域上運行apache的django項目

現在的事情是,我想擁有www.setakshop.ir上的主頁和shop.setakshop.ir上的商店。事情是,在設置必要的設置後,shop.setakshop.ir和setakshop.ir都指向主頁面!我只能通過setakshop.ir:8000看到這個店的頁面,這是我預期的Apache代理它shop.setakshop.ir 我所服務的DNS我,這裏是我的DNS設置:

; 
; BIND data file for setakshop.ir 
; 
$TTL 3h 
@  IN  SOA  ns1.setakshop.ir. admin.setakshop.ir. (
          1  ; Serial 
          3h  ; Refresh after 3 hours 
          1h  ; Retry after 1 hour 
          1w  ; Expire after 1 week 
          1h)  ; Negative caching TTL of 1 day 
; 
@  IN  NS  ns1.setakshop.ir. 
@  IN  NS  ns2.setakshop.ir. 


setakshop.ir. IN  MX  10  mail.setakshop.ir. 
setakshop.ir. IN  A  xx.xx.xx.xx 
ns1      IN  A  xx.xx.xx.xx 
ns2      IN  A  xx.xx.xx.xx 
www      IN  CNAME setakshop.ir. 
mail     IN  A  xx.xx.xx.xx 
ftp      IN  CNAME setakshop.ir. 
shop      IN  A  xx.xx.xx.xx 

當運行

nslookup shop.setakshop.ir 

我收到了有效的答覆。所以我認爲DNS設置實際上很好。

現在我懷疑的另一件事是我的apache設置。我懷疑我沒有設置正確的代理設置。這裏是:

<VirtualHost *:80> 
    WSGIDaemonProcess main python-path=/var/www/setak:/var/www/setak/setakenv/lib/python2.7/site-packages 
    WSGIProcessGroup main 
    WSGIScriptAlias//var/www/setak/setakenv/main/ashop/ashop/ashop/wsgi.py 
    ServerAdmin [email protected] 
    ServerName www.setakshop.ir 
    ProxyPass/http://www.setakshop.ir:8001/ 
    ProxyPassReverse/http://www.setakshop.ir:8001/ 

    Alias /media/ /var/www/setak/setakenv/main/ashop/ashop/static/media/ 
    Alias /static/ /var/www/setak/setakenv/main/ashop/ashop/static/ 

    <Directory /var/www/setak/setakenv/main/ashop/ashop/static> 
     Order allow,deny 
     allow from all 
    </Directory> 

    <Directory /var/www/setak/setakenv/main/ashop/ashop/static/media> 
     Order allow,deny 
     allow from all 
    </Directory>  

    DocumentRoot /var/www 
    <Directory /> 
     Options FollowSymLinks 
     AllowOverride None 
    </Directory> 
    <Directory /var/www/> 
     Options Indexes FollowSymLinks MultiViews 
     AllowOverride None 
     Order allow,deny 
     allow from all 
    </Directory> 

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
    <Directory "/usr/lib/cgi-bin"> 
     AllowOverride None 
     Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch 
     Order allow,deny 
     Allow from all 
    </Directory> 

    ErrorLog ${APACHE_LOG_DIR}/error.log 

    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. 
    LogLevel warn 

    CustomLog ${APACHE_LOG_DIR}/access.log combined 

    Alias /doc/ "/usr/share/doc/" 
    <Directory "/usr/share/doc/"> 
     Options Indexes MultiViews FollowSymLinks 
     AllowOverride None 
     Order deny,allow 
     Deny from all 
     Allow from 127.0.0.0/255.0.0.0 ::1/128 
    </Directory> 

</VirtualHost> 


<VirtualHost *:8080> 
    WSGIDaemonProcess setak python-path=/var/www/setak:/var/www/setak/setakenv/lib/python2.7/site-packages 
    WSGIProcessGroup setak 
    WSGIScriptAlias//var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/wsgi.py 
    ServerAdmin [email protected] 
    ServerName shop.setakshop.ir 

    <Proxy *> 
     Order deny,allow 
     Allow from all 
    </Proxy> 
    ProxyPass/http://shop.setakshop.ir:8000 
    ProxyPassReverse/http://shop.setakshop.ir:8000 

    Alias /robots.txt /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/static/robots.txt 

    Alias /media/ /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/media/ 
    Alias /static/ /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/static/ 

    <Directory /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/static> 
     Order allow,deny 
     allow from all 
    </Directory> 

    <Directory /var/www/setak/setakenv/setakmain/django-oscar/sites/sandbox/public/media> 
     Order allow,deny 
     allow from all 
    </Directory>  

    DocumentRoot /var/www 
    <Directory /> 
     Options FollowSymLinks 
     AllowOverride None 
    </Directory> 
    <Directory /var/www/> 
     Options Indexes FollowSymLinks MultiViews 
     AllowOverride None 
     Order allow,deny 
     allow from all 
    </Directory> 

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
    <Directory "/usr/lib/cgi-bin"> 
     AllowOverride None 
     Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch 
     Order allow,deny 
     Allow from all 
    </Directory> 

    ErrorLog ${APACHE_LOG_DIR}/error.log 

    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. 
    LogLevel warn 

    CustomLog ${APACHE_LOG_DIR}/access.log combined 

    Alias /doc/ "/usr/share/doc/" 
    <Directory "/usr/share/doc/"> 
     Options Indexes MultiViews FollowSymLinks 
     AllowOverride None 
     Order deny,allow 
     Deny from all 
     Allow from 127.0.0.0/255.0.0.0 ::1/128 
    </Directory> 

</VirtualHost> 

現在我沒有得到我在做什麼錯在這裏! 我同時運行使用下面的命令的項目:

./manage.py runserver 0.0.0.0:8000 
./manage.py runserver 0.0.0.0:8001 

誰能告訴我什麼,我做錯了什麼?

在此先感謝。

+0

我甚至不知道從哪裏開始形式。首先,django服務器不適合生產:「runserver只適用於本地機器上的開發,不適用於公開Web上的暴露。」 https://docs.djangoproject.com/en/1.8/howto/deployment/ – Wtower

+1

否,該教程甚至沒有提及使用Apache作爲代理。而且它提到runserver的唯一時間就是測試Django是否正確設置。 –

回答

4

真的不是你如何通過Apache運行Django應用程序。 runserver不適合或不適合生產;將Apache用作runserver的反向代理是沒有意義的。

令人困惑的是,您似乎已經爲您的某個網站正確設置了WSGIScriptAlias,但您仍在代理它。你需要完全刪除代理的東西,忘記runserver,並在整個過程中使用mod_wsgi。

讓Apache在兩個域上提供兩個站點的方法是使用NamedVirtualHosts。您只需設置兩個單獨的,每個都有正確的服務器名稱。

+0

非常感謝。你的答案的問題是它太籠統了,我對此很新,所以你介意給你的解決方案增加細節? –

+0

這個答案是非常具體的......它概括了你需要做什麼來修復事情。除了完全編寫你的Apache配置之外,這個答案已經完成了。 – rnevius

0

首先,您不應該使用./manage.py runserver和apache,並且在將網站移至生產環境時,不應使用./manage.py runserver。 runserver僅適用於開發環境,一次只能處理一個請求。

您應該使用mod_wsgi來運行使用Apache的項目。看文檔here

否則gunicorn和nginx可用於運行您的項目。看看this