ffmpeg的常用选项

map选项

默认情况下只在输入文件中的每种流里选择第一个流。

需要指定放入输出文件的流就需要使用-map选项。

用法举例:

-map 0
From input index #0 (the 1st input) select all streams.

-map 1:a
From input index #1 (the 2nd input) select all audio streams.

-map 3:s:4
From input index #3 (the 4th input) select subtitle stream index #4 (the fifth subtitle stream)。

-map 0 -map -0:s
Will select all streams from input index #0 (the 1st input) except subtitles.

改变视频帧率

有两种方法可以改变输出文件的视频流帧率:

  1. 使用 -r 选项
  2. 使用 fps滤镜。
1
2
ffmpeg -i [inputfile] -r 30 [output]
ffmpeg -i [inputfiel] -filter:v fps=30 [output]

有两种方法可以改变输出文件的视频流帧率:

改变默认音轨

使用ffmpeg修改默认的音频轨道,先取消第二音轨的默认值,在设置为第一音轨。

1
ffmpeg -i input.mkv -map 0:0 -map 0:1 -map 0:2  -c copy -disposition:a:1 0 -disposition:a:0 default -y output.mp4

改变音频采样率

-ar 48k 44.1k 96k

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k output.flac

改变音频通道数量

-ac

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -ac:0 2 -filter:0 aformat=channel_layouts=stereo output.flac
1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -ac:0 6 -filter:0 aformat=channel_layouts=5.1 output.flac

改变音频位深 bit depth

-sample_fmt

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -sample_fmt s24 -ac:0 6 -filter:0 aformat=channel_layouts=5.1 output.flac

生成空白音频

可以使用anullsrc来生成静音音频:

1
ffmpeg -f lavfi -i anullsrc=r=48000:cl=stereo -t 0.875 /Users/redtux/Movies/converted/silent-audio.aac
  • sample_rate, r
    指定采样率,默认44100。

  • channel_layout, cl
    指定通道布局,详见 libavutil/channel_layout.cchannel_layout_map 定义,常见mono stereo 5.1

  • -t
    文件时长,单位事秒,不指定就一直生成。

截取视频片段

1
ffmpeg  -i 源文件名 -vcodec copy -acodec copy -ss 00:00:10 -to 00:00:15 目标文件名 -y

裁剪视频文件的多个部分,并将其合并成一个新的视频

调整播放速度

调整视频文件中视频速度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//2倍速播放
ffmpeg -i test.mp4 -an -filter:v "setpts=0.5*PTS" out_test.mp4

-i test.mp4是输入文件名

-an 将音频禁掉 (可以不加)

-filter:v 对视频进行处理

"setpts=0.5PTS" 设置时间戳参数PTS为原先的一半,可接受调整范围为[0.25,4]

out_test.mp4 输出视频文件

还可以在命令中加上指定fps(-r 60),使得不会丢帧

调整视频文件中音频速度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ffmpeg -i test.mp4 -filter:a "atempo=2.0" -vn out_test.mp4
-i 后满test.mp4是输入文件名

-filter:a 对音频进行处理

"atempo=2.0" 设置播放速度是原来的2倍 , 倍率调整范围[0.5, 2.0]

-vn 将视频禁掉 (可以不加)

out_test.mp4 输出视频文件

需要调整到4倍可以采取以下方法:

ffmpeg -i test.mp4 -filter:a "atempo=2.0,atempo=2.0" -vn out_test.mp4

同时调整视频文件的视频、音频:

1
ffmpeg -i test.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]"  out_test.mp4

如果不想丢帧就同时设置 -r ,把帧率设置为原帧率的2倍。

metadata的使用

1
ffmpeg -i /Users/redtux/Movies/Becoming.Jane.2007.1080p.BluRay.x265.2Audio.mp4  -metadata title="Becoming.Jane.2007.1080p.BluRay.x265.2Audio-RARBG" -map 0:0 -c:0 copy -metadata:s:0 title='Becoming.Jane.2007.1080p.BluRay.x265-RARBG' -metadata:s:0 handler='Becoming.Jane.2007.1080p.BluRay.x265-RARBG' -map 0:1 -c:1 copy -metadata:s:1 title="国语配音" -metadata:s:1 handler="国语配音" -metadata:s:1 language="chi"  -disposition:1 default -map 0:2 -c:2 copy -metadata:s:2 title="English" -metadata:s:2 handler="English" -metadata:s:2 language="eng"  -disposition:2  0 /Users/redtux/Movies/Becoming.Jane.2007.1080p.BluRay.x265.2Audio-RARBG.mp4

Ubuntu的SSH安全配置

查看登录日志文件:

1
sudo vim /var/log/auth.log

修改SSH的默认端口

1
iptables -A INPUT -p tcp -m tcp --dport 6022 -j ACCEPT

保存并重启iptables防火墙

1
2
service iptables save
service iptables restart

修改SSH的默认端口

1
sudo vim /etc/ssh/sshd_config

把 # Port 22 修改为 Port 6022

最好使用1024到65535之间的一个别人猜不到的端口号

重启ssh服务

1
sudo service sshd restart 

1
sudo /etc/init.d/ssh restart

可以用iptables检查端口是否开启

禁止SSH的root登录

修改 /etc/ssh/sshd_config 文件

1
PermitRootLogin no

重启ssh服务

1
sudo service sshd restart

禁用密码登陆,使用RSA私钥登录

1
2
ssh-keygen #在客户端生成密钥
ssh-copy-id myserver1 #将公钥添加至服务端

还需要配置服务端

1
2
3
sudo vim /etc/ssh/sshd_config
PasswordAuthentication no #禁止密码认证
PermitEmptyPasswords no #禁止空密码用户登录

重启ssh服务

1
sudo service sshd restart

使用 Fail2ban

Ubuntu 16.04 系统源里带有 denyhosts ,到了Ubuntu 20.04默认不再包含。DenyHosts现在几乎不再更新了,所以来使用Fail2ban

检查是否安装了特定软件包

1
2
sudo apt list --installed | grep denyhosts
sudo apt list --installed | grep fail2ban

安装fail2ban

1
sudo apt-get install fail2ban

配置fail2ban

配置文件在 /etc/fail2ban/jail.conf 。 在配置文件的[DEFAULT]区,可以在此定义所有受监控的服务的默认参数

1
2
3
4
5
6
7
8
9
10
11
12
13
[DEFAULT]
# 以空格分隔的列表,可以是 IP 地址、CIDR 前缀或者 DNS 主机名
# 用于指定哪些地址可以忽略 fail2ban 防御
ignoreip = 127.0.0.1/8 ::1

# 客户端主机被禁止的时长
bantime = 60m

# 查找失败次数的时长
findtime = 10m

# 客户端主机被禁止前允许失败的次数
maxretry = 5

根据上述配置,fail2ban会自动禁止在最近10分钟内有超过5次访问尝试失败的任意IP地址。一旦被禁,这个IP地址将会在1小时内一直被禁止访问 SSH 服务

保存配置后重启服务

1
sudo service fail2ban restart

查看fail2ban运行状态

验证fail2ban成功运行:

1
2
$ sudo fail2ban-client ping
Server replied: pong

检验fail2ban状态

1
2
3
4
$ sudo fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd

检验一个特定监狱的状态

1
sudo fail2ban-client status 

上面的命令会显示出被禁止IP地址列表

解锁特定的IP地址

1
sudo fail2ban-client set sshd unbanip 192.168.1.8

自建开源远程桌面工具Rustdesk中继服务器

Rustdesk支持Windows、macOS、Linux、iOS、Android、Web等几乎所有平台,客户端可以在官方网站下载,安装后自动分配id,立即可以使用。但是官方免费的服务器速度有点慢,官方还提供了服务器端程序,我们可以用docker来安装:

1
2
3
4
docker volume create rustdesk_server_data

docker run --name hbbs --net=host -v "rustdesk_server_data:/root" -d rustdesk/rustdesk-server:latest hbbs -r www.redtux.cn
docker run --name hbbr --net=host -v "rustdesk_server_data:/root" -d rustdesk/rustdesk-server:latest hbbr

这样自建的中继服务器就已经成功运行了,然后在自己的每个客户端设置中继服务器为自己的ip就可以了。

使用Acme.sh工具为Ubuntu安装免费ssl证书

安装acme.sh

1
curl https://get.acme.sh | sh -s email=my@example.com

或者

1
wget -O -  https://get.acme.sh | sh -s email=my@example.com

如果上面两个连接不成功,可以用下面国内的源:

1
2
3
git clone https://gitee.com/neilpang/acme.sh.git
cd acme.sh
./acme.sh --install -m my@example.com

acme会安装到~/acme.sh目录中,安装完成后重新加载Bash:

1
source ~/.bashrc

配置acme.sh

开启自动更新:

1
acme.sh --upgrade --auto-upgrade

选择默认 CA
目前 acme.sh 支持四个正式环境 CA,分别是 Let’s Encrypt、Buypass、ZeroSSL 和 SSL.com,默认使用 ZeroSSL,如果需要更换可以使用如下命令:

切换 Let’s Encrypt

1
acme.sh --set-default-ca --server letsencrypt

切换 Buypass

1
acme.sh --set-default-ca --server buypass

切换 ZeroSSL

1
acme.sh --set-default-ca --server zerossl

切换 SSL.com

1
acme.sh --set-default-ca --server ssl.com

切换 Google Public CA

1
acme.sh --set-default-ca --server google

使用 DNS 验证签发证书

家用网络都封了80端口,不能使用正常模式申请证书,只能使用DNS验证的模式申请,acme.sh 的 DNS API 模式申请证书:

1
acme.sh --issue --dns dns_cf -d example.com -d *.example.com

acme.sh 支持几十种 DNS 插件,常用的下面几种,:

阿里云 –dns dns_ali

1
2
export Ali_Key="1234"
export Ali_Secret="sADDsdasdgdsf"

Dnspod –dns dns_dp

1
2
export DP_Id="1234"
export DP_Key="sADDsdasdgdsf"

Godaddy –dns dns_gd

1
2
export GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
export GD_Secret="asdfsdfsfsdfsdfdfsdf"

AWS –dns dns_aws

1
2
export AWS_ACCESS_KEY_ID="sdfsdfsdfljlbjkljlkjsdfoiwje"
export AWS_SECRET_ACCESS_KEY="xxxxxxx"

Linode –dns dns_linode

1
export LINODE_API_KEY="xxxxxxxx"

证书申请完成后会提示证书位置:

1
2
3
4
[Sat Jul 29 11:51:54 AM UTC 2023] Your cert is in: /root/.acme.sh/redtux.cn/redtux.cn.cer
[Sat Jul 29 11:51:54 AM UTC 2023] Your cert key is in: /root/.acme.sh/redtux.cn/redtux.cn.key
[Sat Jul 29 11:51:54 AM UTC 2023] The intermediate CA cert is in: /root/.acme.sh/redtux.cn/ca.cer
[Sat Jul 29 11:51:54 AM UTC 2023] And the full chain certs is there: /root/.acme.sh/redtux.cn/fullchain.cer

安装证书

然后我们可以安装证书

Nginx

1
2
3
4
5
acme.sh --install-cert -d example.com \
--key-file /etc/nginx/ssl/example.com.key \
--fullchain-file /etc/nginx/ssl/example.com.crt \
--ca-file /etc/nginx/ssl/example.com.ca.crt \
--reloadcmd "systemctl restart nginx"

对应的 Nginx 配置指定证书文件

1
2
3
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_trusted_certificate /etc/nginx/ssl/example.com.ca.crt;

Apache

1
2
3
4
5
acme.sh --install-cert -d example.com \
--key-file /etc/apache2/ssl/example.com.key \
--fullchain-file /etc/apache2/ssl/example.com.crt \
--ca-file /etc/apache2/ssl/example.com.ca.crt \
--reloadcmd "curl https://ssl-config.mozilla.org/ffdhe2048.txt >> /etc/apache2/ssl/example.com.crt && systemctl restart apache2"

对应的 Apache 配置指定证书文件

1
2
SSLCertificateFile      /etc/apache2/ssl/example.com.crt
SSLCertificateKeyFile /etc/apache2/ssl/example.com.key

替代群晖音乐的工具:Navidrome

Linux下的安装

首先安装依赖,navidrome依赖ffmpeg:

1
2
3
sudo apt update
sudo apt upgrade
sudo apt install vim ffmpeg

然后创建navidrome工作的目录结构:

1
2
sudo install -d -o <user> -g <group> /opt/navidrome
sudo install -d -o <user> -g <group> /var/lib/navidrome

下载Navidrome

GitHub下载最新发布版本(用最新版的下载地址替换下面命令中的地址),解压到程序目录/opt/navidrome,赋予目录可执行权限。

1
2
3
wget https://github.com/navidrome/navidrome/releases/download/v0.XX.0/navidrome_0.XX.0_Linux_x86_64.tar.gz -O Navidrome.tar.gz
sudo tar -xvzf Navidrome.tar.gz -C /opt/navidrome/
sudo chown -R <user>:<group> /opt/navidrome

创建配置文件

In the working directory, /var/lib/navidrome create a new file named navidrome.toml with the following settings.

MusicFolder = “
For additional configuration options see the configuration options page.

1
2
3
4
5
6
7
8
9
10
LogLevel = 'DEBUG'
ScanSchedule = '@every 24h'
TranscodingCacheSize = '150MiB'
MusicFolder = '/mnt/music'
Port = '6019'
DefaultLanguage = 'zh-Hans'
EnableSharing = true
EnableTranscodingConfig = true
LastFM.Language = 'zh-Hans'
UIWelcomeMessage = 'Redtuxs Family Music Server'

创建系统服务配置文件

Create a new file under /etc/systemd/system/ named navidrome.service with the following data. Make sure you replace and with the user and group you want to run Navidrome under.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[Unit]
Description=Navidrome Music Server and Streamer compatible with Subsonic/Airsonic
After=remote-fs.target network.target
AssertPathExists=/var/lib/navidrome

[Install]
WantedBy=multi-user.target

[Service]
User=<user>
Group=<group>
Type=simple
ExecStart=/opt/navidrome/navidrome --configfile "/var/lib/navidrome/navidrome.toml"
WorkingDirectory=/var/lib/navidrome
TimeoutStopSec=20
KillMode=process
Restart=on-failure

# See https://www.freedesktop.org/software/systemd/man/systemd.exec.html
DevicePolicy=closed
NoNewPrivileges=yes
PrivateTmp=yes
PrivateUsers=yes
ProtectControlGroups=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictNamespaces=yes
RestrictRealtime=yes
SystemCallFilter=~@clock @debug @module @mount @obsolete @reboot @setuid @swap
ReadWritePaths=/var/lib/navidrome

# You can uncomment the following line if you're not using the jukebox This
# will prevent navidrome from accessing any real (physical) devices
#PrivateDevices=yes

# You can change the following line to `strict` instead of `full` if you don't
# want navidrome to be able to write anything on your filesystem outside of
# /var/lib/navidrome.
ProtectSystem=full

# You can uncomment the following line if you don't have any media in /home/*.
# This will prevent navidrome from ever reading/writing anything there.
#ProtectHome=true

# You can customize some Navidrome config options by setting environment variables here. Ex:
#Environment=ND_BASEURL="/navidrome"

启动Navidrome服务

Reload the service daemon, start the newly create service, and verify it has started correctly.

1
2
3
4
sudo systemctl daemon-reload
sudo systemctl start navidrome.service
sudo systemctl status navidrome.service
sudo systemctl restart navidrome.service

如果服务成功启动,你就可以访问http://localhost:4533来访问web端.

让Navidrome随系统启动

1
sudo systemctl enable navidrome.service

用Nginx配置反向代理

反向代理配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
server {
listen 619 default_server;
listen [::]:619 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

#root /var/www/start;

# Add index.php to the list if you are using PHP
#index index.html index.htm index.nginx-debian.html;

server_name www.redtux.cn;

client_max_body_size 50000M;

location / {
proxy_pass http://192.168.0.6:6019;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_buffering off;
}
}

也可以Docker的方式安装,详见官方文档Navidrome官方网站开源音乐服务器,自建云端音乐播放器

客户端

Android手机可以从F-Droid直接安装Subtracks或者Ultrasonic,然后设置服务器地址就可以了。

PC可以使用Sonixd客户端,下载地址,下载对应系统的版本。

号外

其实还有一个自建音乐流媒体服务器koel——基于php fpm docker界面漂亮,唯一缺点就是手机客户端只能从Google play下载,国内只能用浏览器访问。

替代群晖相册的工具:immich

一直以来,我都用群晖自带的 Photos 来备份手机照片,因为除了群晖相册之外,还没用过其他好用的相册工具,自从上次群晖文件系统都变成只读之后就有了用Ubuntu替换群晖的想法。然后就在网络中发现了Immich,immich有Android手机客户端,可以直接从f-droid安装。

官方部署教程

下面简要记录部署教程:

下载安装部署必要文件

我们使用docker-compose部署,首先创建一个存储docker-compose.yml.env 文件的目录.

1
2
mkdir ./immich-app
cd ./immich-app

下载docker-compose.ymlexample.env文件

下载docker-compose.yml文件

1
wget https://github.com/immich-app/immich/releases/latest/download/docker-compose.yml

下载.env文件

1
wget -O .env https://github.com/immich-app/immich/releases/latest/download/example.env

或者用浏览器下载这两个文件,然后放到刚刚创建的目录中。

Note: If you downloaded the files from your browser, also ensure that you rename example.env to .env.

修改.env文件中的变量值

  • Populate custom database information if necessary.
  • UPLOAD_LOCATION 设置成你的照片存储路径.
  • DB_PASSWORD 设置数据库的密码
  • TYPESENSE_API_KEY 设置成随机字符串。

可以修改docker-compose.yml文件,在Postgres数据库那里增加5432端口映射,把Postgres容器的5432端口映射到本地,这样后期本地安装应用的时候可以用到docker的数据库。

创建容器

From the directory you created in Step 1, (which should now contain your customized docker-compose.yml and .env files) run docker-compose up -d.

Start the containers using docker compose command

1
docker-compose up -d     # or `docker compose up -d` based on your docker-compose version

现在可以访问http://<machine-ip-address>:2283开始创建你的管理员账户了。

设置Nginx反向代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
listen 618 default_server;
listen [::]:618 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

#root /var/www/start;

# Add index.php to the list if you are using PHP
#index index.html index.htm index.nginx-debian.html;

server_name www.redtux.cn;

client_max_body_size 50000M;

location / {
proxy_pass http://127.0.0.1:2283;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

# http://nginx.org/en/docs/http/websocket.html
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}

升级

Immich的版本迭代速度相当快,如果有新版本发布,可以切换到docker-compose.yml文件存放到目录,然后执行如下命令:

1
docker-compose pull && docker-compose up -d     # Or `docker compose up -d`

备份数据库

The recommended way to backup and restore the Immich database is to use the pg_dumpall command.

备份

1
docker exec -t immich_postgres pg_dumpall -c -U postgres | gzip > "/path/to/backup/dump.sql.gz"

恢复

1
2
3
4
5
6
7
docker-compose down -v  # CAUTION! Deletes all Immich data to start from scratch.
docker-compose pull # Update to latest version of Immich (if desired)
docker-compose create # Create Docker containers for Immich apps without running them.
docker start immich_postgres # Start Postgres server
sleep 10 # Wait for Postgres server to start up
gunzip < "/path/to/backup/dump.sql.gz" | docker exec -i immich_postgres psql -U postgres -d immich # Restore Backup
docker-compose up -d # Start remainder of Immich apps

桌面系统上传工具

依赖 Node.js 16以上版本和npm

安装node.js后执行如下命令安装immich桌面端上传工具:

1
npm i -g immich

把本地目录~/toUpload中的照片上传到服务器:

1
immich upload --key sikjo7P3o5jLddv3pfiDjH3RVfj4yIuptOti8SetM --server http://192.168.0.6:2283/api --recursive ~/toUpload

把本地目录~/toUpload中的照片上传到服务器,并加入到AlbumName相册中:

1
immich upload --key sikjo7P3o5jLddv3pfiDjH3RVfj4yIuptOti8SetM --server http://192.168.0.6:2283/api --recursive --album AlbumName ~/toUpload

选项:

Parameter Description
–yes / -y Assume yes on all interactive prompts
–recursive / -r Include subfolders
–delete / -da Delete local assets after upload
–key / -k User’s API key
–server / -s Immich’s server address
–threads / -t Number of threads to use (Default 5)
–album/ -al Create albums for assets based on the parent folder or a given name
–import/ -i Import gallery (assets are not uploaded)

号外

还有一个相册工具 PhotoPrism,这是一个优秀的成熟的相册,唯一缺憾就是没有手机客户端。

安装可道云Kodexplorer

可道云Kodexplorer是一个基于web的文件管理器,这里可以查看官方文档

nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
listen 620 default_server;
listen [::]:620 default_server;

root /var/www/kodexplorer;
index index.php
server_name www.redtux.cn;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
}

location ~ /\.ht {
deny all;
}
}

php库缺失 curl
php库缺失 mb_string
须开启php GD库,否则验证码、缩略图使用将不正常

配置优化

  1. 修改nginx的配置文件
1
2
3
4
5
6
7
8
client_max_body_size 500M;        
client_header_timeout 3600s;
client_body_timeout 3600s;
fastcgi_connect_timeout 3600s;
fastcgi_send_timeout 3600s;
fastcgi_read_timeout 3600s;
# 其他webserver相应修改类似限制;如apache需要修改LimitRequestBody
# 修改完成重启nginx生效: service nginx reload
  1. 修改php配置文件php.ini;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
post_max_size = 500M;
upload_max_filesize = 500M;
memory_limit = 500M;
max_execution_time = 3600;
max_input_time = 3600;

# php中设置set_time_limit无效(safe_mode=on时php代码中修改超时无效) php.ini
safe_mode = off

# php-fpm.conf 配置优化(超时时间; 4G内存推荐如下子进程配置)
request_terminate_timeout 3600
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 500
  • 注意:
    • 如果某一类例如exe文件不能上传,其他文件可上传, 一般是服务器(杀毒软件或防火墙)做了拦截误判,取消相应软件上拦截即可;
    • 超时时间设置; 如果经常有超大文件上传,php和nginx超时时间需要设置大一些;
    • 分片上传: 一个大文件切分成多个分片上传,所有片上传完成后服务器自动合并;一个分片上传失败只需要重传该分片即可;
    • 分片大小设置: 管理员登陆后台–基础设置–上传下载; 调整上传分片大小; 必须小于php.ini和nginx的限制;
    • 修改了php或nginx配置文件后,需要重启php-fpm和nginx;
    • 上传文件限制及超时时间可以根据自己需求设置; 超时时间需大于文件上传下载的时间,否则超时会导致操作失败;
  1. 修改可道云配置

设置方法:管理员登陆可道云进入后台 系统设置—基础设置—上传/下载

  • 设置分片大小: 推荐5M

大文件上传时切分成片进行并发上传,从而实现加速和断点续传,
推荐5M; 此值必须小于下述配置;否则会引起上传异常(上传失败,进度回退)
php.ini: post_max_size, upload_max_filesize ==> 5M
nginx: client_max_body_size ==> 5M;

  • 上传并发数量; 推荐15个并发;
  1. nginx + php-fpm上传优化

在nginx.conf中添加如下代码,参考更多nginx优化

1
2
3
4
#使用共享内存做临时存贮提高上传速度,共享内存需要大一些,否则上传大文件内存不足
client_body_in_file_only clean;
client_body_temp_path /dev/shm 1 2;
fastcgi_param REQUEST_BODY_FILE $request_body_file;

安装docker及docker-compose

卸载旧版本

在你安装docker之前一定要把系统中的旧版本卸载掉,一半系统发行商在官方apt源中带有docker,但是不是最新版的,如果你是全新安装的系统,你还没有安装过docker,那就可以省掉这一步。

需要卸载掉的非官方包包括:

  • docker.io
  • docker-compose
  • docker-doc
  • podman-docker

运行如下命令卸载这些包:

1
$for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

apt-get 可能会提示你这些包没有安装。

Images, containers, volumes, and networks 存储在 /var/lib/docker/ 目录中不会自动删除,如果你想要一个全新安装的docker,你最好手动删除他们。

安装途径

你可以根据你的需要使用如下方法安装docker:

  • 安装docker桌面版,桌面版包含了docker,这是最简单最快捷的方法;

  • 从Docker的官方apt库安装;

  • 自己手动安装;

  • 用脚本自动安装,只推荐用在测试和开发环境中。

从Docker的官方apt库安装

在新装系统第一次安装Docker之前,首先要设置Docker的官方apt库,设置apt库之后就可以使用apt命令安装了。

设置Docker的官方apt库

按照如下命令,安装一些工具和允许使用https协议:

1
2
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

添加Docker的官方GPG key:

1
2
3
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

按照如下命令设置Docker的官方apt库:

1
2
3
4
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

注意

If you use an Ubuntu derivative distro, such as Linux Mint, you may need to use UBUNTU_CODENAME instead of VERSION_CODENAME.

更新包索引:

1
sudo apt-get update

安装 Docker Engine

运行如下命令安装最新版Docker:

1
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

如果要安装特定版本Docker,可以先用如下命令列出可用版本:

1
2
# List the available versions:
$ apt-cache madison docker-ce | awk '{ print $3 }'

返回可用的版本号:

1
2
3
5:24.0.0-1~ubuntu.22.04~jammy
5:23.0.6-1~ubuntu.22.04~jammy
<...>

选择你期望的版本进行安装:

1
2
VERSION_STRING=5:24.0.0-1~ubuntu.22.04~jammy
sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin

运行一下 hello-world 镜像验证Docker是否安装成功。

1
sudo docker run hello-world

这条命令会下载hello-world镜像,并在一个新建的容器中运行。

到此Docker就已经安装成功了。

安装 docker-compose

上面已经安装了docker compose插件,可以通过docker compose命令运行,就是版本会旧一些,最新版的可以去docker-compose的Github下载二进制文件,然后复制到PATH路径中。

卸载 Docker Engine

1.Uninstall the Docker Engine, CLI, containerd, and Docker Compose packages:

1
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

2.Images, containers, volumes, or custom configuration files on your host aren’t automatically removed. To delete all images, containers, and volumes:

1
2
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

You have to delete any edited configuration files manually.

Docker服务控制

停止Docker服务

1
systemctl stop docker

启动Docker服务

1
systemctl start docker

重新启动Docker服务

1
systemctl restart docker

Docker常用命令

  • docker start : 启动一个容器
  • docker restart : 重新启动一个容器
  • docker stop : 关闭一个容器
  • docker status : 查看docker运行状态
  • docker ps : 列出正在运行的容器
  • docker volume : 新建docker卷
  • docker run : 从一个镜像放在新建的容器中运行。

其他详细命令可以参考官方文档

其他特定命令:

获取容器ID为282c7625119d的ip

1
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 282c7625119d

启动所有容器:

1
docker start $(docker ps -aq) 

关闭所有容器:

1
docker stop $(docker ps -aq) 

创建时设置容器开机自启:

1
docker run -d --restart=always -name 容器名 使用镜像

修改已有容器开机自启:

1
2
docker update --restart=always 容器ID(或者容器名)
(容器ID或者容器名根据实际情况修改)

对已有docker容器增加新的端口映射

1、 docker ps -a #查看容器的信息

2、 docker port 容器ID/容器名称 #查看容器的端口映射情况

3、 docker inspect 容器ID |grep Id #查找要修改容器的全ID

4、 cd /var/lib/docker/containers/全ID #进到全Id相同的目录,修改 其中的hostconfig.json 和 config.v2.json文件: #注意:若该容器还在运行中,需要先停掉 docker stop 容器ID #再停掉docker服务 systemctl stop docker

5、修改hostconfig.json如下 # 格式如:”{容器内部端口}/tcp”:[{“HostIp”:””,”HostPort”:”映射的宿主机端口”}]
“PortBindings”:{“22/tcp”:[{“HostIp”:””,”HostPort”:”3316”}],”80/tcp”:[{“HostIp”:””,”HostPort”:”180”}]}

{“5432/tcp”:[{“HostIp”:””,”HostPort”:”5432”}]}

6、修改config.v2.json在ExposedPorts中加上要暴露的端口 # 格式如:”{容器内部端口}/tcp”:{} “ExposedPorts”:{“22/tcp”:{},”80/tcp”:{}}

{“5432/tcp”:{}}

修改docker镜像源

镜像源参考
中国区官方镜像:https://registry.docker-cn.com
中科大源:https://docker.mirrors.ustc.edu.cn
阿里源:https://cr.console.aliyun.com
腾讯源:https://mirror.ccs.tencentyun.com
网易源:http://hub-mirror.c.163.com

1、关闭 Docker 服务

2、修改配置文件

创建或修改/etc/docker/daemon.json文件,
默认没有daemon文件,先创建。

1
vim /etc/docker/daemon.json

增加/更新”registry-mirrors”字段

1
2
3
4
5
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn"
]
}

注:需确认”daemon.json”的位置。

3、重启 Docker 服务

删除系统自带的snap

snap list可以查看已经安装的包,默认带的组件是core、lxd、snapd。

删除一个snap包

1
sudo snap remove docker

卸载snap

1
sudo apt autoremove --purge snapd

这样就会连带snap安装的所有软件全部都卸载掉了。

安装Bind9用于本地域名解析

对于家里的移动客户端,在家的时候用Wi-Fi,在外的时候用的移动网,那我们希望在家用Wi-Fi的时候可以直接连内网ip,而不需要连公网ip再转发到Nas的内网ip。以前用小米路由器的时候可以自定义hosts,非常方便。群晖中有一个DNS Server套件可以实现域名直接解析到内网IP,Ubuntu中可以安装Bind9实现同样功能。

Bind9的官方Howto在这里

Bind9安装

1
2
sudo apt update
sudo apt install bind9

Bind9的配置文件位于/etc/bind/,主要配置文件为下面的三个:

1
2
3
/etc/bind/named.conf
/etc/bind/named.conf.options
/etc/bind/named.conf.local

配置转发DNS地址,就是当本地没有解析记录的,就转发到指定的DNS地址解析。编辑配置文件/etc/bind/named.conf.options,指定转发DNS,一般填大网络公司的公共DNS,谷歌8.8.8.8;阿里:223.5.5.5等:

1
2
3
4
5
6
7
8
[...]

forwarders {
8.8.8.8;
114.114.114.114;
};

[...]

现在Bind9就可以工作了,重启Bind9:

1
sudo service bind9 restart

现在就可以在路由器的DHCP设置中将DNS服务器地址设置为NAS的IP了。

接下来我们要编辑区域文件/etc/bind/named.conf.local,增加我们要解析的域名Zone:

1
2
3
4
5
6
7
8
[...]

zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};

[...]

这一段记录example.com域名的解析文件为/etc/bind/db.example.com,接下来我们编辑/etc/bind/db.example.com/etc/bind目录中有一份样板文件db.local:

1
sudo cp /etc/bind/db.local /etc/bind/db.example.com

编辑区域文件/etc/bind/db.example.com,把localhost修改为你的域名example.com,保留后面的点,把127.0.0.1修改为本机IP,root.localhost. 修改为邮件地址,只不过要把@替换为.,同样末尾点保留。

增加一条A记录,把ns.example.com解析到DNS服务器IP,然后就可以添加其他子域名解析,比如我只有一台NAS,所有的记录都解析到NAS的IP上,修改完大概就是这样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
;
; BIND data file for local loopback interface
;
$TTL 604800
@ IN SOA ns.example.com. root.example.com. (
1 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS ns.example.com.
ns IN A 192.168.1.10

;also list other computers
www IN A 192.168.1.10
music IN A 192.168.1.10

这样内网访问www.example.commusic.example.com都会解析到192.168.1.10。

注意每次修改此文件,都需要把Serial值增加一次,这样重启的时候Bind9就会知道配置文件有修改。

很多人喜欢用修改日期来作为Serial的值,例如 2023080500,表示2023年8月5日第一次修改。

修改完成后记得重启bind9:

1
sudo service bind9 restart

这样移动设备连接内网的时候,从路由器获取的DNS服务器就是你的NAS地址,NAS就会把你的域名解析到NAS上。这样你就可以用一个域名根据网络环境自动切换内网和外网了。

当然,Bind9除了用来解析域名到局域网外,还可以用来解析某些被DNS劫持的域名,我们可以先去https://tools.ipip.net/dns.php,查询一下域名对应IP,再ping一下选一个延迟低丢包少的ip,然后配置bind9,把域名解析到查询到的ip上。