gitlab安装部署及问题解决
Linux常用命令——systemctl
一、systemctl 核心概念
systemctl 是 Linux 系统管理服务工具,用于控制 systemd 进程管理器中的服务单元(Unit)。它能启动、停止、重启服务,管理开机自启,监控服务状态等。所有操作均需 root 权限或通过 sudo 提升权限。
二、常用命令 & 示例
1. 基础操作
命令 作用 示例
systemctl start <服务> 启动服务 systemctl start nginx
systemctl stop <服务> 停止服务 systemctl stop postgresql
systemctl restart <服务> 重启服务 systemctl restart docker
systemctl enable <服务> 开机自启服务 systemctl enable cron
systemctl disable <服务> 取消开机自启 systemctl disable firewalld
systemctl status <服务> 查看服务状态(输出包含启动时间、日志等) systemctl status sshd
2. 状态检查进阶
查看服务状态码
active:运行中
inactive:停止
failed:启动失败
exit-code:查看退出码(如 systemctl show <服务> | grep ExitCode)
# 示例:检查 Nginx 是否正常运行
systemctl is-active --quiet nginx && echo "Nginx 正在运行" || echo "Nginx 停止"
3. 服务重载与配置
重载配置文件(修改服务文件后必须执行)
systemctl daemon-reload
重新加载特定服务配置
systemctl restart <服务> # 重启服务以应用新配置
三、高级用法
1. 服务优先级调整
修改服务文件的 Before= / After= 指定依赖关系
# /etc/systemd/system/myapp.service
After=network.target postgresql.service
2. 日志管理
查看服务实时日志
journalctl -u <服务> -f # -f 表示跟随日志
查看服务错误日志
journalctl -u <服务> --since "today" --level=error
四、故障排查指南
1. 服务启动失败
检查配置文件语法
systemctl daemon-reload # 重载前确保语法正确
systemctl edit <服务> # 临时修改配置(优先级高于原文件)
查看详细错误
journalctl -u <服务> --b # 查看本次启动的日志
2. 服务无法停止
强制终止
systemctl kill <服务>
3. 开机自启失败
检查服务文件路径是否正确(默认 /etc/systemd/system/)
确认 ExecStart 指令无误(避免死循环或权限问题)
五、实战场景
场景1:部署一个 Web 服务
编写服务文件 /etc/systemd/system/webserver.service:
[Unit]
Description=Apache Web Server
After=network.target
[Service]
User=www-data
ExecStart=/usr/sbin/apache2ctl start
ExecStop=/usr/sbin/apache2ctl stop
Restart=always
[Install]
WantedBy=multi-user.target
重载配置并启用开机自启:
sudo systemctl daemon-reload
sudo systemctl enable webserver
启动服务并检查状态:
sudo systemctl start webserver
systemctl status webserver
场景2:定时备份脚本
创建服务文件 /etc/systemd/system/backup.service:
[Unit]
Description=Daily Backup
[Service]
Type=simple
ExecStart=/usr/bin/backup-script.sh
Timezone=Asia/Shanghai # 指定时区
Restart=on-failure
[Install]
WantedBy=timers.target
创建定时器文件 /etc/systemd/system/backup.timer:
[Unit]
Description=Run backup daily at 3 AM
[Timer]
OnCalendar=daily
Persistent=true # 跨重启执行
启用并启动定时器:
sudo systemctl daemon-reload
sudo systemctl enable backup.timer
sudo systemctl start backup.timer
六、注意事项
权限问题:敏感操作(如 systemctl poweroff)需谨慎使用。
服务依赖:复杂服务需通过 After= / Before= 明确依赖关系。
安全性:避免直接修改系统自带服务文件(如 /lib/systemd/system/),应复制到 /etc/systemd/system/ 下。
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_26613259/article/details/146190742
------------------------------------------------------------------
一、GitLab简介
GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务。公司自用为GitLab,公有的为GitHub。
二、GitLab系统架构
git用户的主目录通常是/home/git(~git表示主目录路径),GitLab主要以/home/git用户身份安装在用户主目录中git。在主目录中是gitlabhq服务器软件所在的位置以及存储库(尽管存储库位置是可配置的)。裸存储库位于/home/git/repositories。GitLab是一个ruby on rails应用程序,因此可以通过研究ruby on rails应用程序的工作原理来学习内部工作的细节。为了通过SSH提供存储库,有一个名为gitlab-shell的附加应用程序,它安装在/home/git/gitlab-shell。
GitLab 应用程序是下面所述的所有组件的集合:
1. repository:代码库,可以是硬盘或 NFS 文件系统
2. Nginx:Web 入口
3. 数据库:包含以下信息:
- repository 中的数据(元数据,issue,合并请求 merge request 等)
- 可以登录 Web 的用户(权限)
4. Redis:缓存,负责分发任务
5. sidekiq:后台任务,主要负责发送电子邮件。任务需要来自 Redis
6. Unicorn:Gitlab 自身的 Web 服务器,包含了 Gitlab 主进程,负责处理快速/一般任务,与 Redis 一起工作。工作内容包括:
- 通过检查存储在 Redis 中的用户会话来检查权限
- 为 Sidekiq 制作任务
- 从仓库(warehouse)取东西或在那里移动东西
7. gitlab-shell:用于 SSH 交互,而不是 HTTP。gitlab-shell 通过 Redis 与 Sidekiq 进行通信,并直接或通过 TCP 间接访问 Unicorn
8. gitaly:后台服务,专门负责访问磁盘以高效处理 git 操作,并缓存耗时操作。所有的 git 操作都通过 Gitaly 处理
9. gitlab-workhorse:反向代理服务器,可以处理与 Rails 无关的请求(磁盘上的CSS、JS 文件等),处理Git Push/Pull 请求,处理到Rails的连接(修改由Rails发送的响应或发送给 Rails 的请求,管理 Rails 的长期 WebSocket 连接等)。
10. mail_room:处理邮件请求。回复 GitLab 发出的邮件时,GitLab 会调用此服务
Sidekiq. Unicorn 和 GitLab-shell 是GitLab中处理任务的 3 个程序。
三、Gitlab安装、配置、启动管理
安装Gitlab必要的依赖项
安装命令:yum install -y curl policycoreutils-python openssh-server
安装Postfix以发送通知电子邮件
安装命令:yum install postfix
激活命令:systemctl enable postfix
启用命令:systemctl start postfix
下载GitLab软件包(社区版),地址:https://packages.gitlab.com/gitlab/gitlab-ce
查看Gitlab相关目录,命令:find / -name gitlab
Gitlab目录结构
/opt/gitlab/ # 主目录
/etc/gitlab/ # 放置配置文件
/var/opt/gitlab/ # 各个组件
/var/log/gitlab/ # 放置日志文件
/var/opt/gitlab/git-data/repositories #数据库的地址
/var/opt/gitlab/postgresql/data #gitlab组和项目的地址
/etc/gitlab/gitlab.rb #gitlab配置文件
初始化Gitlab命令(保存配置或重新载入配置):
gitlab-ctl reconfigure
Gitlab服务的启停管理
启动服务: gitlab-ctl start
停止服务: gitlab-ctl stop
重启服务: gitlab-ctl restart
查看状态: gitlab-ctl status
Gitlab的supervisor方式启动服务
服务启动命令: systemctl start gitlab-runsvdir.service
服务停止命令: systemctl stop gitlab-runsvdir.service
服务重启命令: systemctl restart gitlab-runsvdir.service
开机启动命令: systemctl enable gitlab-runsvdir.service
取消开机启动命令: systemctl disable gitlab-runsvdir.service
服务查看命令: systemctl list-unit-files
Gitlab服务日志查看:/usr/bin/gitlab-ctl tail #可以查看到gitlab所有插件的日志情况
四、Centos下Gitlab快速安装的操作记录
1. Gitlab安装过程(自己虚拟机上)
yum install curl openssh-server postfix cronie policycoreutils-python #下载所需要的软件包
systemctl start postfix #启动postfix
rpm -ivh gitlab-ce-11.1.4-ce.0.el7.x86_64.rpm #RPM安装gitlab
更改gitlab的配置文件
cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab-bak.rb #先将配置文件进行备份
vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.26.128' #修改访问地址
gitlab_rails['time_zone'] = 'Asia/Shanghai' #修改时区
gitlab-ctl reconfigure #初次配置gitlab服务
如果开启了防火墙,请注意注意配置iptables规则,和sexlinux,然后浏览器访问测试。
如果访问出现502报错,类似下图的这种,请参考以下链接进行排查。
502问题排查链接
安装后的小细节
1、Gitlab如果是编译安装的默认管理员账号密码是:admin@local.host|5iveL!fe,如果是 rpm包安装则管理员账号密码是root|5iveL!fe
首次访问的时候,如果不知道管理员账号和密码,尽管可以注册用户,但注册的用户都不是管理员。这个时候,可以重置管理员的密码,管理员默认是root。
重置管理员密码(密码要是8位)的方法如下:
Gitlab管理员的登录权限就是:root/root@123,管理员的默认邮箱是部署机的本机邮箱,也是从本机发的邮件。这也就是为什么在开头要安装postfix。
[root@gitlab ~]# gitlab-rails console production
Loading production environment (Rails 4.2.10)
irb(main):001:0> user = User.where(id:1).first
irb(main):002:0> user.password='root@123'
irb(main):003:0> user.password_confirmation = 'root@123'
irb(main):004:0> user.save!
修改gitlab中的邮件配置为本机。
cat /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml|grep 192.168.26.128 #查看邮件配置是否是本机
访问http://192.168.26.128/admin/application_settings,
如下:在管理员账号(root)登录后,先把"注册"功能关了,这样就只能在管理员账号下创建用户。关闭注册功能方法:
3. Gitlab批量添加账号
[root@gitlab ~]# cat gitlab.sh
#!/bin/bash
#批量创建gitlab用户
userinfo="userinfo.text"
while read line
do
password=`echo $line |awk '{print $1}'`
mail=`echo $line |awk '{print $2}'`
username=`echo $line |awk '{print $3}'`
name=`echo $line |awk '{print $4}'`
curl -d"reset_password=$password&email=$mail&username=$username&name=$name&private_token=ucUctguWU6-2qrvRnGiB" "http://192.168.62.128/api/v4/users"
done <$userinfo
[root@gitlab ~]# cat userinfo.text
1 zhanjiang.feng@wang.com zhanjiang.feng zhanjiang.feng
1 hongkang.yan@wang.com hongkang.yan hongkang.yan
1 yansong.wang@wang.com yansong.wang yansong.wang
1 bo.xue@wang.com bo.xue bo.xue
1 junlong.li@wang.com junlong.li junlong.li
1 luyu.cao@wang.com luyu.cao luyu.cao
1 xueqing.wang@wang.com xueqing.wang xueqing.wang
1 xu.guo@wang.com xu.guo xu.guo
1 bing.xing@wang.com bing.xing bing.xing
1 mengmeng.li@wang.com linan linan
注意上面脚本中的private_token(这个很重要,否则批量创建不了用户)的值是从gitlab的管理员账号登录后的"settings-Account"界面里找到的,
如下:注意:上面userinfo.text文件里的四行分别表示密码,邮箱,用户名,别名。上面命令执行后,就可以批量创建用户了!
其中密码用1表示重置密码,也就是用户创建之后,会给用户邮箱发送两封邮件:
-> 一封确认绑定邮箱的邮件,一定要点击这个邮件里的confirm确认地址(否则登录无效);
-> 另一封是重置用户密码的邮件。重置后就可以使用邮箱或用户名登陆了。
访问脚本中gitlab的用户接口地址http://192.168.26.128/api/v4/users,试试能否访问!
Email的smtp设置
上面默认是用部署机本地的postfix发邮件。如果要想使用第三方邮箱发邮件,请见下面
[root@gitlab ~]# # cat /etc/gitlab/gitlab.rb|grep -v "^#"|grep -v "^$"
external_url'http://192.168.26.182'
gitlab_rails['gitlab_email_from'] ='xxxxxx@qq.com'
gitlab_rails['smtp_enable'] =true
gitlab_rails['smtp_address'] ="smtp.qq.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] ="xxxxxx@qq.com"
gitlab_rails['smtp_password'] ="*******"
gitlab_rails['smtp_domain'] ="qq.com"
gitlab_rails['smtp_authentication'] ="login"
gitlab_rails['smtp_enable_starttls_auto'] =true
user['git_user_email'] ="xxxxxx@qq.com"
gitlab-ctl reconfigure
修改Gitlab登录界面
选择gitlab新的主题风格,新主题会在左边栏展示选择项
gitlab-12版本以后就可以在设置中配置中文界面显示了。
查看gitlab版本号
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
———————————————
------------------------------------------------------------------