如何在 Amazon EC2 托管上设置 Django 网站?
有人可以给出如何使用 Amazon 的 EC2 托管服务实现基于 Django 的网站托管的具体步骤吗?
这可能吗?
我的网站源代码可以在这里找到。
我搜索并发现 这篇文章。
但在做任何事情之前,我只想了解一些有关 Amazon EC2 托管的基本想法。
Can someone give specific steps on how to implement hosting of a Django-based website using Amazon's EC2 hosting service?
Is that possible?
My website source code can be found here.
I searched and found this article.
But before doing anything I just want to get some basic ideas about Amazon EC2 hosting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这当然是可能的,但听起来 EC2 并不是您的最佳选择。有关执行此操作的示例,请参阅此 或此。
从非常简单的意义上来说,EC2 只是一个可以按小时租用的服务器。您可以让它运行 Windows 或 Linux,然后像平常一样安装 Python 和 Django。事实上,可能有一个已经预配置了 Django 的镜像。
您应该了解,存在各种不同类型的托管。在一种极端情况下,您可以购买自己的物理服务器、安装自己的操作系统(如 Windows 或 Linux)、安装自己的 Python、自己的 Web 服务器(如 Apache 或 IIS)、自己的 Django 库、自己的数据库(如MySQL)等,然后将您的网站上传到其中。在另一个极端,您可以在共享托管服务上购买一个帐户,其他人已经完成了操作系统、Python、网络服务器等的所有设置,而您所需要做的就是上传您的网站代码。 EC2 更接近第一个极端,并且对您来说可能有点大材小用。我认为就您的情况而言,您应该寻找更易于管理的解决方案。
我会查看这个网页,其中列出了许多不同的 Django 托管公司:
Django 托管
It certainly is possible, but it sounds like EC2 is not the best option for you. For examples of people doing it, see for example this or this.
In a very oversimplified sense, EC2 is just a server you can rent by the hour. You can have it run Windows or Linux, and then install Python and Django like you normally would. In fact there is probably an image that has Django preconfigured already.
You should understand that there are all different types of hosting out there. At one extreme, you can pay for your very own physical server, install your own operating system (like Windows or Linux), install your own Python, you own web server like Apache or IIS, your own Django libraries, your own database (like MySQL) etc, and then upload your web site to that. At the other extreme you can pay for an account on a shared hosting service, where someone else has done all the setup of the OS, Python, the web server, etc, and all you need to do is upload your web site code. EC2 is a lot closer to the first extreme, and is probably overkill for you. I think in your case you should be looking for a more managed solution.
I would check out this web page, which lists a bunch of different Django hosting companies:
Django hosting
如果您不想从头开始设置服务器,另一个选择是使用 BitNami Django Stack Amazon 映像。我是一名 BitNami 开发人员,致力于为多个 Python 应用程序创建堆栈。 BitNami Django Stack 已包含 MySQL 5.1、Apache 2.2(带有 mod_wsgi)和安装了 MarkDown、html5lib 和 python-openid 的 Python。它还包括 django 1.3。
您需要安装 Django 调试工具栏,创建数据库并将文件复制到 /opt/bitnami/apps/django/django_projects 中,然后运行 python manage.py 命令。之后,如果您想在生产中使用(而不是 django 服务器),您将需要配置 apache 来为您的项目提供服务器。
在尝试直接在云中部署应用程序之前,您可以使用本机安装程序并在本地计算机中测试部署。
我们还有一个成本估算工具。这只是为了对简单的 EC2/EBS 设置有一个粗略的了解,亚马逊并不总是像您预期的那么贵,尽管它取决于很多因素。 (尽管根据您的评论,您似乎已经考虑过成本)。
Another option for you if you don't want to deal with setting up your server from scratch is to use BitNami Django Stack Amazon image. I'm a BitNami developer and worked on creating stacks for several Python applications. The BitNami Django Stack already includes MySQL 5.1, Apache 2.2 (with mod_wsgi) and Python with MarkDown, html5lib and python-openid installed. It also included django 1.3.
You will need to install Django Debug Toolbar, create the database and copy your files in /opt/bitnami/apps/django/django_projects and run the python manage.py commands. After that you will need to configure apache to server your project if you want to use in production (instead of the django server).
Before you try to deploy your application directly in the cloud you could use the native installers and test the deploy in your local machine.
We also have a cost estimation tool. This is just for getting a rough idea for a simple EC2/EBS setup, Amazon is not always as expensive as you can expect although it depends on a lot of factors. (Although per your comments it seems that you already took a look at the costs).
我在 AWS EC2 t2.micro 实例(AWS 免费套餐)上托管了自己的 django 网站。我在这个项目中使用了 Django 1.9,并使用 MySQL 作为数据库。
创建通往您的实例的 SSH 隧道并按照以下步骤操作:
在您的实例上安装 apache2 和 libapache2-mod-wsgi:
sudo apt-get install apache2 libapache2-mod-wsgi
在您的实例上安装 django :
sudo pip install django
安装 mysql:
sudo apt-get install mysqldb
sudo pip install mysql-python
sudo apt-get install libmysqlclient-dev
(如果您没有安装 pip :
sudo apt-get install python-pip
)配置 mysql,为您的Django 项目。将您的 django 项目导入到
/var/www/html
。 (使用 git 是最好的方法)。编辑
/etc/apache2/sites-available/000-default.conf
:运行迁移到同步数据库:
python manage.py migrate
重新启动 apache2:
sudo service apache2 reload
我希望你没有在settings.py中硬编码你的模板和静态路径,如果是,则将其更改为动态路径,否则进行相应的编辑。
就是这样!
访问您的公共 IP 或 DNS 以访问托管在 AWS EC2 实例上的 Django 网站。
如果您遇到任何错误,请在下面评论。
I have hosted my own django website on AWS EC2 t2.micro instance (AWS free tier). I used Django 1.9 for this project and MySQL as database.
Make SSH tunnel to your instance and follow the steps:
Install apache2 and libapache2-mod-wsgi on your instance:
sudo apt-get install apache2 libapache2-mod-wsgi
Install django on your instance :
sudo pip install django
Install mysql:
sudo apt-get install mysqldb
sudo pip install mysql-python
sudo apt-get install libmysqlclient-dev
(if you don't have pip installed :
sudo apt-get install python-pip
)Configure mysql, for your django project. Import your django project to
/var/www/html
. (using git is the best way).Edit
/etc/apache2/sites-available/000-default.conf
:Run migrate to sync db:
python manage.py migrate
Restart apache2:
sudo service apache2 reload
I hope you have not hard-coded your template and static paths in settings.py, if yes then change it to dynamic path, or else edit it accordingly.
That's it!
Visit your public IP or DNS to access your Django Website hosted on AWS EC2 instance.
Please comment below if you get any error.
假设您在实例上使用 Apache 服务器,Django 网站上的官方说明比许多博客文章效果更好。这是我从链接中复制的内容:
https://docs.djangoproject.com/en/1.5/howto/编辑
以下代码并将其添加到 /etc/apache2/apache2.conf 即可。
Assume you are using Apache server on your instance, the official instruction on Django site works better than a lot of blog posts. Here is what I copied from the link:
https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/modwsgi/
Edit and add the following code to /etc/apache2/apache2.conf would work out.