Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shfshanyue committed Jan 4, 2020
1 parent 585820e commit 5411c82
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 106 deletions.
16 changes: 8 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# 当我有服务器时我做了什么 · <small>个人服务器运维指南</small>

去年我写了一篇文章: [当我有一台服务器时做了什么](https://shanyue.tech/op/when-server.html)当时为了不至于浪费我在阿里云低价优惠买的服务器,于是使用 docker 跑了一个应用,并参照我司的技术架构搭建了相关的基础设施。
在 2018 年时,我写了一篇文章: [当我有一台服务器时做了什么](https://shanyue.tech/op/when-server.html)为了不至于浪费我在阿里云低价优惠买的服务器,于是使用 docker 跑了一个应用,并参照我司的技术架构搭建了相关的基础设施。

**现在仔细想来,这些经验也非常有益于有一台服务器却不知所措的人,于是有了本系列文章,希望能够帮助到那些服务器买来已久却在吃灰的人。** 另外如果你是一个自由开发者,本系列文章或许对你环境搭建也会有些许启发。
**现在仔细想来,这些经验非常适用于有一台服务器却不知所措的人,于是有了本系列文章,希望能够帮助到那些服务器买来已久却仍在吃灰的人。如果你是一个自由开发者,本系列文章或许对你环境搭建也会有些许启发。**

> 如果对你能够有所帮助,可以帮我在 [shfshanyue/op-note](https://github.com/shfshanyue/op-note) 上点个 star。
如果你的服务器配置高于1核2G,则本系列文章将会适用于你。如果你的服务器配置较高,并且拥有多台,你可以参考我另一系列文章 [k8s 与微服务实践](https://github.com/shfshanyue/learn-k8s)

> 本系列文章托管在 github 的仓库 [shfshanyue/op-note](https://github.com/shfshanyue/op-note)
## 目录

1.
1. [导读](https://shanyue.tech/op/introduction.html)
1. [序·当我有一台服务器时我做了什么](https://shanyue.tech/op/when-server.html)
1. [序·当我有一台服务器时我做了什么(2019)](https://shanyue.tech/op/when-server-2019.html)
1. [序·个人服务器应用开发架构推荐]() - TODO
1. 如果你只想搭建博客
1. [如果你只想搭一个博客](https://shanyue.tech/op/if-you-want-a-blog.html)
1. [使用 netlify 托管博客与持续集成](https://shanyue.tech/op/deploy-fe-with-netlify.html)
1. [使用 alioss 托管博客](https://shanyue.tech/op/deploy-fe-with-alioss.html)
1. [使用 github action 持续集成](https://shanyue.tech/op/github-action-guide.html)
1. [使用 github actions 持续集成](https://shanyue.tech/op/github-action-guide.html)
1. 服务器初始化配置
1. [服务器快速登录配置:ssh-config](https://shanyue.tech/op/init.html)
1. [git 基本命令及安装](https://shanyue.tech/op/git.html)
Expand All @@ -36,7 +36,7 @@
1. [docker compose 编排架构简介](https://shanyue.tech/op/docker-compose-arch.html)
1. [docker compose 简易入门](https://shanyue.tech/op/docker-compose.html)
1. [使用 traefik 做反向代理](https://shanyue.tech/op/traefik.html)
1. [使用 traefik 自动生成 https 的证书]() - TODO
1. [使用 traefik 自动生成 https 的证书](https://shanyue.tech/op/traefik-https.html)
1. [使用 dnsmasq 搭建本地 DNS 服务](https://shanyue.tech/op/dnsmasq.html)
1. [使用 openvpn 访问内部集群私有服务](https://shanyue.tech/op/openvpn.html)
1. [使用 postgres 做数据存储](https://shanyue.tech/op/deploy-postgres.html)
Expand Down Expand Up @@ -66,6 +66,6 @@

## 关注我

我是山月,我会定期分享文章在个人公众号【全栈成长之路】中。你可以添加我微信 `shanyue94` 或者在公众号中联系我
我是山月,我会定期分享文章在个人公众号【全栈成长之路】中。你可以添加我微信 `shanyue94` 或者在公众号中联系我,添加好友时回复**个人服务器**可以拉你进个人服务器运维交流群。

![如果你对全栈面试,前端工程化,graphql,devops,个人服务器运维以及微服务感兴趣的话,可以关注我](https://shanyue.tech/qrcode.jpg)
5 changes: 0 additions & 5 deletions ansible-ssh.md

This file was deleted.

Binary file modified assets/ctop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/htop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/traefik-dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion compose/traefik/traefik.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
entryPoint = "metrics"

[certificatesResolvers.le.acme]
email = "1144542900@qq.com"
email = "xianger94@qq.com"
storage = "acme.json"

[certificatesResolvers.le.acme.tlsChallenge]
Expand Down
4 changes: 3 additions & 1 deletion compose/whoami/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ services:
whoami:
image: containous/whoami
labels:
- "traefik.http.routers.whoami.rule=Host(`whoami.shanyue.tech`)"
- traefik.http.routers.whoami.rule=Host(`whoami.shanyue.tech`)
- traefik.http.routers.whoami.tls=true
- traefik.http.routers.whoami.tls.certresolver=le
# environments:
# TMUX

Expand Down
4 changes: 2 additions & 2 deletions deploy-fe.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ tags:

<!--more-->

+ 原文地址: [前端部署演化史](https://shanyue.tech/op/deploy-fe)
+ 系列文章: [个人服务器运维指南](https://shanyue.tech/op)
+ 原文地址: [前端部署演化史](https://shanyue.tech/op/deploy-fe.html)
+ 系列文章: [个人服务器运维指南](https://shanyue.tech/op/)

> 如果本篇文章能够对你有所帮助,可以帮我在 [shfshanyue/op-note](https://github.com/shfshanyue/op-note) 上点个 star
Expand Down
89 changes: 0 additions & 89 deletions introduction.md

This file was deleted.

109 changes: 109 additions & 0 deletions traefik-https.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# 在 traefik 中为服务开通 https

`https` 已经成为一个现代网站的标配,以至于当一个网站没有 `https` 时,某些浏览器都会把它标识为不安全。而除了安全方面,`https` 对网站的SEO也影响很多,而对于某些新型的浏览器 API,也只有在 `https` 下才能使用。不管怎么说,`https` 也成为一个网站的刚需。

而当你使用了 `traefik` 作为反向代理时,你可以配置 `ACME` 自动为域名提供证书,只需几行即可解决问题。免费的证书,当然是通过 `Let's Encrypt` 来解决。

## ACME 配置

通过它可以很方便地自动签发证书并且自动续期,我们在 `traefik.toml` 中进行相关配置

``` toml
[certificatesResolvers.le.acme]
email = "[email protected]"
storage = "acme.json"

[certificatesResolvers.le.acme.tlsChallenge]
```

其中,`storage` 指存放证书的位置

## Traefik 容器配置

在配置好 `traefik.toml` 配置完成后,我们需要修改 `traefik` 容器启动的相关配置

1. 暴露 443 端口
1. 挂载 acme.json,持久化证书

由于 `acme.json` 是一个文件,我们现在宿主机中创建它

``` bash
$ touch acme.json
$ docker-compose up
```

随后启动容器,配置文件如下

``` yaml
version: '3'

services:
reverse-proxy:
image: traefik:v2.0
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- ./traefik.toml:/etc/traefik/traefik.toml
- ./acme.json:/acme.json
- ./log:/log
- /var/run/docker.sock:/var/run/docker.sock
container_name: traefik
env_file: .env
labels:
- "traefik.http.routers.api.rule=Host(`traefik.shanyue.local`)"
- "traefik.http.routers.api.service=api@internal"
```
## 服务配置
如果你需要为你的服务提供 https 流量,只需要添加两行代码
``` yaml
labels:
- traefik.http.routers.whoami.tls=true
- traefik.http.routers.whoami.tls.certresolver=le
```
我们依然使用 `whoami` 做测试,`docker-compose.yaml` 文件内容如下

``` yaml
version: '3'
services:
whoami:
image: containous/whoami
labels:
- traefik.http.routers.whoami.rule=Host(`whoami.shanyue.tech`)
- traefik.http.routers.whoami.tls=true
- traefik.http.routers.whoami.tls.certresolver=le
# environments:
# TMUX

networks:
default:
external:
name: traefik_default
```
服务启动后,使用 `curl` 测试服务是否正常工作,我们可以看到 `X-Forwarded-Proto` 为 `https`,配置成功

``` bash
$ curl https://whoami.shanyue.tech
Hostname: c9c3cc850e2b
IP: 127.0.0.1
IP: 172.18.0.2
RemoteAddr: 172.18.0.3:35320
GET / HTTP/1.1
Host: whoami.shanyue.tech
User-Agent: curl/7.29.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 59.110.159.217
X-Forwarded-Host: whoami.shanyue.tech
X-Forwarded-Port: 443
X-Forwarded-Proto: https
X-Forwarded-Server: 9d783174aca9
X-Real-Ip: 59.110.159.217
```
2 changes: 2 additions & 0 deletions traefik.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ Creating whoami_whoami_3 ... done

## Traefik Dashboard

![traefik dashboard](./assets/traefik-dashboard.png)

`traefik` 默认有一个 `dashboard`,通过 `:8080` 端口暴露出去。我们可以在浏览器中直接通过 `<IP>:8080` 访问,但是

1. 使用 `IP` 地址肯定不是特别方便,此时我们可以配置 `Host`
Expand Down
2 changes: 2 additions & 0 deletions when-server-2019.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ $ rsync ~/Documents/tmux.png dev:/path/Documents/blog/op/assets/dev-env.png

## traefik

![traefik dashboard](./assets/traefik-dashboard.png)

前后端需要做一个反向代理,选择了 traefik,更方便的服务配置以及服务发现,只需要配置容器的 `labels` 就可以部署成功

另外 `traefik` 可以很方便的自动生成 ssl/tls 证书,为你提供 https 服务
Expand Down

0 comments on commit 5411c82

Please sign in to comment.