如何在一台Apache服务器上部署多个Python 2和3 Django项目?

时间:2017-12-13 17:59:39

标签: python django apache ubuntu mod-wsgi

问题: Stack Overflow上有一个类似的问题:"Can I deploy both Python 2 and 3 Django app with Apache using mod_wsgi?"从答案那里我知道可能有两个有多个Django项目(用Python 2和3编写)一台Apache服务器。但是,我无法做到这一点。

到目前为止我有什么

  1. 我使用的是Linux(Debian / Ubuntu)。
  2. 三个Django项目存储在三个独立的Python虚拟环境中(即py3venv1,py3venv2,py2venv1):

    /var/www/
    
    .........py3venv1/                             <-- Python 3 venv
    ..................bin/
    ..................include/
    ..................lib/
    ..................project1/                    <-- Python 3 Django Project
    ........................../manage.py
    ........................../project1/wsgi.py
    ........................../myapp
    
    .........py3venv2/                             <-- Python 3 venv
    ..................bin/
    ..................include/
    ..................lib/
    ..................project2/                    <-- Python 3 Django Project
    ........................../manage.py
    ........................../project2/wsgi.py
    ........................../myapp
    
    .........py2venv1/                             <-- Python 2 venv
    ..................bin/
    ..................include/
    ..................lib/
    ..................project3/                    <-- Python 2 Django Project
    ........................../manage.py
    ........................../project3/wsgi.py
    ........................../myapp
    
  3. 我为Python3安装了mod_wsgi(pip3 install mod_wsgi

  4. Apache配置:项目1和2的/etc/apache2/sites-available/000-default.conf(仅限Python3),未配置项目3(Python 2):

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        # Project 1 (Python3)
        WSGIScriptAlias /project1 /var/www/py3venv1/project1/project1/wsgi.py process-group=group1
        WSGIDaemonProcess group1 python-home=/var/www/py3venv1/lib/python3.5 python-path=/var/www/py3venv1/project1
        WSGIProcessGroup group1
        Alias /project1/static /var/www/py3env1/project1/assets
        <Directory /var/www/py3venv1/project1/project1>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        # Project 2 (Python3)
        WSGIScriptAlias /project2 /var/www/py3venv2/project2/project2/wsgi.py process-group=group2
        WSGIDaemonProcess group2 python-home=/var/www/py3venv2/lib/python3.5 python-path=/var/www/py3venv2/project2
        WSGIProcessGroup group2
        Alias /project2/static /var/www/py3env2/project2/assets
        <Directory /var/www/py3venv2/project2/project2>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
    </VirtualHost>
    
  5. 结果:两个Python 3项目都运行良好。 Python 2项目不起作用。

    问题:如何让所有三个Django项目都能在Apache上运行?

1 个答案:

答案 0 :(得分:1)

您清楚地链接的另一个问题是,您不能在同一个Apache服务器实例中同时拥有Python 2和3应用程序。你怎么读它说你可以?

如果你想要不同的Python版本,你需要使用其他帖子中提到的mod_wsgi-express并将其作为单独的实例运行,并将特定站点的前端Apache代理请求传递给另一个实例。

你有没有看过mod-wsgi-express并尝试安装并运行它?