설치 준비
# curl을 통해 최신 LTS 버전을 가져온다
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
# Nodejs를 설치한다.
sudo apt install -y nodejs
Quartz 설치
git clone https://github.com/jackyzha0/quartz.git
cd quartz
npm i
npx quartz create
# 알파인리눅스는 아래와 같이 실행
node --no-deprecation quartz/bootstrap-cli.mjs create
서비스 등록
sudo vi /etc/systemd/system/quartz.service
[Unit]
Description=Quartz
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/npx quartz build --serve --port 3002 --wsPort 3001
Restart=always
User=ubuntu
Environment=NODE_ENV=production
WorkingDirectory=/home/ubuntu/quartz
[Install]
WantedBy=multi-user.target
sudo systemctl enable quartz --now
Nginx 설치 및 설정
Nginx 설치
sudo apt install -y nginx
SSL 임시 인증 처리
sudo mkdir /etc/nginx/ssl
cd /etc/nginx/ssl
sudo gitea cert --host quartz.lhk.o-r.kr #임시 키
Nginx 설정파일 작성
sudo vi /etc/nginx/sites-available/quartz
server {
listen 80;
server_name quartz.lhk.o-r.kr;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name quartz.lhk.o-r.kr;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
location / {
proxy_pass http://localhost:3002;
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_set_header X-Forwarded-Proto $scheme;
}
}
Nginx 설정 적용 및 테스트 및 재시작
sudo ln -s /etc/nginx/sites-available/quartz /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
SSL 인증 및 재인증 자동화 처리
# Nginx 플러그인
sudo apt install -y python3-certbot-nginx
# --nginx 옵션을 이용해 certbot이 nginx 리로드를 자동으로 수행하게 설정
sudo certbot --nginx -d quartz.lhk.o-r.kr
업데이트
# git pull을 사용한 업데이트로 간혹 컨플릭트(충돌)가 발생하니 자동화는 지양한다.
npx quartz update
자동빌드 세팅
# 필요 패키지 설치
npm init -y
npm install express body-parser
# 웹훅 서버코드 작성
vi /home/ubuntu/webhook-server.js
const express = require('express');
const bodyParser = require('body-parser');
const { exec } = require('child_process');
const app = express();
const port = 3030; // 또는 원하는 포트
app.use(bodyParser.json());
app.post('/webhook', (req, res) => {
// Gitea 웹훅 페이로드 확인
if (req.body.ref === 'refs/heads/main') { // main 브랜치에 푸시된 경우
console.log('Received a push to main, triggering build...');
// 빌드 스크립트 실행
exec('cd /root/quartz/content && git pull', (error, stdout, stderr) => {
if (error) {
console.error(`Git Pull error: ${error}`);
return res.status(500).send('Git Pull failed');
}
console.log(`Git Pull output: ${stdout}`);
});
exec('cd /home/ubuntu/quartz && npx quartz build', (error, stdout, stderr) => {
if (error) {
console.error(`Build error: ${error}`);
return res.status(500).send('Build failed');
}
console.log(`Build output: ${stdout}`);
res.status(200).send('Build triggered successfully');
});
} else {
res.status(200).send('Ignoring push to non-main branch');
}
});
app.listen(port, () => {
console.log(`Webhook server listening at http://localhost:${port}`);
});
sudo vi /etc/systemd/system/webhook.service
[Unit]
Description=webhook
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/node webhook-server.js
server Restart=always
User=ubuntu
Environment=NODE_ENV=production
WorkingDirectory=/home/ubuntu
[Install]
WantedBy=multi-user.target
sudo systemctl enable webhook --now