阿里雲ubuntu服務器在本地nginx的基礎上安裝gitlab

準備在阿里雲服務器上安裝gitlab,以前在本地安裝過,過程並不複雜,只是在換源上花了點時間,但萬萬沒想到,用了雲服務器後,由於安裝了nginx用來配置本身的項目,結果形成了gitlab內置的nginx和本地衝突,花了3天時間才處理好這問題。這裏就記錄一下配置過程,以避免下次遇到。html

處理兩個nginx衝突的方法仍是挺多的

1. 禁用gitlab自帶nginx,在本地增長gitlab的配置
2. 禁用本地nginx,用gitlab的(這個我沒有嘗試,但網上有此種操做的文章)
3. 兩個nginx並用,只要解決端口衝突就好

以上方法,我嘗試了一、3,都成功了。但方法三兩個nginx並行總感受不靠譜,因而我最終選擇了方法一,如下是詳情

一、下載好gitlab後進行配置nginx

編輯配置文件git

#vi /etc/gitlab/gitlab.rb

修改訪問路徑(http://localhost:1024 是我改的 這裏你要改爲本身的端口)程序員

external_url 'http://localhost:1024'

使配置生效(這裏說明如下爲何配置沒改完就先操做此命令,由於配置不生效,不少文件不生成,以後的不少操做作不了)web

#sudo gitlab-ctl reconfigure

在配置文件中關閉gitlab nginx (要麼直接複製,要麼找到位置去掉註釋修改)ruby

nginx['enable'] = false

容許啓動gitlab的用戶(這裏用戶名從本地nginx裏找) 服務器

web_server['external_users'] = ['nobody']

配置nginx (添加到原nginx配置中)app

#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接口socket

#vi /var/opt/gitlab/gitlab-rails/etc/unicorn.rb

重啓nginxtcp

#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
相關文章
相關標籤/搜索