Linux安装Gitlab15详解

Linux安装Gitlab15详解

镜像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

官方网站:https://about.gitlab.com/

安装所需最小配置,内存至少4G

https://docs.gitlab.cn/jh/install/requirements.html

前置准备

  • 检查一下域名解析
[root@centos120 ~]# ping baidu.com
PING baidu.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=36.9 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=34.3 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=128 time=34.2 ms
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 34.217/35.167/36.983/1.293 ms
[root@centos120 ~]#

使用 yum install wget -y 的时候,输出:“Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error”,

有问题的话设置域名解析服务器,任意一个地址就可以

[root@centos120 ~]# vim /etc/resolv.conf
# Generated by NetworkManager
nameserver 114.114.114.114 (推荐)
[root@centos120 ~]#

YUM设置

  • 重新下载阿里云的yum源:
[root@centos111 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos111 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@centos111 ~]# yum clean all
[root@centos111 ~]# yum makecache
[root@centos111 ~]# yum update

关闭 防火墙(选做)

[root@centos111 ~]# systemctl stop firewalld
[root@centos111 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@centos111 ~]#

安装依赖

[root@centos110 ~]# yum install -y curl policycoreutils policycoreutils-python openssh-server perl openssh-clients postfix

[root@centos110 ~]# systemctl enable sshd
[root@centos110 ~]# systemctl start sshd
[root@centos110 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-08-13 13:56:39 CST; 4min 1s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 1237 (sshd)
   CGroup: /system.slice/sshd.service
           └─1237 /usr/sbin/sshd -D

启动postfix,并设置开机自启动

目的:支持gitlab邮件发送

systemctl enable postfix && systemctl start postfix

查看是否安装Openssh

$ rpm -qa|grep openssh

查看是否安装postfix

$ rpm -qa|grep postfix

将ssh服务设置开机启动

$ systemctl enable sshd

启动ssh服务

$ systemctl start sshd

查看ssh服务状态

$ systemctl status sshd

添加HTTP服务器到Firewalld

[root@centos110 ~]# firewall-cmd --permanent --add-service=http
success
[root@centos130 ~]# firewall-cmd --permanent --add-service=ssh

[root@centos110 ~]# systemctl reload firewalld
[root@centos110 ~]#

如果关闭防火墙就不需要做以上配置

安装

做好以上准备后接下来就可以安装gitlab了,cd到上传gitlab安装包的目录中,使用rpm命令安装gitlab

$ rpm -i gitlab-ce-15.2.2-ce.0.el7.x86_64.rpm

[root@centos110 data]# ll
total 1438684
-rw-r--r--.  1 root root 1136813194 Aug 13 14:04 gitlab-ce-15.2.2-ce.0.el7.x86_64.rpm

[root@centos110 data]# rpm -ivh gitlab-ce-15.2.2-ce.0.el7.x86_64.rpm
warning: gitlab-ce-15.2.2-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key                                                                                                ID f27eab47: NOKEY

It looks like GitLab has not been configured yet; skipping the upgrade script.

       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.



     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/


Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
  sudo gitlab-ctl reconfigure

For a comprehensive list of configuration options please see the Omnibus GitLab                                                                                                readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

Help us improve the installation experience, let us know how we did with a 1 min                                                                                               ute survey:
https://gitlab.fra1.qualtrics.com/jfe/form/SV_6kVqZANThUQ1bZb?installation=omnib                                                                                               us&release=15-2

[root@centos110 data]#


.修改gitlab配置文件指定服务器ip和自定义端口:

vim /etc/gitlab/gitlab.rb 
external_url 'http://192.168.134.130:9090'

-------- 以下选做 --------
vim /etc/gitlab/gitlab.rb 
external_url 'http://192.168.134.130:82'

##! **Override only if you use a reverse proxy**
##! Docs: https://docs.gitlab.com/omnibus/settings/nginx.html#setting-the-nginx-listen-port
nginx['listen_port'] = 82

ps:注意这里设置的端口不能被占用,默认是8080端口,如果8080已经使用,请自定义其它端口,并在防火墙设置开放相对应得端口

[root@centos110 log]# firewall-cmd --zone=public --add-port=9090/tcp --permanent
success
[root@centos110 log]# systemctl reload firewalld
[root@centos110 log]#

修改日志目录权限

chmod -R 755 /var/log/gitlab

启动

5.重置并启动GitLab

[root@centos110 log]# gitlab-ctl reconfigure
...
...

Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.

NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

gitlab Reconfigured!
[root@centos110 log]#

启动Gitlab

[root@centos110 log]# gitlab-ctl restart
ok: run: alertmanager: (pid 19726) 1s
ok: run: gitaly: (pid 19753) 1s
ok: run: gitlab-exporter: (pid 19776) 0s
ok: run: gitlab-kas: (pid 19778) 1s
ok: run: gitlab-workhorse: (pid 19796) 0s
ok: run: grafana: (pid 19817) 0s
ok: run: logrotate: (pid 19838) 1s
ok: run: nginx: (pid 19850) 0s
ok: run: node-exporter: (pid 19858) 1s
ok: run: postgres-exporter: (pid 19872) 0s
ok: run: postgresql: (pid 19897) 1s
ok: run: prometheus: (pid 19933) 0s
ok: run: puma: (pid 20026) 0s
ok: run: redis: (pid 20032) 1s
ok: run: redis-exporter: (pid 20054) 0s
ok: run: sidekiq: (pid 20104) 0s
[root@centos110 log]#

登录

http://192.168.134.110:9090/users/sign_in

默认用户 root

用gitlab-ctl tail 命令查看实时log

发现日志报错:端口被占用。
在服务器上查看 netstat -tnlp | grep “被占用端口”
之后重启gitlab :gitlab-ctl restart

查到的日志是

badgateway: failed to receive response: dial unix /var/opt/gitlab/gitlab-rails/sockets/gitlab.socket: connect: connection refused"
原因是puma端口冲突,解决方法是,打开配置文件,将puma[‘port’] = 8080取消注释,并且改为别的端口,不能和external_url 的端口重复,然后再保存,停止服务后,重新加载配置文件和重启

这里注意,改端口的时候搜一下配置文件里有没有相同的端口,包括注释掉的,我本来想改8090,后来搜了一下8090有的注释也用了,后改为8095、

修改客户端密码

首先root登录linux服务器

一,切换GitLab命令行

gitlab-rails console -e production

二,查找root用户

这里root用户的id是1,查找其他用户的话一般用邮箱。然后置root密码;最后保存修改。

以下是操作详情:

[root@centos110 log]# gitlab-rails console -e production
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]
 GitLab:       15.2.2 (4ecb014a935) FOSS
 GitLab Shell: 14.9.0
 PostgreSQL:   13.6
------------------------------------------------------------[ booted in 27.41s ]
Loading production environment (Rails 6.1.4.7)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id:1 @root>
irb(main):002:0> user.password = '1qazxsw2'
=> "1qazxsw2"
irb(main):003:0> user.password_confirmation = '1qazxsw2'
=> "1qazxsw2"
irb(main):004:0> user.save!
=> true
irb(main):005:0> exit
[root@centos110 log]#

一些其他常用指令:

# 启动所有 gitlab 组件
sudo gitlab-ctl start 
# 停止所有 gitlab 组件
sudo gitlab-ctl stop 
# 重启所有 gitlab 组件
sudo gitlab-ctl restart 
# 查看服务状态
sudo gitlab-ctl status 
 # 重新配置服务
sudo gitlab-ctl reconfigure
 # 修改默认的配置文件
sudo vim /etc/gitlab/gitlab.rb
# 检查gitlab
gitlab-rake gitlab:check SANITIZE=true --trace 
 # 查看日志
sudo gitlab-ctl tail

查看启动状态

[root@centos130 data]# gitlab-ctl status
run: alertmanager: (pid 19342) 1055s; run: log: (pid 16121) 1418s
run: gitaly: (pid 19367) 1054s; run: log: (pid 14428) 1576s
run: gitlab-exporter: (pid 19388) 1053s; run: log: (pid 15904) 1436s
run: gitlab-kas: (pid 19390) 1053s; run: log: (pid 14897) 1552s
run: gitlab-workhorse: (pid 19407) 1052s; run: log: (pid 15614) 1460s
run: grafana: (pid 19417) 1052s; run: log: (pid 16886) 1341s
run: logrotate: (pid 19433) 1051s; run: log: (pid 14261) 1588s
run: nginx: (pid 19445) 1051s; run: log: (pid 15716) 1453s
run: node-exporter: (pid 19453) 1050s; run: log: (pid 15792) 1447s
run: postgres-exporter: (pid 19466) 1050s; run: log: (pid 16188) 1412s
run: postgresql: (pid 19480) 1049s; run: log: (pid 14671) 1563s
run: prometheus: (pid 19497) 1049s; run: log: (pid 16041) 1424s
run: puma: (pid 19516) 1048s; run: log: (pid 15474) 1471s
run: redis: (pid 19527) 1048s; run: log: (pid 14344) 1582s
run: redis-exporter: (pid 19533) 1048s; run: log: (pid 15977) 1429s
run: sidekiq: (pid 19648) 1043s; run: log: (pid 15538) 1465s
[root@centos130 data]#

卸载GitLab

1、停止gitlab

gitlab-ctl stop

2、卸载gitlab(注意这里写的是gitlab-ce)

rpm -e gitlab-ce

3、查看gitlab进程

ps aux | grep gitlab

4、删除所有包含gitlab文件

find / -name gitlab | xargs rm -rf

5、可以把这三个目录给干掉也可以:

rm -rf  /opt/gitlab
rm -rf  /etc/gitlab
rm -rf  /var/log/gitlab

在卸载gitlab然后再次安装执行sudo gitlab-ctl reconfigure的时候往往会出现:ruby_block[supervise_redis_sleep] action run,会一直卡无法往下进行!

解决方案:

1、按住CTRL+C强制结束;

2、运行:sudo systemctl restart gitlab-runsvdir;

3、再次执行:sudo gitlab-ctl reconfigure