Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VPS : Setup Ubuntu VPS. #36

Open
hhstore opened this issue May 16, 2018 · 12 comments
Open

VPS : Setup Ubuntu VPS. #36

hhstore opened this issue May 16, 2018 · 12 comments

Comments

@hhstore
Copy link
Owner

hhstore commented May 16, 2018

📖 Abstract:

  • ✅ 云主机(Ubuntu 22.04) 配置 & 初始化教程.

💯 Related:

反向代理 & 负载均衡:

域名解析:

域名购买:

@hhstore
Copy link
Owner Author

hhstore commented May 16, 2018

云主机 + Ubuntu 配置教程

  • 从 0 配置 VPS 主机.

Checklist:

  • ✅ VPS 主机, 安装必备软件: zsh, git, curl, wget, docker 等.
  • ✅ VPS 平台控制台, 放开对外端口: 80, 8080, 7000, 9000, 等开发/测试端口.
    • 华为云, 控制台, 网络设置, 单独配置
    • Azure, 也需要单独配置.
  • ✅ 域名托管&解析平台: 将购买的域名, 解析到云主机 IP.
    • 配置 二级域名.

参考:

配置 VPS 教程:

@hhstore
Copy link
Owner Author

hhstore commented Dec 1, 2021

配置服务器:

网络设置/静态IP:

更改镜像源:

sudo bash -c "cat << EOF > /etc/apt/sources.list && apt update
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
EOF"

安装 clash:

下载包:

echo -e "export http_proxy=http://127.0.0.1:7890\nexport https_proxy=http://127.0.0.1:7890" >> ~/.bashrc


echo -e "export http_proxy=http://127.0.0.1:7890\nexport https_proxy=http://127.0.0.1:7890" >> ~/.zshrc

@hhstore
Copy link
Owner Author

hhstore commented Dec 1, 2021

从0配置 Ubuntu VPS 主机:

安装依赖:

  • git

  • zsh

  • root 用户:

apt update -y

apt install curl -y
apt install git -y
apt install zsh -y
apt install vim -y
apt install tmux -y

# ssh 登录保护:
apt install fail2ban -y

# Python 包安装: for docker-compose
apt install python3-pip -y

  • 非 root 用户:
sudo apt update -y


sudo apt install curl -y
sudo apt install git -y
sudo apt install zsh -y
sudo apt install vim -y
sudo apt install tmux -y

# set zsh:
sudo chsh -s $(which zsh)

创建 dev 用户:

# 参数说明:
# -m 创建用户的主目录,如 /home/xxx
# -g 指定用户所属的群组
# -G 指定用户所属的附加群组,将新用户加入 sudo 群组中,以获得 root 权限
# -s 指定用户登入后所使用的shell。注意使用 /bin/bash 之类的路径,而非 /usr/bin/bash
useradd -m -g users -G sudo -s /bin/zsh dev


# 设置密码:
passwd dev

配置 zsh:

# 切换到新用户(注意要切换身份+env)
su - dev

q: 先不定制zsh(未装oh-my-zsh)

# 进入用户目录
cd ~


# 安装
sh -c "$(wget https://github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"


# zsh theme:
wget https://raw.githubusercontent.com/skylerlee/zeta-zsh-theme/master/zeta.zsh-theme

#
cp zeta.zsh-theme ~/.oh-my-zsh/themes

# vim ~/.zshrc
ZSH_THEME="zeta"

# 自带:
ZSH_THEME="candy"

# 登出账号
exit

@hhstore
Copy link
Owner Author

hhstore commented Dec 2, 2021

配置 SSH key 登录:

非 server 版本 Ubuntu, 需要安装:

sudo apt install openssh-server -y

特别提醒:

  • ✅ 建议设置过程中, 多开几个 VPS 连接窗口.
  • ✅ 避免更改 ssh 配置过程种, 出现错误, 重启服务之后, 导致无法访问服务器.
  • ✅ 这样还可以在服务器上, 修改配置.

✅ 基于密码的方式登录 VPS 主机:

ssh root@22.33.22.33 -p 22

# 交互式输入密码

🔥 VPS 侧 设置:

  • ✅ 生成新的 ssh key.
cd ~
mkdir .ssh


ssh-keygen

cd .ssh/
cat id_rsa.pub >> authorized_keys

chmod 600 authorized_keys
chmod 700 ~/.ssh

image

更改配置:

  • ✅ 更改配置:
  • ✅ 在文件最末添加:
vim /etc/ssh/sshd_config


# update:

RSAAuthentication yes
PubkeyAuthentication yes

PermitRootLogin yes

# set ok, then add this:
PasswordAuthentication no

image

  • ✅ 重启 ssh: 如果发现不生效, 可能存在 额外的配置文件, 参数覆盖问题.
  • ✅ 注意检查 /etc/ssh/sshd_config.d 目录, 是否存在其他配置文件, 如无用, 可删除.
  • ✅ (实际也可在此, 新增文件来改配置项, 这样更安全)
# 检测配置文件是否异常
sshd -t


# 重启
service sshd restart

本机设置:

  • ✅ 将 VPS 生成的 ssh key 的私钥, 复制本地路径: ~/.ssh/vps/xxx-key`.
  • ✅ 注意, 文本复制后, 要在文件末尾, 追加一个换行符! 否则报错: 格式不对.
  • ✅ 文件 chmod 600 xxx 改一下访问权限.
# 放入本地路径: 追加一个 `换行符`
~/.ssh/vps/id_rsa.crypto

# 修正权限:
chmod 600 ~/.ssh/vps/id_rsa.crypto
  • ✅ 本地基于 ssh key 远程访问 VPS 主机:
# 示例方式:
ssh root@22.33.22.33 -p 22 -i ~/.ssh/vps/id_rsa.crypto

image

  • ✅ 测试正常后, 继续 VPS 禁用密码访问

禁用密码访问服务器:

  • ✅ vim /etc/ssh/sshd_config
# 设置登录成功, 再改此:
PasswordAuthentication no
  • ✅ 重启 ssh 服务:
service sshd restart
  • ✅ 验证基于密码的方式, 被禁用:
ssh root@22.33.22.33 -p 22

image

  • ✅ 还可以更改 SSH 的默认端口, 防止被爬虫乱扫.

配置 SSH Config

Host vps.hw    # 别名
    HostName xx.xxx.xx.xxx    # 替换 xx.xxx.xx.xxx 为服务器 ip 地址
    Port 22    # 端口,稍后会进行修改
    User xxx    # 用户名
    IdentityFile ~/.ssh/vps/key.hw    # 私钥文件
  • ✅ 连接:
# 命令行直接通过如下参数, 自动连接.
ssh vps.hy
image

@hhstore
Copy link
Owner Author

hhstore commented Dec 2, 2021

SSH 超时断开问题:

ClientAliveInterval 60
ClientAliveCountMax 3

@hhstore
Copy link
Owner Author

hhstore commented Dec 2, 2021

中间件定制:

Tmux 使用:

brew install tmux

@hhstore
Copy link
Owner Author

hhstore commented Dec 2, 2021

Linux 性能调优:

内核参数调优:

@hhstore
Copy link
Owner Author

hhstore commented Dec 2, 2021

安装 docker:

配置源:

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings

# 
# change:
# - https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg
# 
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

安装 docker 组件:

  • 相同指令:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

  • 验证安装成功:
sudo docker run hello-world

配置 docker:

run Docker as a non-privileged user:

dockerd-rootless-setuptool.sh install

配置开机启动:

# 开机启动
sudo systemctl enable docker
sudo systemctl start docker
sudo systemctl status docker

创建 docker 用户组:

  1. 创建 docker 用户组
# 1. 创建 docker 用户组
sudo groupadd docker

# 2. 添加用户到 docker 用户组:
sudo usermod -aG docker $USER

# 3. 重启
sudo service docker restart

# 4. exit, 需要退出当前终端, 重新登入, 才能生效.
exit

验证 docker 配置成功:

docker run hello-world

配置 docker-compose:

# 方法1:
apt install python3-pip -y

pip3 install docker-compose

# 方法2:
curl -sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py

python3 get-pip.py


pip3 install docker-compose

限制 docker 容器日志:

{
  "log-driver": "json-file",
  "log-opts": { "max-size": "50m", "max-file": "3" }
}



{
  "log-driver": "local",
  "log-opts": {
    "max-size": "50m"
  }
}
  • 重启 docker
sudo service docker restart

@hhstore
Copy link
Owner Author

hhstore commented Dec 2, 2021

docker 更新国内镜像源:

2024 更新源:

sudo mkdir -p /etc/docker


sudo tee /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": [
        "https://hub.uuuadc.top",
        "https://docker.anyhub.us.kg",
        "https://dockerhub.jobcher.com",
        "https://dockerhub.icu",
        "https://docker.ckyl.me",
        "https://docker.awsl9527.cn"
    ]
}
EOF



sudo systemctl daemon-reload
sudo systemctl restart docker

可能已经过期的源:

sudo mkdir -p /etc/docker


sudo tee /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io",
        "https://hub.uuuadc.top",
        "https://docker.anyhub.us.kg",
        "https://dockerhub.jobcher.com",
        "https://dockerhub.icu",
        "https://docker.ckyl.me",
        "https://docker.awsl9527.cn"
    ]
}
EOF



sudo systemctl daemon-reload
sudo systemctl restart docker

  • 验证修改成功:
# 自动走镜像源:
docker pull redis

docker run hello-world

@hhstore
Copy link
Owner Author

hhstore commented Dec 2, 2021

git 设置:

git config --global user.name "crypto"

git config --global user.email crypto@gmail.com

git config --global core.editor vim

@hhstore
Copy link
Owner Author

hhstore commented Jun 28, 2024

安全防护:

防止 SSH 登录扫描:

apt install fail2ban -y

开启 BBR,root 账户执行

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

@hhstore hhstore changed the title VPS : init VPS dev env. VPS : Setup Ubuntu VPS. Jun 28, 2024
@hhstore
Copy link
Owner Author

hhstore commented Jun 28, 2024

Linux 服务器状态检测:

内核信息查看:

hostnamectl

uname -a

cat /proc/version

查看磁盘空间:

df -Th
df -hl
df -h

查看内存:

# 单位 MB
free -m

cat /proc/meminfo

查看最大文件描述符:

cat /proc/sys/fs/file-max
9223372036854775807

ulimit -n
1024


  • 更改:
# vim /etc/security/limits.conf

* soft nofile 65535
* hard nofile 65535

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant