准备在阿里云服务器上安装gitlab,之前在本地安装过,过程并不复杂,只是在换源上花了点时间,但万万没想到,用了云服务器后,因为安装了nginx用来配置自己的项目,结果造成了gitlab内置的nginx和本地冲突,花了3天时间才处理好这问题。这里就记录一下配置过程,以免下次遇到。
处理两个nginx冲突的方法还是挺多的
1. 禁用gitlab自带nginx,在本地增加gitlab的配置
2. 禁用本地nginx,用gitlab的(这个我没有尝试,但网上有此种操作的文章)
3. 两个nginx并用,只要解决端口冲突就好
以上方法,我尝试了1、3,都成功了。但方法三两个nginx并行总感觉不靠谱,于是我最终选择了方法一,以下是详情
1、下载好gitlab后进行配置
编辑配置文件
#vi /etc/gitlab/gitlab.rb
修改访问路径(http://localhost:1024 是我改的 这里你要改成自己的端口)
external_url 'http://localhost:1024'
使配置生效(这里说明以下为什么配置没改完就先操作此命令,因为配置不生效,很多文件不生成,之后的很多操作做不了)
#sudo gitlab-ctl reconfigure
在配置文件中关闭gitlab nginx (要么直接复制,要么找到位置去掉注释修改)
nginx['enable'] = false
允许启动gitlab的用户(这里用户名从本地nginx里找)
web_server['external_users'] = ['nobody']
配置nginx (添加到原nginx配置中)
#vi /usr/local/nginx/conf/nginx.conf
upstream gitlab {
# 7.x 版本在此位置
# server unix:/var/opt/gitlab/gitlab-rails/tmp/sockets/gitlab.socket;
# 8.0 位置
server unix://var/opt/gitlab/gitlab-rails/sockets/gitlab.socket;
}
server {
listen *:1024;
server_name 孙宇豪.online; # 请修改为你的域名
server_tokens off; # don't show the version number, a security best practice
root /opt/gitlab/embedded/service/gitlab-rails/public;
# Increase this if you want to upload large attachments
# Or if you want to accept large git objects over http
client_max_body_size 250m;
# individual nginx logs for this gitlab vhost
access_log /var/log/gitlab/nginx/gitlab_access.log;
error_log /var/log/gitlab/nginx/gitlab_error.log;
location / {
# serve static files from defined root folder;.
# [@gitlab](https://my.oschina.net/daodaoclub) is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html [@gitlab](https://my.oschina.net/daodaoclub);
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location [@gitlab](https://my.oschina.net/daodaoclub) {
# If you use https make sure you disable gzip compression
# to be safe against BREACH attack
proxy_read_timeout 300; # Some requests take more than 30 seconds.
proxy_connect_timeout 300; # Some requests take more than 30 seconds.
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
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-Frame-Options SAMEORIGIN;
proxy_pass http://gitlab;
}
# Enable gzip compression as per rails guide: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
# WARNING: If you are using relative urls do remove the block below
# See config/application.rb under "Relative url support" for the list of
# other files that need to be changed for relative url support
location ~ ^/(assets)/ {
root /opt/gitlab/embedded/service/gitlab-rails/public;
# gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
error_page 502 /502.html;
}
我还改了一下 unicorn.rb 文件的监听接口,因为它占用了我的8080接口
#vi /var/opt/gitlab/gitlab-rails/etc/unicorn.rb
重启nginx
#sudo /usr/local/nginx/sbin/nginx -s reload
重启gitlab
#sudo gitlab-ctl reconfigure
用你的ip+端口号访问gitlab
一般到这里就成功了,当然还有可能碰到一些问题,比如端口冲突等,在出现问题后必须先去日志看情况,不能一味的只通过问题搜答案,毕竟造成问题的原因很多,仅仅只靠搜出来的结果不停的尝试,经常会造成更多的问题,最后有可能把整个环境都整崩了
查看gitlab日志
#sudo gitlab-ctl tail
我遇到过端口冲突的状况,于是用命令
#sudo fuser -k 1024/tcp
没权限造成的访问页面502
#sudo chmod -R o+x /var/opt/gitlab/gitlab-rails
#chmod 755 /var/opt/gitlab/gitlab-rails/sockets
出现了问题别着急,耐心细心坚持下去总能解决,良好的解决问题的能力是一个优秀程序员必备的的素养
Nothing is impossible