Centos 7 VPS 搭建 git服务(gogs)

本来想搭个gitlab,无奈gitlab配置要求太高,跑不起来,又想要方便,所以选择了gogs

这里用docker安装,省事方便,手动安装参考这里,安装包有问题看这里

添加git 用户

$ useradd git
$ echo -e "\ngit ALL=(ALL) ALL\n" >> /etc/sudoers
$ tail -3 /etc/sudoers

$ sudo su - git

安装 Docker:

$ curl -sSL https://get.docker.com/ | sh
$ sudo usermod -aG docker git

安装 Compsoe:

$ sudo curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

创建docker-compose.yml

version: '2'
services:
  nginx:
    container_name: nginx
    image: nginx:alpine
    volumes:
      - "~/nginx/:/etc/nginx/conf.d/"
    ports:
      - "80:80"
      - "443:443"
    restart: always
  db:
    container_name: db
    image: mysql:5.7
    volumes:
      - "~/mysql:/var/lib/mysql"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: "你的密码"
      MYSQL_DATABASE: GOGS
      MYSQL_USER: root
      MYSQL_PASSWORD: "你的密码"
  gogs:
    container_name: gogs
    depends_on:
      - db
    image: gogs/gogs
    volumes:
      - ~/gogs:/data
    links:
      - db
    ports:
      - "3000:3000"
      - "3022:22"
    restart: always

运行docker-compose up -d
可以运行docker ps查看状态

添加https支持和nginx代理

$ git clone https://github.com/letsencrypt/letsencrypt
$ cd letsencrypt
$ ./letsencrypt-auto certonly -d git.example.com -d xxx.xxx.com -d xxx.xxx.com

nginx配置文件

server
{
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name git.你的域名.com;
    server_tokens off;
    location /generate_204 { return 204; }
    # Discourage deep links by using a permanent redirect to home page of HTTPS site
    return 301 https://$host;
    # Alternatively, redirect all HTTP links to the matching HTTPS page
    return 301 https://$host$request_uri;
}
server
{
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    server_name git.你的域名.com;
    server_tokens off;
    location /generate_204 { return 204; }

    # ssl on;
    ################
    # SSL 配置
    ################
    ssl_certificate /etc/letsencrypt/live/你的域名.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/你的域名.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5:!MEDIUM:!LOW";
    ssl_ecdh_curve secp384r1;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;
    # Disable preloading HSTS for now.  You can use the commented out header line that includes
    # the "preload" directive if you understand the implications.
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
    # add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    # ssl_dhparam /etc/nginx/certs/dhparam.pem;
    ################
    # SSL END
    ################
    add_header 'Access-Control-Allow-Origin' *;
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';

    location / {
        proxy_redirect off;
        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_pass http://你的域名:3000; 
    }
}

重启nginx服务即可,此时访问你的域名(这里是git.xxx.com)会进入安装页面
修改下列内容

  • HTTP Port(HTTP端口)设置为3000

  • SSH Port(SSH端口)设置为(3022)

  • Application URL(应用网址)设置为https://git.xxx.com

  • 数据库地址不是 localhost 而是 db

你可能感兴趣的:(Centos 7 VPS 搭建 git服务(gogs))