Files
gitsites/Gogs.CentOS.md
2016-03-21 18:44:38 +08:00

295 lines
6.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# CentOS 从源代码安装 Gogs
*以下所有命令,含 # 标识的为 root 用户下执行,含 $ 标识的为 git 用户下执行。可以搭配 `sudo su - git` 和 `exit` 在两者之间切换*
## 安装或更新 Git
先删除系统中原有的老版本 git
# yum -y remove git
# yum install zlib-devel perl-CPAN gettext curl-devel expat-devel gettext-devel openssl-devel
从官方网站下载源代码进行:
# yum -y groupinstall 'Development Tools'
# cd ~
# curl --progress https://www.kernel.org/pub/software/scm/git/git-2.7.3.tar.gz | tar xz
# cd git-2.7.3/
# ./configure
# make
# make prefix=/usr/local install
然后使用下面这个命令检测安装是否有效:
# which git
## 为 Gogs 添加系统用户
# adduser --system --shell /bin/bash --comment 'Gogs' --create-home --home-dir /home/git/ git
## 安装 Go 语言
如果您的系统已经安装要求版本的 Go 语言,可以跳过此小节。
### 下载
前往 [go lang 官网](https://golang.org/dl/ "https://golang.org/dl/") 选择合适的版本,安装 Go 语言到 /home/git/local/go 目录
# sudo su - git
$ cd ~
$ mkdir local
$ wget https://storage.googleapis.com/golang/go1.6.linux-386.tar.gz
$ tar -C /home/git/local -xzf go1.6.linux-386.tar.gz
### 设置环境
请设置和您系统环境对应的路径
# sudo su - git
$ cd ~
$ echo 'export GOROOT=/home/git/local/go' >> /home/git/.bashrc
$ echo 'export GOPATH=/home/git/go' >> /home/git/.bashrc
$ echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> /home/git/.bashrc
$ source /home/git/.bashrc
## 安装 Gogs
### 下载并安装依赖
$ go get -u github.com/gogits/gogs
### 构建主程序
$ cd $GOPATH/src/github.com/gogits/gogs
$ go build
$ cd $GOPATH/src/github.com/gogits/gogs/scripts
$ sh build.sh
### 放置程序文件
$ cd ~
$ mkdir gogs
$ mkdir gogs/log
$ mv $GOPATH/src/github.com/gogits/gogs/scripts/output/* ~/gogs/
### 测试
$ cd gogs
$ ./gogs web
如果您没有发现任何错误信息,则可以使用 Ctrl-C 来终止运行。
### 初始化脚本
# cp /home/git/go/src/github.com/gogits/gogs/scripts/init/centos/gogs /etc/init.d/gogs
赋予启动权限,启动服务
# chmod +x /etc/init.d/gogs
# service gogs start
### 设置gogs开机启动
# chkconfig gogs on
## 安装配置 Apache
这里介绍使用 apache 作为网页服务器Nginx 请参考官方或其他文档
### 安装
如有没有安装Apache。
# yum install httpd
注意查看安装的 apache 版本。
# vim /etc/httpd/conf/httpd.conf
在文件末尾添加下面这行:
Include /etc/httpd/conf/vhosts/*.conf
然后建立文件夹
# mkdir /etc/httpd/conf/vhosts
这样所有的站点都可以在 vhosts 目录下配置了。
### 配置
这里以上一节的配置环境为例。
将下面的 YOUR_SERVER_FQDN 替换成你的IP或域名
<VirtualHost *:80>
ServerAdmin admin@admin.com
ServerAlias YOUR_SERVER_FQDN
ProxyPreserveHost On
ProxyRequests Off
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
ErrorLog logs/git.jebbs.co_error.log
CustomLog logs/YOUR_SERVER_FQDN_access.log combined env=!dontlog
CustomLog logs/YOUR_SERVER_FQDN.log combined
</VirtualHost>
新建一个服务器配置,将上述内容输入
# vim /etc/httpd/conf/vhosts/gogs.conf
启动 apache
# service httpd start
测试是否能正常访问
http://you.do.main
设置 apache 随系统启动
# chkconfig httpd on
此时应该可以看到初始化的页面但如果要配合MySQL使用还需要进行下面的操作。
## 安装 mysql 数据库
### CentOS 6 安装数据库
gogs 要求 mysql 5.5 以上版本utf8mb4支持但 CentOS/Red Hat (RHEL) 官方自带的 mysql 版本为 5.1。
首先安装软件源,查看系统架构
$ exit
# getconf LONG_BIT
根据系统架构选择合适的 epel 源:
64位
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
32位
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
导入 key:
# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
添加 remi 源
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
安装、启动、添加自启动
# yum --enablerepo=remi update
# yum --enablerepo=remi list mysql mysql-server
# yum --enablerepo=remi install mysql mysql-server
# service mysqld start
# chkconfig mysqld on
### CentOS 7 安装数据库
MySQL 已经不再包含在 CentOS 7 的源中,而改用了 MariaDB先搜索 MariaDB 现有的包:
# rpm -qa | grep mariadb
然后全部删除:
# rpm -e --nodeps mariadb-*
然后创建 /etc/yum.repos.d/MariaDB.repo
# vim /etc/yum.repos.d/MariaDB.repo
将以下内容添加至该文件中:
# MariaDB 10.0 CentOS repository list - created 2015-05-04 19:16 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
然后运行下面命令安装 MariaDB 10.0
# yum install MariaDB-server MariaDB-client
然后启动 MariaDB 服务:
# service mysql start
# chkconfig mysql on
## 配置数据库
运行 mysql_secure_installation
# mysql_secure_installation
登录到 mysql
# mysql -uroot -p
### 创建 git 数据库用户
注意修改这里的密码123后续配置时需要提供这里设置的密码。
> CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';
> use mysql;
> UPDATE user SET password=PASSWORD("123") WHERE user='git';
> FLUSH PRIVILEGES;
### 创建数据库
> SET storage_engine=INNODB;
> DROP DATABASE IF EXISTS gogs;
> CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
### 赋予权限
> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gogs`.* TO 'git'@'localhost';
> \q
尝试使用新用户连接数据库:
$ mysql -u git -p -D gogs
> \q
# 升级 Gogs
## 保存旧的配置文件
$ cd ~
$ cp -a gogs/custom ./
## 更新源码以及依赖
$ cd ~
$ go get -u github.com/gogits/gogs
## 移除旧的二进制
$ rm gogs
## 或将旧的二进制进行备份
$ mv gogs gogs.$(date +%Y-%m-%d).old
## 重新构建 Gogs
重复教程中的构建与放置动作
## 放回设置文件
$ cd ~
$ mv -a custom gogs/