Ubuntu Server 18.04 安装 Gogs 作者: 萧禹 时间: 2019-01-15 分类: Ubuntu 访问: 2,782 次 # Ubuntu Server 18.04 安装 Gogs - Ubuntu Server 18.04.1 LTS, 4.15.0-43-generic - MySQL Ver 14.14 Distrib 5.7.24 - Git 2.17.1 - Gogs gogs_0.11.79_linux_amd64.tar.gz ## 环境说明 - Ubuntu 登陆用户(管理员) myuser ## 前期准备 ### 依赖安装 sudo apt install mysql sudo apt install git ### MySQL - 创建 gogs 用户 手动版创建 gogs 数据库: - 以 utf8-generic-cl 编码创建 gogs 数据库 自动版创建 gogs 数据库: - (mysql/phpmyadmin)运行安装包内的 `gogs\scripts\mysql.sql` - gogs 数据库赋予 gogs 用户所有权限 ### Ubuntu 创建 git 用户 创建 git 用户, git 组, /home/git 主目录等(建议设置**强密码**) `sudo adduser git` 更改 git 用户 shell, 可避免 git 用户登录系统 `sudo nano /etc/passwd` - 该 git 用户目的只为运行 gogs - gogs 默认需要 git 用户运行 `./gogs web` ## 安装 gogs su git #切换到 git 用户 cd /opt mkdir gogs cd gogs # /opt/gogs/ wget https://dl.gogs.io/0.11.79/gogs_0.11.79_linux_amd64.tar.gz tar -xzf gogs_0.11.79_linux_amd64.tar.gz cd gogs #/opt/gogs/gogs/ `./gogs web` # 运行 gogs 以上操作后的目录结构 /opt/gogs/gogs/custom/conf/app.ini /opt/git/gogs/gogs/log/ # 由 gogs 创建 /opt/gogs/gogs/public/ /opt/gogs/gogs/scripts/ /opt/gogs/gogs/templates/ /opt/gogs/gogs/gogs.exe /opt/gogs-repositories/ # 由 gogs 创建 访问 `内网地址:3000/install` 初始化配置 | Web 界面配置项(部分) | app.ini 配置 | | ------ | ------ | | 应用名称 | APP_NAME | | 运行系统用户 | RUN_USER | | 仓库根目录 | [repository] -> ROOT | | SSH 端口号 | [server] -> SSH_PORT | | HTTP 端口号 | [server] -> HTTP_PORT | | 域名 | [server] -> DOMAIN | | 应用 URL | [server] -> ROOT_URL | | 日志路径 | [log] -> ROOT_PATH | | 禁止用户自主注册 | [service] -> DISABLE_REGISTRATION | | 启用登录访问限制 | [service] -> REQUIRE_SIGNIN_VIEW | | 数据库类型 | [database] -> DB_TYPE | | 数据库主机 | [database] -> HOST | | 数据库用户 | [database] -> USER | | 数据库用户密码 | [database] -> PASSWD | | 数据库名称 | [database] -> NAME | ### 配置 app.ini 部分示例 ... [service] ... DISABLE_REGISTRATION = true #关闭注册(只能由管理员增加用户) ... [repository] ... FORCE_PRIVATE = true #强制新建的仓库只能是"私有"的 ... ## 开启自启动 Gogs `cd /opt/gogs/gogs/scripts/systemd/` 复制单元文件到默认单元启动目录中 `sudo cp gogs.service /etc/systemd/system/` 根据实际情况编辑 `gogs.service`([Linux 单元文件](https://www.freedesktop.org/software/systemd/man/systemd.unit.html)), 以下只给出需要编辑的部分 ... User=git #运行的用户 Group=git #运行的用户组 WorkingDirectory=/opt/gogs/gogs # gogs所在目录? ExecStart=/opt/gogs/gogs/gogs web # 执行的命令 Restart=always Environment=USER=git HOME=/home/git # git环境 ... 增加开机启动 `sudo systemctl enable gogs.service` 关闭开机启动 `sudo systemctl disable gogs.service` 手动开启 gogs `sudo systemctl start gogs.service` 手动停止 gogs `sudo systemctl stop gogs.service` 手动重启 gogs `sudo systemctl restart gogs.service` 配置改动后手动重新加载配置文件 `sudo systemctl reload gogs.service ` ## apache 反向代理 Gogs apache2 启用反向代理模块(如果没启用的话) $sudo a2enmod rewrite $sudo a2enmod proxy_http apache2 增加 gogs.conf 方向代理配置 $sudo nano /etc/apache2/sites-available/gogs.conf ``` ProxyPass /gogs http://127.0.0.1:3000/ ProxyPassReverse /gogs http://127.0.0.1:3000/ ``` 创建软链接以启用配置 $sudo a2ensite gogs.conf gogs 修改外部主机地址 nano /opt/gogs/gogs/custom/conf/app.ini ``` ... [server] ... ROOT_URL = https://www.example.com/gogs/ # 如是有域名绑定的话 SSH_DOMAIN = www.example.com:22 # 主机的 SSH ... ``` 重启 apache2, gogs sudo systemctl restart gogs apache2 ## 问答 问: **git 用户 /etc/passwd 不用改为 /usr/bin/git-shell?** 答: 网上说的需要改的是基于简单的 git server 服务器, 公钥增加都是手动的, gogs 不用设置为 git-shell 问: **gogs 初始化后 gogs-repositories 位置能移动吗?** 答: 可以, 移动后需在 gogs [管理面板-控制面板]执行`重新同步所有仓库的 pre-receive、update 和 post-receive 钩子` ## 参考 - [官方配置文件手册](https://gogs.io/docs/advanced/configuration_cheat_sheet) 标签: Ubuntu Gogs MySQL Git 文章版权:萧禹的博客 - 这个人很懒, 什么都没写 本文链接:https://www.flysfeq.site/index.php/archives/104/ 转载请注明文章原始出处 !