简体   繁体   English

在AWS Elastic Beanstalk上部署Python

[英]Deploying Python on AWS Elastic Beanstalk

I'm coming from a php background and now I'd like to host a Python web application on AWS but have a few questions about it. 我来自php背景,现在我想在AWS上托管一个Python Web应用程序但是有一些问题。

  1. Do I need to use a framework like Django/Flask? 我需要使用像Django / Flask这样的框架吗? Is there anyway I can use core Python to do the same? 反正我是否可以使用核心Python来做同样的事情? Back in php, although I tried learning Laravel in the middle, I was able to just use core php with apache and host up my website. 回到php,虽然我尝试在中间学习Laravel,但我能够使用带有apache的核心php并托管我的网站。
  2. Tried deploying my Python code on my AWS but got an error that application.py was missing. 尝试在我的AWS上部署我的Python代码,但收到了缺少application.py的错误。 I'd like to know, what exactly is application.py and what is it supposed to contain? 我想知道,究竟什么是application.py以及它应该包含什么? Similarly, what's wsgi.py I see here and there. 同样,我在这里和那里看到的是什么wsgi.py。 Are these actual files I need to create for the web app to be hosted? 我需要为托管的Web应用程序创建这些实际文件吗? Is there some specific code that has to go into them or are the pre-created files by frameworks like Django, etc? 是否有一些特定的代码必须进入它们或者是由Django等框架预先创建的文件? Because I could hardly find too much information on them online. 因为我很难在网上找到太多关于它们的信息。

I had recently tried following this tutorial from the AWS official site but to no luck. 我最近尝试从AWS官方网站上学习教程,但没有运气。

The reason for being reluctant to use Django is the shortage of time to learn it. 不愿意使用Django的原因是缺乏学习它的时间。 But if it were to make the task of hosting a Python web app easier, I would definitely look at it. 但如果要让托管Python Web应用程序的任务更容易,我肯定会看到它。 And how is the version of Python set? 那Python的版本是如何设置的? Because the Python codes I've written use the python3 libraries for BeautifulSoup and urllib. 因为我编写的Python代码使用了BeautifulSoup和urllib的python3库。

I have read a lot of articles on the web but the first thing I get on searching for Python on the web or with AWS, is Django or Flask or something. 我在网上看了很多文章,但我在网上或使用AWS搜索Python的第一件事就是Django或Flask等等。 How exactly does it work? 它究竟是如何工作的? When it came to php, it was simple copying the files into the /www/ folder of the server machine and I could access the website via it's url. 当谈到php时,将文件复制到服务器机器的/ www /文件夹中很简单,我可以通过它的URL访问该网站。 Maybe I've read too many posts to put them all together so could someone please set it straight for me? 也许我已经阅读了太多帖子将它们全部放在一起所以有人可以直接为我设置它吗? It would be greatly appreciated! 这将不胜感激!

Thanks a lot! 非常感谢!

You will waste a lot more time trying to write a WSGI application yourself from scratch. 您将浪费更多时间尝试从头开始编写WSGI应用程序。 Use a framework, it will save you a lot of time. 使用框架,它将为您节省大量时间。

PHP is very different to WSGI and WSGI sits well below the level of functionality that PHP provides out of the box. PHP与WSGI非常不同,WSGI远低于PHP提供的开箱即用功能。 PHP is more like what frameworks in Python provide. PHP更像是Python提供的框架。 So go learn a framework. 所以去学习一个框架。 If Django seems too complicated, try Flask first. 如果Django看起来太复杂,请首先尝试Flask。

Also don't try and do it on AWS from the outset, learn the frameworks by using the development servers they provide on your own box. 另外,请不要尝试从一开始就在AWS上执行此操作,通过使用他们在您自己的盒子上提供的开发服务器来学习框架。 Just work through their respective tutorials. 只需完成各自的教程。

  1. Actually AWS Elastic Beanstalk give you pure Python (2.6/2.7/3.4) with Apache + mod_wsgi as web proxy. 实际上,AWS Elastic Beanstalk为您提供纯Python(2.6 / 2.7 / 3.4),Apache + mod_wsgi作为Web代理。 You can look all supported Python environment in here . 您可以在此处查看所有受支持的Python环境。 So, you can use any Python web framework (such as Django, Flask, etc.) in your web app. 因此,您可以在Web应用程序中使用任何Python Web框架(例如Django,Flask等)。 If you can, choose common and supported framework by AWS (Django/Flask). 如果可以,请选择AWS(Django / Flask)的通用和受支持的框架。

    You can think: Python + Apache + mod_wsgi is equivalent to PHP + Apache + mod_php . 您可以认为: Python + Apache + mod_wsgi等同于PHP + Apache + mod_php

  2. Please take a look into AWS Elastic Beanstalk documentation for how to working with Python here . 在此处查看有关如何使用Python的AWS Elastic Beanstalk文档。 Read the Deploying a Django Application or Deploying a Flask Application if you choose one of them. 如果您选择其中一个,请阅读部署Django应用程序部署Flask应用程序 You need to provide what Elastic Beanstalk environment (mod_wsgi) needs. 您需要提供Elastic Beanstalk环境(mod_wsgi)所需的内容。

Same as PHP, Python actually only copy and paste the files. 与PHP相同,Python实际上只复制和粘贴文件。 If you want to make Python web app without framework, you need to follow the WSGI standard. 如果您想在没有框架的情况下制作Python Web应用程序,则需要遵循WSGI标准。 You can take look into this question . 你可以看一下这个问题 In my opinion, better if you use a framework, because it handles the WSGI part for you. 在我看来,如果你使用框架会更好,因为它会为你处理WSGI部分。

First of all this is a good blog post to start from if you are using Django 首先,如果您使用Django, 是一个很好的博客文章

  1. I don't know much about Flask, with Django once you understand the core concepts it's not hard at all. 我不太了解Flask,一旦你理解了核心概念,Django根本不是很难。

  2. application.py is the file that aws looks for as stated in the blog post I pointed to: By default eb assumes our wsgi file is called application.py this can be changed to your local wcgi.py file that Django makes when you start your project with django. application.py是aws查找的文件,如我在上面指出的博客文章中所述:默认情况下,eb假设我们的wsgi文件名为application.py,这可以更改为Django在您启动时创建的本地wcgi.py文件与django项目。

  3. Beware that you want to use your static url correctly so aws will read them from the right folder. 请注意,您要正确使用静态URL,因此aws将从正确的文件夹中读取它们。 I personally disagree about the way the static files configuration in the post. 我个人不同意帖子中静态文件配置的方式。 It's better to stay with the aws default which is "static" and just set static url in django settings to "/static/" 最好保持aws默认值为“static”,只需将django设置中的静态URL设置为“/ static /”

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

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