# Part9-服务器选购与基本配置

\[\[toc]]

## 一、购买服务器

### 1、服务器选配

直接上 [aliyun.com](https://ecs-buy.aliyun.com/wizard?byShare=true/#/prepay/cn-wulanchabu) 购买一台服务器：

[![](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndd82vlj31330u0n1l.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndd82vlj31330u0n1l.jpg)

> 由于CentOS从2022年起已经不再维护与支持，因此请选用Ubuntu或其他镜像。

### 2、购买时长与IP分配

选择购买时长：

[![image-20211226182624315](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndlm0fxj319s02k0sr.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndlm0fxj319s02k0sr.jpg)

可以适当延长，无需与我一样选择1周。

选择好镜像后，点击 `下一步`，进入网络和安全组，这里主要是多加了一项 `公网IP`，默认是勾选上的，但价格也会接近翻倍：

[![image-20210802193855467](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ofypc4nj31j00u0n2e.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ofypc4nj31j00u0n2e.jpg)

接下来直接 `确认订单`：

[![image-20210802193946547](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ogtr7f5j31j80u0afd.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ogtr7f5j31j80u0afd.jpg)

## 二、工作台

至此，我们完成了购买，然后到你的 `云服务器ECS` 工作台

[![image-20210802194203279](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ojb2q4wj31jd0u00yi.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ojb2q4wj31jd0u00yi.jpg)

选择你购买的云服务器，点击进去。

这里看到你的公网IP，点击进入 `管理`：

[![image-20210802194444705](https://tva1.sinaimg.cn/large/008i3skNgy1gt2olvgwhnj32la0kcwjd.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2olvgwhnj32la0kcwjd.jpg)

### 1、重置实例密码

然后点击 `重置实例密码`，这个密码你需要记住，是你登录服务器的密码。

[![image-20210802194550734](https://tva1.sinaimg.cn/large/008i3skNgy1gt2on9jmafj31ja0u0797.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2on9jmafj31ja0u0797.jpg)

然后点击 `重启`。

### 2、安全组配置

实例中选择安全组：

[![image-20211226192755226](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndnezd3j30ym0lhq51.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndnezd3j30ym0lhq51.jpg)

点击“配置规则”：

[![image-20211226192836709](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndkno6sj31b809uab4.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndkno6sj31b809uab4.jpg)

项目如果运行在指定的端口，就需要现在安全组进行端口号添加：

[![image-20211226192927718](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndohq6jj31am0n9jtz.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndohq6jj31am0n9jtz.jpg)

## 三、Linux基本知识

先来简单学几个Linux指令。CentOS也是属于Linux系统。打开页面右上角的命令行图标：

[![image-20210802195126798](https://tva1.sinaimg.cn/large/008i3skNgy1gt2osvsjpgj32l20pqn2n.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2osvsjpgj32l20pqn2n.jpg)

### 1、登录服务器

执行：

```shell
$ ssh root@你的ip
# 然后输入密码，完成登录
```

### 2、文件颜色

首先，

这些文件是有颜色的：

* 蓝色 表示文件夹；
* 灰色 表示普通文件；
* 绿色 表示可执行文件；
* 红色 表示压缩文件；
* 天蓝色 表示链接文件（快捷方式）；

常用目录的作用如下：

* **bin：** 存放普通用户可执行的指令，普通用户也可以执行；
* **dev ：** 设备目录，所有的硬件设备及周边均放置在这个设备目录中；
* **boot ：** 开机引导目录，包括 Linux 内核文件与开机所需要的文件；
* **home：** 这里主要存放你的个人数据，具体每个用户的设置文件，用户的桌面文件夹，还有用户的数据都放在这里。每个用户都有自己的用户目录，位置为：`/home/用户名`。当然，root 用户除外；
* **usr：** 应用程序放置目录；
* **lib：** 开机时常用的动态链接库，bin 及 sbin 指令也会调用对应的 lib 库；
* **tmp：** 临时文件存放目录 ；
* **etc：** 各种配置文件目录，大部分配置属性均存放在这里；

### 3、文件操作

| 指令          | 备注        |
| ----------- | --------- |
| vim <文件名>   | 编辑某个文件    |
| cat <文件名>   | 查看某个文件的内容 |
| touch <文件名> | 新建某个文件    |

### 4、vim编辑器

通过 `vim <文件名>` 即可进入对该文件的编辑模式，按下 `i` 切换到编辑状态。

想要结束输入，可以按 `esc` 键，想要退出编辑模式，可以敲下 `:wq` 或者按快捷键：`shift+zz`。

在命令模式下按下 `i` 就进入了输入模式。

在输入模式中，可以使用以下按键取消与显示行号：

| 功能     | 命令          |
| ------ | ----------- |
| 行号显示   | `:set nu`   |
| 行号显示取消 | `:set nonu` |

### 5、杀死进程

查看进程：

```shell
$ ps -ef
```

查看到进程中的 `PID` ，然后执行：

```shell
# 正常杀死
$ kill -1 2235

# 强制杀死
$ kill -9 2235
```

### 6、SSH 上传/下载文件

SSH 可以通过 scp 命令来上传文件，是 Linux 系统下基于 SSH 登陆进行安全的远程文件拷贝命令，scp 是 secure copy 的简写，可以使用它上传本地文件夹到远程服务器，也可以从远程服务器上下载文件夹到本地：

```shell
# 打开你的某个项目

# 上传文件夹到远程服务器
$ scp -P port -r /local/dir username@servername:/remote/dir
# scp -r C:\\Users\\用户名\\Desktop\\文件夹名称 root@8.130.25.41:/home

# 从远程服务器下载文件夹
$ scp -P port -r username@servername:/remote/dir/ /local/dir
# scp -r root@8.130.25.41:/home ./
```

`-r` 参数表示递归复制，即复制该目录下面的文件和目录，如果要上传单个文件，只要把 `-r` 删除。大写的 `P` 表示的是端口，如果还是默认的 SSH 端口 22 没有更改，则不需要 `-P`。

注意：这两行代码要在你本机的终端运行，而不是在阿里云的终端。

### 7、FTP上传

你也可以使用ftp工具上传，如 [File Zilla](https://www.filezilla.cn/)。点击即可下载。

使用也很简单：

[![image-20211226193156283](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndnwd5ij30js0f475e.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndnwd5ij30js0f475e.jpg)

按这里填写即可登录。然后：

[![image-20211226193437966](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndpdclej31f30jq40k.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndpdclej31f30jq40k.jpg)

### 8、ssh长连接

ssh连接服务器每隔一小段时间没响应就会自动断开，这是因为sshd\_config的配置影响了。如果想保持比较久的连接，需要如下操作：

#### a. 搜索ssh配置文件

先ssh连接上服务器，然后查看ssh的位置：

```shell
$ whereis ssh
```

正常来讲会出现类似于 `/etc/ssh` 的地址。

#### b. 修改ssh配置

打开配置：

```shell
$ cd /etc/ssh/sshd_config
```

修改以下两行代码：

```shell
#ClientAliveInterval 0
#ClientAliveCountMax 3
```

改为：

```shell
ClientAliveInterval 30
ClientAliveCountMax 86400
```

> 这两行的意思分别是：
>
> 1、客户端每隔多少秒向服务发送一个心跳数据
>
> 2、客户端多少秒没有相应，服务器自动断掉连接

#### c. 重启sshd服务

按下 `:wq` 保存并退出编辑，然后重启sshd：

```shell
$ sudo /etc/init.d/ssh restart

[ ok ] Restarting ssh (via systemctl): ssh.service.
```

看到ok的提示，就代表重启成功了，使用 `exit;` 退出ssh连接再重新进入即可。

## 四、Nginx

Nginx 是开源、高性能、高可靠的 Web 和反向代理服务器，而且支持热部署，几乎可以做到 7 \* 24 小时不间断运行，即使运行几个月也不需要重新启动，还能在不间断服务的情况下对软件版本进行热更新。

这里我们重点提一下服务器安装Nginx、node及项目打包上线后产生的跨域问题的解决方案。

### 1、快速安装Nginx

Nginx 在默认的 Ubuntu 源仓库中可用。

首先，确保自己连接上了服务器 `ssh 用户名@公网ip`，然后运行：

```shell
$ sudo apt update
$ sudo apt install nginx
```

一旦安装完成，Nginx 将会自动被启动。你可以运行下面的命令来验证它：

```shell
$ sudo systemctl status nginx
```

当出现以下图示，证明安装Nginx已经成功：

[![image-20211226194030365](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndmjaa4j30w107qdhc.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndmjaa4j30w107qdhc.jpg)

### 2、防火墙配置

安装好Nginx后，你需要确保你的防火墙被配置好，允许流量通过 HTTP（`80`）和 HTTPS（`443`）端口。假设你正在使用`UFW`（就是ubuntu上的防火墙工具）,你可以做的是启用 ‘Nginx Full’ profile，它包含了这两个端口：

```shell
# 允许所有端口
$ sudo ufw allow 'Nginx Full'
# 开启防火墙
$ sudo ufw enable
# 关闭防火墙
$ sudo ufw disable
```

想要验证状态，输入：

```shell
$ sudo ufw status
```

得到以下图示，即代表防火墙开启：

[![image-20211226194836706](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndj89y1j30f5045aa3.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gxsndj89y1j30f5045aa3.jpg)

> **注意：**&#x9632;火墙不要随便开，否则会导致ssh无法连接

然后设置 Nginx 的开机启动：

```shell
$ systemctl enable nginx
```

启动 Nginx ：

```shell
$ systemctl start nginx
```

### 3、浏览器访问公网IP

在浏览器输入你的公网IP，无需端口号（默认80），如果看到 `Welcome to nginx` 之类的界面，就是Nginx安装并启动成功了。

### 4、安装 nvm & node & git

```shell
# 下载 nvm，或者看官网的步骤 https://github.com/nvm-sh/nvm#install--update-script
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

$ source ~/.bashrc    	# 安装完毕后，更新配置文件即可使用 nvm 命令
$ nvm ls-remote         # 查看远程 node 版本
$ nvm install v12.16.3  # 选一个你要安装的版本安装，我这里选择 12.16.3
$ nvm list              # 安装完毕查看安装的 node 版本
$ node -v               # 查看是否安装好了

$ apt install git   	# git 安装
$ git --version			# 查看git版本
```

### 5、Nginx重启

```shell
$ nginx -s reload  # 向主进程发送信号，重新加载配置文件，热重启
```

### 6、Nginx配置文件

我们一般对Nginx的修改主要就是修改 `/etc/nginx/nginx.conf` 这个配置文件，`nginx.conf` 结构图可以这样概括：

```nginx
main        # 全局配置，对全局生效
├── events  # 配置影响 Nginx 服务器或与用户的网络连接
├── http    # 配置代理，缓存，日志定义等绝大多数功能和第三方模块的配置
│   ├── upstream # 配置后端服务器具体地址，负载均衡配置不可或缺的部分
│   ├── server   # 配置虚拟主机的相关参数，一个 http 块中可以有多个 server 块
│   ├── server
│   │   ├── location  # server 块可以包含多个 location 块，location 指令用于匹配 uri
│   │   ├── location
│   │   └── ...
│   └── ...
└── ...
```

通常我会找到server，然后做如下设置：

```nginx
server {
    listen 80;
    server_name 公网IP;

    location / {
        root  /usr/local/html;
        index index.html index.htm;
    }
}
```

这里要确保你在 `/usr/local` 这个目录下创建了一个 `html` 文件夹。

### 7、反向代理解决跨域

反向代理是工作中最常用的服务器功能，经常被用来解决跨域问题，首先进入 Nginx 的主配置文件：

```shell
vim /etc/nginx/nginx.conf
```

为了看起来方便，把行号显示出来 `:set nu` ，然后我们去 `http` 模块的 `server` 块中的 `location /` 同级的位置添加：

```nginx
location /api {
	proxy_pass 跨域地址的baseURL;
}
```

### 8、开启 gzip 压缩

gzip 是一种常用的网页压缩技术，传输的网页经过 gzip 压缩之后大小通常可以变为原来的一半甚至更小。更小的网页体积也就意味着带宽的节约和传输速度的提升，特别是对于访问量巨大大型网站来说，每一个静态资源体积的减小，都会带来可观的流量与带宽的节省。

这里可以使用 [站长工具](http://tool.chinaz.com/Gzips/Default.aspx) 对一个网站查询是否进行过gzip压缩。

[![image-20210802224537379](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ttzxq27j30x70cdgmt.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2ttzxq27j30x70cdgmt.jpg)

可以看到我的这个ip目前是没有进行过gzip压缩的。然后我们如下操作：

```shell
cd /etc/nginx/conf.d/
touch gzip.conf
vim gzip.conf
```

接下来把下面代码粘贴进去：

```nginx
gzip on; # 默认off，是否开启gzip
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
```

然后退出编辑模式，执行：

```shell
cd /usr/sbin
./nginx -s reload
```

然后再到站长工具测试：

[![image-20210802225044894](https://tva1.sinaimg.cn/large/008i3skNgy1gt2tzbwv1kj30xb0dowfx.jpg)](https://tva1.sinaimg.cn/large/008i3skNgy1gt2tzbwv1kj30xb0dowfx.jpg)

可以看到，我们成功压缩了。

### 9、其他

关于Nginx还有很多亮点，例如：动静分离、负载均衡等等，我们就不再做更多的阐述。

> 特别鸣谢：
>
> [《Nginx 从入门到实践，万字详解！》](https://juejin.cn/post/6844904144235413512) 这篇文章给了很多启示，本文部分文字及代码参考了该文章，有兴趣的同学可以去深入阅读。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gb.akanote.cn/cms/part9-fu-wu-qi-xuan-gou-yu-ji-ben-pei-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
