Linux服务器运维踩坑记录:Git服务搭建与日常管理

Linux服务器运维踩坑记录:Git服务搭建与日常管理

这篇文章是我在2021年管理Linux服务器时遇到的各种问题和解决方案的整理,涉及Git服务、Redis、SSH、监控脚本等方面。

CentOS搭建Git服务器

安装Git

1
2
3
4
yum install -y git

# 验证安装
git --version

创建Git管理账户

1
2
3
4
5
6
# 添加git账户
adduser git

# 设置git密码
passwd git
# 按提示输入两次密码确认

创建Git仓库

1
2
3
4
5
6
7
8
9
10
11
# 切换到git账号
su git

# 进入git用户主目录
cd /home/git

# 创建仓库文件夹并初始化
mkdir test.git && cd test.git
git init --bare

# 成功输出:Initialized empty Git repository in /home/git/test.git/

设置目录权限

1
2
3
4
chmod -R 777 *
git repo-config core.sharedRepository 0777
chgrp -R git *
git repo-config --list

Git免密SSH登录配置

服务器端(生成密钥):

1
2
3
4
5
6
7
ssh-keygen -t dsa
# 按回车使用默认文件id_dsa

cp id_dsa.pub authorized_keys
chmod 600 id_dsa
chmod 600 id_dsa.pub
chmod 600 authorized_keys

客户端配置:

1
2
3
4
5
6
7
# 将私钥id_dsa复制到客户端
chmod 600 id_dsa
ssh-agent bash
ssh-add id_dsa

# 免密克隆仓库
git clone xxx@xxx.com.cn:/data/gitrepo/xxx.git

解决Git Push错误

错误: error unpack failed unable to create temporary object directory

原因: 目录权限问题

解决方案:

1
2
3
4
chmod -R 777 *
git repo-config core.sharedRepository 0777
chgrp -R git *
git repo-config --list

Push时出现unpacker error:

1
git push --no-thin

禁止Git用户Shell登录

步骤一:创建git-shell-commands目录

1
2
su git
mkdir /home/git/git-shell-commands

步骤二:修改/etc/passwd文件

1
2
3
4
5
6
7
8
# 找到这行(注意1000可能是其他数字)
git:x:1000:1000::/home/git:/bin/bash

# 改为:
git:x:1000:1000::/home/git:/bin/git-shell

# 建议先复制一行再修改,保留原始配置做备份
# vim快捷键:yy复制行,p粘贴,:wq!保存退出

Redis安装与配置

Amazon Linux安装Redis

AWS EC2默认没有Redis源,需要下载Fedora的EPEL仓库:

1
2
3
4
5
6
7
8
# 安装EPEL仓库
yum install epel-release

# 如果找不到,手动安装
sudo amazon-linux-extras install epel

# 安装Redis
yum install redis

Redis服务管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动Redis
service redis start

# 停止Redis
service redis stop

# 查看状态
service redis status

# 查看进程
ps -ef | grep redis

# 开启自启动
chkconfig redis on

SSH安全配置

AWS EC2使用root登录

创建root密码:

1
sudo passwd root

切换到root:

1
su root

修改sshd配置文件:

1
2
3
4
5
6
vi /etc/ssh/sshd_config

# 修改以下配置:
PermitRootLogin yes
PasswordAuthentication yes
UsePAM no

重启SSH服务:

1
sudo /sbin/service sshd restart

限制特定用户SSH登录

允许指定用户:

1
2
3
4
5
# 编辑sshd_config
vi /etc/ssh/sshd_config

# 添加:
AllowUsers aliyun testxxxx

拒绝指定用户:

1
DenyUsers testxxxx aliyun

重启SSH服务:

1
sshd restart

取消密钥登录

1
2
3
4
5
6
7
vi /etc/ssh/sshd_config

# 找到RSAAuthentication、PubkeyAuthentication
# 将yes改为no

# 重启SSH
sshd restart

服务监控与自动重启

Node.js服务监控脚本

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
#!/bin/bash

code1=`ps -ef|grep node|wc -l`
code2=`netstat -lntup|grep mongod|wc -l`
code3=`netstat -lntup|grep nginx|wc -l`

# 邮件通知函数
my_mail(){
mail_list=("111@qq.com")
now_date=`date "+%Y-%m-%d %T"`
for i in ${mail_list[@]}
do
echo -e "服务器服务异常,IP 13.127.1.1,$now_date" |
mail -s "服务器服务异常" $i
done
}

# 重启服务函数
my_node(){
cd /data/nodejs/
sudo sh xxx-api.sh
sudo sh xxx-service.sh
}

n=1
count=1
while ((n<4))
do
if [ $code1 -gt 3 ] && [ $code2 -gt 0 ] && [ $code3 -gt 0 ]
then
n=$(($n+1))
sleep 2
else
count=$(($count+1))
n=$(($n+1))
sleep 2
if [ $count -eq 3 ];then
my_mail
if [ $code1 -lt 3 ];then
my_node
fi
fi
fi
done

注意事项: 重启脚本中去掉pm2 logs xxxx等输出命令,否则可能遗留node日志进程,导致统计数量不准确。

定时执行监控

1
2
3
4
5
# 编辑crontab
crontab -e

# 每分钟检测一次
* * * * * sh /data/check/check_service.sh > /data/check/check_service.log 2>&1 &

通用服务监控脚本

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
#!/bin/bash

code1=`netstat -lntup|grep node|wc -l`
code2=`netstat -lntup|grep mongod|wc -l`

my_mail(){
mail_list=("a@a.com" "b@b.com")
now_date=`date "+%Y-%m-%d %T"`
for i in ${mail_list[@]}
do
echo -e "service is down,please check; $now_date" |
mail -s "service down" $i
done
}

n=1
count=1
while ((n<4))
do
if [ $code1 -gt 0 ] && [ $code2 -gt 0 ]
then
n=$(($n+1))
sleep 2
else
count=$(($count+1))
n=$(($n+1))
sleep 2
if [ $count -eq 3 ];then
my_mail
fi
fi
done

mailx邮件发送配置

安装mailx

1
2
3
4
5
6
7
8
# 关闭其他邮件工具
chkconfig postfix off
service postfix stop
chkconfig sendmail off
service sendmail stop

# 安装mailx
yum -y install mailx

配置mail.rc

1
2
3
4
5
6
7
8
9
10
vim /etc/mail.rc

# 添加以下配置:
set from=your_email@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=your_email@qq.com
set smtp-auth-password=your_auth_code
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs

注意: smtp-auth-password处填写QQ邮箱授权码,不是邮箱密码。

请求数字证书

1
2
3
4
5
6
7
8
9
10
mkdir -p /root/.certs/

echo -n | openssl s_client -connect smtp.qq.com:465 |
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs/./ -i qq.crt

certutil -L -d /root/.certs

发送测试邮件

1
2
3
4
5
6
# 基本格式
echo "邮件正文" | mail -s "邮件主题" xxx@163.com

# 从文件发送
cat file.txt | mail -s "邮件主题" xxx@163.com
mail -s "邮件主题" xxx@163.com < file.txt

AWS EC2磁盘管理

挂载数据盘

查看数据盘:

1
fdisk -l

分区数据盘:

1
2
fdisk -u /dev/nvme1n1
# 依次输入:p, n, p, 回车, 回车, 回车, p, w

创建文件系统:

1
2
3
4
5
# ext4格式
mkfs -t ext4 /dev/nvme1n1p1

# xfs格式(MongoDB推荐)
mkfs -t xfs /dev/nvme1n1p1

配置fstab:

1
2
3
4
5
6
7
8
9
10
# 备份
cp /etc/fstab /etc/fstab.bak

# 添加ext4分区信息
echo `blkid /dev/nvme1n1p1 | awk '{print $2}' | sed 's/\"//g'` \
/data ext4 defaults 0 0 >> /etc/fstab

# 或添加xfs分区信息
echo `blkid /dev/nvme1n1p1 | awk '{print $2}' | sed 's/\"//g'` \
/data xfs defaults 0 0 >> /etc/fstab

挂载文件系统:

1
2
mkdir /data
mount /dev/nvme1n1p1 /data/

磁盘扩容

扩容分区:

1
2
3
lsblk                       # 查看卷和分区信息
growpart /dev/nvme1n1 1 # 扩容分区
lsblk

扩容文件系统:

1
resize2fs /dev/nvme1n1p1

Node.js与PM2管理

解决sudo pm2命令找不到

问题原因: sudo使用secure_path限制命令路径

解决方案:

1
2
3
cd /usr/bin
ln -s /usr/local/bin/node node
ln -s /usr/local/bin/pm2 pm2

PM2定时重启

1
2
# 每周五定时重启
pm2 start app.js --node-args="--nouse-idle-notification" --cron '0 0 0 0 0 5'

crontab定时任务

crontab时间格式

1
minute hour day month week command
字段 范围 说明
minute 0-59 分钟
hour 0-23 小时
day 1-31 日期
month 1-12 月份
week 0-7 星期(0和7代表星期日)

特殊字符

符号 含义
* 所有可能的值
, 列表范围,如”1,2,5,7,8,9”
- 整数范围,如”2-6”
/ 间隔频率,如”*/10”表示每10分钟

crontab常用命令

1
2
3
4
5
6
7
8
9
10
11
# 安装crontab
yum install crontabs

# 列出定时任务
crontab -l

# 编辑定时任务
crontab -e

# 查看日志
tail -f /var/log/cron

实例

1
2
3
4
5
6
7
8
# 每分钟执行检查脚本
* * * * * sh /data/check/check_service.sh > /dev/null 2>&1 &

# 每小时执行备份
0 * * * * sh /data/backup/backup.sh

# 每天凌晨3点执行清理
0 3 * * * sh /data/clean/clean.sh

Nginx配置

worker_connections警告

错误: 1024 worker_connections exceed open file resource limit 256

解决方案:

1
ulimit -n 1024

以上就是我在2021年运维Linux服务器时的一些经验记录,希望能帮到遇到同样问题的朋友。