GitLab服务器搭建教程

安装前准备

  • VMWare虚拟机
  • 版本推荐CentOS7(我当前使用的版本是CentOS7)
  • 配置好Linux的IP地址
  • 保证Gitlab可用运行内存大于4G(否则虚拟机会变得非常消耗磁盘读写)
  • 安装依赖软件
1
yum -y install policycoreutils openssh-server openssh-clients postfix

设置postfix开机自启,并启动,postfix支持gitlab发信功能

1
systemctl enable postfix && systemctl start postfix

安装GitLab

下载GitLab安装包

国内GitLab安装包地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/

  • /el6/ CentOS 6系统目录
  • /el7/ CentOS 7系统目录
  • /el8/ CentOS 8系统目录

我下载的是 11.3.3 版本,大家可以根据自己需求选择下载。

进入/usr/local/src/目录下载GitLab安装包

1
[root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.3.3-ce.0.el7.x86_64.rpm

安装GitLab安装包

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
[root@localhost src]# rpm -i gitlab-ce-11.3.3-ce.0.el7.x86_64.rpm 
warning: gitlab-ce-11.3.3-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

修改GitLab配置文件

  1. 修改GitLab配置文件指定服务器ip和自定义端口
1
vim /etc/gitlab/gitlab.rb

修改成如下(IP地址+端口号):

1
external_url 'http://192.168.100.14:8801'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
## GitLab configuration settings
##! This file is generated during initial installation and **is not** modified
##! during upgrades.
##! Check out the latest version of this file to know about the different
##! settings that can be configured by this file, which may be found at:
##! https://gitlab.com/gitlab-org/omnibus-gitlab/raw/master/files/gitlab-config-template/gitlab.rb.template


## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
external_url 'http://192.168.100.14:8801'

## Roles for multi-instance GitLab
##! The default is to have no roles enabled, which results in GitLab running as an all-in-one instance.
##! Options:
##! redis_sentinel_role redis_master_role redis_slave_role geo_primary_role geo_secondary_role
##! For more deatils on each role, see:
##! https://docs.gitlab.com/omnibus/roles/README.html#roles
##!
# roles ['redis_sentinel_role', 'redis_master_role']
  1. 重置并重启GitLab

重置

注:第一次预计需要几分钟

1
# gitlab-ctl reconfigure

居然用了11分钟…

1
2
3
4
Running handlers:
Running handlers complete
Chef Client finished, 435/620 resources updated in 11 minutes 31 seconds
gitlab Reconfigured!

重启

1
# gitlab-ctl restart

访问GitLab页面

如果没有开启8801端口,是无法访问滴

1
/sbin/iptables -I INPUT -p tcp --dport 8801 -j ACCEPT

浏览器输入 http://192.168.100.14:8801

如果访问的时候报502错误,修改权限

1
chmod -R 755 /var/log/gitlab

修改root密码

第一次访问时,会自动跳转到修改root密码的界面,填写好密码确认即可。愉快地开始管理你的项目吧!

GitLab邮箱配置

1
vim /etc/gitlab/gitlab.rb

参考官方文档

重启配置

1
2
# gitlab-ctl reconfigure
# gitlab-ctl restart

进入控制台(等待进入控制台)

1
# gitlab-rails console

执行测试

1
2
Loading production environment (Rails 6.1.4.7)
irb(main):001:0>Notify.test_email("接收邮箱","标题","内容").deliver_now

修改GitLab SSH 端口

使用 SSH 克隆仓库链接 默认端口是 22,修改 vim /etc/gitlab/gitlab.rb

1
gitlab_rails['gitlab_shell_ssh_port'] = 2222

修改后,重置配置

1
# gitlab-ctl reconfigure

修改 GitLab 域名

GitLab默认Nginx80端口

如果机器未占有80端口,可以直接替换成域名,解析后就可以直接访问

查看当前绑定的域名或者IP

1
# grep "^external_url" /etc/gitlab/gitlab.rb

打开配置文件

1
# vim /etc/gitlab/gitlab.rb

修改成域名访问

1
# external_url 'http://gitlab.yuming.com'

重新跑一下配置

1
# gitlab-ctl reconfigure

如果机器80端口以占有,需要修改GitLab端口

查看默认端口

1
# grep "'listen_port" /etc/gitlab/gitlab.rb

打开配置文件

1
# vim /etc/gitlab/gitlab.rb

找到取消注释,修改端口

1
nginx['listen_port'] = 8086

重新跑一下配置

1
gitlab-ctl reconfigure

查看Nginx中GitLab配置文件

1
# cat ~git/nginx/conf/gitlab-http.conf
1
2
3
4
5
6
7
8
9
10
###################################
## configuration ##
###################################


server { ## HTTPS server
listen *:8086;
server_name gitlab.yuming.com;
······
}

重启gitlab

1
# gitlab-ctl restart

浏览器访问 域名:端口

如果想直接域名不加端口访问,则需要配置反向代理

在Nginx新增配置文件

/usr/local/nginx/conf/vhost/gitlab.new.com.conf

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
server
{
listen 80;
server_name git.pennonedu.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/git.pennonedu.com;

proxy_pass http://127.0.0.1:8086;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;

add_header X-Cache $upstream_cache_status;

#Set Nginx Cache


set $static_fileozqOzGtC 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
set $static_fileozqOzGtC 1;
expires 12h;
}
if ( $static_fileozqOzGtC = 0 )
{
add_header Cache-Control no-cache;
}
}

重启Nginx,直接可以用域名访问

坚持原创技术分享,您的支持将鼓励我继续创作!
0%