不能 运行 子域上带有 apache 的 django 项目

can't run a django project with apache on a subdomain

我有一个包含主页和商店页面的在线商店。现在这个主页和商店页面实际上是两个不同的项目,所以为了让它们在线,我必须 运行 两个 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 admin@setakshop.ir
    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 admin@setakshop.ir
    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

谁能告诉我我做错了什么?

提前致谢。

真的 不是您 运行 Django 应用程序通过 Apache 的方式。 runserver 不适合或不适合生产;使用 Apache 作为 运行server.

的反向代理是没有意义的

令人困惑的是,您似乎已为您的其中一个网站正确设置 WSGIScriptAlias,但您仍在代理它。您需要完全删除代理内容,忘记 运行 服务器,并在整个过程中使用 mod_wsgi。

让 Apache 在两个域上为两个站点提供服务的方法是使用 NamedVirtualHosts。您只需设置两个独立的,每个都具有正确的服务器名称。

首先,您不应将 ./manage.py runserver 与 apache 一起使用,并且当您将网站移至生产环境时,也不应使用 ./manage.py runserver。 runserver 仅用于开发环境,一次只能处理一个请求。

对于使用 Apache 的 运行 项目,您应该使用 mod_wsgi。查看文档 here

否则gunicorn and nginx can be used for running your project. Have a look at .