Ubuntu

Ubuntu 是一个基于Debian的Linux发行版,常用于Python开发和web应用部署。

Official Ubuntu logo. Copyright Canonical Ltd.

为什么Python对Ubuntu很重要?

无论对于本地开发还是服务器部署,Ubuntu都是最常用的Linux发行版。有些PaaS比如Heroku 将Ubuntu作为底层操作系统,因此Python开发者很有可能会跟Ubuntu打交道。

Ubuntu LTS是什么意思?

每隔2两Ubuntu会发布一个Long-Term Support (LTS),提供5年的升级支持,非LTS只提供2年的升级支持。但是,LTS的问题是,你只能通过官方的仓库升级安全包must only use packages from the main repository,除非你打算自己手动升级安全包。

Ubuntu资源

操作系统

为什么操作系统重要?

操作系统帮我们调度计算机的硬件资源,让我们读写文件、建立网络连接、同时运行多个程序,没有操作系统,我们需要直接操作底层资源以访问CPU,内存,网络及各种硬件组件。

如果没有现有的操作系统像Linux,Mac OS X和Windows,你就不得不自己写一个操作系统来运行你的web应用。

幸运的是,开源社区提供了Linux作为Python web应用的操作系统。

推荐的操作系统

Python web部署推荐的操作系统是LinuxFreeBSD。Linux有多种发行版可以作为生产服务器。Ubuntu长期支持版Long Term Support (LTS),红帽企业版Red Hat Enterprise Linux,以及CentOS都可以作为选项。

Mac OS X和Windows作为开发环境没问题,但是不适合作为生产系统,除非你有特殊理由为什么不能使用Linux。

Ubuntu

Ubuntu是Canonical公司出品的Linux发行版。Ubuntu使用Debian发行版作为基础,包含了aptitude package manager包管理器。桌面版的Ubuntu使用GNOME或Unity作为图形用户界面。

Ubuntu长期支持版Long Term Support (LTS)建议作为部署服务器。LTS版有5年的维护周期。每隔两年,Canonical公司会发布一个新的LTS版本。到2016年4月,16.04 Xenial Xerus是最新的Ubuntu长期支持版。Xenial Xerus包含了Python 3.5作为默认Python版本,该版本是Python 2.7之后的一个主要升级。

Ubuntu Python包

有多种Linux系统上运行的Python包ptitude

Red Hat 和 CentOS

Red Hat Enterprise Linux (RHEL) 和 Community ENTerprise Operating System (CentOS) 是同一个发行版,主要区别是CentOS是开源软件,是RHEL的免费版。

RHEL 和 CentOS 使用不同的包管理器和命令行界面:RPM包管理器和YUM。RPM有一个.rpm后缀,YUM提供了命令行界面与RPM系统交互。

操作系统资源

  • Linux Performance
  • Lifehacker’s guide to choosing a Linux distro.
  • Linux Journey is a really well designed curriculum for learning Linux basics such as the command line, package management, text handling. There are also courses for more advanced topics such as how the kernel works, setting up logging and device management.
  • The Ops School curriculum is a comprehensive resource for learning about Linux fundamentals and how to perform the work that system administrators typically handle.
  • Since Linux is your go-to production operating system, it’s important to get comfortable with the Unix/Linux commands and philosophy. Study up on this introduction to Unix tutorial to become more familiar with the operating system.
  • First 5 Minutes on a Server shows the first several security steps that should be done manually or automatically on any server you stand up.
  • Digital Ocean has a detailed walkthrough for setting up Python web applications on Ubuntu.
  • linux-internals is a series of posts about how Linux works under the covers, starting from the low level booting process.
  • While not quite necessary to run your Python application, if you want to dig into how operating systems are built, check out this free book How to Make a Computer Operating System, which was originally written by a high school student and later updated as he became a professional software developer.

操作系统学习清单

  1. 选一个基于Debian的Linux发行版比如Ubuntu或基于Fedora的发行并比如CentOS。
  2. 通过一些步骤加强安全。安装基本的安全包,比如fail2ban 和 unattended-upgrades。用sudo创建一个新的用户账号,并禁止root登录。禁止仅凭密码登录,使用一个公私秘钥对代替。阅读关系系统安全方面的更多资料。
  3. 安装Python相关包,制作Python应用运行环境。安装哪些包取决于你选择的Linux发行版。

平台即服务 Platform-as-a-service

平台即服务 (PaaS)提供了基础设施以及一个软件层,让web应用部署其上。在Paas上运行web应用,你就无需知道底层的服务器,操作系统,web服务器,WSGI服务器等。

PaaS定义了应用如何访问计算时间、文件、外部服务等资源,提供了相对IaaS更加高层次的抽象。

PaaS让部署变得更容易。比如,Heroku会在仓库的根目录下查找Python的requirements.txt文件。

Traditional LAMP server stack versus a Platform-as-a-Service stack

如果使用PaaS,你就无需考虑配置操作系统、web服务器,PaaS会提供WSGI服务器层作为你工作的起点。

PaaS的责任 responsibilities

要知道你所用的PaaS使用什么操作系统会有所帮助。比如Heroku使用Ubuntu 10.04。部署人员有责任确保web应用的安全。

支持Python的PaaS

PaaS资源

PaaS学习清单

  1. Review the potential Python platform-as-a-service options listed above.
  2. Sign up for a PaaS account at the provider that appears to best fit your application needs. Heroku is the PaaS option recommended for starters due to their detailed documentation and walkthroughs available on the web. However, the other options are also viable since their purpose is to make deploying applications as easy as possible.
  3. Check if there are any PaaS-specific configuration files needed for your app to run properly on the PaaS after it is deployed.
  4. Deploy your app to the PaaS.
  5. Sync your application’s configuration with the database.
  6. Set up a content delivery network for your application’s static content unless your PaaS provider already handles this deployment step for you.
  7. Check if the application’s functionality is working and tweak as necessary.

静态内容

网站上的静态内容不会变动,因此可以直接通过服务器或CDN部署。静态内容包括图像,JavaScript和CSS文件。

静态内容类型

静态内容可以死你部署时产生的数据,比如你的登录页的图片,或者用户生成数据。Django框架把这两类内容分别称为资产assets和多媒体media。

内容分发网络

内容分发网络(CDN)是一个第三方的存储和托管静态内容的服务商。Amazon CloudFront, AkamaiRackspace Cloud Files都是CDN。CDN的目的是把对静态内容的请求从服务器剥离出来,让服务器专注于生成动态内容。比如,你有一个Nginx服务器,运行在512M内存的虚拟服务器上,处理静态文件,同时作为一个WSGI服务器的前端。当流量大时会导致服务性能瓶颈。CDN能把所有的静态文件访问请求接管过来,让Nginx服务器只处理针对Green Unicorn WSGI服务器的请求。

CDNs send content responses from data centers with the closest proximity to the requester.

静态内容资源

静态内容学习清单

  1. Identify a content delivery network to offload serving static content files from your local web server. I recommend using Amazon S3 with CloudFront as it’s easy to set up and will scale to high bandwidth demands.
  2. Update your web application deployment process so updated static files are uploaded to the CDN.
  3. Move static content serving from the www subdomain to a static (or similarly named) subdomain so browsers will load static content in parallel to www HTTP requests.

服务器 Servers

服务器是运行web应用的实体基础设施。

为什么服务器必须?

服务器必须一周7天,一天24小时可访问。托管你的web应用的服务器被生成生产服务器。生产服务器保存真实的数据(而不是测试数据),并通过安全措施禁止非授权访问。

裸机服务器 Bare metal servers

购买硬件,通过ISP把服务器连上网。裸机服务器能让你最大程度的控制服务器的配置。成本包括支付托管的费用、电费、机器后续的维修费用。比如从Dell或HP买服务器(小型刀片式)。

虚拟服务器

虚拟服务器是在裸机服务器上分割虚拟出来的。虚拟化软件比如XenVMWare允许供应商比如Linodeprgmr提供一台真实服务器的部分做虚拟服务器。比如,一个真实服务器,8核Xeon处理器,16G内存,能被分割成8块,每块1核,2G内存。

虚拟服务器的主要缺点是虚拟化过程中的资源虚耗。此外,物理限制比如一个虚拟实例的高I/O操作会导致该物理服务器上其他虚拟服务器的性能瓶颈。

虚拟服务器资源

基础设施即服务Infrastructure-as-a-service

基础设施即服务Infrastructure-as-a-service (IaaS) 跟虚拟服务器有类似之处。计算资源能以类似的方式提供给用户。Iaas与虚拟服务器的区别是付款方式,Iaas可以按天、按小时、按流量付费,而虚拟服务器通常要按月、按年付费。

IaaS能跟虚拟服务器结合起来应对可伸缩的流量。当流量小的时候,只使用虚拟服务器。

最常用的Iaas是亚马逊云服务Amazon Web ServicesRackspace Cloud

Iaas平台的缺点是如果你需要自己写定制化的代码去部署、动态伸缩、了解你自己的平台。每一个平台都有自己的问题,比如亚马逊的标准Elastic Block Store存储基础设施比你本地的磁盘存储慢了一阶。你的应用在你本地查询数据库可能很好,当你部署到亚马逊上,性能就不如意。亚马逊有吞吐性能更好的higher throughput EBS instances但是你得多花钱。EBS只是你把应用部署到Iaas上之前需要了解的一个情况,其他缺陷还有很多。

IaaS资源

服务器学习清单

  1. Sign up for a hosting provider. I recommend getting a Linode VPS to set up your initial infrastructure and deploy your web application there. Digital Ocean and prgrmr are other VPS options. You can change hosting providers later after the deployment process is automated.
  2. Provision your first server. It will be ready but in a shutdown state while awaiting your instructions.
  3. Move to the operating systems section to learn how to load Ubuntu 14.04 LTS as a base OS for Python web applications.

部署 Deployment

部署包括了把你的web应用打包安装到生产环境并让它跑起来。

为什么部署是必须?

你的web应用要放到生产环境,而不是你的台式机或笔记本里。

部署图

Python web应用部署有许多步骤需要处理。下图显示了每一步与其余步骤如何联系。

Full Stack Python site map.

选择部署的主机

主机的选项一般有4种:

  1. “Bare metal” servers
  2. Virtualized servers
  3. Infrastructure-as-a-service
  4. Platform-as-a-service

前3种比较类似。部署人员需要找一个Linux发行版本,安装数据库,Python环境。最后把应用安装到主机。environment.

部署资源

  • If you need a step-by-step guide to deploying a Python web application, I wrote a whole book on exactly this topic called The Full Stack Python Guide to Deployments that you’ll find super helpful.
  • Deploying Python web applications is an episode of the great Talk Python to Me podcast series where I discuss deploying web applications based on a fairly traditional virtual private server, Nginx and Green Unicorn stack.
  • Thoughts on web application deployment walks through stages of deployment with source control, planning, continuous deployment and monitoring the results.
  • Deploying Software is a long must-read for understanding how to deploy software properly.
  • Practical continuous deployment defines delivery versus deployment and walks through a continuous deployment workflow.
  • In this free video by Neal Ford, he talks about engineering practices for continuous delivery. He explains the difference between continuous integration, continuous deployment and continuous delivery. Highly recommended for an overview of deployment concepts and as an introduction to the other videos on those subjects in that series.
  • Continuous deployment at Instagram is the story of how their deployment process evolved over time from a large Fabric script to continuous deployments. Along the way they encountered issues with code reviews, test failures, canary builds and rollbacks. It’s a great read that sheds some light on how Python deployments can be done well at large scale.
  • Stack Overflow’s guide on how they do deployment is an awesome in-depth read covering topics ranging from git branching to database migrations.
  • If you’re using Flask this detailed post on deploying it to Ubuntu is a great way to familiarize yourself with the deployment process.

部署学习清单

  1. If you’re tight on time look at the platform-as-a-service (PaaS) options. You can deploy a low traffic project web app for free or low cost. You won’t have to worry about setting up the operating system and web server compared to going the traditional server route. In theory you should be able to get your application live on the web sooner with PaaS hosting.
  2. Traditional server options are your best bet for learning how the entire Python web stack works. You’ll often save money with a virtual private server instead of a platform-as-a-service as you scale up.
  3. Read about servers, operating systems, web servers and WSGI servers to get a broad picture of what components need to be set up to run a Python web application.