CentOS 7 x64 利用宝塔安装gitea

本文最后更新于:几秒前

准备阶段

  • 准备域名并解析到服务器IP(code.demo.com –> IP)

  • 确保宝塔已经安装并安装好lnmp环境

  • 确保mySQL版本至少为5.5.3

  • 安装git

  • 首先检查CentOS 7系统中安装的git版本:

    git --version

    git version 1.8.3.1

从IUS存储库安装git的方法

IUS是一个社区项目,为Enterprise Linux发行版的新版精选软件提供RPM包,该项目的目的是为红帽企业Linux(RHEL)和CentOS创建高质量的RPM包。

1、删除旧的git:

sudo yum remove git

2、添加IUS CentOS 7 repo:

sudo yum install  https://centos7.iuscommunity.org/ius-release.rpm
sudo yum install  git2u-all

安装git2u-all软件包后检查git版本:

git --version

git version 2.16.5

如上所述,当前版本Git是2.16.5,如果要安装最新的Git版本,最好是下载源代码并安装。

安装过程

  • 在宝塔中新建网站code.demo.com,创建数据库MySQL,字符集选择utf8_general_ci

    远程下载gitea

    cd /www/wwwroot/code.demo.com/
    wget -O gitea https://dl.gitea.io/gitea/main/gitea-main-linux-amd64
    chmod +x gitea

    成功之后运行gitea,访问http://ip:3000即可测试。

    ./gitea web
  • 在DNS服务商处绑定code.demo.com到服务器IP地址后,打开宝塔面板网站的SSL,使用Let’s Encrypt进行证书签名

  • 接着打开宝塔网站的配置文件,示例配置文件内容如下:

server
{
	listen 443 ssl http2;
	listen 80;
	  client_max_body_size 100000m;
    server_name code.demo.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/code.demo.com;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate    /www/server/panel/vhost/cert/code.demo.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/code.demo.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;



    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    # include enable-php-00.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    # include /www/server/panel/vhost/rewrite/code.demo.com.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
        proxy_pass http://127.0.0.1:3000;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null; 
        proxy_pass http://127.0.0.1:3000;
    }
    
    location / {
        proxy_pass      http://127.0.0.1:3000;
        proxy_redirect      off;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        }
    
    access_log  /www/wwwlogs/code.demo.com.log;
    error_log  /www/wwwlogs/code.demo.com.error.log;
    
}

其中proxy_pass http://127.0.0.1:3000用来代理访问3000端口的请求。

  • 新建一个systemd服务文件

    vim /lib/systemd/system/gitea.service

    内容如下:

[unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
After=mysqld.service
###
# Don't forget to add the database service requirements
###
#
#Requires=mysql.service
#Requires=mariadb.service
#Requires=postgresql.service
#Requires=memcached.service
#Requires=redis.service
#
###
# If using socket activation for main http/s
###
#
#After=gitea.main.socket
#Requires=gitea.main.socket
#
###
# (You can also provide gitea an http fallback and/or ssh socket too)
#
# An example of /etc/systemd/system/gitea.main.socket
###
##
## [Unit]
## Description=Gitea Web Socket
## PartOf=gitea.service
##
## [Socket]
## Service=gitea.service
## ListenStream=<some_port>
## NoDelay=true
##
## [Install]
## WantedBy=sockets.target
##
###

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
RestartSec=2s
Type=simple
User=root
Group=root
WorkingDirectory=/www/wwwroot/code.demo.com/
# If using Unix socket: tells systemd to create the /run/gitea folder, which will contain the gitea.sock file
# (manually creating /run/gitea doesn't work, because it would not persist across reboots)
#RuntimeDirectory=gitea
ExecStart=/www/wwwroot/code.demo.com/gitea web --config /www/wwwroot/code.demo.com/custom/conf/app.ini
Restart=always
Environment=USER=root HOME=/home/ GITEA_WORK_DIR=/www/wwwroot/code.demo.com/
# If you want to bind Gitea to a port below 1024, uncomment
# the two values below, or use socket activation to pass Gitea its ports as above
###
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#AmbientCapabilities=CAP_NET_BIND_SERVICE
###

[Install]
WantedBy=multi-user.target

修改完毕后输入:wq退出vim编辑器。

启动gitea并设置开机自动启动:

systemctl start gitea
systemctl enable gitea

待以上步骤完成后,ssh到服务器重载设置文件并重启nginx服务:

sudo /etc/init.d/nginx reload
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start

现在打开浏览器访问 https://code.demo.com 即可进行gitea的安装过程了。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!