Centos 7.6搭建Gogs服務並設置HTTPS與郵箱詳細教程

前言

①什麼是 Gogs?

Gogs 是一款極易搭建的自助 Git 服務。

②Gogs使用介紹

Gogs是一款類似Github/GitLab的開源文件/代碼管理系統(基於Git)。

③目前功能基本介紹

遠程代碼倉庫管理
代碼倉庫權限分配、管理
團隊管理
代碼審查

④選擇原因

Gogs是輕量級Git服務器的不二選擇,因爲GitLab比較重,對資源要求比較高,而且搭建流程相對複雜(使用docker搭建除外)。

我們可以看到Gogs的硬件要求:

最低的系統硬件要求爲一個廉價的樹莓派
如果用於團隊項目管理,建議使用 2 核 CPU 及 512MB 內存
當團隊成員大量增加時,可以考慮添加 CPU 核數,內存佔用保持不變
相對於GitLab的2核4G起步,Gogs可以說是對硬件要求非常低了。

好了,下面開始詳細的搭建流程。

1.環境準備

默認環境已經安裝Nginx,Mysql,git,這裏不做詳細介紹。

不知道的可以參考這篇安裝WordPress的配置教程:最新阿里(騰訊)雲主機LANMP搭建WordPress詳細教程

因爲本次配置中我們使用到了郵箱,所以我們申請了域名郵箱。

域名郵箱建議使用網易免費企業郵箱或者騰訊企業郵箱。

2.下載與安裝

下載linux 64位安裝包:

wget https://dl.gogs.io/0.11.91/gogs_0.11.91_linux_amd64.tar.gz

解壓:

tar -xvf gogs_0.11.91_linux_amd64.tar.gz

3.運行配置

①預置數據庫

使用root用戶登錄:

mysql -u root -p 

爲用戶創建一個數據庫(gogs):

mysql> create database gogs;

創建gogs用戶並授予上面創建的gogs數據庫所有權限:

mysql> grant all on gogs.* to gogs@localhost Identified by "19921111Yasin#$";
mysql> flush privileges;

grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by “密碼”

②配置Nginx

因爲gogs默認使用3000端口,所以我們將80端口對gogs的請求反向代理到3000端口。

增加Nginx配置文件:

vim /etc/nginx/conf.d/gogs.conf

添加下面的內容:

server {
    listen 80;
    server_name gogs.site;   # 修改爲你的域名
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
    }
}

server {
   server_name www.gogs.site;
   return 301 $scheme://gogs.site$request_uri;
}

重啓Nginx:

systemctl restart nginx

③創建git用戶

# 創建用戶 git
adduser git
# 修改文件夾權限
chown git:git gogs -R

④創建gogs服務

首先修改配置文件,將gogs工作目錄修改爲你自己存放的目錄:

cd gogs
vim scripts/systemd/gogs.service

主要修改配置文件中的WorkingDirectoryExecStart

[Unit]
Description=Gogs
After=syslog.target
After=network.target
After=mariadb.service mysqld.service postgresql.service memcached.service redis.service

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=simple
User=git
Group=git
WorkingDirectory=/yasin/gogs
ExecStart=/yasin/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git

# Some distributions may not support these hardening directives. If you cannot start the service due
# to an unknown option, comment out the ones not supported by your version of systemd.
ProtectSystem=full
PrivateDevices=yes
PrivateTmp=yes
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

將服務文件複製到system文件夾:

cp scripts/systemd/gogs.service /etc/systemd/system/

設爲開機啓動:

systemctl enable gogs.service

啓動服務:

systemctl start gogs.service

查看狀態:

systemctl status gogs.service

⑤安裝配置

上面一切正確的話,我們打開網頁輸入我們的域名就可以進入首次安裝配置了。

(1)數據庫配置

填寫我們上面預置的賬號和密碼:

gogs.mysql

(2)應用基本設置

根據你自己的配置進行填寫:

gogs_app

(3)郵件服務設置

填寫自己郵箱的SMTP服務器地址:

gogs_mail

這裏要注意的是,上面網易免費企業郵箱的SMTP地址應該爲smtp.ym.163.com

注意,郵件配置這裏坑比較多,在配置完郵箱後發現還是不能發送郵件。但是這時候使用Foxmail是可以連接郵箱併發送郵件的,於是排查服務器本身。

發送測試郵件示例:

gogs_mail_test

最終發現是雲主機服務商把25端口給封了,這時候需要去阿里雲或者騰訊雲申請解封25端口,具體流程就不敘述了。

申請解封完再選擇發送測試郵件,終於成功了!

gogs_mail_ok

⑥配置HTTPS

因爲環境採用的LANMP配置,所以Aapache和Nginx都需要配置。

(1)安裝ssl模塊

如果已經安裝可以跳過。

yum -y install mod_ssl

(2)修改Apache配置

使用命令httpd -M查看已安裝的模塊,默認開啓,沒有的話在http.conf配置文件中開啓下面的配置:

LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_connect_module libexec/apache2/mod_proxy_connect.so
LoadModule proxy_ftp_module libexec/apache2/mod_proxy_ftp.so
LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
LoadModule proxy_balancer_module libexec/apache2/mod_proxy_balancer.so

添加配置文件:

vim /etc/httpd/cond.d/gogs.conf

增加下面的內容,配置證書並將請求反向代理給Gogs服務端口:

<VirtualHost *:443>
  ServerName gogs.site
  SSLEngine on
  SSLProxyEngine On
  SSLCertificateFile /yasin/ssl/gogs.site/2_gogs.site.crt
  SSLCertificateKeyFile /yasin/ssl/gogs.site/3_gogs.site.key
  SSLCertificateChainFile /yasin/ssl/gogs.site/1_root_bundle.crt

  ProxyRequests off  
  ProxyPreserveHost On
 
  <Proxy />
    Order deny,allow
    Allow from all
  </Proxy>
 
  <Location />
    ProxyPass https://127.0.0.1:3000/      
    ProxyPassReverse https://127.0.0.1:3000/     
  </Location>

</VirtualHost>


<VirtualHost *:443>
 ServerName www.gogs.site
 Redirect permanent / https://gogs.site/
 
</VirtualHost>

重啓服務:

systemctl restart httpd

(3)修改Nginx配置

將80端口全部重定向到https鏈接。

vim /etc/nginx/cond.d/gogs.conf

修改配置:

server {
   listen 80;
   server_name www.gogs.site gogs.site;
   return 301 https://gogs.site.com$request_uri;
}

重啓服務:

systemctl restart nginx

如果只用Nginx一個服務器的話,參考下面的配置:

server {
    listen 443;
    server_name www.host.com;
    ssl on;
	ssl_certificate /home/git/gogs/custom/https/fileName.pem;
	ssl_certificate_key /home/git/gogs/custom/https/fileName.key;
	ssl_session_timeout 5m;
	ssl_protocols SSLv2 SSLv3 TLSv1;
	ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
	ssl_prefer_server_ciphers on;
    location / {
        #這裏主要也要將http://修改爲https://
        proxy_pass https://localhost:3000;
    }
}
server {
	#HTTP跳轉HTTPS
    listen  80;
    server_name www.host.com;
    rewrite ^(.*)$  https://$host$1 permanent;
}

(4)修改gogs配置

打開配置文件:

cd gogs
vim custom/conf/app.ini 

修改server配置:

[server]
DOMAIN           = gogs.site
HTTP_PORT        = 3000
PROTOCOL         = https
ROOT_URL         = https://gogs.site/
CERT_FILE        = /yasin/ssl/gogs.site/2_gogs.site.crt
KEY_FILE         = /yasin/ssl/gogs.site/3_gogs.site.key
DISABLE_SSH      = false
SSH_PORT         = 22
START_SSH_SERVER = false
OFFLINE_MODE     = false

重啓gogs:

systemctl restart gogs.service

如果一切順利的話,這時候再輸入你的域名,比如 https://gogs.site,你會發現HTTPS啓用成功!

4.Enjoy

好了,至此Gogs配置教程已經全部完畢,更多功能等待你去慢慢探索!

使用界面

https://camo.githubusercontent.com/b97b2bf37fca6a2ee38c4fa76438ab11d5f8bfed/68747470733a2f2f676f67732e696f2f696d672f73637265656e73686f74732f312e706e67

https://camo.githubusercontent.com/050c3e0938c634688f1eac460b2133440b725f7a/68747470733a2f2f676f67732e696f2f696d672f73637265656e73686f74732f322e706e67

https://camo.githubusercontent.com/4846236d332b35a347edc4ff935ca50b2be418e2/68747470733a2f2f676f67732e696f2f696d672f73637265656e73686f74732f342e706e67

在線體驗

https://try.gogs.io/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章