mysql 中創(chuàng)建 gogs 數(shù)據(jù)庫 和gogs用戶
連接本地?cái)?shù)據(jù)庫坎拐,建立gogs數(shù)據(jù)庫,并把權(quán)限賦予gogs用戶
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin;
mysql> GRANT ALL ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY 'YourPassword';
mysql> FLUSH PRIVILEGES;
mysql> exit;
[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
下載Gogs并解壓
解壓之后會(huì)多出來一個(gè) gogs 目錄
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget https://dl.gogs.io/0.11.4/linux_amd64.zip
[root@localhost src]# unzip linux_amd64.zip
配置與運(yùn)行Gogs
編輯配置文件
[root@localhost src]# vim /usr/local/src/gogs/scripts/init/debian/gogs
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Gogs"
NAME=gogs
SERVICEVERBOSE=yes
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
WORKINGDIR=/usr/local/src/gogs? #倉庫 自行更改
DAEMON=$WORKINGDIR/$NAME
DAEMON_ARGS="web"
USER=git
#如果運(yùn)行g(shù)ogs不是用的這個(gè)用戶侯勉,修改對(duì)應(yīng)用戶
#1.如果是創(chuàng)建的git用戶 則目錄修改為/usr/local/src/gogs.對(duì)應(yīng)的USER修改為git
#2.如果是在root用戶下安裝在/usr/local/gogs/下 則對(duì)應(yīng)的目錄為:/usr/local/gogs/gogs.對(duì)應(yīng)的USER修改為root
我們可以啟動(dòng) gogs 服務(wù)器啦!它的啟動(dòng)十分簡單,在 gogs 目錄下運(yùn)行如下命令即可:
[root@localhost src]# ./gogs/gogs web
看到有日志信息刷出延届,說明 gogs 服務(wù)器啟動(dòng)成功!
gogs 服務(wù)器默認(rèn)監(jiān)聽 3000 端口贸诚,我們?cè)跒g覽器中輸入http://遠(yuǎn)程主機(jī)IP:3000或者http://域名:3000便可以打開 gogs 的 web 界面方庭。
后臺(tái)運(yùn)行 gogs
使用 nohup 可以后臺(tái)運(yùn)行 gogs ,這樣即使關(guān)掉 ssh 連接也不會(huì)影響 gogs 的運(yùn)行酱固,運(yùn)行如下命令:
[root@localhost src]# nohup ./gogs/gogs web > /dev/null 2>&1 &
[1] 2893
nginx 反向代理至二級(jí)域名
瀏覽器中輸入http://遠(yuǎn)程主機(jī)IP:3000或者http://域名:3000便可以打開 gogs頁面械念,這樣url很丑,我們利用nginx的反向代理實(shí)現(xiàn)美化url运悲,設(shè)立二級(jí)域名 gogs龄减,訪問http://gogs.summer.com便能跳轉(zhuǎn)到 gogs web 頁面。
配置nginx的反向代理班眯,在配置conf文件下建立conf.d,用來存放各配置欺殿,nginx.conf需要重寫下
[root@localhost src]# mkdir /usr/local/nginx/conf/conf.d
[root@localhost src]# vim /usr/local/nginx/conf/nginx.conf
下面是nginx.conf配置, ‘ include /usr/local/nginx/conf/conf.d/*.conf ’這句將加載conf.d下的多server配置
#user nginx;
worker_processes? 1;
error_log? /var/log/nginx/error.log warn;
#pid? ? ? ? /var/run/nginx.pid;
events {
? ? worker_connections? 1024;
}
http {
? ? include? ? ? mime.types;
? ? default_type? application/octet-stream;
? ? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
? ? ? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"'
? ? ? ? ? ? ? ? ? ? ? '\t "$request_body"';
? ? access_log? /var/log/nginx/access.log? main;
? ? sendfile? ? ? ? on;
? ? #tcp_nopush? ? on;
? ? keepalive_timeout? 65;
? ? #gzip? on;
? ? include /usr/local/nginx/conf/conf.d/*.conf;
}
按wq退出鳖敷,新建gogs.conf進(jìn)行編輯
[root@localhost src]# vim /usr/local/nginx/conf/conf.d/gogs.conf
#輸入以下信息
upstream gogs {
? ? server 127.0.0.1:3000;
}
server {
? ? listen? ? ? 80;
? ? server_name? gogs.summer.com;
? # root /home/php/test;
? ? charset utf-8;
? ? access_log? /var/log/nginx/gogs.access.log? main;
? ? error_log /var/log/nginx/gogs.error.log? error;
? ? location / {
? ? ? ? try_files /_not_exists_ @backend;
? ? }
? ? location @backend {
? ? ? ? ? ? ? ? proxy_pass http://gogs;
? ? ? ? ? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? ? ? ? ? proxy_set_header Host $host;
? ? ? ? ? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? ? ? ? ? proxy_http_version 1.1;
? ? ? ? ? ? ? ? proxy_read_timeout? 360000s;
? ? ? ? ? ? ? ? proxy_set_header Upgrade $http_upgrade;
? ? ? ? ? ? ? ? proxy_set_header Connection "upgrade";
? ? ? ? }
}
然后重啟nginx脖苏,添加hosts文件指向遠(yuǎn)程主機(jī)IP ? gogs.summer.com,訪問gogs.summer.com就可以見到
建議倉庫根目錄位置與安裝gogs位置一致(我的位置是 /usr/local/src/gogs/gogs-repositories)
域名定踱、應(yīng)用 URL改成相應(yīng)的
如果 報(bào)運(yùn)行系統(tǒng)用戶非當(dāng)前用戶:git -> root 執(zhí)行
[root@s132-148-242-150 src]# su git -c "/usr/local/src/gogs/gogs web? 2>&1 &"
使用git賬號(hào)啟動(dòng)gogs棍潘。
配置了ssh后,gogs無法啟動(dòng)崖媚,錯(cuò)誤信息是:Fail?to?start?SSH?server:?listen?tcp?0.0.0.0:22:?bind:?permission?denied這個(gè)錯(cuò)誤是用于服務(wù)器使用了端口22亦歉,所以要更改服務(wù)器gogs的端口號(hào):
[root@localhost src]# vim /usr/local/src/gogs/custom/conf/app.ini
SSH_PORT = 1111
gogs開機(jī)自啟
[root@s132-148-242-150 src]# cp /usr/local/src/gogs/scripts/systemd/gogs.service /etc/systemd/system/gogs.service
[root@s132-148-242-150 src]# systemctl enable gogs
[root@s132-148-242-150 src]# systemctl start gogs
項(xiàng)目自動(dòng)部署
訪問gogs.summer.com進(jìn)入用戶設(shè)置,把SSH秘鑰加上(怎么生成公鑰和秘鑰在這就不說了)畅哑,就可以通過git命令上傳文件啦
上傳文件保存在倉庫的根目錄/usr/local/src/gogs/gogs-repositories/用戶名/項(xiàng)目名.git 文件夾下肴楷。
例如:SSH :root@localhost:coen/12buy.git 這個(gè)項(xiàng)目地址 用戶coen是管理者 12buy是項(xiàng)目名稱 上傳的文件則在/usr/local/src/gogs/gogs-repositories/coen/12buy.git 下。
項(xiàng)目自動(dòng)部署需要用到鉤子(hook)
進(jìn)入倉庫設(shè)置-》管理Git鉤子(管理者才有權(quán)限)荠呐,編輯post-receive赛蔫,push成功會(huì)自動(dòng)觸發(fā)這個(gè)鉤子,完成git倉庫到站點(diǎn)的克隆泥张,完成自動(dòng)部署
寫入下面內(nèi)容(target_branch是克隆那個(gè)分支 working_tree 是站點(diǎn)位置)
#!/bin/bash
target_branch="master"
working_tree="/home/www/12buy"
while read oldrev newrev refname
do
? ? branch=$(git rev-parse --symbolic --abbrev-ref $refname)
? ? if [ -n "$branch" ] && [ "$target_branch" == "$branch" ]; then
? ? ? GIT_WORK_TREE=$working_tree git checkout $target_branch -f
? ? ? NOW=$(date +"%Y%m%d%H%M%S")
? ? ? git tag release_$NOW $target_branch
? ? ? echo "? /==============================="
? ? ? echo "? | DEPLOYMENT COMPLETED"
? ? ? echo "? | Target branch: $target_branch"
? ? ? echo "? | Target folder: $working_tree"
? ? ? echo "? | Tag name? ? : release_$NOW"
? ? ? echo "? \=============================="
fi
done
保存呵恢,服務(wù)器上位置在/usr/local/src/gogs/gogs-repositories/custom_hooks下有個(gè)post-receive文件∶拇矗可以了渗钉,push一下,/home/www/12buy目錄下已經(jīng)有了文件