feat: Add PM2 process manager configuration
- Add ecosystem.config.js with auto-restart settings - Add PM2 management scripts to package.json - Create PM2.md documentation - Update .gitignore to exclude logs - Create logs directory for PM2 output Scripts available: - npm run pm2:start - Start with PM2 - npm run pm2:stop - Stop process - npm run pm2:restart - Restart process - npm run pm2:logs - View logs - npm run pm2:status - Check status - npm run pm2:monit - Real-time monitoring
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -35,3 +35,8 @@ build/
|
||||
# Temporary files
|
||||
*.tmp
|
||||
*.temp
|
||||
|
||||
# PM2
|
||||
logs/
|
||||
.pm2/
|
||||
*.log
|
||||
|
||||
98
PM2.md
Normal file
98
PM2.md
Normal file
@@ -0,0 +1,98 @@
|
||||
# PM2 Process Manager - LPD Gerana Webcam
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Start aplikasi dengan PM2
|
||||
```bash
|
||||
npm run pm2:start
|
||||
```
|
||||
|
||||
### Stop aplikasi
|
||||
```bash
|
||||
npm run pm2:stop
|
||||
```
|
||||
|
||||
### Restart aplikasi
|
||||
```bash
|
||||
npm run pm2:restart
|
||||
```
|
||||
|
||||
### Lihat logs
|
||||
```bash
|
||||
npm run pm2:logs
|
||||
```
|
||||
|
||||
### Lihat status
|
||||
```bash
|
||||
npm run pm2:status
|
||||
```
|
||||
|
||||
### Monitor real-time
|
||||
```bash
|
||||
npm run pm2:monit
|
||||
```
|
||||
|
||||
### Hapus dari PM2
|
||||
```bash
|
||||
npm run pm2:delete
|
||||
```
|
||||
|
||||
## Auto Start saat Server Boot
|
||||
|
||||
### Setup PM2 startup (jalankan sekali)
|
||||
```bash
|
||||
# Generate startup script
|
||||
npx pm2 startup
|
||||
|
||||
# Salin dan jalankan command yang muncul (biasanya dengan sudo)
|
||||
# Contoh: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u wartana --hp /home/wartana
|
||||
|
||||
# Start aplikasi
|
||||
npm run pm2:start
|
||||
|
||||
# Save PM2 process list untuk auto-start
|
||||
npx pm2 save
|
||||
```
|
||||
|
||||
### Disable auto-start
|
||||
```bash
|
||||
npx pm2 unstartup
|
||||
```
|
||||
|
||||
## Fitur PM2
|
||||
|
||||
- ✅ **Auto Restart**: Aplikasi otomatis restart jika crash
|
||||
- ✅ **Logs Management**: Log tersimpan di folder `logs/`
|
||||
- ✅ **Memory Management**: Auto restart jika memory > 500MB
|
||||
- ✅ **Monitoring**: Real-time monitoring dengan `npm run pm2:monit`
|
||||
- ✅ **Status Tracking**: Cek uptime dan status aplikasi
|
||||
- ✅ **Startup Script**: Auto start saat server boot
|
||||
|
||||
## Log Files
|
||||
|
||||
- **Error Log**: `logs/pm2-error.log`
|
||||
- **Output Log**: `logs/pm2-out.log`
|
||||
|
||||
## Tips
|
||||
|
||||
1. **Update aplikasi tanpa downtime**:
|
||||
```bash
|
||||
git pull
|
||||
npm install
|
||||
npm run pm2:restart
|
||||
```
|
||||
|
||||
2. **Lihat logs 100 baris terakhir**:
|
||||
```bash
|
||||
npx pm2 logs lpd-gerana-webcam --lines 100
|
||||
```
|
||||
|
||||
3. **Flush logs**:
|
||||
```bash
|
||||
npx pm2 flush
|
||||
```
|
||||
|
||||
4. **Cek resource usage**:
|
||||
```bash
|
||||
npx pm2 monit
|
||||
```
|
||||
22
ecosystem.config.js
Normal file
22
ecosystem.config.js
Normal file
@@ -0,0 +1,22 @@
|
||||
module.exports = {
|
||||
apps: [{
|
||||
name: 'lpd-gerana-webcam',
|
||||
script: './server.js',
|
||||
instances: 1,
|
||||
exec_mode: 'fork',
|
||||
autorestart: true,
|
||||
watch: false,
|
||||
max_memory_restart: '500M',
|
||||
env: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 3000
|
||||
},
|
||||
error_file: './logs/pm2-error.log',
|
||||
out_file: './logs/pm2-out.log',
|
||||
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
|
||||
merge_logs: true,
|
||||
min_uptime: '10s',
|
||||
max_restarts: 10,
|
||||
restart_delay: 4000
|
||||
}]
|
||||
};
|
||||
@@ -6,7 +6,14 @@
|
||||
"scripts": {
|
||||
"start": "node server.js",
|
||||
"dev": "nodemon server.js",
|
||||
"setup-db": "mysql -u doc -p'doc2026!' < database.sql"
|
||||
"setup-db": "mysql -u doc -p'doc2026!' < database.sql",
|
||||
"pm2:start": "npx pm2 start ecosystem.config.js",
|
||||
"pm2:stop": "npx pm2 stop lpd-gerana-webcam",
|
||||
"pm2:restart": "npx pm2 restart lpd-gerana-webcam",
|
||||
"pm2:delete": "npx pm2 delete lpd-gerana-webcam",
|
||||
"pm2:logs": "npx pm2 logs lpd-gerana-webcam",
|
||||
"pm2:status": "npx pm2 status",
|
||||
"pm2:monit": "npx pm2 monit"
|
||||
},
|
||||
"keywords": [
|
||||
"lpd-gerana",
|
||||
|
||||
Reference in New Issue
Block a user