如何在 Amazon EC2 托管上设置 Django 网站?

发布于 2024-08-09 09:27:49 字数 375 浏览 5 评论 0原文

有人可以给出如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绮筵 2024-08-16 09:27:49

这当然是可能的,但听起来 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

自控 2024-08-16 09:27:49

如果您不想从头开始设置服务器,另一个选择是使用 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).

梦过后 2024-08-16 09:27:49

我在 AWS EC2 t2.micro 实例(AWS 免费套餐)上托管了自己的 django 网站。我在这个项目中使用了 Django 1.9,并使用 MySQL 作为数据库。
创建通往您的实例的 SSH 隧道并按照以下步骤操作:

  1. 在您的实例上安装 apache2 和 libapache2-mod-wsgi:

    sudo apt-get install apache2 libapache2-mod-wsgi

  2. 在您的实例上安装 django :

    sudo pip install django

  3. 安装 mysql:

    sudo apt-get install mysqldb

    sudo pip install mysql-python

    sudo apt-get install libmysqlclient-dev

(如果您没有安装 pip :sudo apt-get install python-pip

  1. 配置 mysql,为您的Django 项目。将您的 django 项目导入到 /var/www/html。 (使用 git 是最好的方法)。

  2. 编辑/etc/apache2/sites-available/000-default.conf

    <虚拟主机 *:80>
        别名 /static /path_to_my_static_folder
    
        <目录/path_to_my_project_folder_container_wsgi.py>
            <文件 wsgi.py>
                要求全部授予
            
        
    
        WSGIDaemonProcess 项目名称 python-path=/path_to/lib/python2.7/site-packages
    
        WSGIProcessGroup 项目名称
        WSGIScriptAlias //path_to_wsgi.py
    
    
  3. 运行迁移到同步数据库:

    python manage.py migrate

  4. 重新启动 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:

  1. Install apache2 and libapache2-mod-wsgi on your instance:

    sudo apt-get install apache2 libapache2-mod-wsgi

  2. Install django on your instance :

    sudo pip install django

  3. 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)

  1. Configure mysql, for your django project. Import your django project to /var/www/html. (using git is the best way).

  2. Edit /etc/apache2/sites-available/000-default.conf:

    <VirtualHost *:80>
        Alias /static /path_to_my_static_folder
    
        <Directory /path_to_my_project_folder_containing_wsgi.py>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        WSGIDaemonProcess project_name python-path=/path_to/lib/python2.7/site-packages
    
        WSGIProcessGroup project_name
        WSGIScriptAlias / /path_to_wsgi.py
    </VirtualHost>
    
  3. Run migrate to sync db:

    python manage.py migrate

  4. 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.

触ぅ动初心 2024-08-16 09:27:49

假设您在实例上使用 Apache 服务器,Django 网站上的官方说明比许多博客文章效果更好。这是我从链接中复制的内容:
https://docs.djangoproject.com/en/1.5/howto/编辑

以下代码并将其添加到 /etc/apache2/apache2.conf 即可。

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>

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.

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文