本文记录 AlmaLinux OS 9 系统下个人服务器环境搭建过程,以便后续查阅。
服务器主要用作部署 Web 服务,生态为 Node.js 相关。
登入服务器
默认是以root
用户登录的,之后会切换到普通用户,不再使用root
用户登录
检查系统版本
uname -a
cat /etc/redhat-release
lsb_release -a
hostnamectl
配置用户
添加普通用户
用户名自行修改并设置密码
useradd -m -s /bin/bash yumine
passwd yumine
授予普通用户 sudo 权限
tee /etc/sudoers.d/yumine <<< 'yumine ALL=(ALL) ALL'
chmod 440 /etc/sudoers.d/yumine
检查 SELinux 状态
个人选择关闭 SELinux
vi /etc/selinux/config
sestatus
修改 SSH 配置
开放新端口
首先开放一个新端口,以便切换 SSH 默认的22
端口,例如2333
firewall-cmd --permanent --zone=public --add-port=2333/tcp
安装 nano 编辑器
然后安装 nano 编辑器,使用教程:https://p3terx.com/archives/linux-nano-tutorial.html
dnf install nano
修改 SSH 配置文件
nano /etc/ssh/sshd_config
主要修改的地方:
1. 禁止 root 登录
找到 PermitRootLogin
,修改为:
PermitRootLogin no
2. 修改 SSH 端口
找到 Port
,修改为:
Port 2333
重启 sshd 服务
service sshd restart
此时可以新建一个连接验证配置是否生效,注意原先的不要关,以便有问题的话再改回来
配置 BBR(按需)
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
cat /etc/sysctl.conf
sysctl -p
sysctl net.ipv4.tcp_available_congestion_control
lsmod | grep bbr
切换到普通用户
接下来的命令都切换到刚才新建的普通用户下运行,用户名改成自己的
su yumine
确认 GCC 版本
gcc --version
g++ --version
安装 Git
sudo dnf install git
git --version
配置 nanorc
curl https://raw.githubusercontent.com/scopatz/nanorc/master/install.sh | sh
安装 epel-release
sudo dnf install epel-release
安装 nginx 并开放 HTTP(S) 端口
dnf search nginx
sudo dnf install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
ps -ef | grep nginx
nginx -V
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
安装 btop 和 ncdu
btop
是资源监控工具,ncdu
是磁盘占用检查工具
sudo dnf install btop
btop
sudo dnf install ncdu
sudo ncdu .
安装 Deno/Bun/Node.js
curl -fsSL https://bun.sh/install | bash
source ~/.bashrc
bun --help
bun -v
# Deno 的版本可通过 `-s` 指定
curl -fsSL https://deno.land/install.sh | sh -s v1.46.3
source ~/.bashrc
deno -v
curl -fsSL https://fnm.vercel.app/install | bash
source ~/.bashrc
fnm use --install-if-missing 22
node -v
npm -v
安装常用 npm 包
npm i -g tldr
npm i -g yarn
npm i -g pnpm
npm i -g pm2
pm2 使用
# 开机启动
pm2 startup
# 新增进程,最大内存占用自行修改或删掉
pm2 start "deno run -A main.js" --name "demo" --max-memory-restart 500M
# 同步配置
pm2 save
# 进程列表
pm2 ls
# 停止进程
pm2 stop demo
# 重启进程
pm2 restart demo
# 日志查看
pm2 log demo
安装 ffmpeg
方式一
sudo dnf install ffmpeg
ffmpeg -version
方式二
dnf 里的版本可能比较老,如果需要新版本可以手动安装
链接到 https://github.com/BtbN/FFmpeg-Builds/releases 获取新的
mkdir -p ~/env
cd ~/env
wget https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
tar -xvf ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
mv ffmpeg-n7.1-latest-linux64-gpl-7.1/bin/ffmpeg .
rm -rf ffmpeg-n7.1-latest-linux64-gpl-7.1
rm -rf ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
./ffmpeg -version
配置 .bashrc
然后编辑 ~/.bashrc
,将~/env
路径添加到环境变量 Path 里,顺带配置一下 alias 和历史记录条数
# ~/.bashrc
# custom env path
export PATH="/home/yumine/env:$PATH"
# alias
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias pn='pnpm'
alias g='git'
alias prs='pm2 restart'
alias pl='pm2 ls'
alias plg='pm2 log'
HISTSIZE=500 # 增大内存中保存的历史条数
HISTFILESIZE=10000 # 增大 .bash_history 文件保存的历史条数
运行source ~/.bashrc
后生效,之后就根据个人需要进行应用部署啦
SSH 工具推荐使用 Xterminal,编辑文件、传输文件、资源监控什么的都挺好用
Reference
https://p3terx.com/archives/improve-linux-server-security.html
https://p3terx.com/archives/add-normal-users-with-adduser-and-useradd.html
https://p3terx.com/archives/linux-nano-tutorial.html
题图 PID:96542934
fin.