简体   繁体   English

生产中的Django项目结构

[英]Django project structure in production

I'm ready to deploy my completely revamped Django website, and I'm trying to figure out the best folder structure to use. 我已经准备好部署完全改版的Django网站,并且正在尝试找出要使用的最佳文件夹结构。 When I had it deployed last time I did it all wrong, even though it was functional, so this time I'm trying to do it the "right" way. 上次部署它时,即使它可以正常运行,我还是做错了所有事情,因此,这次我尝试以“正确”的方式进行操作。 Unfortunately, it seems sometimes like there are too many "right" ways for any one way to make sense, so I'm trying to get some clarity on that. 不幸的是,有时似乎有太多“正确”的方法对任何一种方法都有意义,所以我试图对此进行一些澄清。 I'm using a Linode VPS with Ubuntu 14.04 LTS and nginx. 我正在将Linode VPS与Ubuntu 14.04 LTS和nginx一起使用。 I've been looking through walkthroughs, forum threads, and answered stackoverflow questions, and amazingly, nothing has been able to answer my questions. 我一直在浏览演练,论坛主题并回答了stackoverflow问题,令人惊讶的是,没有任何事情能够回答我的问题。 So here goes. 所以去。

I'm planning to have the Django root (the top level project root folder generated with startproject ) live at /srv/www/<site name>/djcode . 我计划在/srv/www/<site name>/djcode使用Django根目录(由startproject生成的顶级项目根目录)。 What permissions should I assign this folder, what user should own it, and how should the groups be set up? 我应该为此文件夹分配什么权限,什么用户应该拥有它,以及如何设置组? Theoretically, someone else will be helping me maintain this project in the near future, so it doesn't seem to make sense that I would chown it to my personal user, even though that was suggested in several posts that I saw. 从理论上说,别人会帮助我保持在不久的将来这个项目,所以它似乎并没有什么意义,我会chown到我的个人用户,即使是在几个职位,我看到了建议。 Would I let www-data own the folder and all the files and then add myself and my future collaborator into the www-data group? 我将让www-data拥有该文件夹和所有文件,然后将自己和我的未来协作者添加到www-data组中吗? (I'm also very weak on my understanding of how groups work in Linux, so any pointers to clear explanations of that system would also be welcome.) (我对组在Linux中的工作方式的理解也很薄弱,因此也欢迎提供任何指向该系统的清晰解释的指针。)

Backing up a bit, does it make sense for this to be where my Django code lives? 进行一些备份,将其作为我的Django代码所在的地方有意义吗? I saw only one answer in a good hour or so of searching that had a suggestion for where the code could live (the rest of the examples had the most unhelpful path /path/to/django/root in place of an actual path), so I'm not entirely sure what is the right thing to do. 在大约一个小时的搜索过程中,我只看到一个答案,其中提出了代码可以存放在哪里的建议(其余示例都使用最无用的路径/path/to/django/root代替了实际路径),所以我不完全确定正确的做法是什么。

Also, I've noticed that people sometimes seem to use /var/www/ for static HTML files, which doesn't make sense. 另外,我注意到人们有时似乎将/var/www/用于静态HTML文件,但这没有任何意义。 Isn't the point of /var that the file sizes could be subject to change? /var的意义不是文件大小可以更改吗? This then begs the question, though, of why we have a separate directory for files of variable size. 但是,这引出了一个问题,即为什么我们要为可变大小的文件建立一个单独的目录。 I assume I'll want to put my sqlite database file in there somewhere, but where exactly would it live, and what would the advantages be of putting it there? 我假设我想将我的sqlite数据库文件放在某个地方,但是它到底会放在哪里,以及将它放在那里的好处是什么?

Thank you in advance! 先感谢您!

The reason some people put static files in /var/www is simply that that's where the default Apache configuration puts the DocumentRoot on Debian-based systems. 有人将静态文件放在/ var / www中的原因仅仅是因为这是默认的Apache配置将DocumentRoot放在基于Debian的系统上的原因。 But I wouldn't recommend doing that; 但我不建议您这样做; much better to add an alias pointing at the STATICFILES dir inside your project itself. 在项目本身内部添加一个指向STATICFILES目录的别名更好。

As for permissions, your suggestion of using www-data and adding your users to that group is sensible. 至于权限,建议使用www-data并将用户添加到该组是明智的。 The recommendation to put the code under /srv was probably mine in the first place, but I think it's a good idea; 将代码放在/ srv下的建议最初可能是我的,但我认为这是个好主意; it's an easily identifiable location which is probably not used by anything else. 这是一个易于识别的位置,可能其他任何地方都不会使用。

You really shouldn't be using sqlite in production. 您真的不应该在生产中使用sqlite。 Install a proper RDBMS; 安装适当的RDBMS; preferably Postgres, but otherwise MySQL if you must. 最好是Postgres,如果需要的话最好是MySQL。 Neither is very hard to configure. 两者都不是很难配置的。

Django is not PHP or ColdFusion, where you should keep project files inside /var/www in root. Django不是PHP或ColdFusion,您应该在其中将项目文件保存在/ var / www根目录下。

Read chapter from Two Scoops of Django: Best Practices for Django 1.8 about project structure - you should have customized settings for each environment. 阅读Two Scoops of Django: Best Practices for Django 1.8报道Two Scoops of Django: Best Practices for Django 1.8有关项目结构的章节-您应该为每个环境定制化设置。

Also very good and reasonable idea is to have the same SQL engine on every environment- it wouldn't corrupt data or make migrations unstable. 同样非常好的和合理的想法是,在每个环境中都使用相同的SQL引擎-不会损坏数据或使迁移不稳定。 If your project doesn't require extraordinary DB, choose PostgreSQL. 如果您的项目不需要特殊的数据库,请选择PostgreSQL。 MySQL is other option, but MySQL doesn't support migrations in transactions. MySQL是另一种选择,但是MySQL不支持事务中的迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM