【发布时间】:2019-02-19 17:40:31
【问题描述】:
我正在设置一个 django Web 应用程序,并且我正在使用 github 进行版本管理。所以我需要一个测试页面。我如何做到这一点?
我发现了这个问题: Is it possible to host multiple django projects under the same domain?
但它已经 6 岁了,我更愿意将它托管在一个子域下,我不知道答案在说什么,因为我是 Django 新手。
【问题讨论】:
我正在设置一个 django Web 应用程序,并且我正在使用 github 进行版本管理。所以我需要一个测试页面。我如何做到这一点?
我发现了这个问题: Is it possible to host multiple django projects under the same domain?
但它已经 6 岁了,我更愿意将它托管在一个子域下,我不知道答案在说什么,因为我是 Django 新手。
【问题讨论】:
是的,这是可能的。我在我的 DigitalOcean droplet 上运行了一个 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 一样简单。
【讨论】:
如果您想在同一服务器上托管子域,我已经使用 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”替换为您想要的任何服务器。
【讨论】: