如何用树莓派搭建个人 web 服务器
树莓派 Web
![How to set up a personal web server with a Raspberry Pi](/data/attachment/album/201705/14/204010zy1j8yjjz30pv70v.png "How to set up a personal web server with a Raspberry Pi")


个人 Web 服务器即 “云”，只不过是你拥有和控制它，而不是一个大型公司。


拥有一个自己的云有很多好处，包括可定制、免费存储、免费的互联网服务、通往开源软件之路、高安全性、完全控制您的内容、快速更改的能力、实验代码的地方等等。 这些好处大部分是无法估量的，但在财务上，这些好处可以为您每个月节省超过 100 美元。


![Building your own web server with Raspberry Pi](/data/attachment/album/201705/14/204012so6u3n67lb7ilin6.png "Building your own web server with Raspberry Pi")


我本可以选择 AWS ，但我更喜欢完全自由且安全性可控，并且我可以学一下这些东西是如何搭建的。


* 私有 Web 托管：而非 BlueHost 或 DreamHost
* 云存储：而非 Dropbox、Box、Google Drive、Microsoft Azure、iCloud 或是 AWS
* 自主部署安全
* HTTPS：Let’s Encrypt
* 分析： Google
* OpenVPN：不需要专有互联网连接（预计每个月花费 $7）


我所使用的物品清单：


* 树莓派 3 代 Model B
* MicroSD 卡（推荐使用 32 GB， [兼容树莓派的 SD 卡](http://elinux.org/RPi_SD_cards)）
* USB microSD 卡读卡器
* 以太网络线
* 连接上 Wi-Fi 的路由器
* 树莓派盒子
* 亚马逊倍思的 MicroUSB 数据线
* 苹果的充电器
* USB 鼠标
* USB 键盘
* HDMI 线材
* 显示器 (支持接入 HDMI)
* MacBook Pro


### 步骤 1： 启动树莓派


下载最新发布的 Raspbian (树莓派的操作系统)。 [Raspbian Jessie](https://www.raspberrypi.org/downloads/raspbian/) 的 ZIP 包就可以用 [脚注 1]。解压缩或提取下载的文件然后把它拷贝到 SD 卡里。使用 [Pi Filler](http://ivanx.com/raspberrypi/) 可以让这些过程变得更简单。[下载 Pi Filer 1.3](http://ivanx.com/raspberrypi/files/PiFiller.zip) 或最新的版本。解压或提取下载文件之后打开它，你应该会看到这样的提示：


![Pi Filler prompt](/data/attachment/album/201705/14/204012phzyrqh3eqnbqw34.png "Pi Filler prompt")


确保 USB 读卡器这时还没有插上。如果已经插上了那就先弹出。点 “Continue” 继续下一步。你会看到一个让你选择文件的界面，选择你之前解压缩后的树莓派系统文件。然后你会看到另一个提示，如图所示：


![USB card reader prompt](/data/attachment/album/201705/14/204013d47aa6nd8y1e7z7w.png "USB card reader")


把 MicroSD 卡（推荐 32 GB ，至少 16GB）插入到 USB MicroSD 卡读卡器里。然后把 USB 读卡器接入到你的电脑里。你可以把你的 SD 卡重命名为 “Raspberry” 以区别其他设备。然后点击 “Continue”。请先确保你的 SD 卡是空的，因为 Pi Filler 会在运行时 *擦除* 所有事先存在 SD 卡里的内容。如果你要备份卡里的内容，那你最好就马上备份。当你点 “Continue” 的时候，Raspbian OS 就会被写入到 SD 卡里。这个过程大概会花费一到三分钟左右。当写入完成后，推出 USB 读卡器，把 SD 卡拔出来插入到树莓派的 SD 卡槽里。把电源线接上，给树莓派供电。这时树莓派就会自己启动。树莓派的默认登录账户信息是：


* 用户名： pi
* 密码：raspberry


当树莓派首次启动完成时，会跳出一个标题为 “<ruby> 设置选项 <rt>  Setup Options </rt></ruby>” 的配置界面，就像下面的图片一样 [脚注 2]：


![Raspberry Pi software configuration setup](/data/attachment/album/201705/14/204013s38ype334yi3er3c.png "Raspberry Pi software configuration setup")


选择 “<ruby> 扩展文件系统 <rt>  Expand Filesystem </rt></ruby>” 这一选项并回车 [脚注 3]。 同时，我还推荐选择第二个选项 “<ruby> 修改密码 <rt>  Change User Password </rt></ruby>”。这对保证安全性来说尤为重要。它还能个性化你的树莓派。


在选项列表中选择第三项 “<ruby> 启用引导到桌面 <rt>  Enable Boot To Desktop/Scratch </rt></ruby>” 并回车。这时会跳到另一个标题为 “<ruby> 选择引导选项 <rt>  Choose boot option </rt></ruby>” 的界面，就像下面这张图这样：


![Choose boot option](/data/attachment/album/201705/14/204013iewsecnggt11zayy.png "Choose boot option")


在这个界面选择第二个选项 “<ruby> 以用户‘pi’登录图形化桌面 <rt>  Desktop log in as user 'pi' at the graphical desktop </rt></ruby>” 并回车 [脚注 4]。完成这个操作之后会回到之前的 “<ruby> 设置选项 <rt>  Setup Options </rt></ruby>” 界面。如果没有回到之前的界面的话就选择当前界面底部的 “OK” 按钮并回车。


当这些操作都完成之后，选择当前界面底部的 “Finish” 按钮并回车，这时它就会自动重启。如果没有自动重启的话，就在终端里使用如下命令来重启。



```
$ sudo reboot

```

接上一步的重启，如果所有步骤都顺利进行的话，你会进入到类似下面这样桌面环境中。


![Raspberry Pi desktop](/data/attachment/album/201705/14/204014p0fcbmfh10omfhup.png "Raspberry Pi desktop")


当你进入了桌面之后，在终端中执行如下命令来更新树莓派的固件。



```
$ sudo apt-get update
$ sudo apt-get upgrade-y
$ sudo apt-get dist-upgrade -y
$ sudo rpi-update

```

这些操作可能会花费几分钟时间。完成之后，现在运行着的树莓派就是最新的了。


### 步骤 2： 配置树莓派


SSH 指的是 Secure Shell，是一种加密网络协议，可让你在计算机和树莓派之间安全地传输数据。 你可以从 Mac 的命令行控制你的树莓派，而无需显示器或键盘。


要使用 SSH，首先需要你的树莓派的 IP 地址。 打开终端并输入：



```
$ sudo ifconfig

```

如果你在使用以太网，看 `eth0` 部分。如果你在使用 Wi-Fi, 看 `wlan0` 部分。


查找 `inet addr`，后跟一个 IP 地址，如 192.168.1.115，这是本篇文章中使用的默认 IP。


有了这个地址，在终端中输入 :



```
$ ssh pi@192.168.1.115

```

对于 PC 上的 SSH，请参见 [脚注 5]。


出现提示时输入默认密码 `raspberry`，除非你之前更改过密码。


现在你已经通过 SSH 登录成功。


### 远程桌面


使用 GUI（图形用户界面）有时比命令行更容易。 在树莓派的命令行（使用 SSH）上键入：



```
$ sudo apt-get install xrdp

```

xrdp 支持 Mac 和 PC 的 Microsoft Remote Desktop 客户端。


在 Mac 上，在 App store 中搜索 “Microsoft Remote Desktop”。 下载它。 （对于 PC，请参见 [脚注 6]。）


安装完成之后，在你的 Mac 中搜索一个叫 “Microsoft Remote Desktop” 的应用并打开它，你会看到 :


![Microsoft Remote Desktop](/data/attachment/album/201705/14/204014aowhpgmvvhpnopou.png "Microsoft Remote Desktop")


点击 “New” 新建一个远程连接，在空白处填写如下配置。


![Setting up a remote connection](/data/attachment/album/201705/14/204015fcjjci9jx2pr6cip.png "Setting up a remote connection")


关闭 “New” 窗口就会自动保存。


你现在应该看到 “My Desktop” 下列出的远程连接。 双击它。


简单加载后，你应该在屏幕上的窗口中看到你的树莓派桌面，如下所示：


![Raspberry Pi desktop](/data/attachment/album/201705/14/204015wq3r020ryrqrk0mu.png "Raspberry Pi desktop")


好了，现在你不需要额外的鼠标、键盘或显示器就能控制你的树莓派。这是一个更为轻量级的配置。


### 静态化本地 IP 地址


有时候你的本地 IP 地址 192.168.1.115 会发生改变。我们需要让这个 IP 地址静态化。输入:



```
$ sudo ifconfig

```

从 `eth0` 部分或 `wlan0` 部分，记下 `inet addr`（树莓派当前 IP），`bcast`（广播 IP 范围）和 `mask`（子网掩码地址）。 然后输入：



```
$ netstat -nr

```

记下 `destination` 和 `gateway/network`。


![Setting up a local IP address](/data/attachment/album/201705/14/204015it2dvpj7cig07pi2.png "Setting up a local IP address")


大概应该是这样子的:



```
net address 192.168.1.115
bcast 192.168.1.255
mask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.1
destination 192.168.1.0

```

有了这些信息，你可以很简单地设置一个静态 IP。输入:



```
$ sudo nano /etc/dhcpcd.conf

```

不要去动 `/etc/network/interfaces`。


剩下要做的就是把这些内容追加到这个文件的底部，把 IP 换成你想要的 IP 地址。



```
interface eth0
static ip_address=192.168.1.115
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

```

一旦你设置了静态内部 IP 地址，这时需要通过如下命令重启你的树莓派 :



```
$ sudo reboot

```

重启完成之后，在终端中输入 :



```
$ sudo ifconfig

```

这时你就可以看到你的树莓派上的新的静态配置了。


### 静态化全局 IP 地址


如果您的 ISP（互联网服务提供商）已经给您一个静态外部 IP 地址，您可以跳到端口转发部分。 如果没有，请继续阅读。


你已经设置了 SSH、远程桌面和静态内部 IP 地址，因此现在本地网络中的计算机将会知道在哪里可以找到你的树莓派。 但是你仍然无法在本地 Wi-Fi 网络外部访问你的树莓派。 你需要树莓派可以从互联网上的任何地方公开访问。这需要静态的外部 IP 地址 [脚注 7]。


联系您的 ISP 并请求静态的外部（有时称为静态全局）IP 地址可能会是一个非常敏感的过程。 ISP 拥有决策权，所以我会非常小心处理。 他们可能拒绝你的的静态外部 IP 地址请求。 如果他们拒绝了你的请求，你不要怪罪于他们，因为这种类型的请求有法律和操作风险。 他们特别不希望客户运行中型或大型互联网服务。 他们可能会明确地询问为什么需要一个静态的外部 IP 地址。 最好说实话，告诉他们你打算主办一个低流量的个人网站或类似的小型非营利互联网服务。 如果一切顺利，他们应该会建立一个工单，并在一两个星期内给你打电话。


### 端口转发


这个新获得的 ISP 分配的静态全局 IP 地址是用于访问路由器。 树莓派现在仍然无法访问。 你需要设置端口转发才能访问树莓派。


端口是信息在互联网上传播的虚拟途径。 你有时需要转发端口，以使计算机像树莓派一样可以访问 Internet，因为它位于网络路由器后面。 VollmilchTV 专栏在 YouTube 上的一个视频，名字是[什么是 TCP/IP，端口，路由，Intranet，防火墙，互联网](https://www.youtube.com/watch?v=iskxw6T1Wb8)，可以帮助你更好地了解端口。


端口转发可用于像 树莓派 Web 服务器或 VoIP 或点对点下载的应用程序。 有 [65000个以上的端口](https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers)可供选择，因此你可以为你构建的每个 Internet 应用程序分配一个不同的端口。


设置端口转发的方式取决于你的路由器。 如果你有 Linksys 的话，Gabriel Ramirez 在 YouTbue 上有一个标题叫 [如何让你的 Apache Ubuntu 服务器连到互联网](https://www.youtube.com/watch?v=i1vB7JnPvuE#t=07m08s) 的视频解释了如何设置。 如果您没有 Linksys，请阅读路由器附带的文档，以便自定义和定义要转发的端口。


你需要转发 SSH 以及远程桌面端口。


如果你认为你已经过配置端口转发了，输入下面的命令以查看它是否正在通过 SSH 工作：



```
$ ssh pi@your_global_ip_address

```

它应该会提示你输入密码。


检查端口转发是否也适用于远程桌面。 打开 Microsoft Remote Desktop。 你之前的的远程连接设置应该已经保存了，但需要使用静态的外部 IP 地址（例如 195.198.227.116）来更新 “PC 名称” 字段，而不是静态的内部地址（例如 192.168.1.115）。


现在，尝试通过远程桌面连接。 它应该简单地加载并显示树莓派的桌面。


![Raspberry Pi desktop](/data/attachment/album/201705/14/204016f1dj2m2z47da0271.png "Raspberry Pi desktop")


好了， 树莓派现在可以从互联网上访问了，并且已经准备好进行高级项目了。


作为一个奖励选项，您可以保持到您的 Pi 的两个远程连接。 一个通过互联网，另一个通过 LAN（局域网）。很容易设置。在 Microsoft Remote Desktop 中，保留一个称为 “Pi Internet” 的远程连接，另一个称为 “Pi Local”。 将 Pi Internet 的 “PC 名称” 配置为静态外部 IP 地址，例如 195.198.227.116。 将 Pi Local 的 “PC 名称” 配置为静态内部 IP 地址，例如 192.168.1.115。 现在，您可以选择在全局或本地连接。


如果你还没有看过由 Gabriel Ramirez 发布的 [如何让你的 Apache Ubuntu 服务器连到互联网](https://www.youtube.com/watch?v=i1vB7JnPvuE#t=07m08s)，那么你可以去看一下，作为过渡到第二个项目的教程。 它将向您展示项目背后的技术架构。 在我们的例子中，你使用的是树莓派而不是 Ubuntu 服务器。 动态 DNS 位于域名公司和您的路由器之间，这是 Ramirez 省略的部分。 除了这个微妙之处外，视频是在整体上解释系统的工作原理。 您可能会注意到本教程涵盖了树莓派设置和端口转发，这是服务器端或后端。 查看原始来源，涵盖域名，动态 DNS，Jekyll（静态 HTML 生成器）和 Apache（网络托管）的更高级项目，这是客户端或前端。


### 脚注


[1] 我不建议从 NOOBS 操作系统开始。 我更喜欢从功能齐全的 Raspbian Jessie 操作系统开始。


[2] 如果没有弹出 “Setup Options”，可以通过打开终端并执行该命令来始终找到它：



```
$ sudo-rasps-config

```

[3] 我们这样做是为了将 SD 卡上存在的所有空间用作一个完整的分区。 所有这一切都是扩大操作系统以适应 SD 卡上的整个空间，然后可以将其用作树莓派的存储内存。


[4] 我们这样做是因为我们想启动进入熟悉的桌面环境。 如果我们不做这个步骤，树莓派每次会进入到终端而不是 GUI 中。


[5]


![PuTTY configuration](/data/attachment/album/201705/14/204016l65kon533wpvqk5j.png "PuTTY configuration")


[下载并运行 PuTTY](http://www.chiark.greenend.org.uk/%7Esgtatham/putty/download.html) 或 Windows 的其它 SSH 客户端。 在该字段中输入你的 IP 地址，如上图所示。 将默认端口保留为 22。 回车，PuTTY 将打开一个终端窗口，提示你输入用户名和密码。 填写然后开始在树莓派上进行你的远程工作。


[6] 如果尚未安装，请下载 [Microsoft Remote Desktop](https://www.microsoft.com/en-us/store/apps/microsoft-remote-desktop/9wzdncrfj3ps)。 搜索您的计算机上的的 Microsoft Remote Desktop。 运行。 提示时输入 IP 地址。 接下来，会弹出一个 xrdp 窗口，提示你输入用户名和密码。


[7] 路由器具有动态分配的外部 IP 地址，所以在理论上，它可以从互联网上暂时访问，但是您需要 ISP 的帮助才能使其永久访问。 如果不是这样，你需要在每次使用时重新配置远程连接。


*原文出自 [Mitchell McLaughlin's Full-Stack Computer Projects](https://mitchellmclaughlin.com/server.html)。*




---


作者简介：


Mitchell McLaughlin - 我是一名开放网络的贡献者和开发者。我感兴趣的领域很广泛，但我特别喜欢开源软件/硬件，比特币和编程。 我住在旧金山，我有过一些简短的 GoPro 和 Oracle 工作经验。




---


via: <https://opensource.com/article/17/3/building-personal-web-server-raspberry-pi-3>


作者：[Mitchell McLaughlin](https://opensource.com/users/mitchm)  译者：[chenxinlong](https://github.com/chenxinlong) 校对：[jasminepeng](https://github.com/jasminepeng)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
A personal web server is "the cloud," except you own and control it as opposed to a large corporation.

Owning a little cloud has a lot of benefits, including customization, free storage, free Internet services, a path into open source software, high-quality security, full control over your content, the ability to make quick changes, a place to experiment with code, and much more. Most of these benefits are immeasurable, but financially these benefits can save you over $100 per month.

![Raspberry Pi as a web server Raspberry Pi as a web server](https://opensource.com/sites/default/files/uploads/raspberry_pi_web_server.png)

Raspberry Pi as a web server, by Raspberry Pi Guy, CC-BY-SA 4.0

I could have used AWS, but I prefer complete freedom, full control over security, and learning how things are built.


- Self web-hosting: No BlueHost or DreamHost
- Cloud storage: No Dropbox, Box, Google Drive, Microsoft Azure, iCloud, or AWS
- On-premise security
- HTTPS: Let’s Encrypt
- Analytics: Google
- OpenVPN: Do not need private Internet access (at an estimated $7 per month)

Things I used:

- Raspberry Pi 3 Model B
- MicroSD Card (32GB recommended,
[Raspberry Pi Compatible SD Cards](http://elinux.org/RPi_SD_cards)) - USB microSD card reader
- Ethernet cable
- Router connected to Wi-Fi
- Raspberry Pi case
- Amazon Basics MicroUSB cable
- Apple wall charger
- USB mouse
- USB keyboard
- HDMI cable
- Monitor (with HDMI input)
- MacBook Pro


## Step 1: Setting up the Raspberry Pi

Download the most recent release of Raspbian (the Raspberry Pi operating system). [Raspbian Jessie](https://www.raspberrypi.org/downloads/raspbian/) ZIP version is ideal [1]. Unzip or extract the downloaded file. Copy it onto the SD card. [Pi Filler](http://ivanx.com/raspberrypi/) makes this process easy. [Download Pi Filer 1.3](http://ivanx.com/raspberrypi/files/PiFiller.zip) or the most recent version. Unzip or extract the downloaded file and open it. You should be greeted with this prompt:

Make sure the USB card reader has NOT been inserted yet. If it has, eject it. Proceed by clicking Continue. A file explorer should appear. Locate the uncompressed Raspberry Pi OS file from your Mac or PC and select it. You should see another prompt like the one pictured below:

Insert the MicroSD card (32GB recommended, 16GB minimum) into the USB MicroSD Card Reader. Then insert the USB reader into the Mac or PC. You can rename the SD card to "Raspberry" to distinguish it from others. Click Continue. Make sure the SD card is empty. Pi Filler will *erase* all previous storage at runtime. If you need to back up the card, do so now. When you are ready to continue, the Raspbian OS will be written to the SD card. It should take between one to three minutes. Once the write is completed, eject the USB reader, remove the SD card, and insert it into the Raspberry Pi SD card slot. Give the Raspberry Pi power by plugging the power cord into the wall. It should start booting up. The Raspberry Pi default login is:

**username: pi
password: raspberry**

When the Raspberry Pi has completed booting for the first time, a configuration screen titled "Setup Options" should appear like the image below [2]:

Select the "Expand Filesystem" option and hit the Enter key [3]. Also, I recommend selecting the second option, "Change User Password." It is important for security. It also personalizes your Raspberry Pi.

(Note: For an extra layer of security install fail2ban. Fail2Ban blocks suspicious requests coming from the internet. For example, if there are too many attempts to guess the password, it will block that IP address. It can be installed by typing into terminal: **$ ****sudo**** apt-get install fail2ban**)

Select the third option in the setup options list, "Enable Boot To Desktop/Scratch" and hit the Enter key. It will take you to another window titled "Choose boot option" as shown in the image below.

In the "Choose boot option" window, select the second option, "Desktop log in as user 'pi' at the graphical desktop" and hit the Enter button [4]. Once this is done you will be taken back to the "Setup Options" page. If not, select the "OK" button at the bottom of this window and you will be taken back to the previous window.

Once both these steps are done, select the "Finish" button at the bottom of the page and it should reboot automatically. If it does not, then use the following command in the terminal to reboot.

**$ sudo reboot**

After the reboot from the previous step, if everything went well, you will end up on the desktop similar to the image below.

Once you are on the desktop, open a terminal and enter the following commands to update the firmware of the Raspberry Pi.

```
$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get dist-upgrade -y
$ sudo rpi-update
```


This may take a few minutes. Now the Raspberry Pi is up-to-date and running.

## Step 2: Configuring the Raspberry Pi

SSH, which stands for Secure Shell, is a cryptographic network protocol that lets you securely transfer data between your computer and your Raspberry Pi. You can control your Raspberry Pi from your Mac's command line without a monitor or keyboard.

To use SSH, first, you need your Pi's IP address. Open the terminal and type:

`$ sudo ifconfig`


If you are using Ethernet, look at the "eth0" section. If you are using Wi-Fi, look at the "wlan0" section.

Find "inet addr" followed by an IP address—something like 192.168.1.115, a common default IP I will use for the duration of this article.

With this address, open terminal and type:

`$ ssh pi@192.168.1.115`


For SSH on PC, see footnote [5].

Enter the default password "raspberry" when prompted, unless you changed it.

You are now logged in via SSH.

### Remote desktop

Using a GUI (graphical user interface) is sometimes easier than a command line. On the Raspberry Pi's command line (using SSH) type:

`$ sudo apt-get install xrdp`


Xrdp supports the Microsoft Remote Desktop Client for Mac and PC.

On Mac, navigate to the app store and search for "Microsoft Remote Desktop." Download it. (For a PC, see footnote [6].)

After installation, search your Mac for a program called "Microsoft Remote Desktop." Open it. You should see this:

![Microsoft Remote Desktop Microsoft Remote Desktop](https://opensource.com/sites/default/files/images/life-uploads/7-image_by_mitchell_mclaughlin_cc_by-sa_4.0.png)

Image by Raspberry Pi Guy, CC BY-SA 4.0


Click "New" to set up a remote connection. Fill in the blanks as shown below.



![Setting up a remote connection Setting up a remote connection](https://opensource.com/sites/default/files/images/life-uploads/8-image_by_mitchell_mclaughlin_cc_by-sa_4.0.png)

Image by Raspberry Pi Guy, CC BY-SA 4.0

Save it by exiting out of the "New" window.

You should now see the remote connection listed under "My Desktops." Double click it.

After briefly loading, you should see your Raspberry Pi desktop in a window on your screen, which looks like this:

Perfect. Now, you don't need a separate mouse, keyboard, or monitor to control the Pi. This is a much more lightweight setup.

### Static local IP address

Sometimes the local IP address 192.168.1.115 will change. We need to make it static. Type:

`$ sudo ifconfig`


Write down from the "eth0" section or the "wlan0" section, the "inet addr" (Pi's current IP), the "bcast" (the broadcast IP range), and the "mask" (subnet mask address). Then, type:

`$ netstat -nr`


Write down the "destination" and the "gateway/network."

The cumulative records should look something like this:

```
net address 192.168.1.115
bcast 192.168.1.255
mask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.1
destination 192.168.1.0
```


With this information, you can set a static internal IP easily. Type:

`$ sudo nano /etc/dhcpcd.conf`


Do not use **/etc/network/interfaces**.

Then all you need to do is append this to the bottom of the file, substituting the correct IP address you want.

```
interface eth0
static ip_address=192.168.1.115
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
```


Once you have set the static internal IP address, reboot the Raspberry Pi with:

`$ sudo reboot`


After rebooting, from terminal type:

`$ sudo ifconfig`


Your new static settings should appear for your Raspberry Pi.

### Static global IP address

If your ISP (internet service provider) has already given you a static external IP address, you can skip ahead to the port forwarding section. If not, continue reading.

You have set up SSH, a remote desktop, and a static internal IP address, so now computers inside the local network will know where to find the Pi. But you still can't access your Raspberry Pi from outside the local Wi-Fi network. You need your Raspberry Pi to be accessible publicly from anywhere on the Internet. This requires a static external IP address [7].

It can be a sensitive process initially. Call your ISP and request a static external (sometimes referred to as static global) IP address. The ISP holds the decision-making power, so I would be extremely careful dealing with them. They may refuse your static external IP address request. If they do, you can't fault the ISP because there is a legal and operational risk with this type of request. They particularly do not want customers running medium- or large-scale Internet services. They might explicitly ask why you need a static external IP address. It is probably best to be honest and tell them you plan on hosting a low-traffic personal website or a similar small not-for-profit internet service. If all goes well, they should open a ticket and call you in a week or two with an address.

### Port forwarding

This newly obtained static global IP address your ISP assigned is for accessing the router. The Raspberry Pi is still unreachable. You need to set up port forwarding to access the Raspberry Pi specifically.

Ports are virtual pathways where information travels on the Internet. You sometimes need to forward a port in order to make a computer, like the Raspberry Pi, accessible to the Internet because it is behind a network router. A YouTube video titled [What is TCP/IP, port, routing, intranet, firewall, Internet](https://www.youtube.com/watch?v=iskxw6T1Wb8) by VollmilchTV helped me visually understand ports.

Port forwarding can be used for projects like a Raspberry Pi web server, or applications like VoIP or peer-to-peer downloading. There are [65,000+ ports](https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers) to choose from, so you can assign a different port for every Internet application you build.

The way to set up port forwarding can depend on your router. If you have a Linksys, a YouTube video titled * How to go online with your Apache Ubuntu server* by Gabriel Ramirez explains how to set it up. If you don't have a Linksys, read the documentation that comes with your router in order to customize and define ports to forward.

You will need to port forward for SSH as well as the remote desktop.

Once you believe you have port forwarding configured, check to see if it is working via SSH by typing:

`$ ssh pi@your_global_ip_address`


It should prompt you for the password.

Check to see if port forwarding is working for the remote desktop as well. Open Microsoft Remote Desktop. Your previous remote connection settings should be saved, but you need to update the "PC name" field with the static external IP address (for example, 195.198.227.116) instead of the static internal address (for example, 192.168.1.115).

Now, try connecting via remote desktop. It should briefly load and arrive at the Pi's desktop.

Good job. The Raspberry Pi is now accessible from the Internet and ready for advanced projects.

As a bonus option, you can maintain two remote connections to your Pi. One via the Internet and the other via the LAN (local area network). It's easy to set up. In Microsoft Remote Desktop, keep one remote connection called "Pi Internet" and another called "Pi Local." Configure Pi Internet's "PC name" to the static external IP address—for example, 195.198.227.116. Configure Pi Local's "PC name" to the static internal IP address—for example, 192.168.1.115. Now, you have the option to connect globally or locally.

If you have not seen it already, watch * How to go online with your Apache Ubuntu server* by Gabriel Ramirez as a transition into Project 2. It will show you the technical architecture behind your project. In our case, you are using a Raspberry Pi instead of an Ubuntu server. The dynamic DNS sits between the domain company and your router, which Ramirez omits. Beside this subtlety, the video is spot on when explaining visually how the system works. You might notice this tutorial covers the Raspberry Pi setup and port forwarding, which is the server-side or back end. See the original source for more advanced projects covering the domain name, dynamic DNS, Jekyll (static HTML generator), and Apache (web hosting), which is the client-side or front end.

### Footnotes

[1] I do not recommend starting with the NOOBS operating system. I prefer starting with the fully functional Raspbian Jessie operating system.

[2] If "Setup Options" does not pop up, you can always find it by opening Terminal and executing this command:

`$ sudo-raspi-config`


[3] We do this to make use of all the space present on the SD card as a full partition. All this does is expand the operating system to fit the entire space on the SD card, which can then be used as storage memory for the Raspberry Pi.

[4] We do this because we want to boot into a familiar desktop environment. If we do not do this step, the Raspberry Pi boots into a terminal each time with no GUI.

[5]

[Download and run PuTTY](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) or another SSH client for Windows. Enter your IP address in the field, as shown in the above screenshot. Keep the default port at 22. Hit Enter, and PuTTY will open a terminal window, which will prompt you for your username and password. Fill those in, and begin working remotely on your Pi.

[6] If it is not already installed, download [Microsoft Remote Desktop](https://www.microsoft.com/en-us/store/apps/microsoft-remote-desktop/9wzdncrfj3ps). Search your computer for Microsoft Remote Desktop. Run it. Input the IP address when prompted. Next, an xrdp window will pop up, prompting you for your username and password.

[7] The router has a dynamically assigned external IP address, so in theory, it can be reached from the Internet momentarily, but you'll need the help of your ISP to make it permanently accessible. If this was not the case, you would need to reconfigure the remote connection on each use.

*For the original source, visit Mitchell McLaughlin's Full-Stack Computer Projects.*

## 22 Comments