JasperXu's 笔记本
 
发布时间: 2021-03-08 17:00
修订时间: 2021-04-29 12:00

Debian 安装 gogs

1. 安装 MySQL

sudo apt update
sudo apt install git

下载 Linux 对应的存储库 下载页面

wget https://repo.mysql.com/mysql-apt-config_0.8.16-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.16-1_all.deb

直接选择 ok 即可。

需要重选使用命令 sudo dpkg-reconfigure mysql-apt-config .

开始安装:

sudo apt update
sudo apt install mysql-server
  • 输入 root 密码
  • 选择加密方式

初始化命令:

# 启动服务
sudo systemctl restart mysql.service
# 进行初始化设置
sudo mysql_secure_installation

# 输入root密码。
# 输入 y 选择启用密码验证组件。
# 输入 0 选择密码强度Low。
# 输入 n 选择不更改root密码。
# 输入 y 选择删除匿名账户。
# 输入 n 选择允许远程root登陆。 如果是服务器环境请禁止,只允许localhost登陆。
# 输入 y 选择删除测试数据库。
# 输入 y 选择重新加载特权表。
# 完成配置。

连接 mysql

mysql -u root -p
# 查看版本
> select version();

# 查看字符集。
> SHOW VARIABLES LIKE 'character%';
# utf8 :三字节utf8【过时】
# utf8mb4 :四字节【默认值】

# 查看排序规则
> SHOW VARIABLES LIKE 'collation_%';
# utf8mb4_general_ci :非精确匹配,大小写不敏感。【过时】
# utf8mb4_unicode_ci :精确匹配,大小写不敏感。
# utf8mb4_0900_ai_ci :非精确匹配,大小写不敏感。【默认值】
# utf8mb4_0900_as_ci :精确匹配,大小写不敏感。
# utf8mb4_0900_ai_cs :非精确匹配,区分大小写。
# utf8mb4_0900_as_cs :精确匹配,区分大小写。

1.3. 增加 mysql 配置设置排序规则

修改 /etc/mysql/mysql.conf.d/mysqld.cnf 增加 collation-server 行。

[mysqld]
collation-server = utf8mb4_0900_ai_ci

1.4. 允许 MySQL 登录范围

修改 /etc/mysql/mysql.conf.d/mysqld.cnf 注释掉 bind-address

[mysqld]
# bind-address = 127.0.0.1

1.5. 设置允许 root 登录范围

  • 任何 IP 都允许 root 登录
$ mysql -u root -p
grant all on *.* to root@'%' identified by '你的密码' with grant option;
FLUSH PRIVILEGES;
quit
  • 允许 192.168.1.199 和 172.17.0.1~255 用 root 登录
$ mysql -u root -p
grant all on *.* to root@'192.168.1.199' identified by '你的密码' with grant option;
grant all on *.* to root@'172.17.0.%' identified by '你的密码' with grant option;
FLUSH PRIVILEGES;
quit

1.6. 加密模式由 caching_sha2_password 改为 mysql_native_password

$ mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
FLUSH PRIVILEGES;

修改 /etc/mysql/mysql.conf.d/mysqld.cnf 添加 default_authentication_plugin

[mysqld]
default_authentication_plugin = mysql_native_password

重启 mysql 服务

systemctl restart mysql.service

2. 安装 gogs

服务器地址:192.168.31.12

准备使用的端口:8080

准备使用的域名(设置 host 也行):git.jasperxu.com

请适当替换上面的个性化设置。

2.1. 创建数据库 gogs

$ mysql -u root -p
mysql> create database gogs default character set utf8mb4 collate utf8mb4_0900_ai_ci;

2.2. 下载 Gogs 并运行

wget https://dl.gogs.io/0.12.3/gogs_0.12.3_linux_amd64.tar.gz
tar -zxvf gogs_0.12.3_linux_amd64.tar.gz
cd gogs
./gogs web

然后打开浏览器输入 http://192.168.31.12:3000

2.3. 设置域名解析

git.jasperxu.com 域名解析设置为 192.168.31.12

2.4. 设置 nginx

创建新的配置文件 /etc/nginx/conf.d/gogs.conf

server {
    listen      80;
    server_name git.jasperxu.com;
    return         301 https://git.jasperxu.com$request_uri;
}

server {
    listen      443;
    ssl         on;
    server_name git.jasperxu.com;

    ssl_certificate     /etc/nginx/conf.d/gogs.pem;
    ssl_certificate_key /etc/nginx/conf.d/gogs.key;

    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass_header   Server;
        proxy_pass          http://localhost:8080;
        proxy_read_timeout  90;

        proxy_set_header    Host $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_redirect off;
    }
}

上传对应的 gogs.pemgogs.key

scp gogs.pem root@192.168.31.12:/etc/nginx/conf.d/
scp gogs.key root@192.168.31.12:/etc/nginx/conf.d/

在 nginx 的配置文件 /etc/nginx/nginx.conf 中的 http 节点最后增加下面两句:

    server_names_hash_bucket_size 64;
    client_max_body_size 10240M;

nginx 重新加载配置文件:

# 检查语法
nginx -t
# 重新加载
nginx -s reload

设置 gogs 自动启动

创建服务文件 /etc/init.d/gogs

#! /bin/sh
### BEGIN INIT INFO
# Provides:          gogs
# Required-Start:    $syslog $network
# Required-Stop:     $syslog
# Should-Start:      mysql postgresql
# Should-Stop:       mysql postgresql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: A self-hosted Git service written in Go.
# Description:       A self-hosted Git service written in Go.
### END INIT INFO

# Author: Danny Boisvert

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Gogs"
NAME=gogs
SERVICEVERBOSE=yes
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
WORKINGDIR=/root/gogs
DAEMON=$WORKINGDIR/$NAME
DAEMON_ARGS="web"
USER=root

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
  # Return
  #   0 if daemon has been started
  #   1 if daemon was already running
  #   2 if daemon could not be started
  sh -c "USER=$USER start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile \\
      --test --chdir $WORKINGDIR --chuid $USER \\
      --exec $DAEMON -- $DAEMON_ARGS > /dev/null \\
      || return 1"
  sh -c "USER=$USER start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile \\
      --background --chdir $WORKINGDIR --chuid $USER \\
      --exec $DAEMON -- $DAEMON_ARGS \\
      || return 2"
}

#
# Function that stops the daemon/service
#
do_stop()
{
  # Return
  #   0 if daemon has been stopped
  #   1 if daemon was already stopped
  #   2 if daemon could not be stopped
  #   other if a failure occurred
  start-stop-daemon --stop --quiet --retry=TERM/1/KILL/5 --pidfile $PIDFILE --name $NAME
  RETVAL="$?"
  [ "$RETVAL" = 2 ] && return 2
  start-stop-daemon --stop --quiet --oknodo --retry=0/1/KILL/5 --exec $DAEMON
  [ "$?" = 2 ] && return 2
  # Many daemons don't delete their pidfiles when they exit.
  rm -f $PIDFILE
  return "$RETVAL"
}


case "$1" in
  start)
  [ "$SERVICEVERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  do_start
  case "$?" in
    0|1) [ "$SERVICEVERBOSE" != no ] && log_end_msg 0 ;;
    2) [ "$SERVICEVERBOSE" != no ] && log_end_msg 1 ;;
  esac
  ;;
  stop)
  [ "$SERVICEVERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  do_stop
  case "$?" in
    0|1) [ "$SERVICEVERBOSE" != no ] && log_end_msg 0 ;;
    2) [ "$SERVICEVERBOSE" != no ] && log_end_msg 1 ;;
  esac
  ;;
  status)
  status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
  ;;
  restart|force-reload)
  log_daemon_msg "Restarting $DESC" "$NAME"
  do_stop
  case "$?" in
    0|1)
    do_start
    case "$?" in
      0) log_end_msg 0 ;;
      1) log_end_msg 1 ;; # Old process is still running
      *) log_end_msg 1 ;; # Failed to start
    esac
    ;;
    *)
    # Failed to stop
    log_end_msg 1
    ;;
    esac
  ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
    ;;
esac

然后执行如下命令:

sudo chmod +x /etc/init.d/gogs
cd /etc/init.d/
sudo update-rc.d gogs defaults

使用方式

systemctl start gogs
systemctl restart gogs
systemctl stop gogs
systemctl status gogs