【问题标题】:How can I run two django projects through Apache webserver in Ubuntu 14.04?如何在 Ubuntu 14.04 中通过 Apache 网络服务器运行两个 django 项目?
【发布时间】:2015-11-18 19:57:21
【问题描述】:

我能够通过 Apache 服务器运行一个 django 项目。 我有两个项目:yota_admin_module 和 yotasite。 我的 Apache 配置文件:/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

Alias /static /home/abhay/django-project/yota_admin_module/yota/static/
WSGIDaemonProcess yota_admin_module python-path=/home/abhay/django-project/yota_admin_module:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup yota_admin_module
WSGIScriptAlias / /home/abhay/django-project/yota_admin_module/yota_admin_module/wsgi.py

<Directory /home/abhay/django-project/yota_admin_module/ >
       Require all granted
</Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

但是现在我在尝试使用该 Apache 服务器运行另一个 django 项目时遇到了问题。 所以,我做了什么:在 /etc/apache2/sites-available/000-default.conf 这样修改:

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

Alias /static /home/abhay/django-project/yota_admin_module/yota/static/
WSGIDaemonProcess yota_admin_module python-path=/home/abhay/django-project/yota_admin_module:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup yota_admin_module
WSGIScriptAlias / /home/abhay/django-project/yota_admin_module/yota_admin_module/wsgi.py

<Directory /home/abhay/django-project/yota_admin_module/ >
       Require all granted
</Directory>

WSGIDaemonProcess yotasite python-path=/home/abhay/django-project/yotasite:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup yotasite
WSGIScriptAlias / /home/abhay/django-project/yotasite/yotasite/wsgi.py

<Directory /home/abhay/django-project/yotasite/ >
       Require all granted
</Directory>


# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

所以,修改后当“sudo service apache2 restart”出现错误时:

[Tue Aug 25 13:21:20.325955 2015] [mpm_event:notice] [pid 4761:tid 140704687433600] AH00491: caught SIGTERM, shutting down
[Tue Aug 25 13:21:21.491426 2015] [mpm_event:notice] [pid 4895:tid 140635456407424] AH00489: Apache/2.4.12 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Tue Aug 25 13:21:21.491930 2015] [core:notice] [pid 4895:tid 140635456407424]  AH00094: Command line: '/usr/sbin/apache2'
[Tue Aug 25 13:21:34.751733 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] mod_wsgi (pid=4899): Target WSGI script '/home/abhay/django-project/yota_admin_module/yota_admin_module/wsgi.py' cannot be loaded as Python module.
[Tue Aug 25 13:21:34.752015 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] mod_wsgi (pid=4899): Exception occurred processing WSGI script '/home/abhay/django-project/yota_admin_module/yota_admin_module/wsgi.py'.
[Tue Aug 25 13:21:34.752198 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] Traceback (most recent call last):
[Tue Aug 25 13:21:34.752358 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]   File "/home/abhay/django-project/yota_admin_module/yota_admin_module/wsgi.py", line 17, in <module>
[Tue Aug 25 13:21:34.752715 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]     application = get_wsgi_application()
[Tue Aug 25 13:21:34.752865 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Tue Aug 25 13:21:34.753125 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] django.setup()
[Tue Aug 25 13:21:34.753255 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
[Tue Aug 25 13:21:34.753513 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Aug 25 13:21:34.753654 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__
[Tue Aug 25 13:21:34.754139 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] self._setup(name)
[Tue Aug 25 13:21:34.754294 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 44, in _setup
[Tue Aug 25 13:21:34.754445 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]     self._wrapped = Settings(settings_module)
[Tue Aug 25 13:21:34.754557 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 92, in __init__
[Tue Aug 25 13:21:34.754693 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Aug 25 13:21:34.754800 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Tue Aug 25 13:21:34.755095 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] __import__(name)
[Tue Aug 25 13:21:34.755256 2015] [:error] [pid 4899:tid 140635300349696] [remote 127.0.0.1:27198] ImportError: No module named settings

所以,我需要一些指导来解决这个问题,以便使用 Apache 服务器运行两个 django 项目。

【问题讨论】:

    标签: python django apache ubuntu


    【解决方案1】:

    各种错误。请参阅下面的 cmets。

    # Best to match trailing slashes on both arguments to be safe. If
    # leave one off on one, can't remember which, the final filesystem
    # path will not be formed correctly. Use have trailing slash on both
    # or not on either.
    
    Alias /static/ /home/abhay/django-project/yota_admin_module/yota/static/
    
    # If you are needing to include /usr/local/lib/python2.7/site-packages then
    # you must be doing something wrong. That should be included by default.
    # If it is required, then your mod_wsgi is not compiled for that Python
    # installation.
    
    WSGIDaemonProcess yota_admin_module python-path=/home/abhay/django-project/yota_admin_module:/usr/local/lib/python2.7/site-packages
    
    # You can't use WSGIProcessGroup unless you scoped it by a Directory
    # of Location directive so it knows which Django instance it applied to.
    # Better to use process-group option to WSGIScriptAlias instead so it
    # knows for sure. Also force main interpreter context using
    # application-group with value of %{GLOBAL}
    
    WSGIScriptAlias / /home/abhay/django-project/yota_admin_module/yota_admin_module/wsgi.py \
        process-group=yota_admin_module application-group=%{GLOBAL}
    
    <Directory /home/abhay/django-project/yota_admin_module/ >
           Require all granted
    </Directory>
    
    WSGIDaemonProcess yotasite python-path=/home/abhay/django-project/yotasite:/usr/local/lib/python2.7/site-packages
    
    # You can't have both sites mounted at the root of the host. This second
    # one will always be hidden by the first and never get any requests.
    # If wanting to have both under same server name, you would need to have
    # one at a sub URL. That for a sub URL must be first. That is before
    # that for the root of the site.
    
    WSGIScriptAlias / /home/abhay/django-project/yotasite/yotasite/wsgi.py \
        process-group=yotasite application-group=%{GLOBAL}
    
    <Directory /home/abhay/django-project/yotasite/ >
           Require all granted
    </Directory>
    

    【讨论】:

    • -> 感谢您的指导。还有一件事请您解释一下: # 您不能将两个站点都安装在主机的根目录下。这第二个 #one 将始终被第一个隐藏,并且永远不会收到任何请求。 # 如果希望两者都使用相同的服务器名称,则需要在子 URL 中设置 # 一个。子 URL 必须是第一个。那是在#之前,用于站点的根目录。以及如何实施。我希望两个站点都通过 Apache 运行。
    • 您必须将它们作为不同的主机名,这意味着您必须拥有多个 VirtualHost,每个虚拟主机一个。然后,您需要在您的控制下单独的主机名,这些主机名可用于映射到它们。否则,您使用不同的端口,使用不同的 VirtualHost,或者将一个放在子 URL 下。您不能让两者都尝试在站点根目录接受相同主机名的请求。
    猜你喜欢
    • 2011-07-12
    • 2014-11-23
    • 1970-01-01
    • 2019-09-10
    • 2019-12-18
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多