vps初始化设置

 
本文最后更新于

vps 一些基本设置

本文以 Debian 10 x64 为例。

部分内容参考了这里

修改密码

# 当前用户
passwd

其他初始化操作

# 重启后自动开启网络
echo 'auto ens3' >> /etc/network/interfaces
echo 'nameserver 8.8.8.8' >> /etc/resolv.conf
# 更新一下
apt-get update -y
apt-get upgrade -y
# 安装 ssh 
apt-get remove openssh-client -y
apt-get install openssh-client -y
apt-get install openssh-server -y

# 添加配置
echo "alias ls='ls --color'" >>  ~/.bashrc
echo "alias ll='ls -l'" >>  ~/.bashrc

echo "export LANG='en_US.UTF-8'" >>  /etc/profile
echo "export LC_CTYPE='zh_CN.UTF-8'" >>  /etc/profile
echo "export TZ='Asia/Shanghai'"  >> /etc/profile
echo "export TIME_STYLE='+%Y/%m/%d %H:%M:%S'" >>  ~/.bashrc

source ~/.bashrc

常用命令

netstat -lntp # 查看端口占用
ps aux # 查看进程

# 查看指定目录使用情况
du -sh {目录}

# iptables
iptables -L # 查看
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 端口 -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport 端口 -j ACCEPT
# 删除防火墙规则,内容一样把 -I 换成 -D 就行了:
iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 端口 -j ACCEPT
iptables -D INPUT -m state --state NEW -m udp -p udp --dport 端口 -j ACCEPT

配置 ssh 服务

ssh 客户端配置为/etc/ssh/ssh_config

ssh 服务器配置为 /etc/ssh/sshd_config

可查看配置文件内容,注释掉的为默认配置,取消注释的为修改过的配置。

使用密钥登录分为 3 步:

  1. 生成密钥(公钥与私钥);
  2. 放置公钥到服务器~/.ssh/authorized_key文件中;
  3. 配置ssh客户端使用密钥登录。

生成秘钥可执行ssh-keygen -t rsa

服务器配置/etc/ssh/sshd_config可参考这里,建议修改如下:

# Protocol 2,1
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
#禁用root账户登录,非必要,但为了安全性,请配置
#PermitRootLogin no
#有了证书登录了,就禁用密码登录吧,安全要紧
#PasswordAuthentication no

直接复制下面的脚本,修改{.ssh/id_rsa.pub}为自己生成的公钥,执行即可:

echo 'RSAAuthentication yes' >> /etc/ssh/sshd_config
echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_config
echo 'AuthorizedKeysFile %h/.ssh/authorized_keys' >> /etc/ssh/sshd_config
mkdir ~/.ssh
# 可添加多个公钥到 ~/.ssh/authorized_keys
echo {.ssh/id_rsa.pub} >> ~/.ssh/authorized_keys
service ssh restart

ssh 登录报错

使用 putty 时配置好秘钥登录,提示Couldn't load this key (OpenSSH SSH-2 private key(old PEM format))。可参考此篇文章。简述如下:

  • 使用 puttygem.exe
  • 单击Conversations下拉列表的Import Key
  • 选中自己的私钥(~/.ssh/id_rsa
  • 可修改key comment(描述),选择Save private key,保存为后缀是.ppk的文件
  • 在 putty.exe 的 settings -> Connection -> SSH -> Auth,设置私钥为刚保存的*.ppk文件

奇怪问题

使用 testing 分支,更新 openssh-server 到最新版本后,可能在重启 debian 后过很久才可以 ssh 登录。通常发生在 debian 安装在虚拟机中时,若使用 console 登录一次 debian,则远程的 ssh 即可立即登录,否则需等待很久才可以登录甚至无法登录。

想到的解决办法,将 openssh-server 版本降级。

apt install openssh-client/stable
apt install openssh-server/stable openssh-sftp-server/stable
# 阻止更新
echo openssh-client hold | sudo dpkg --set-selections
echo openssh-server hold | sudo dpkg --set-selections
echo openssh-sftp-server hold | sudo dpkg --set-selections
# 恢复更新
echo openssh-client install | sudo dpkg --set-selections
echo openssh-server install | sudo dpkg --set-selections
echo openssh-sftp-server install | sudo dpkg --set-selections

另外也可以在官网安装最最新版的 openssh-server:参考这里

putty 修改backspace键乱码

settings -> terminal -> keyboard ,The Backspace key勾选“Control-H”,The Home and End keys勾选“VT100+”。

安装 Brook Server

『原创』一个优秀的跨平台 Socks5代理软件 —— Brook 一键安装管理脚本

wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/brook.sh && chmod +x brook.sh && sh brook.sh
# 其他操作
启动:/etc/init.d/brook start
停止:/etc/init.d/brook stop
重启:/etc/init.d/brook restart
查看状态:/etc/init.d/brook status
安装目录:/usr/local/brook
配置文件:/usr/local/brook/brook.conf
日志文件:/usr/local/brook/brook.log(注意正常情况下,日志是不会记录使用信息的,只会记录报错)

备用:在我的 gitee 私人仓库。

安装 Goflyway Server

『原创』Go语言轻量化 HTTP Tunnel 代理工具 —— GoFlyway 一键安装管理脚本

wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/goflyway.sh && chmod +x goflyway.sh && bash goflyway.sh
# 伪装 http 站点时可选填写
http://ftp.jp.debian.org/debian/

有关使用 Cloudflare CDN 时端口限制的说明

安装 server-status

『原创』多服务器 云探针、云监控 —— ServerStatus 一键管理脚本

wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/status.sh && chmod +x status.sh

# 客户端管理菜单
./status.sh c
# 服务端管理菜单
./status.sh s

如果在安装 server-status 之前安装了 Caddy,设置 http 服务时选择 N。并手动配置 Caddy 的配置文件设置 root 目录为/usr/local/ServerStatus/web(安装服务端时有提示)

安装 Caddy

『原创』Go语言 跨平台支持的极简 HTTP Server —— Caddy一键安装脚本

利用 Caddy 非常简单的部署 反向代理/镜像(支持自签SSL证书)

利用 Caddy FileBrowser扩展 非常简单的部署 私人网盘/在线文件管理器

wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/caddy_install.sh && chmod +x caddy_install.sh && bash caddy_install.sh install http.webdav,tls.dns.cloudflare
# 新建虚拟主机文件夹
mkdir /home/www/ && mkdir /home/www/goflyway

配置 Caddy

默认 Caddy 会监听 80 和 443 端口,无论配置与否。

Caddy自动申请SSL证书位置:/.caddy/acme/acme-v01.api.letsencrypt.org/sites/xxx.xxx(域名)/

# 域名反向代理
echo "http://toyoo.pw {
 gzip
 proxy / http://xxx.xx
}" >> /usr/local/caddy/Caddyfile

# 手动指定SSL证书,不会重定向http到https,需手动配置redir重定向
echo "http://xxx.xxx ,http://www.xxx.xxx {
redir https://xxx.xxx{url}
}
xxx.xx {
gzip
tls /root/xxx.crt /root/xxx.key
proxy / https://www.google.com.hk
}" > /usr/local/caddy/Caddyfile

# 网页加密
basicauth /PATH user passwd
# 日志。放到 /tmp 文件夹的话,每次重启都会自动清空日志的。
log /tmp/caddy_1.log

启动显示成功,但是实际未运行,应该去查看启动日志:

tail -f /tmp/caddy.log

简单设置:

# 以下全部内容是一个整体,是一个命令,全部复制粘贴到SSH软件中并一起执行!
# :8080 指的是监听 0.0.0.0:8080 。
# /home/www/goflyway/ 这是虚拟主机文件夹,如果不一样请修改
echo ":8080 {
root /home/www/goflyway/
timeouts none
gzip
}" > /usr/local/caddy/Caddyfile

做成目录列表程序下载网站,添加browse即可。

# 以下全部内容是一个整体,是一个命令,全部复制粘贴到SSH软件中并一起执行!
# :8080 指的是监听 0.0.0.0:8080 。
# /home/www/goflyway/ 这是虚拟主机文件夹,如果不一样请修改
echo ":8080 {
root /home/www/goflyway/
timeouts none
gzip
browse
}" > /usr/local/caddy/Caddyfile

启动 Caddy:/etc/init.d/caddy start

filebrowser

原 filebrowser 插件现已独立,官网:https://filebrowser.xyz

基本配置:

curl -fsSL https://filebrowser.xyz/get.sh | bash
filebrowser -d /usr/local/caddy/filebrowser.db config init
filebrowser -d /usr/local/caddy/filebrowser.db config set -p 1111
filebrowser -d /usr/local/caddy/filebrowser.db config set --locale zh-cn
filebrowser -d /usr/local/caddy/filebrowser.db config set --log /usr/local/caddy/filebrowser.log
filebrowser -d /usr/local/caddy/filebrowser.db config set --baseurl /admin
filebrowser -d /usr/local/caddy/filebrowser.db users add root password --perm.admin

安装代理缓存服务器 squid

apt install squid

官方 wiki:https://wiki.squid-cache.org/

也可在另一篇文章查看sqiud的具体配置详解。

命令开机自动运行

使用update-rc.d命令:

将执行的脚本放到/etc/init.d/并在文件开头添加:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          xda
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: xda's default scripts
# Description:       Run some scripts when os start
### END INIT INFO

check_running(){
        PID=`ps -ef |grep "$1" |grep -v "grep" |grep -v "init.d" |grep -v "service" |awk '{print $2}'`
        if [[ ! -z ${PID} ]]; then
                return 0
        else
                return 1
        fi
}

check_running "filebrowser"
if [[ $? -eq 0 ]]; then
        kill -9 ${PID}
fi
filebrowser -d /usr/local/caddy/filebrowser.db &

exit 0

然后执行:

update-rc.d <NAME> defaults

下面是网上找的方法

将命令添加到文件:

nano /etc/rc.local

注意文件开头添加:

#! /bin/sh
# -*- coding: utf-8 -*-

命令前加上nohup ,防止命令执行未结束导致无法往下执行。

certbot 获得 https 证书

Let’s Encrypt 的官方客户端。官网文档:https://certbot.eff.org/docs/

acme.sh 自动配置 https 证书

访问http://acme.sh,会自动跳转到 github:https://github.com/acmesh-official/acme.sh

wiki:https://github.com/acmesh-official/acme.sh/wiki/

安装与更新

# 安装
curl https://get.acme.sh | sh
# OR
wget -O -  https://get.acme.sh | sh
# 更新
acme.sh --upgrade
# 开启自动更新
acme.sh --upgrade --auto-upgrade
# 禁止自动更新
acme.sh --upgrade --auto-upgrade 0

安装结束,根据输出可看到:

  • 自动安装到~/.acme.sh/。并设置一个alias,可在~/.bashrc里查看。

  • 安装了cron job。用于自动更新证书。可运行crontab -l查看。

发布证书

仅发布

acme.sh --issue -d example.com -w /home/wwwroot/example.com

可以多个域名,多个-d

发布的证书被放在了~/.acme.sh/example.com/

不要用 web 服务器直接使用~/.acme.sh/里面的证书。必须复制出来再使用。下面可使用--install-cert来设置证书保存的位置。

使用 Apache 发布:

acme.sh --install-cert -d example.com \
--cert-file      /path/to/certfile/in/apache/cert.pem  \
--key-file       /path/to/keyfile/in/apache/key.pem  \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd     "service apache2 force-reload"

使用 Nginx 发布

acme.sh --installcert -d chandao.test.com --key-file /usr/local/nginx/ssl_cert/test.com/chandao.test.com.key --fullchain-file /usr/local/nginx/ssl_cert/test.com/chandao.test.com.cer --reloadcmd "service nginx force-reload"

上面的方式必须保证web服务器使用的是80/443端口。若端口封堵了,则需要 DNS 手动模式或者使用 DNS API 模式。

DNS 手动模式

acme.sh --issue --dns -d example.com -d www.example.com -d cp.example.com

按提示设置域名的txt 解析记录,等 DNS 解析生效,然后运行:

acme.sh --renew -d example.com

DNS API

如使用 CloudFlare,获取API key 后,运行【Test this token】里的命令,会获得CF_Account_ID

运行一次:

# Global API Key
export CF_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
export CF_Email="xxx@example.com"
# new API Token
export CF_Token="xxxx"
export CF_Account_ID="xxxx"
export CF_Zone_ID="xxxx"

发布证书:

acme.sh --issue --dns dns_cf -d example.com -d www.example.com

CF_TokenCF_Account_ID等变量会保存到~/.acme.sh/account.conf,下次运行acme.sh会自动获取

小内存增加 SWAP

dd if=/dev/zero of=/home/swap bs=1024 count=1024000 # 生成SWAP空间文件1G(bs*count)
/sbin/mkswap /home/swap # 创建SWAP分区
/sbin/swapon /home/swap # 激活SWAP分区
echo '/home/swap swap swap defaults 0 0' >> /etc/fstab # 重启后可以自动挂载

安装 docker

推荐资料:

https://blog.csdn.net/u012954706/article/details/79592099

https://yq.aliyun.com/articles/110806

按照官方文档:https://docs.docker.com/install/linux/docker-ce/debian/#install-docker-ce

Set up the repository

Update the apt package index:

apt-get update

Install packages to allow apt to use a repository over HTTPS:

apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \
     software-properties-common

Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add

Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

apt-key fingerprint 0EBFCD88

Use the following command to set up the stable repository

add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"

Install docker-ce

Update the apt package index.

apt-get update

Install the latest version of Docker CE, or go to the next step to install a specific version:

apt-get install docker-ce # 可加参数 -y

Verify that Docker CE is installed correctly by running the hello-world image.

docker run hello-world

后续可使用docker pull xxx来安装 docker 镜像,镜像被安装在/var/lib/docker/目录。存放在该目录下的containers 子目录。

安装 XAMPP

wget -O xampp7.2.run https://www.apachefriends.org/xampp-files/7.2.9/xampp-linux-x64-7.2.9-0-installer.run
chmod 777 xampp7.2.run
./xampp7.2.run

安装在/opt/lampp/下。默认网站的入口为/opt/lampp/htdocs/

apache 的 config 文件为/opt/lampp/apache2/conf/httpd.conf

使用非 80/443 端口时的 CDN 设置

cloudflare cdn 端口使用说明:https://support.cloudflare.com/hc/en-us/articles/200169156-Which-ports-will-Cloudflare-work-with-

     Cloudflare Support > Troubleshooting General > Troubleshooting 

Which ports will Cloudflare work with?

Cloudflare can only proxy traffic going over the HTTP/HTTPS ports listed below.

If your traffic is on a different port, you should create a subdomain and then add it as a record in your Cloudflare DNS zone file as something we don't proxy (gray cloud = no Cloudflare proxy or caching on a record).

The HTTP ports that Cloudflare support are:
80
8080
8880
2052
2082
2086
2095

The HTTPs ports that Cloudflare support are:
443
2053
2083
2087
2096
8443

For the Pro plan and above, you can block traffic on ports other than 80 and 443 using WAF rule id 100015: "Block requests to all ports except 80 and 443".

Ports 80 and 443 are the only ports:

- For HTTP/HTTPS traffic within China for zones that have the China Network enabled
- For Cloudflare Apps to be able to proxy on
- Where Cloudflare Caching is available 


亲测:若在 CloudFlare 开启了 Always use HTTPS,则设置非 80 端口的 http 则会无法访问。(因为会自动跳转为 https 访问)。可手动配置 rewrite http to https。

在此,强烈建议所有服务均使用 https 访问。