与 Nana 的日常 —— 从零开始的 Linux 调教指北(1)
(Nana 印象图 初版)
速览本文:
- FTP 配置
- 网络配置
- Cloudflared 内网穿透
- nginx 反向代理
- gunicorn [Python WSGI HTTP Server for UNIX]
- Supervisor 设置守护进程
FTP 配置
前置知识:【FTP 文件传输】
安装 vsftpd
什么是 vsftpd ?
vsftpd is a GPL licensed FTP server for UNIX systems, including Linux.
sudo apt install vsftpd
sudo systemctl start vsftpd
// 开启进程sudo systemctl enable vsftpd
// 开机启动
配置 FTP 用户
- 创建用户目录:
sudo mkdir /home/ftpuser
- 创建用户并关联用户文件夹,这样使用 ftpuser 用户登陆的时候,就可以直接登陆到 /home/ftpuser 下:
sudo useradd -d /home/ftpuser -s /bin/bash ftpuser
- 更改用户目录权限:
sudo chown ftpuser:ftpuser /home/ftpuser
- 重设 ftpuser 的密码:
sudo passwd ftpuser
配置 FTP 设置
sudo vi /etc/vsftpd.conf
# Uncomment this to enable any form of FTP write command.
#write_enable=YES
把这行注释解除.
vim /etc/pam.d/vsftpd
注释掉
#auth required pam_shells.so
重启 FTP 服务
sudo service vsftpd restart
测试连接
Reference:
- https://www.huaweicloud.com/articles/4fc98eb98f27a99aa1b06b0d71dab20a.html
- https://devanswers.co/vsftpd-550-permission-denied-ubuntu/
网络配置
Cloudflared 内网穿透
为什么要做内网穿透?
在没有做内网穿透之前,我们的 Nana 只能被 Peking-Visitor(也就是路由器管理的网络)和 Zerotier 创建的虚拟网络中被访问到.
而内网穿透就是为了让我们的 Nana 提供的服务能够被 public Internet 访问到.
什么是 Cloudflare?
[这里认为读者已经对域名,域名解析有了基本的理解]
Cloudflare.com 是一家域名解析商. 可以为域名提供解析服务.
例如:
[请自行探索 DNS 记录:什么是 A 记录?什么是 CNAME 记录?]
什么是 Cloudflared?
Cloudflare 这家公司十分大方,为用户们提供了隧道服务。
Cloudflare Tunnel requires the installation of a lightweight server-side daemon, cloudflared, to connect your infrastructure to Cloudflare.
cloudflared is an open source project maintained by Cloudflare.
daemon: 守护进程,需要常驻后台的进程
cloudflared: 我们的主人公,需要安装配置到 Nana 上.
软件下载
在这里下载对应的安装文件,然后安装.
我们直接下载二进制的文件 cloudflared-linux-arm
后上传到服务器.
sudo cp ./cloudflared-linux-arm /usr/local/bin/cloudflared
// 添加到 Path
sudo chmod +x /usr/local/bin/cloudflared
// 切换执行权限
cloudflared -v
// 验证是否安装成功
配置穿透
Before you start:
向 Cloudflare 添加一个域名并确认其已被接管
- 验证身份:
cloudflared tunnel login
,复制链接到浏览器,登录后选择域名,完成认证. - 创建隧道:
cloudflared tunnel create <NAME>
,随机生成隧道的 UUID. - 编辑配置文件
sudo vi ~/.cloudflared/config.yml
tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /root/.cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json
ingress:
- hostname: gitlab.widgetcorp.tech
service: http://localhost:80
- hostname: gitlab-ssh.widgetcorp.tech
service: ssh://localhost:22
- service: http_status:404
- 绑定隧道到域名
cloudflared tunnel route dns <UUID or NAME> <DOMAIN>
设置自动启动
sudo cloudflared service install
✘ ubuntu@ubuntu /etc/cloudflared sudo mv ~/.cloudflared/config.yml /etc/cloudflared
ubuntu@ubuntu /etc/cloudflared sudo cloudflared service install
2021-08-23T14:16:19Z INF Using Systemd
2021-08-23T14:16:21Z INF systemctl daemon-reload
设置完成.
Reference:
- https://www.jianshu.com/p/8fdd0e3b7339
- https://blog.zapic.moe/Archives/Tutorial-176.html
- https://docs.pi-hole.net/guides/dns/cloudflared/#armhf-architecture-32-bit-raspberry-pi
nginx 反向代理
nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev.
什么是正向代理?什么是反向代理?
正向代理(forward proxy):是一个位于客户端和目标服务器之间的服务器(代理服务器),为了从目标服务器取得内容,客户端向代理服务器发送一个请求并指定目标,然后代理服务器向目标服务器转交请求并将获得的内容返回给客户端。(例如 酸酸乳,幻影飞梭,蓝色宝灯 etc.)
反向代理(reverse proxy):是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。(例如我们本节的主角 nginx)
二者的功能不同,适用的场景也不同。
安装 Nginx
sudo apt install nginx
命令
nginx -s stop
关闭nginx -s reload
重载配置
常用功能介绍
- 提供静态文件(根据访问路径路由)
我们可以先去看一下默认配置,sudo vi /etc/nginx/nginx.conf
先把 user
改成 ftpuser
,给予文件读写权限.
可以看到,在 HTTP 节 的 Virtual Host Configs
中 include 了 sites-enabled 的所有文件. 也就是说,我们可以通过在这个目录中新建文件的方式,来进行站点的添加.
在文件的最后,我们可以看到一份默认站点的样子.
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
改一改,写一份自己的网站配置,然后丢到 sites-enabled 里面:
# 10001-TestPage.conf
server {
listen 10001;
listen [::]:10001;
server_name server.cc7w.cf;
root /home/ftpuser/10001-TestPage;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Reference:
- https://cloud.tencent.com/developer/article/1418457
- https://nginx.org/en/docs/beginners_guide.html
- https://tengine.taobao.org/nginx_docs/cn/docs/http/request_processing.html
Gunicorn 绿色独角兽
什么是 gunicorn?
Gunicorn (Green Unicorn) 是 Python Web 服务器网关接口HTTP服务器。
对于 Python App 接口,我们显然是不可能像上面的静态文件一样,直接提供给用户,因此便需要 Gunicorn 进行转接.
我还没有安装 pip ?
sudo apt install python3-pip
我的 pip 还没有连接 Tsinghua 源?
sudo pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
安装
sudo pip3 install gunicorn
在之前进行 pip 包管理时,不同用户安装的包会存储在不同的地方,有些需要修改 Path 的包也没能获得足够的权限,懒得改配置文件的 c7w 决定所有的包安装干脆全用 sudo 完事.
以命令方式运行 Gunicorn 的参数
-c CONFIG_PATH
以配置文件运行-b BIND
Support(HOST)
,(HOST):(PORT)
, orunix:(PATH)
(Use socket)-w WORKERS
线程数
通过配置文件写入
这里我们选择通过配置文件运行 Gunicorn.
import multiprocessing
import uvicorn
debug = False
reload = True
loglevel = 'debug'
bind = 'unix:/home/ftpuser/10002-TikTokTeenBackend/gunicorn/gunicorn.sock'
pidfile = '/home/ftpuser/10002-TikTokTeenBackend/gunicorn/gunicorn.pid'
logfile = '/home/ftpuser/10002-TikTokTeenBackend/gunicorn/debug.log'
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = 'uvicorn.workers.UvicornWorker' # 这里一般用 sync
# 但因为我要部署 FastAPI 项目,所以就用了 uvicorn
注意到,我们的 bind 填写了本地 socket,这样可以让 nginx 与 gunicorn 通过本地 socket 通信.
然后在 nginx 里新建一个站点.
server {
listen 10002;
root /home/ftpuser/10002-TikTokTeenBackend/Backend;
server_name server.cc7w.cf;
location / {
proxy_set_header x-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass unix:/home/ftpuser/10002-TiktokTeenBackend/gunicorn/gunicorn.sock
}
}
Supervisor 设置守护进程
什么是 Supervisor?
Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon(守护进程,一直在运行的进程),并监控进程状态,异常退出时能自动重启。
安装
sudo apt install supervisor
这里真的很坑,c7w 查资料发现是 Python 开发的后,没动脑子直接
pip3 install supervisor
,发现配置无效后sudo pip3 install supervisor
,发现还是没有默认配置后去 stackoverflow 了一波甚至补全了配置文件,但是还是没有成功运行. 仔细阅读安装方式后才发现应该用 apt 装.
配置
[program:10002-TikTokTeenBackend]
command=/usr/local/bin/gunicorn -c gunicorn_config.py main:app
directory=/home/ftpuser/10002-TikTokTeenBackend/backend
user=root
autorestart=true
startretires=3
这样我们的 App 应该至少是能够跑起来了.
Reference:
TO BE CONTINUED