托管两个不同的项目django

时间:2019-02-19 17:40:31

标签: python django server

我正在设置django Web应用程序,并且正在使用github进行版本管理。所以我需要一个测试页面。我该如何实现?

我发现了这个问题: Is it possible to host multiple django projects under the same domain?

但是它已经6岁了,我宁愿将其托管在一个子域下,而且我也不知道答案在说什么,因为我是Django的新手。

2 个答案:

答案 0 :(得分:2)

是的,有可能。我在我的DigitalOcean Droplet上运行了一个LAMP Stack,该Lamp Stack上托管着十多个实时django网站。所有这些实际上都集中在您的站点配置中的虚拟环境设置上。

这是一个例子,如果您的学习足以使您前进…… /etc/apache2/sites-available/website1.com.conf

<VirtualHost *:80>

        ServerName website1.com
        ServerAlias www.website1.com

        ServerAdmin youremail@domain.com
        DocumentRoot /var/www/html/website1.com/djangoproject
        WSGIScriptAlias / /var/www/html/website1.com/djangoproject/djangoproject/wsgi.py

        WSGIDaemonProcess website1.com processes=2 threads=15 display-name=%{GROUP} \
                python-home=/var/www/html/website1.com/venv \
                python-path=/var/www/html/website1.com/ainet
        WSGIProcessGroup website1.com

        <Directory/var/www/html/website1.com/djangoproject>
                AllowOverride all
                Require all granted
                Options FollowSymlinks
        </Directory>

        Alias /static/ /var/www/html/website1.com/djangoproject/static/

        <Directory /var/www/html/website1.com/djangoproject/static/>
                Require all granted
        </Directory>

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

</VirtualHost>

/etc/apache2/sites-available/website2.com.conf

<VirtualHost *:80>

        ServerName subdomain.website1.com
        ServerAlias subdomain.website1.com

        ServerAdmin youremail@domain.com
        DocumentRoot /var/www/html/subdomain.website1.com/djangoproject
        WSGIScriptAlias / /var/www/html/subdomain.website1.com/djangoproject/djangoproject/wsgi.py

        WSGIDaemonProcess subdomain.website1.com processes=2 threads=15 display-name=%{GROUP} \
                python-home=/var/www/html/subdomain.website1.com/venv \
                python-path=/var/www/html/subdomain.website1.com/ainet
        WSGIProcessGroup subdomain.website1.com

        <Directory/var/www/html/subdomain.website1.com/djangoproject>
                AllowOverride all
                Require all granted
                Options FollowSymlinks
        </Directory>

        Alias /static/ /var/www/html/subdomain.website1.com/djangoproject/static/

        <Directory /var/www/html/subdomain.website1.com/djangoproject/static/>
                Require all granted
        </Directory>

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

</VirtualHost>

以上假设您将django项目放在/var/www/html/website1.com/中。它还假定该文件夹包含位于venv文件夹中的虚拟环境。

/var/www/html/website1.com
/var/www/html/website1.com/djangoproject
/var/www/html/website1.com/venv
/var/www/html/subdomain.website1.com
/var/www/html/subdomain.website1.com/djangoproject
/var/www/html/subdomain.website1.com/venv

但是,如果您需要一个测试页面,那么Django有一个内置的本地开发服务器,我强烈建议您使用。

设置好环境后,就像运行python manage.py runserver一样简单。

答案 1 :(得分:0)

如果您想在同一服务器的子域上托管,我已经使用Nginx作为反向代理来做到了。设置非常简单,您可以将指向不同子域的请求指向运行django内容的任何服务器的不同实例。

看看这个:https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-14-04-droplet 显然,您可以将“ apache”替换为所需的任何服务器。

相关问题