您的位置:首页 > 编程语言 > ASP

如何在5美元的Raspberry Pi上构建个人开发服务器

2020-08-19 19:32 1791 查看

In this article, you'll learn how to build a personal dev server by installing Git, Node.js, Rust, and Docker on a Raspberry Pi. The cheapest option costs just $5. You can get a starter kit ($25) for free here.

在本文中,您将学习如何通过在Raspberry Pi上安装Git,Node.js,Rust和Docker来构建个人开发服务器。 最便宜的选择仅需5美元。 您可以在此处免费获得入门工具包(25美元)

The Raspberry Pi is a very powerful computer in a tiny package. The cheapest option, the Raspberry Pi Zero, is capable of running a fully featured Linux distribution and driving a high definition display. It is the size of two coins (US Quarters) and costs $5.

Raspberry Pi是一款功能非常强大的计算机,采用小巧的包装。 最便宜的选项Raspberry Pi Zero可以运行功能齐全的Linux发行版并驱动高清显示。 它的大小为两个硬币(US Quarters),价格为5美元。

At $10, the Raspberry Pi Zero W comes with integrated WiFi and Bluetooth.

Raspberry Pi Zero W售价10美元,带有集成的WiFi和蓝牙。

At the “high end”, you can purchase a Raspberry Pi 4 desktop kit for less than $100. It has a 4-core ARM CPU running at 1.5GHz, GPU, 2GB (up to 8 GB) of RAM, 16GB (up to 2TB) of storage on MicroSD cards, wifi and Ethernet connectors, USB ports, HDMI ports that can drive 4K displays, as well as a keyboard and mouse.

在“高端”,您可以以不到100美元的价格购买Raspberry Pi 4桌面套件 。 它具有运行于1.5GHz的4核ARM CPU,GPU,2GB(最大8GB)的RAM,MicroSD卡,WiFi和以太网接口,USB端口,可驱动的HDMI端口的16GB(最大2TB)的存储空间。 4K显示器以及键盘和鼠标。

The Raspberry Pi is also more than a standard computer. It is fun and hackable. The Raspberry Pi exposes a row of GPIO (General Purpose Input Output) pins. You can attach simple sensors (eg. temperature, humidity, light) to those pins, and capture their data from your applications running on the Pi.

Raspberry Pi还不仅仅是标准计算机。 这很有趣而且很容易破解。 Raspberry Pi暴露了一行GPIO(通用输入输出)引脚。 您可以在这些引脚上连接简单的传感器(例如温度,湿度,光线),并从运行在Pi上的应用程序捕获其数据。

You could also attach LED lights and motors to those pins, and use your Pi application to drive those peripheral devices.

您还可以将LED灯和电机连接到这些引脚,然后使用Pi应用程序驱动那些外围设备。

For more complex sensors or devices, such as camera modules, you can also connect to the Pi via USB or Wifi, and access them in software. The Pi is a great device for learning and hardware hacking. Because of this, it is widely used in educational settings.

对于更复杂的传感器或设备(例如相机模块),您还可以通过USB或Wifi连接到Pi,并通过软件对其进行访问。 Pi是学习和硬件黑客的绝佳设备。 因此,它广泛用于教育环境。

However, fun and learning are not just for kids. With so much computing power and easy networking, the Raspberry Pi can easily become a personal application server for you.

但是,乐趣和学习不仅仅针对孩子。 Raspberry Pi具有强大的计算能力和便捷的联网功能,可以轻松地成为您的个人应用服务器。

For example, you can put a web application (for example, a collaborative note taking app, or just some documents / videos to share) on a Pi, bring it to a meeting, and make it accessible to everyone in the room. You do not even need the Internet. It is completely decentralized and censorship-resistant.

例如,您可以将Web应用程序(例如,协作笔记记录应用程序,或仅一些文档/视频共享)放在Pi上,将其带到会议上,并使房间中的每个人都可以访问它。 您甚至不需要互联网。 它完全是分散的,并且不受审查。

The personal server is especially useful for developers. You can have a separate environment to deploy and test your server-side applications without having to mess with your laptop. A personal dev server is like Docker on steroids. In this article, I will teach you how to set one up.

个人服务器对开发人员特别有用。 您可以有一个单独的环境来部署和测试服务器端应用程序,而不必麻烦您的笔记本电脑。 个人开发服务器就像类固醇上的Docker。 在本文中,我将教您如何进行设置。

首先,获得Raspberry Pi (First, get a Raspberry Pi)

If this is your first Raspberry Pi, the easiest (and most expensive) way to set up is just to buy a desktop kit for around $100. It comes with everything you need for a computer except for the display.

如果这是您的第一台Raspberry Pi,最简单(也是最昂贵)的设置方法就是以100美元左右的价格购买台式机套件 。 除了显示器外,它还提供了计算机所需的一切。

If you are using the Pi as a personal dev server, you would NOT need a display after the initial setup. You can just SSH into it from your laptop once it is turned on!

如果您将Pi用作个人开发服务器,则在初始设置后不需要显示。 一旦打开笔记本电脑,您就可以通过SSH将其登录!

Learn how to get your Raspberry Pi starter kit for free when you participate in this high performance web application learning exercise.

参加此高性能Web应用程序学习练习时, 了解如何免费获取Raspberry Pi入门套件。

Of course, if you have spare computer parts, such as MicroSD cards, USB power supply, a keyboard, and a mouse laying around, you could save money by purchasing only the boards. You could get a Raspberry Pi Zero board for $5 and a Raspberry Pi 4 board for $35.

当然,如果您有备用的计算机零件,例如MicroSD卡,USB电源,键盘和鼠标,那么仅购买板子就可以节省金钱。 您可以以5美元的价格获得Raspberry Pi Zero板,而以35美元的价格获得Raspberry Pi 4板。

But the thing missing from the board is a MicroSD card that acts as the “hard drive” for storing the operating system and data. You can purchase a 16GB MicroSD card for $10 online, a MicroSD card reader, and use the Raspberry Pi Imager to load an operating system onto the MicroSD card from your laptop.

但是主板上缺少的是MicroSD卡,它可以用作存储操作系统和数据的“硬盘”。 您可以在线购买10美元的16GB MicroSD卡,MicroSD读卡器,然后使用Raspberry Pi Imager从笔记本电脑上将操作系统加载到MicroSD卡上。

The two popular choices are Raspberry Pi OS and Ubuntu Linux. Both are Debian-based Linux distributions. Most starter kits pre-install the Raspberry Pi OS on their MicroSD cards (it is called NOOBS).

两种流行的选择是Raspberry Pi OS和Ubuntu Linux。 两者都是基于Debian的Linux发行版。 大多数入门工具包都在其MicroSD卡上预装了Raspberry Pi OS(称为NOOBS)。

In the next two sections, I will talk you through both operating systems.

在接下来的两节中,我将介绍两种操作系统。

如何设置Raspberry Pi OS (How to set up Raspberry Pi OS)

Once you put in the MicroSD card with NOOBS, and connect a display, a keyboard, and a mouse, you can turn on the power!

将带有NOOBS的MicroSD卡插入并连接显示器,键盘和鼠标后,就可以打开电源了!

From there, just follow on screen instructions to install Raspberry Pi OS (previously known as the Raspbian OS). Then setup a password for the user pi, and setup the wifi connection.

从那里开始,按照屏幕上的说明安装Raspberry Pi OS(以前称为Raspbian OS)。 然后为用户pi设置密码,并建立wifi连接。

After you are logged in, go to the Preferences → Raspberry Pi Configuration menu and enable SSH. That will allow you to log into the Pi from another computer.

登录后,转到“首选项”→“ Raspberry Pi配置”菜单并启用SSH。 这样您就可以从另一台计算机登录到Pi。

Note: in order to use the Pi as a “headless” server, you could request a static IP address from your router. In the future, you can just power on the Pi, and connect to it via SSH from your other computers or phones.

注意 :为了将Pi用作“无头”服务器,可以从路由器请求一个静态IP地址。 将来,您只需打开Pi的电源,然后从其他计算机或电话通过SSH连接到它即可。

The Raspberry Pi OS is derived from the Debian Linux distribution. It comes with a full desktop UI environment with a modern web browser, a command line terminal, and learning programs such as IDEs for Python, Java, and Scratch.

Raspberry Pi操作系统派生自Debian Linux发行版。 它带有一个完整的桌面UI环境,带有一个现代的Web浏览器,一个命令行终端以及学习程序,例如适用于Python,Java和Scratch的IDE。

For our purposes, we are mostly interested in installing dev and server software through the command line terminal.

就我们的目的而言,我们最有兴趣通过命令行终端安装开发和服务器软件。

At this point, you could also find out the IP address of the Pi on your local network by running the following command. Then you can SSH into the Pi using that local IP address, username pi, and the password you gave pi during setup.

此时,您还可以通过运行以下命令在本地网络上找到Pi的IP地址。 然后,您可以使用该本地IP地址,用户名pi和设置过程中输入的pi密码SSH到Pi。

$ hostname -I
192.168.2.108 172.17.0.1

You can find a complete list of software packages installed on the Raspberry Pi OS here. It is always a good idea to update and upgrade to the latest packages. Run the command below and be patient. It could take an hour.

您可以在此处找到Raspberry Pi OS上安装的软件包完整列表 。 更新和升级到最新软件包始终是一个好主意。 运行以下命令,请耐心等待。 可能要花一个小时。

$ sudo apt update && sudo apt upgrade

如何设置Ubuntu Server 20.04 (How to set up Ubuntu Server 20.04)

The Raspberry Pi OS is primarily geared toward a desktop experience. For developers who just want to use the device as a server or IoT device, the Ubuntu Linux is a much better choice. It has the latest software packages and libraries, and could be far more efficient without the desktop windows, web browser, Java, games, and learning tools.

Raspberry Pi OS主要面向桌面体验。 对于只想将该设备用作服务器或IoT设备的开发人员,Ubuntu Linux是更好的选择。 它具有最新的软件包和库,并且如果没有桌面窗口,Web浏览器,Java,游戏和学习工具,其效率可能更高。

You can download Ubuntu Server images for Raspberry Pi from the web, and load it on a MicroSD card. But perhaps a much easier way is just to use the Raspberry Pi Imager, select Ubuntu Server 20.04 TLS from the menu, and write into an empty MicroSD card.

您可以从网上下载Raspberry Pi的Ubuntu Server映像 ,并将其加载到MicroSD卡上。 但是,也许更简单的方法是使用Raspberry Pi Imager ,从菜单中选择Ubuntu Server 20.04 TLS,然后将其写入空的MicroSD卡。

Once the MicroSD card is prepared, you should follow these instructions to put in your WiFi network name and password. This allows the Raspberry Pi device to connect to the network as soon as it boots.

准备好MicroSD卡后,您应该按照以下说明输入WiFi网络名称和密码。 这允许Raspberry Pi设备在启动时立即连接到网络。

Basically, you can just put the MicroSD card into the Raspberry Pi, connect USB power, then wait for it to come online. You can find the

raspberrypi
device IP from your WiFi router, and then SSH into from any computer on your network.

基本上,您可以将MicroSD卡放入Raspberry Pi中,连接USB电源,然后等待其上线。 您可以从WiFi路由器找到

raspberrypi
设备IP,然后从网络上的任何计算机SSH进入。

The initial username and password are

ubuntu / ubuntu
. There is no need to even connect a monitor or keyboard. That’s it for a completely headless setup!

初始用户名和密码为

ubuntu / ubuntu
。 甚至无需连接显示器或键盘。 仅此而已,完全没有头了!

Note: if, for some reason, your Raspberry Pi cannot connect to WiFi at startup, you can hook up an HDMI display and a USB keyboard to it. Then follow these instructions to debug and set up WiFi on the running system.

注意 :如果由于某种原因您的Raspberry Pi在启动时无法连接到WiFi,则可以将HDMI显示器和USB键盘连接到它。 然后按照以下说明在运行的系统上调试和设置WiFi。

Next, let's install the developer tool stack on the Pi.

接下来,让我们在Pi上安装开发人员工具堆栈。

安装Git (Install Git)

I always install Git on all my development environments because a lot of software can be directly retrieved from Git repositories. It saves me the trouble of downloading and copying.

我总是在所有开发环境上安装Git,因为可以从Git存储库中直接检索很多软件。 它为我省去了下载和复制的麻烦。

Git also allows me to save and backup my own work in private repositories. For a small computer like Raspberry Pi, I would recommend that you save work in Git in case you lose the device or MicroSD card.

Git还允许我在私有存储库中保存和备份自己的工作。 对于像Raspberry Pi这样的小型计算机,我建议您在Git中保存工作,以防丢失设备或MicroSD卡。

The following command installs Git:

以下命令将安装Git:

$ sudo apt install git

安装Node.js (Install Node.js)

To turn the Raspberry Pi into a personal dev server for web applications, you need to install a modern web application runtime.

要将Raspberry Pi变成用于Web应用程序的个人开发服务器,您需要安装现代的Web应用程序运行时。

For most developers today, the best starting point is Node.js, which allows you to write server-side applications in JavaScript. The following two commands install Node.js on your Pi.

对于当今的大多数开发人员来说,最好的起点是Node.js,它使您可以用JavaScript编写服务器端应用程序。 以下两个命令可在Pi上安装Node.js。

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
$ sudo apt install nodejs

You can verify the installation is done correctly by running the following two commands. Both node and npm are now available.

您可以通过运行以下两个命令来验证安装是否正确完成。 节点和npm现在都可用。

$ node -v
v10.19.0
$ npm -v
5.8.0

From here, you can use npm to install modules. For example, a commonly used npm module is the express framework for web applications.

在这里,您可以使用npm安装模块。 例如,常用的npm模块是Web应用程序的快速框架。

$ npm install express

Now, you can go through the ExpressJS hello world example to create a web server on your Pi, and use web browsers from any computer on your network to access the application!

现在,您可以遍历ExpressJS hello世界示例 ,在Pi上创建Web服务器,并使用网络上任何计算机上的Web浏览器来访问应用程序!

安装Rust (Install Rust)

Rust is a fast growing programming language for writing both systems and web applications. It is close to the hardware, high performance, and memory safe. That makes Rust a great language for writing applications on resource constrained devices like the Raspberry Pi.

Rust是一种快速增长的编程语言,用于编写系统和Web应用程序。 它接近于硬件,高性能和内存安全性。 这使Rust成为在诸如Raspberry Pi之类的资源受限设备上编写应用程序的绝佳语言。

Also, Rust is the most beloved programming language by StackOverflow users for the past 5 years in a row. It is well worth your time to learn it!

而且,Rust是StackOverflow用户连续5年最喜欢的编程语言。 非常值得您花时间学习它!

An important use case of Rust is to compile Rust functions into WebAssembly and run them inside Node.js applications to achieve performance, safety, and code portability. It is a great choice for running computationally intensive web applications on a small Raspberry Pi device. In fact, you could get a free Raspberry Pi starter kit if you learn how to do that.

Rust的一个重要用例是将Rust函数编译到WebAssembly中,并在Node.js应用程序中运行它们,以实现性能,安全性和代码可移植性 。 对于在小型Raspberry Pi设备上运行计算密集型Web应用程序来说,这是一个绝佳的选择。 实际上,如果您了解如何进行操作,则可以免费获得Raspberry Pi入门套件

Note: strictly speaking, you do not need to install Rust tools on the Pi. You typically only need to run Rust programs in the Pi. You can compile your Rust program on any computer and then copy the compiled binaries to the Pi.

注意:严格来说,您不需要在Pi上安装Rust工具。 通常,您只需要在Pi中运行Rust程序即可。 您可以在任何计算机上编译Rust程序,然后将已编译的二进制文件复制到Pi。

But still, with the powerful CPU, you can compile Rust programs on the Raspberry Pi. So why not?

但是,仍然可以使用功能强大的CPU在Raspberry Pi上编译Rust程序。 那为什么不呢?

The following command installs the Rust compiler toolchain on the Pi.

以下命令在Pi上安装Rust编译器工具链。

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Run the following command to set up the correct path without logging out and back in again.

运行以下命令以设置正确的路径,而无需注销并再次登录。

$ source $HOME/.cargo/env

The above command also installs the Rust package manager called cargo. Most Rust developers use cargo to build and share their work.

上面的命令还安装了名为cargo的Rust软件包管理器。 大多数Rust开发人员使用货物来构建和共享他们的工作。

$ cargo -V
cargo 1.44.1 (88ba85757 2020-06-11)

Next, you can clone our Rust learning repository, and learn from examples.

接下来,您可以克隆我们的Rust学习资料库 ,并从示例中学习。

$ git clone https://github.com/second-state/wasm-learning.git

Here is the hello world example. Have fun!

这是你好世界的例子 。 玩得开心!

$ cd wasm-learning/rust/hello
$ cargo build
Compiling hello v0.1.0 (/home/pi/Dev/wasm-learning/rust/hello)
Finished dev [unoptimized + debuginfo] target(s) in 4.35s
$ target/debug/hello
Hello, world!

Check out the official Rust web site and the Rust by Example books for more learning resources.

查看官方Rust网站Rust by Example书籍以获取更多学习资源。

学习Docker (Learn Docker)

We have seen that the Raspberry Pi OS and Ubuntu Server are both very capable Linux distributions with lots of software packages.

我们已经看到Raspberry Pi OS和Ubuntu Server都是非常强大的Linux发行版,带有许多软件包。

But what if I want to test applications on other OSes? Do I need to wipe clean and reinstall a different OS on the MicroSD card? The answer is no. You can just use Docker! The following two commands install docker on the Raspberry Pi:

但是,如果我想在其他操作系统上测试应用程序怎么办? 我需要擦拭干净并在MicroSD卡上重新安装其他操作系统吗? 答案是不。 您可以只使用Docker! 以下两个命令在Raspberry Pi上安装docker:

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

Run the following command so that you can use Docker as the pi user:

运行以下命令,以便可以将Docker作为pi用户使用:

$ sudo usermod -aG docker pi

The Docker info command shows that Docker is now installed on an ARM system with Raspberry Pi OS.

Docker info命令显示Docker现在已安装在具有Raspberry Pi OS的ARM系统上。

$ docker info
... ...
Kernel Version: 4.19.118-v7l+
Operating System: Raspbian GNU/Linux 10 (buster)
OSType: linux
Architecture: armv7l
CPUs: 4
Total Memory: 3.814GiB
Name: raspberrypi
ID: XERI:ZVVZ:XQVA:HXSH:KRPI:6GL2:5QRE:E7GZ:Z72Q:6SGF:CEI6:GKTC
Docker Root Dir: /var/lib/docker
... ...

Next, you can pull a Docker image for the latest Ubuntu distribution, run it, and log into Ubuntu as a command line user.

接下来,您可以提取Docker映像以获取最新的Ubuntu发行版,运行它,然后以命令行用户身份登录Ubuntu。

$ docker pull ubuntu
... ...
$ docker run -it ubuntu bash
root# ... enter commands ...

下一步是什么? (What’s next?)

In this article, we have touched on the basics and learned how to turn your Raspberry Pi 4 device into a personal dev server for software developers.

在本文中,我们介绍了基础知识,并学习了如何将Raspberry Pi 4设备转变为软件开发人员的个人开发服务器。

There is much to learn about Git, Node.js, Rust, WebAssembly and Docker. There are also many other developer stacks you can install on the Raspberry Pi.

有关Git,Node.js,Rust,WebAssembly和Docker的知识很多。 您还可以在Raspberry Pi上安装许多其他开发人员堆栈。

Grab your free Raspberry Pi kit and let us know what you did with it!

索取免费的Raspberry Pi套件 ,让我们知道您的使用情况!

Subscribe to our newsletter and stay in touch.

订阅我们的新闻通讯并保持联系。

翻译自: https://www.freecodecamp.org/news/build-a-personal-dev-server-on-a-5-dollar-raspberry-pi/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐