git
搭建过程
安装
gitlab
的安装是很简单的,先下载安装包:
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.2.5-ce.0.el7.x86_64.rpm
安装:
rpm -Uvh gitlab-ce-10.2.5-ce.0.el7.x86_64.rpm
配置
gitlab
内置了nginx
的服务,所以默认会占用80
和443
端口。一般来说,我们做WEB
开发,服务器上早就安装了nginx
或者apache
,把80
和443
端口给占了,所以需要做些修改防止冲突。
简单的修改端口是不可行的,比如80
改成85
,当查看gitlab
项目时,下图中的项目地址会变成http://git.papamk.com:85/papamk/groupbill
这样,看着就不舒服。启用https
则在使用过程中则会出现其他的问题。这里不一一论述,直接说明正确的配置方法。
具体步骤:
1.首先确保你的服务器已经有运行nginx
,并且该nginx
监听宿主机的80
和443
端口。(如果你原来使用的apache
,请通过nginx
反向代理到apache
,这样apache
原来的服务仍然可用)。
2.编辑/etc/gitlab/gitlab.rb
:
# 编辑对外的域名(gitlab.papamk.com请添加A记录指向本服务器的公网IP):
external_url 'http://gitlab.papamk.com/'
# 禁用`gitlab`内置的`nginx`:
nginx['enable'] = false
# 修改成与nginx运行时的用户一致
web_server['external_users'] = ['www']
修改监听方式和监听地址(如果nginx
与gitlab
都在宿主机上,不用改也行;如果nginx
在docker
中,则需要修改)
gitlab_workhorse['listen_network'] = "tcp"
# 下面的172.18.147.173为本机IP,根据实际情况修改,不能为localhost或者127.0.0.1,否则docker访问不到
gitlab_workhorse['listen_addr'] = "172.18.147.173:8181"
最后执行下面命令让配置生效:
$gitlab-ctl reconfigure
3.配置nginx
增加gitlab.conf
的配置(所有需要注意的地方都加了中文注释):
upstream gitlab-workhorse {
server 172.18.147.173:8181; #根据实际情况修改
}
## Normal HTTP host
server {
listen 80;
listen [::]:80 default_server;
server_name gitlab.papamk.com; ## 修改成自己的域名;
server_tokens off; ## Don't show the nginx version number, a security best practice
root /opt/gitlab/embedded/service/gitlab-rails/public;
## See app/controllers/application_controller.rb for headers set
## Individual nginx logs for this GitLab vhost
access_log /home/wwwlogs/gitlab_access.log; # 根据实际情况修改
error_log /home/wwwlogs/gitlab_error.log; # 根据实际情况修改
location / {
client_max_body_size 0;
gzip off;
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://gitlab-workhorse;
}
}
该配置根据官网提供的配置修改而来: https://gitlab.com/gitlab-org/gitlab-recipes/blob/master/web-server/nginx/gitlab-omnibus-nginx.conf。
重启nginx
:
$sudo service nginx restart
4.登陆浏览器,这时可以看到安装成功了,如下图所示:
首次登陆会要求重置管理员密码,管理员的默认用户名为root
。
https
的支持
1.使用Let's encrypt
申请免费的SSL
证书,该项目提供了一个叫certbot
/certbot-auto
的工具获取证书,执行下面命令获取工具:
$wget https://dl.eff.org/certbot-auto
$chmod +x certbot-auto
执行下面命令生成生成gitlab.papamk.com
的证书,其中--agree-tos
表示同意协议,--email 95496875@qq.com
为自己的email
。
$./certbot-auto --agree-tos --email 95496875@qq.com certonly --webroot -w /opt/gitlab/embedded/service/gitlab-rails/public/ -d gitlab.papamk.com
执行成功后,可通过以下命令查看下证书位置,nginx
需要引用这些文件。(一般位于/etc/letsencrypt/live/gitlab.papamk.com/
目录)
$./certbot-auto certificates
2.修改/etc/gitlab/gitlab.rb
,对外的URL
改为https
:
external_url 'https://gitlab.papamk.com'
执行重新配置命令以便让新配置生效:
$gitlab-ctl reconfigure
3.修改前面的nginx
的gitlab.conf
的配置,全部替换成下面的内容(所有需要注意的地方都加了中文注释):
upstream gitlab-workhorse {
server 172.18.147.173:8181; #根据实际情况修改
}
## Redirects all HTTP traffic to the HTTPS host
server {
## Either remove "default_server" from the listen line below,
## or delete the /etc/nginx/sites-enabled/default file. This will cause gitlab
## to be served if you visit any address that your server responds to, eg.
## the ip address of the server (http://x.x.x.x/)
listen 0.0.0.0:80;
listen [::]:80 ipv6only=on default_server;
server_name gitlab.papamk.com; ## 改成自己的域名
server_tokens off; ## Don't show the nginx version number, a security best practice
return 301 https://$http_host$request_uri;
access_log /home/wwwlogs/gitlab_access.log; # 根据实际情况修改
error_log /home/wwwlogs/gitlab_error.log; # 根据实际情况修改
}
## HTTPS host
server {
listen 0.0.0.0:443 ssl;
listen [::]:443 ipv6only=on ssl default_server;
server_name gitlab.papamk.com; ## 改成自己的域名
server_tokens off; ## Don't show the nginx version number, a security best practice
root /opt/gitlab/embedded/service/gitlab-rails/public;
## Strong SSL Security
## https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html & https://cipherli.st/
ssl on;
ssl_certificate /etc/letsencrypt/live/gitlab.papamk.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gitlab.papamk.com/privkey.pem;
# GitLab needs backwards compatible ciphers to retain compatibility with Java IDEs
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
## sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096
# ssl_dhparam /etc/ssl/certs/dhparam.pem;
access_log /home/wwwlogs/gitlab_access.log; # 根据实际情况修改
error_log /home/wwwlogs/gitlab_error.log; # 根据实际情况修改
location / {
client_max_body_size 0;
gzip off;
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://gitlab-workhorse;
}
}
重启nginx
:
$sudo service nginx restart
4.定期更新ssl
证书Let's encrypt
提供的证书,有效期为90
天,到期后执行如下命令即可继续使用:
$./certbot-auto renew
将该命令加入到crontab
,每小时刷新一次,就不用担心过期了。先将该命令移到系统目录下:
$sudo mv certbot-auto /usr/bin/certbot-auto
然后执行crontab -e
,添加一行:
0 */1 * * * /usr/bin/certbot-auto renew