目录
Linux 环境基础安装
Nginx、Redis、Mysql安装
PHP、扩展、composer安装
进程管理工具Supervisor安装
Git、版本管理软件Gogs安装
Laravel项目的相关配置
创建git用户
# 确保已存在www用户和www用户组
sudo useradd -g www -d /home/git -r -s /bin/sh -c 'git version control' git
sudo mkdir -p /home/git
sudo chown -R git:www /home/git/
sudo chmod -R 755 /home/git/
# 管理ssh密钥
su git
cd ~
mkdir .ssh
touch .ssh/authorized_keys
cd .ssh
# 添加git用户的密钥
ssh-keygen -t rsa -C 'git' # 取名 git
cat git.pub >> authorized_keys
安装Git
# 安装git
> yum install -y git
安装Go环境
Golang国内下载地址:https://studygolang.com/dl
su www
cd ~/soft_download
# 下载二进制安装包(非源码包)
wget -c https://studygolang.com/dl/golang/go1.20.4.linux-amd64.tar.gz
# 执行tar解压到/usr/loacl目录下(官方推荐),得到go文件夹等
sudo tar -C /usr/local/ -zxvf go1.20.4.linux-amd64.tar.gz
添加/usr/loacl/go/bin
目录到PATH变量中。
sudo vim /etc/profile
# 在最后一行添加
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
# 保存退出后source一下
sudo -s # 先回到root用户
source /etc/profile
############# 或者只加到git用户下
su git
cd ~
echo 'export GOROOT=/usr/local/go' >> $HOME/.bashrc
echo 'export GOPATH=$HOME/go' >> $HOME/.bashrc
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> $HOME/.bashrc
source $HOME/.bashrc
执行go version
,如果显示版本号,则Go环境安装成功
> go version
go version go1.20.4 linux/amd64
编译安装Gogs
# 一定在git用户下操作
su git
cd /home/git/
# 克隆仓库到 "gogs" 子目录
git clone --depth 1 https://github.com/gogs/gogs.git gogs
cd gogs
# 创建日日志目录
mkdir log
# 编译主程序,这个步骤会下载所有依赖(好像要切换到root用户才行)
go build -o gogs
## 上面一步可能会报错=> dial tcp 34.64.4.81:443: i/o timeout
## 因为Go 1.14设置默认GOSUMDB=sum.golang.org,而这个网站被墙了
### step1.设置GOPROXY代理:
go env -w GOPROXY=https://goproxy.cn,direct
### step2.设置GOPRIVATE来跳过私有库,比如常用的Gitlab或Gitee,中间使用逗号分隔:
go env -w GOPRIVATE=.gitlab.com,.gite.com
### step3.设置 GOSUMDB=“sum.golang.google.cn”,这个是专门为国内提供的sum 验证服务
go env -w GOSUMDB="sum.golang.google.cn"
# 检查 Gogs 是否可以正常工作,如果没有发现任何错误信息,则可以使用 Ctrl-C 来终止运行
./gogs web
# Gogs 默认并没有支持一些功能,这些功能需要在构建时明确使用构建标签(build tags)来支持,目前使用标签构建的功能如下:
#### sqlite3:SQLite3 数据库支持
#### pam:PAM 授权认证支持
#### cert:生成自定义证书支持
go build -tags "sqlite cert" -o gogs
配置
cd /home/git/gogs
mkdir -p custom/conf
touch custom/conf/app.ini
vim custom/conf/app.ini
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓写入以下内容↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
; 改变仓库根目录的路径:
[repository]
ROOT = /home/git/gogs-repositories
; web服务
[server]
PROTOCOL = http
DOMAIN = ***服务器IP地址***
HTTP_PORT = 3000
; 外部访问的地址,gogs内部跳转的时候会调用它
EXTERNAL_URL = http://***服务器IP地址***:3000/
DISABLE_SSH = false
SSH_PORT = 22
START_SSH_SERVER = false
OFFLINE_MODE = false
CERT_FILE = custom/https/cert.pem
KEY_FILE = custom/https/key.pem
; 修改数据库配置:
[database]
; The database backend, either "postgres", "mysql" "sqlite3" or "mssql".
; You can connect to TiDB with MySQL protocol.
TYPE = mysql
HOST = 127.0.0.1:3306
NAME = gogs
USER = root
PASSWORD = 1qaz@WSX
; For "postgres" only, either "disable", "require" or "verify-full".
SSL_MODE = disable
; For "sqlite3" only, make sure to use absolute path.
PATH = data/gogs.db
; 日志配置
[log]
MODE = file
LEVEL = Info
ROOT_PATH = /home/git/gogs/log
在数据库中新建名为 gogs 的数据库,并且字符集设置为 utf8mb4.
部署
supervisor
# 其他部署方法试了都失败了,这里用supervisor部署,先切回到root用户
cp /home/git/gogs/scripts/supervisor/gogs /etc/supervisor/config.d/gogs.ini
# 把里面的路径修改正确就可以了,最终内容如下:↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
[program:gogs]
; 进程运行前,会前切换到这个目录
directory=/home/git/gogs/
command=/home/git/gogs/gogs web
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/home/git/gogs/log/supervisor_stdout.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/home/git/gogs/log/supervisor_stderr.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
user = git
environment = HOME="/home/git", USER="git"
# 确保log目录所有者正确,否则服务会挂断
chown -R git:www /home/git/gogs/log/
最后重载下supervisor的配置,可能用到的命令如下
# 启动服务
sudo supervisord -c /etc/supervisor/supervisord.conf
# 重新读取supervisord.conf配置
sudo supervisorctl update
# 启动和重启
sudo supervisorctl start gogs
sudo supervisorctl restart gogs
nginx反向代理
http访问
server {
listen 80;
server_name git.zxddt.com;
location / {
proxy_pass http://localhost:3000;
}
}
https访问
- 申请HTTPS证书,并下载证书相关文件
在阿里云处填写相关信息并提交申请后,你可以下载到一个压缩包,其中包含了 fileName.pem / fileName.key 两个文件
- 修改Gogs配置文件
> vim /home/git/gogs/custom/conf/app.ini
[server]
PROTOCOL = https
EXTERNAL_URL = https://git.zxddt.com/
; 证书文件
CERT_FILE = custom/https/fileName.pem
KEY_FILE = custom/https/fileName.pem
- 修改nginx反向代理配置
server {
# HTTP跳转HTTPS
listen 80;
server_name git.zxddt.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443;
server_name git.zxddt.com;
ssl on;
ssl_certificate /home/git/gogs/custom/https/fileName.pem;
ssl_certificate_key /home/git/gogs/custom/https/fileName.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
location / {
# 这里也要将 http:// 修改为 https://
proxy_pass https://localhost:3000;
}
}
重加载nginx配置文件
nginx -t
nginx -s reload
重启gogs
supervisorctl restart gogs
web访问gogs
访问web管理页面 http://服务器IP地址:3000
- 不要勾选内置ssh
服务器拉取代码
直接从git目录拉取
# 从gogs的服务器本地目录拉取
git clone /home/git/gogs-repositories/root/com.zxddt.git
# 查看当前版本仓库的远程库地址
git remote -v
本地拉取和提交
- 先生成一对密钥,然后在本地
.ssh/config
文件内新增如下内容
Host 服务器IP地址
HostName 服务器IP地址
Port 22
User git
IdentityFile ~/.ssh/id_rsa.ecs.git
- 登录gogs的个人设置页面,添加ssh密钥并且把刚生成的公钥填入.