如何在AWS上设置Python开发环境?
在AWS上设置Python开发环境,您需要安装Python、pip、virtualenv、awswebcli和SSH客户端。您可以按照以下说明安装: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html
安装完成后,您需要设置一个虚拟环境,以避免全局包受到污染。使用以下命令设置虚拟环境:
$ virtualenv -p python2.7 /tmp/hello-world
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /tmp/hello-world/bin/python2.7
Also creating executable in /tmp/hello-world/bin/python
Installing setuptools, pip...done.
虚拟环境准备完成后,通过运行环境的bin目录中的activate脚本启动它。例如,要启动上一步骤中创建的hello-world环境,可以键入以下命令:
$ . /tmp/hello-world/bin/activate
虚拟环境创建后,您可以随时通过再次运行其activate脚本来重新启动虚拟环境。
要为部署配置Python应用程序,请在虚拟环境内返回到项目的目录树顶部,并创建一个requirements.txt文件,其中包含您的应用程序的需求(您正在导入的第三方模块)及其版本号(如果需要最新的一个,则不需要写版本号)。例如:
Flask==0.8
Jinja2==2.6
Werkzeug==0.8.3
certifi==0.0.8
chardet==1.0.1 :
...
或者,您可以使用pip将所有已安装的软件包从计算机复制到requirements.txt文件中,方法如下:
$ pip freeze >requirements.txt
这将使AWS使用与您用于开发和测试应用程序的相同软件包和版本来复制应用程序的Python环境。
现在使用’eb init’命令配置AWS EB CLI存储库。
$ eb init -p python2.7 hello-world
应用程序hello-world已创建。
此命令创建了一个名为hello-world的新应用程序,并配置了您的本地存储库,以创建具有最新的Python 2.7平台配置的环境。再次运行eb init以配置默认密钥,以便您可以通过SSH连接到运行应用程序的EC2实例。
$ eb init
Do you want to set up SSH for your instances?
(y/n): y
Select a keypair.
1) my-keypair
2) [ Create new KeyPair ]
如果您已经有一个密钥对,可以选择一个密钥对,或按照提示创建一个新密钥对。如果没有看到提示或需要稍后更改设置,请运行eb init -i。使用eb create创建一个环境并将应用程序部署到其中:
$ eb create hello-env
此命令创建了一个负载均衡的Elastic Beanstalk环境,名称为hello-env。
如果遇到任何问题,可以在此处查看更详细的文档: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-configure-for-eb
阅读更多:Python 教程
极客教程