Centos 7搭建Gitlab服务器

Posted wei198621

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos 7搭建Gitlab服务器相关的知识,希望对你有一定的参考价值。

参考文章:

https://blog.csdn.net/duyusean/article/details/80011540

一、 安装并配置必要的依赖关系

在CentOS系统上安装所需的依赖:ssh,防火墙,postfix(用于邮件通知) ,wget,以下这些命令也会打开系统防火墙中的HTTP和SSH端口访问。

1.安装ssh

[root@defaultStart41 ~]# sudo yum install -y curl policycoreutils-pythonopenssh-server


... ... 
Updated:
  curl.x86_64 0:7.29.0-59.el7_9.1                                                                                                                                             

Dependency Updated:
  libcurl.x86_64 0:7.29.0-59.el7_9.1                                                       libssh2.x86_64 0:1.8.0-4.el7                                                      

Complete!
[root@defaultStart41 ~]# 

2.将SSH服务设置成开机自启动,安装命令:sudo systemctl enable sshd  

3.启动SSH服务,安装命令:sudo systemctl start sshd

[root@defaultStart41 ~]# sudo systemctl enable sshd
[root@defaultStart41 ~]# sudo systemctl start sshd
[root@defaultStart41 ~]# 

4。安装防火墙(如果已经安装了防火墙并且已经在运行状态,则可直接进行第6步)

yum install firewalld systemd -y

[root@defaultStart41 ~]# yum install firewalld systemd -y
...
...


Updated:
  firewalld.noarch 0:0.6.3-13.el7_9                                                      systemd.x86_64 0:219-78.el7_9.3                                                     

Dependency Updated:
  firewall-config.noarch 0:0.6.3-13.el7_9   firewalld-filesystem.noarch 0:0.6.3-13.el7_9   libgudev1.x86_64 0:219-78.el7_9.3      python-firewall.noarch 0:0.6.3-13.el7_9  
  systemd-libs.x86_64 0:219-78.el7_9.3      systemd-python.x86_64 0:219-78.el7_9.3         systemd-sysv.x86_64 0:219-78.el7_9.3  

Complete!

5.开启防火墙,安装命令:service firewalld  start

[root@defaultStart41 ~]# service firewalld start
Redirecting to /bin/systemctl start firewalld.service
[root@defaultStart41 ~]# 

6.添加http服务到firewalld,pemmanent表示永久生效,若不加--permanent系统下次启动后就会失效。

sudo firewall-cmd --permanent --add-service=http

[root@defaultStart41 ~]# sudo firewall-cmd --permanent --add-service=http
success
[root@defaultStart41 ~]# 

7.重启防火墙,安装命令:sudo systemctl reload firewalld

[root@defaultStart41 ~]# sudo systemctl reload firewalld
[root@defaultStart41 ~]# 

8.接下来,安装Postfix以发送通知邮件,安装命令:sudo yum install postfix

注意:有个提示y/n ,需要输入y

在安装Postfix期间,可能会出现配置屏幕。选择“Internet Site”并按enter键。使用您的服务器的外部DNS以“mail name”并按enter。如果出现额外的屏幕,继续按enter键接受默认值。

[root@defaultStart41 ~]# sudo yum install postfix
...
...
Running transaction
  Updating   : 2:postfix-2.10.1-9.el7.x86_64                                                                                                                              1/2 
  Cleanup    : 2:postfix-2.10.1-7.el7.x86_64                                                                                                                              2/2 
  Verifying  : 2:postfix-2.10.1-9.el7.x86_64                                                                                                                              1/2 
  Verifying  : 2:postfix-2.10.1-7.el7.x86_64                                                                                                                              2/2 

Updated:
  postfix.x86_64 2:2.10.1-9.el7                                                                                                                                               

Complete!
[root@defaultStart41 ~]# 

9.将postfix服务设置成开机自启动,安装命令:sudo systemctl enable postfix

10.启动postfix,安装命令:sudo systemctl start postfix

[root@defaultStart41 ~]# sudo systemctl enable postfix
[root@defaultStart41 ~]# sudo systemctl start postfix
[root@defaultStart41 ~]# 

11.wget 用于从外网上下载插件

检查系统中是否已经安装wget,使用命令若出现下图wget相关版本描述则说明系统中已经安装wget 若报系统找不到命令说明wget未安装

若wget未安装则进行安装,安装命令:yum -y install wget

[root@defaultStart41 ~]# clear
[root@defaultStart41 ~]# wget -V
GNU Wget 1.14 built on linux-gnu.
...
...
Originally written by Hrvoje Niksic <hniksic@xemacs.org>.
Please send bug reports and questions to <bug-wget@gnu.org>.
[root@defaultStart41 ~]# 

12.安装vim编辑器  安装命令:yum install vim -y

[root@defaultStart41 ~]# yum install vim -y
...
...
Updated:
  vim-enhanced.x86_64 2:7.4.629-8.el7_9                                                                                                                                       

Dependency Updated:
  vim-common.x86_64 2:7.4.629-8.el7_9                                                                                                                                         

Complete!
[root@defaultStart41 ~]# 

二、添加GitLab镜像源并安装gitlab服务器

1.添加gitlab镜像

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm

[root@defaultStart41 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm
--2021-07-29 08:29:24--  https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 359519811 (343M) [application/x-redhat-package-manager]
Saving to: ‘gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm’

100%[====================================================================================================================================>] 359,519,811 4.24MB/s   in 63s    

2021-07-29 08:30:29 (5.40 MB/s) - ‘gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm’ saved [359519811/359519811]

[root@defaultStart41 ~]# 

2.安装gitlab 安装命令:rpm -i gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm

安装过程需要些时间,如果出现下图,则说明安装成功。

[root@defaultStart41 ~]# rpm -i gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm
warning: gitlab-ce-10.0.0-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

[root@defaultStart41 ~]# 

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

vim  /etc/gitlab/gitlab.rb

external_url 'http://gitlab.example.com'
修改为
external_url 'http://192.168.121.41:8080'

进入编辑器后按“i”键进入编辑状态,ESC键退出编辑状态

退出并保存,命令输入“:wq”

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

5.重置并启动GitLab

执行:

gitlab-ctl reconfigure
大概3分钟时间

[root@defaultStart41 ~]# gitlab-ctl reconfigure
...
...
Recipe: gitlab::postgres-exporter
  * service[postgres-exporter] action restart
    - restart service service[postgres-exporter]
  * ruby_block[reload postgres-exporter svlogd configuration] action create
    - execute the ruby block reload postgres-exporter svlogd configuration

Running handlers:
Running handlers complete
Chef Client finished, 362/515 resources updated in 04 minutes 08 seconds
gitlab Reconfigured!
[root@defaultStart41 ~]# 

gitlab-ctl restart

提示  "ok: run:"表示启动成功

[root@defaultStart41 ~]# 
[root@defaultStart41 ~]# gitlab-ctl restart
ok: run: gitaly: (pid 39696) 0s
ok: run: gitlab-monitor: (pid 39721) 1s
ok: run: gitlab-workhorse: (pid 39728) 0s
ok: run: logrotate: (pid 39739) 1s
ok: run: nginx: (pid 39749) 0s
ok: run: node-exporter: (pid 39793) 1s
ok: run: postgres-exporter: (pid 39800) 0s
ok: run: postgresql: (pid 39819) 0s
ok: run: prometheus: (pid 39828) 1s
ok: run: redis: (pid 39837) 0s
ok: run: redis-exporter: (pid 39844) 1s
ok: run: sidekiq: (pid 39937) 0s
ok: run: unicorn: (pid 39949) 0s
[root@defaultStart41 ~]# 

6.访问 GitLab页面

如果没有域名,直接输入服务器ip和指定端口进行访问

初始账户: root 密码:5iveL!fe

 http://192.168.121.41:8080/

没有显示

lsof -i:8080  看到8080 被 两个程序占用

[root@defaultStart41 ~]# lsof -i:8080
COMMAND   PID       USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   39749       root    7u  IPv4 262144      0t0  TCP *:webcache (LISTEN)
nginx   39786 gitlab-www    7u  IPv4 262144      0t0  TCP *:webcache (LISTEN)


[root@defaultStart41 ~]# netstat -tunlp | grep 8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      39749/nginx: master 
[root@defaultStart41 ~]# 


参考文章
---https://www.runoob.com/w3cnote/linux-check-port-usage.html

3. 重新配置端口

配置gitlib的IP和端口

vi /etc/gitlab/gitlab.rb 进入gitlab.rb修改IP和端口:进入编辑器后按“i”键进入编辑状态,ESC键退出编辑状态,退出并保存,命令输入“:wq”

[root@defaultStart41 ~]# vim  /etc/gitlab/gitlab.rb

如果8080端口被别的服务占用可以改成8081 8082等,如果开启额防火墙,需要把相应的端口放开:例如:firewall-cmd --zone=public --add-port=8080/tcp --permanent


external_url 'http://192.168.121.41:8081'

改用8081 端口,

[root@defaultStart41 ~]# firewall-cmd --zone=public --add-port=8081/tcp --permanent
success
[root@defaultStart41 ~]# 

重置并启动GitLab

配置:gitlab-ctl reconfigure

大概2,3 分钟配置

[root@defaultStart41 ~]# gitlab-ctl reconfigure
...
...
...

Recipe: gitlab::gitlab-rails
  * execute[clear the gitlab-rails cache] action run
    - execute /opt/gitlab/bin/gitlab-rake cache:clear
Recipe: gitlab::unicorn
  * service[unicorn] action restart
    - restart service service[unicorn]
Recipe: gitlab::sidekiq
  * service[sidekiq] action restart
    - restart service service[sidekiq]
Recipe: gitlab::nginx
  * service[nginx] action restart
    - restart service service[nginx]

Running handlers:
Running handlers complete
Chef Client finished, 11/478 resources updated in 01 minutes 52 seconds
gitlab Reconfigured!
[root@defaultStart41 ~]# 

启动GitLab:gitlab-ctl restart

[root@defaultStart41 ~]# gitlab-ctl restart
ok: run: gitaly: (pid 53795) 0s
ok: run: gitlab-monitor: (pid 53814) 0s
ok: run: gitlab-workhorse: (pid 53819) 0s
ok: run: logrotate: (pid 53830) 1s
ok: run: nginx: (pid 53839) 0s
ok: run: node-exporter: (pid 53846) 1s
ok: run: postgres-exporter: (pid 53852) 0s
ok: run: postgresql: (pid 53884) 0s
ok: run: prometheus: (pid 53919) 0s
ok: run: redis: (pid 53924) 0s
ok: run: redis-exporter: (pid 53934) 0s
ok: run: sidekiq: (pid 53994) 0s
ok: run: unicorn: (pid 54006) 1s
[root@defaultStart41 ~]# 

[root@defaultStart41 ~]# lsof -i:8081
COMMAND   PID       USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   53839       root    7u  IPv4 363001      0t0  TCP *:tproxy (LISTEN)
nginx   53841 gitlab-www    7u  IPv4 363001      0t0  TCP *:tproxy (LISTEN)

虚拟机可以访问 实体机不可以访问 gitLab

访问gitlib

   1、外部系统是win10

   2、虚拟机地址:192.168.121.41,主机地址:10.147.228.92

   问题一:在主机上不能通过http://192.168.121.41:8081来访问虚拟机中的Web服务器

https://www.cnblogs.com/yaomajor/p/8538999.html  

解决方法:

   1、切换到root用户

   2、在终端内输入以下两条命令

       iptables -F

       iptables -P INPUT ACCEPT(设置默认允许规则)

实体机访问web

访问gitlib

在浏览器输入刚才设置的IP和端口号(http://192.168.121.41:8081) 就可以访问的gitlib页面,初始账户: root 密码:5iveL!fe。登录之后就可以创建远程项目了

root: 5iveL!fe

weilei:12346578

暂时都访问不了   20210730

 root: 12345678

weilei: 12345678

以上是关于Centos 7搭建Gitlab服务器的主要内容,如果未能解决你的问题,请参考以下文章

Centos 7搭建Gitlab服务器

Centos 7搭建Gitlab服务器超详细

Centos 7搭建Gitlab服务器

Centos 7搭建Gitlab服务器

Centos 7搭建Gitlab服务器超详细(转)

CentOS 7手把手教你搭建GitLab