隨著github的逐漸普及,越來越多的人會選擇在其中建立自己的項目,也就是在github上發表自己的代碼。然而,github限制了單個賬戶上能建立的私人庫的數量,所以一些高頻使用者可能需要通過其他途徑來建立自己的代碼庫。在這個時候,自己搭建github就成為了一個選擇。本文主要介紹在linux系統下,通過gogs搭建自己的github,以解放github賬戶的限制。
一、安裝環境
在開始搭建前,需要確保系統安裝了相應的環境:
- mysql or PostgreSQL
- Go >= 1.12.x
- Git >= 1.7.1 (2.x recommended)
對于ubuntu系統,可以通過以下命令安裝MySQL:
sudo apt-get update sudo apt-get install mysql-server
Go的安裝方式可以在官網中下載對應的安裝包并按照說明進行處理。
對于某些版本的Ubuntu或debian系統,可能沒有安裝git-core,需要進行安裝:
sudo apt-get update sudo apt-get install git-core
二、安裝Gogs
- 從Github上下載Gogs的最新版本并解壓到該目錄下:
wget https://dl.gogs.io/gogs_latest_linux_amd64.tar.gz tar xvfz gogs_latest_linux_amd64.tar.gz
- 進入下載的Gogs目錄,執行安裝:
cd gogs ./gogs install
在執行安裝時需要輸入以下內容:
Do you want to install as Windows service/daemon? (y/n) n
Please enter the URL: (e.g. http://domain.com[:port] or http://[IP]:[port]) http://localhost:3000
接下來的安裝步驟會要求輸入一些數據庫相關的內容,需要你根據自己的需求進行配置。這里建議使用MySQL作為數據庫,并在這一步中安裝第2臺服務器。
在需要填寫Git信息時,需要注意將使用的ssh-key添加到GitHub上。
- 啟動Gogs:
cd gogs ./gogs web
成功啟動后,你可以在瀏覽器中訪問http://localhost:3000。
三、配置nginx反向代理
如果你的Gogs實例位于生產環境,建議使用Nginx作為反向代理服務器。
- 安裝Nginx:
sudo apt install nginx
- 創建一個vhost文件:
sudo nano /etc/nginx/sites-available/gogs
在其中加入以下內容:
server { listen 80; server_name git.example.com; # your domain name access_log /var/log/nginx/git.access.log; error_log /var/log/nginx/git.error.log; location / { proxy_pass http://localhost:3000; proxy_set_header Host $http_host; } location /ws { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location ~ /. { deny all; } }
請根據自己的需求更改server_name。
- 確保Nginx解析該vhost:
sudo ln -s /etc/nginx/sites-available/gogs /etc/nginx/sites-enabled/
然后重新加載Nginx配置:
sudo nginx -t sudo systemctl reload nginx
現在你便可以在你的Webbrowser里打開你的網站,通過 GitHub OAuth 登錄。
四、總結
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END