Chapter 17. Building on the Basics (第 17 章 夯实基础)

The chapters in this book have covered the fundamental components of a Linux system, from low-level kernel and process organization, to networking, to some of the tools used to build software. With all of that behind you, what can you do now? Quite a lot, as it turns out! Because Linux supports nearly every kind of non proprietary programming environment, it’s only natural that a plethora of applications is available. Let’s look at a few application areas where Linux excels and see how what you’ve learned in this book relates.

这本书的章节涵盖了Linux系统的基本组成部分,从底层的内核和进程组织,到网络,再到一些用于构建软件的工具。

现在,你可以做些什么呢?事实证明,你可以做很多!因为Linux支持几乎所有非专有编程环境,所以自然而然地有大量的应用程序可供使用。

让我们来看看Linux在一些应用领域的优势,并了解一下你在本书中学到的知识如何与之相关。

17.1 Web Servers and Applications(网络服务器和应用程序)

Linux is a popular operating system for web servers, and the reigning monarch of Linux application servers is the Apache HTTP Server (usually referred to as just “Apache”). Another web server that you’ll often hear about is Tomcat (also an Apache project), which provides support for Java-based applications.

Linux是一个流行的用于Web服务器的操作系统,而Linux应用服务器的统治者是Apache HTTP服务器(通常简称为“Apache”)。

另一个经常听到的Web服务器是Tomcat(也是一个Apache项目),它提供了对基于Java的应用程序的支持。

By themselves, web servers don’t do much—they can serve files, but that’s about it. The end goal of most web servers such as Apache is to provide an underlying platform to serve web applications. For example, Wikipedia is built on the MediaWiki package, which you can use to set up your own wiki. Content management systems like Wordpress and Drupal let you build your own blogs and media sites. All of these applications are built on programming languages that run especially well on Linux. For example, MediaWiki, Wordpress, and Drupal are all written in PHP.

单独的Web服务器并不能做很多事情——它们只能提供文件服务,仅此而已。

大多数Web服务器(如Apache)的最终目标是提供一个基础平台来提供Web应用程序。

例如,维基百科是建立在MediaWiki软件包上的,您可以使用它来搭建自己的维基。

像WordPress和Drupal这样的内容管理系统可以让您建立自己的博客和媒体网站。

所有这些应用程序都是基于在Linux上运行特别好的编程语言构建的。

例如,MediaWiki、WordPress和Drupal都是用PHP编写的。

The building blocks that make up web applications are highly modular, so it’s easy to add your own extensions and create applications with frameworks such as Django, Flask, and Rails, which offer facilities for common web infrastructure and features, such as templates, multiple users, and database support.

构成Web应用程序的构建模块非常模块化,因此很容易添加自己的扩展,并使用Django、Flask和Rails等框架创建具有常见Web基础设施和功能(如模板、多用户和数据库支持)的应用程序。

A well-functioning web server depends on a solid operating system foundation. In particular, the material in Chapter 8 through Chapter 10 is particularly important. Your network configuration must be flawless, but perhaps more importantly, you must understand resource management. Adequately-sized, efficient memory and disk are critical, especially if you plan to use a database in your application.

良好运行的Web服务器依赖于坚实的操作系统基础。

特别是,第8章到第10章的内容尤为重要。

您的网络配置必须完美无缺,但更重要的是,您必须了解资源管理。

充足大小、高效的内存和磁盘至关重要,尤其是如果您计划在应用程序中使用数据库。

17.2 Databases(数据库)

Databases are specialized services for storing and retrieving data, and many different database servers and systems run on Linux. Two primary features of databases make them attractive: They offer easy, uniform ways to manage individual pieces and groups of data, and superior access performance.

数据库是专门用于存储和检索数据的服务,许多不同的数据库服务器和系统在Linux上运行。

数据库的两个主要特点使其具有吸引力:它们提供了管理单个数据和数据组的简单、统一的方式,并具有优越的访问性能。

Databases make it easier for applications to examine and alter data, especially when compared with parsing and changing text files. For example, the /etc/passwd and /etc/shadow files on a Linux system can become difficult to maintain over a network of machines. Instead, you can set up a database that offers user information LDAP (Lightweight Directory Access Protocol) to feed this information into the Linux authentication system. The configuration on the Linux client side is easy; all you need to do is edit the /etc/ nsswitch.conf file and add a little extra configuration.

与解析和更改文本文件相比,数据库使应用程序更容易地检查和修改数据。

例如,在Linux系统上,/etc/passwd和/etc/shadow文件在机器网络上变得难以维护。

相反,您可以设置一个数据库,提供用户信息LDAP(轻量级目录访问协议),将此信息输入Linux身份验证系统。

在Linux客户端方面的配置很简单;您只需要编辑/etc/nsswitch.conf文件并添加一些额外的配置即可。

The primary reason that databases generally offer superior performance when retrieving data is that they use indexing to keep track of data locations. For example, say you have a set of data representing a directory containing first and last names and telephone numbers. You can use a database to place an index on any of these attributes, like the last name. Then, when looking up a person by last name, the database simply consults the index for the last name rather than searching the entire directory.

数据库通常在检索数据时具有卓越性能的主要原因是它们使用索引来跟踪数据位置。

例如,假设您有一组数据表示包含名字、姓氏和电话号码的目录。

您可以使用数据库在任何这些属性上建立索引,比如姓氏。

然后,当按照姓氏查找一个人时,数据库只需查询姓氏索引,而不是搜索整个目录。

17.2.1 Types of Databases(数据库类型

Databases come in two basic forms: relational and non-relational. Relational databases (also called Relational Database Management Systems, or RDBMS), such as MySQL, PostgreSQL, Oracle, and MariaDB, are general-purpose databases that excel in tying different sets of data together. For example, say you have two sets of data, one with postal (ZIP) codes and names, and another with the postal codes and their corresponding states. A relational database would allow you to very quickly retrieve all of the names located in a particular state. You normally talk to relational databases using a programming language called SQL (Structured Query Language).

数据库有两种基本形式:关系型和非关系型。

关系型数据库(也称为关系数据库管理系统,或RDBMS),如MySQL、PostgreSQL、Oracle和MariaDB,是通用数据库,擅长将不同的数据集合在一起。

例如,假设您有两组数据,一组是邮政编码和姓名,另一组是邮政编码及其对应的州。

关系型数据库可以让您非常快速地检索出位于特定州的所有姓名。

通常,您使用一种称为SQL(结构化查询语言)的编程语言与关系型数据库进行交互。

Non-relational databases, sometimes known as NoSQL databases, tend to solve particular problems that relational databases don’t easily handle. For example, document-store databases, such as MongoDB, attempt to make storing and indexing entire documents easier. Key-value databases, such as redis, tend to focus on performance. NoSQL databases don’t have a common query language like SQL for access. Instead, you’ll talk to them using a variety of interfaces and commands.

非关系型数据库,有时也称为NoSQL数据库,倾向于解决关系型数据库难以处理的特定问题。

例如,文档存储数据库(如MongoDB)试图使存储和索引整个文档变得更加容易。

键值数据库(如Redis)则更注重性能。

NoSQL数据库没有像SQL那样的通用查询语言来进行访问。相反,您将使用各种接口和命令与它们进行交互。

The disk and memory performance issues discussed in Chapter 8 are extremely important in most database implementations because there’s a trade-off between how much you can store in RAM (which is fast) versus on disk. Most larger database systems also involve significant networking because they’re distributed over many servers. The most common such network setup is called replication, where one database is basically copied to a number of database servers to increase the number of clients that connect to the servers.

第8章中讨论的磁盘和内存性能问题在大多数数据库实现中非常重要,因为在RAM(快速)和磁盘上存储的数据之间存在权衡。

大多数较大的数据库系统还涉及到重要的网络因素,因为它们分布在许多服务器上。

最常见的网络设置称为复制,其中一个数据库基本上被复制到多个数据库服务器上以增加连接到服务器的客户端数量。

17.3 Virtualization(虚拟化)

In most large organizations, it’s inefficient to dedicate hardware to specific server tasks because installing an operating system tailored to one task on one server means that you’re limited to that task until you reinstall it. Virtual machine technology makes it possible to simultaneously install one or more operating systems (often called guests) on a single piece of hardware, and then activate and deactivate the systems at will. You can even move and copy the virtual machines to other machines.

在大多数大型组织中,为特定的服务器任务分配硬件是低效的,因为在一个服务器上安装一个专为某个任务定制的操作系统意味着你只能限制在该任务上,直到重新安装它。

虚拟机技术使得在一台硬件上同时安装一个或多个操作系统(通常称为客户机)成为可能,然后可以随意激活和停用这些系统。

你甚至可以将虚拟机移动和复制到其他机器上。

There are many virtualization systems for Linux, such as the kernel’s KVM (kernel virtual machine) and Xen. Virtual machines are especially handy for web servers and database servers. Although it’s possible to set up a single Apache server to serve several websites, this comes at a cost of flexibility and maintainability. If those sites are all run by different users, you have to manage the servers and the users together. Instead, it’s usually preferable to set up virtual machines on one physical server with their own supporting users, so that they don’t interfere with each other and you can alter and move them at will.

Linux有许多虚拟化系统,如内核的KVM(内核虚拟机)和Xen。

虚拟机对于Web服务器和数据库服务器特别方便。

虽然可以设置一个单独的Apache服务器来为多个网站提供服务,但这会带来灵活性和可维护性的成本。

如果这些站点都由不同的用户运行,你必须同时管理服务器和用户。

相反,通常更好的选择是在一个物理服务器上设置虚拟机,每个虚拟机都有自己的支持用户,这样它们彼此不会干扰,你可以随意更改和移动它们。

The software that operates virtual machines is called a hypervisor. The hypervisor manipulates many pieces of the lower levels of a Linux system that you’ve seen in this book with the result that, if you install a Linux guest on a virtual machine, it should behave just like any other installed Linux system.

操作虚拟机的软件称为hypervisor。hypervisor操作Linux系统的许多低层组件,你在本书中已经见过,因此,如果在虚拟机上安装Linux客户机,它应该表现得就像任何其他已安装的Linux系统一样。

17.4 Distributed and On-Demand Computing(分布式计算和按需计算)

To ease local resource management, you can build sophisticated tools on top of virtual machine technology. The term cloud computing is a catch-all term that’s often used as label for this area. More specifically, infrastructure as a service (IaaS ) refers to systems that allow you to provision and control basic computing resources such as CPU, memory, storage, and networking on a remote server. The OpenStack project is one such API and platform that includes IaaS.

为了简化本地资源管理,您可以在虚拟机技术的基础上构建复杂的工具。

术语“云计算”是一个总称,经常用于描述这个领域。

更具体地说,基础设施即服务(IaaS)是指允许您在远程服务器上提供和控制基本计算资源,如CPU、内存、存储和网络的系统。

OpenStack项目是一个包括IaaS的API和平台。

Moving up past the raw infrastructure, you can also provision platform resources such as the operating system, database servers, and web servers. Systems that offer resources on this level are said to be platform as a service (PaaS).

在超越原始基础设施之后,您还可以提供平台资源,如操作系统、数据库服务器和Web服务器。

提供此级别资源的系统被称为平台即服务(PaaS)。

Linux is central to many of these computing services, as it’s often the underlying operating system behind all of it. Nearly all of the elements that you’ve seen in this book, starting with the kernel, are reflected throughout these systems.

Linux在许多这些计算服务中起着核心作用,因为它通常是所有这些系统背后的底层操作系统。

您在本书中看到的几乎所有元素,从内核开始,都在这些系统中得到体现。

17.5 Embedded Systems(嵌入式系统)

An embedded system is anything designed to serve a specific purpose, such as a music player, video streamer, or thermostat. Compare this to a desktop or server system that can handle many different kinds of tasks (but may not do one specific thing very well).

嵌入式系统是为特定目的而设计的系统,如音乐播放器、视频流媒体或恒温器。

与之相比,台式机或服务器系统可以处理许多不同类型的任务(但可能无法很好地完成某项特定任务)。

You can think of embedded systems as almost the opposite of distributed computing; rather than expanding the scale of the operating system, an embedded system usually (but not always) shrinks it, often into a small device. Android is perhaps the most widespread embedded version of Linux in use today.

你可以把嵌入式系统看作与分布式计算几乎相反的系统;

嵌入式系统通常(但不总是)不是扩大操作系统的规模,而是将其缩小,通常缩小到一个小型设备中。

安卓可能是目前使用最广泛的嵌入式 Linux 版本。

Embedded systems often combine specialized hardware with software. For example, you can set up a PC to do anything a wireless router can by adding enough network hardware and correctly configuring a Linux installation. But it’s usually preferable to buy a smaller, dedicated device consisting of the necessary hardware and eliminate any hardware that isn’t necessary. For example, a router needs more network ports than most desktops but doesn’t need video or sound hardware. And once you have custom hardware, you must tailor the system’s software, such as the operating system internals and user interface. OpenWRT, mentioned in Chapter 9, is one such customized Linux distribution.

嵌入式系统通常结合了专用硬件和软件。

例如,你可以通过添加足够的网络硬件并正确配置Linux安装来使PC执行无线路由器的任何功能。

但通常更倾向于购买一个更小、专用的设备,其中包含必要的硬件,并消除任何不必要的硬件。

例如,路由器需要比大多数桌面设备更多的网络端口,但不需要视频或声音硬件。

一旦你拥有了定制的硬件,你必须调整系统的软件,比如操作系统内部和用户界面。

OpenWRT就是这样一个定制的Linux发行版,在第9章中有提到。

Interest in embedded systems is increasing as more capable small hardware is introduced, particularly system on-a-chip (SoC) designs that can cram a processor, memory, and peripheral interfaces into a small space. For example, the Raspberry Pi and BeagleBone single-board computers are based around such a design, with several Linux variants to choose from as an operating system. These devices have easily accessible output and sensor input that connects to language interfaces such as Python, making them popular for prototyping and small gadgets.

随着越来越多功能强大的小型硬件的推出,对嵌入式系统的兴趣不断增加,特别是能够将处理器、内存和外围接口装入一个小空间的片上系统(SoC)设计。

例如,树莓派和BeagleBone单板计算机就是基于这样的设计,有几个可供选择的Linux变体作为操作系统。

这些设备具有易于访问的输出和传感器输入,可以连接到Python等语言接口,因此在原型设计和小型小工具方面非常受欢迎。

Embedded versions of Linux vary in how many features from the server/ desktop version can be carried over. Small, very limited devices must strip out everything except the bare minimum because of lack of space, which often means that even the shell and core utilities come in the form of a single BusyBox executable. These systems tend to exhibit the most differences between a full-featured Linux installation, and you’ll often see older software on them, such as System V init.

嵌入式Linux的版本在多大程度上可以继承服务器/桌面版本的功能是不同的。

由于空间有限,小型、非常有限的设备必须剥离除了最基本的之外的所有东西,这通常意味着连shell和核心工具也以单个BusyBox可执行文件的形式存在。

这些系统往往在完整功能的Linux安装和它们之间存在最大的差异,你经常会看到其中运行着较旧的软件,比如System V init。

You’ll normally develop software for embedded devices using a regular desktop machine. More powerful devices, such as the Raspberry Pi, have the luxury of more storage and the power to run newer and more complete software, so you can even natively run many development tools on them.

开发嵌入式设备的软件通常是在普通的桌面机器上进行的。

像树莓派这样更强大的设备拥有更多的存储空间和运行更新、更完整的软件的能力,因此你甚至可以在它们上面本地运行许多开发工具。

Regardless of the differences, though, embedded devices still share the Linux genes described in this book: You’ll see a kernel, a bunch of devices, network interfaces, and an init alongside a bunch of user processes. Embedded kernels tend to be close (or identical) to regular kernel releases, simply with many features disabled. As you work your way up through user space, though, the differences become more pronounced.

不过,不管有何种差异,嵌入式设备仍然与本书中描述的Linux基因共享:你会看到一个内核、一堆设备、网络接口和一个init,以及一堆用户进程。

嵌入式内核往往与常规内核发布版本接近(或相同),只是禁用了许多功能。然而,当你进入用户空间时,差异变得更加明显。

17.6 Final Remarks(结束语)

Whatever your goals for gaining a better understanding of Linux systems, I hope that you’ve found this book to be helpful. My goal has been to instill you with confidence when you need to get inside your system to make changes or do something new. At this point, you should feel like you’re really in control of your system. Now go and push it around a little and have some fun.

无论你想更好地了解 Linux 系统的目标是什么,我都希望你能发现本书
有所帮助。

我的目标是在你需要进入你的系统进行修改或做一些新的事情时,给你灌输信心。

进行更改或做一些新的事情。到此为止,你应该感觉到自己真正掌控了系统。

现在就去推动它,享受一些乐趣吧。


Xander
198 声望51 粉丝