git gogs

gogs install

  1. 查看系统版本

    1
    2
    [root@localhost ~]# cat /etc/redhat-release
    CentOS Linux release 7.3.1611 (Core)
  2. 设置主机名

    1
    2
    3
    [root@localhost ~]# hostname gogs && echo gogs | tee /etc/hostname
    [root@localhost ~]# echo '192.168.13.246 gogs' |tee -a /etc/hosts
    [root@localhost ~]# $SHELL
  3. 硬盘格式化挂载

    1
    2
    3
    [root@gogs ~]# mkfs.xfs /dev/vdb
    [root@gogs ~]# echo '/dev/vdb /mnt xfs defaults 0 0' | tee -a /etc/fstab
    [root@gogs ~]# mount -a
  4. 设置打开最大文件数

    1
    2
    3
    [root@gogs ~]# echo '* - nproc  65535' | tee -a /etc/security/limits.conf
    [root@gogs ~]# echo '* - nofile 65535' | tee -a /etc/security/limits.conf
    [root@gogs ~]# ls /etc/security/limits.d/*|xargs rm -f
  5. 设置yum源

    1
    2
    3
    4
    5
    6
    [root@gogs ~]# mkdir /etc/yum.repos.d/backup && mv /etc/yum.repos.d/{*,backup}
    [root@gogs ~]# rpm --import http://yum.ops.com/epel/RPM-GPG-KEY-EPEL-7
    [root@gogs ~]# curl -o /etc/yum.repos.d/epel.repo http://yum.ops.com/epel-7.repo
    [root@gogs ~]# rpm --import http://yum.ops.com/centos/RPM-GPG-KEY-CentOS-7
    [root@gogs ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://yum.ops.com/centos-7.repo
    [root@gogs ~]# yum clean all && yum makecache
  6. 安装基础依赖库和常用工具包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    [root@gogs ~]# yum -y groupinstall "Development Tools"
    [root@gogs ~]# yum -y install \
    make cmake \
    bison-devel \
    bzip2-devel \
    zlib zlib-devel \
    openssl openssl-devel openssl-libs openssl-static \
    pcre pcre-devel pcre-static \
    ncurses ncurses-devel ncurses-libs \
    mariadb mariadb-devel\
    curl-devel \
    expat-devel \
    gettext-devel \
    openldap openldap-devel \
    readline readline-devel readline-static \
    libssh2 libssh2-devel \
    unixODBC unixODBC-devel \
    sqlite sqlite-devel \
    tcl tcl-devel \
    perl-Digest-SHA1 \
    python-libs python-devel python2-pip python-crypto \
    perl-libs \
    perl-ExtUtils-MakeMaker \
    GeoIP GeoIP-devel \
    gperftools gperftools-devel gperftools-libs \
    libatomic_ops-devel \
    gtest gtest-devel \
    gdk-pixbuf2 gdk-pixbuf2-deve \
    libffi libffi-devel \
    libcurl libcurl-devel \
    http-parser http-parser-devel \
    libxml2* \
    libmcrypt* \
    libtool-ltdl-devel*
    [root@gogs ~]# yum -y install bash-completion fop lftp ntp ntpdate vim wget telnet dstat tree lrzsz net-tools nmap-ncat nmap sysstat dmidecode bc
  7. 关闭selinux

    1
    2
    [root@gogs ~]# setenforce 0
    [root@gogs ~]# sed -i s/'SELINUX=enforcing'/'SELINUX=disabled'/g /etc/selinux/config
  8. 关闭防火墙

    1
    [root@gogs ~]# systemctl stop firewalld && systemctl disable firewalld
  9. 设置系统时区

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    [root@gogs ~]# [ -f /etc/localtime ] && cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    [root@gogs ~]# [ -f /etc/sysconfig/clock ] && echo 'ZONE="Asia/Shanghai"' | tee /etc/sysconfig/clock
    [root@gogs ~]# [ -f /etc/timezone ] && echo 'Asia/Shanghai' | tee /etc/timezone
    [root@gogs ~]# [ -f /etc/sysconfig/ntpd ] && echo 'SYNC_HWCLOCK=yes' | tee -a /etc/sysconfig/ntpd

    [root@gogs ~]# ntpdate cn.pool.ntp.org

    [root@gogs ~]# cp -f /etc/{ntp.conf,ntp.conf.bak}
    [root@gogs ~]# cat > /etc/ntp.conf <<EOF
    driftfile /var/lib/ntp/drift
    restrict default nomodify notrap nopeer noquery
    restrict 127.0.0.1
    restrict ::1
    server cn.pool.ntp.org prefer
    server 0.centos.pool.ntp.org iburst
    server 1.centos.pool.ntp.org iburst
    server 2.centos.pool.ntp.org iburst
    server 3.centos.pool.ntp.org iburst
    includefile /etc/ntp/crypto/pw
    keys /etc/ntp/keys
    disable monitor
    EOF

    [root@gogs ~]# cp -f /etc/ntp/{step-tickers,step-tickers.bak}
    [root@gogs ~]# cat > /etc/ntp/step-tickers <<EOF
    cn.pool.ntp.org
    0.centos.pool.ntp.org
    1.centos.pool.ntp.org
    2.centos.pool.ntp.org
    3.centos.pool.ntp.org
    EOF

    [root@gogs ~]# systemctl start ntpd && systemctl enable ntpd

    [root@gogs ~]# iptables -I INPUT -p udp --dport 123 -j ACCEPT
  10. 安装python,并设置python源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@gogs ~]# git clone https://github.com/yyuu/pyenv.git /usr/local/pyenv

    [root@gogs ~]# echo 'export PYENV_ROOT="/usr/local/pyenv"' | tee /etc/profile.d/pyenv.sh
    [root@gogs ~]# echo 'export PATH="$PYENV_ROOT/bin:$PATH"' | tee -a /etc/profile.d/pyenv.sh
    [root@gogs ~]# echo 'eval "$(pyenv init -)"' | tee -a /etc/profile.d/pyenv.sh
    [root@gogs ~]# source /etc/profile

    [root@gogs ~]# mkdir -p ${PYENV_ROOT}/cache
    [root@gogs ~]# ls ${PYENV_ROOT}/cache/Python-2.7.13.tar.xz 将源码包放到这里
    [root@gogs ~]# pyenv install 2.7.13
    [root@gogs ~]# pyenv local 2.7.13

    [root@gogs ~]# mkdir ~/.pip
    [root@gogs ~]# cat > ~/.pip/pip.conf <<EOF
    [global]
    trusted-host=mirrors.aliyun.com
    index-url=http://mirrors.aliyun.com/pypi/simple/
    [list]
    format=columns
    EOF
  11. 设置开机启动文件权限

    1
    [root@gogs ~]# chmod +x /etc/rc.d/rc.local
  12. 创建常见目录

    1
    [root@gogs ~]# mkdir -p /mnt/{app,data,log,web,ops/{app,data,cron}}

  1. 创建用户

    1
    [root@gogs app]# useradd -s /sbin/nologin git
  2. golang install

    1
    2
    3
    4
    5
    6
    7
    [root@gogs app]# tar xzf go1.8.4.linux-amd64.tar.gz -C /mnt/app/
    [root@gogs app]# chown -R git.git /mnt/app/go

    [root@gogs app]# echo 'export GOROOT=/mnt/app/go' |tee /etc/profile.d/go.sh
    [root@gogs app]# echo 'export GOPATH=$HOME/work' |tee -a /etc/profile.d/go.sh
    [root@gogs app]# echo 'export PATH=$GOROOT/bin:$PATH' |tee -a /etc/profile.d/go.sh
    [root@gogs app]# source /etc/profile
  3. mysql install

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    [root@gogs app]# yum -y install mariadb-server mariadb
    [root@gogs app]# cat > /etc/my.cnf <<EOF
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    symbolic-links=0
    default-storage-engine=INNODB
    character_set_server = utf8
    [mysqld_safe]
    log-error=/var/log/mariadb/mariadb.log
    pid-file=/var/run/mariadb/mariadb.pid
    !includedir /etc/my.cnf.d
    EOF

    [root@gogs app]# systemctl start mariadb
    [root@gogs app]# systemctl enable mariadb

    [root@gogs app]# mysql
    MariaDB [(none)]> CREATE DATABASE `gogs` DEFAULT CHARACTER SET utf8mb4;
    MariaDB [(none)]> GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY 'gogs123';
    MariaDB [(none)]> FLUSH PRIVILEGES;
  4. gogs install

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    [root@gogs app]# tar xzf linux_amd64.tar.gz -C /mnt/app/

    [root@gogs app]# cd /mnt/app/gogs/
    [root@gogs gogs]# mkdir -p custom/conf log data
    [root@gogs gogs]# cat > custom/conf/app.ini <<EOF
    APP_NAME = Gogs
    RUN_USER = git
    RUN_MODE = prod

    [database]
    DB_TYPE = mysql
    HOST = 127.0.0.1:3306
    NAME = gogs
    USER = gogs
    PASSWD = gogs
    SSL_MODE = disable
    PATH = data/gogs.db

    [repository]
    ROOT = /mnt/data/gogs.repo

    [server]
    DOMAIN = {gogs.ops.com}
    HTTP_PORT = 3000
    ROOT_URL = https://{gogs.ops.com}
    DISABLE_SSH = false
    SSH_PORT = 22
    START_SSH_SERVER = false
    OFFLINE_MODE = false

    [mailer]
    ENABLED = true
    HOST = smtp.exmail.qq.com:465
    FROM = gogs@{a.com}
    USER = gogs@{a.com}
    PASSWD = {mail passwd}

    [service]
    REGISTER_EMAIL_CONFIRM = true
    ENABLE_NOTIFY_MAIL = true
    DISABLE_REGISTRATION = true
    ENABLE_CAPTCHA = false
    REQUIRE_SIGNIN_VIEW = true

    [picture]
    DISABLE_GRAVATAR = false
    ENABLE_FEDERATED_AVATAR = true

    [session]
    PROVIDER = file

    [log]
    MODE = file
    LEVEL = Info
    ROOT_PATH = /mnt/app/gogs/log

    [security]
    INSTALL_LOCK = true
    SECRET_KEY = 30EYn8NSl5dadf
    EOF

    [root@gogs app]# mkdir -p /mnt/data/gogs.repo
    [root@gogs app]# chown -R git.git /mnt/app/gogs/
    [root@gogs app]# chown -R git.git /mnt/data/gogs.repo

    [root@gogs app]# cat > /usr/lib/systemd/system/gogs.service <<EOF
    [Unit]
    Description=Gogs
    After=syslog.target
    After=network.target
    After=mariadb.service mysqld.service postgresql.service memcached.service redis.service

    [Service]
    LimitMEMLOCK=infinity
    LimitNOFILE=65535
    Type=simple
    User=git
    Group=git
    WorkingDirectory=/mnt/app/gogs
    ExecStart=/mnt/app/gogs/gogs web
    Restart=always
    Environment=USER=git HOME=/home/git

    [Install]
    WantedBy=multi-user.target
    EOF
    [root@gogs app]# systemctl daemon-reload
    [root@gogs app]# systemctl start gogs
    [root@gogs app]# systemctl enable gogs