( ′∀`)σ≡σ☆))Д′)レ(゚∀゚;)ヘ=З=З=Зε≡(ノ´_ゝ`)ノ
# Deploying Fabric-to-Word Service on Ubuntu
This guide will help you deploy the `node-export` service on a clean Ubuntu server (20.04 or 22.04).
## Prerequisites
- SSH access to your Ubuntu server.
- Sudo privileges.
---
## Step 1: Install Node.js and NPM
We will use the official NodeSource repository to install Node.js 18.x (LTS).
```bash
# Update package list
sudo apt update
sudo apt upgrade -y
# Install curl if missing
sudo apt install -y curl
# Add NodeSource repository (Node.js 18)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
# Install Node.js
sudo apt install -y nodejs
# Verify installation
node -v
npm -v
```
---
## Step 2: Upload the Code
You can upload the `node-export` folder to your server using SCP, FTP (FileZilla), or Git.
**Option A: Using SCP (Command Line)**
Run this from your *local* machine:
```bash
# Replace 'user@your-server-ip' with your actual details
scp -r ./node-export user@your-server-ip:/var/www/thebrand/
```
**Option B: Manual Creation**
If you want to just copy-paste the files:
```bash
# On Server
mkdir -p /var/www/thebrand/node-export
cd /var/www/thebrand/node-export
# Create files using nano or vim and paste content
nano index.js
nano package.json
```
---
## Step 3: Install Dependencies
Navigate to the project directory on your server and install the required libraries.
```bash
cd /var/www/thebrand/node-export
# Install dependencies defined in package.json
npm install
# Test if it starts (Press Ctrl+C to stop after verifying)
node index.js
```
---
## Step 4: Setup Process Management (PM2)
We use PM2 to keep the Node.js app running in the background, restart it if it crashes, and start it automatically on boot.
```bash
# Install PM2 globally
sudo npm install -g pm2
# Start the application
# --name "word-export" gives it a friendly name
pm2 start index.js --name "word-export"
# Save the list of running processes
pm2 save
# Setup startup script (Copy the command output by this line and run it)
pm2 startup
```
**Useful PM2 Commands:**
- `pm2 status`: Check status.
- `pm2 logs word-export`: View logs.
- `pm2 restart word-export`: Restart app.
---
## Step 5: Configure Apache Reverse Proxy
Instead of exposing port 3000 directly to the internet (which is insecure), we will use Apache to forward requests from port 80/443 to port 3000.
1. **Install Apache** (if not installed):
```bash
sudo apt install -y apache2
```
2. **Enable Proxy Modules**:
Apache needs specific modules enabled to act as a reverse proxy.
```bash
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo systemctl restart apache2
```
3. **Edit your Apache Config**:
Typically located at `/etc/apache2/sites-available/000-default.conf` or your specific domain config.
```bash
sudo nano /etc/apache2/sites-available/000-default.conf
```
4. **Add the Proxy Configuration**:
Add these lines inside your `<VirtualHost *:80>` block:
```apache
<VirtualHost *:80>
ServerName your-domain.com
# ... existing config ...
# PROXY FOR NODE.JS EXPORT SERVICE
# Note: The trailing slash is important
ProxyPass /api/export-word http://localhost:3000/export
ProxyPassReverse /api/export-word http://localhost:3000/export
# Increase timeout for large file generation (optional)
ProxyTimeout 300
</VirtualHost>
```
5. **Restart Apache**:
```bash
# Check for syntax errors
sudo apache2ctl configtest
# Restart service
sudo systemctl restart apache2
```
---
## Step 6: Firewall (UFW)
Ensure your firewall allows Apache (Port 80/443) but blocks direct access to Port 3000 if possible.
```bash
sudo ufw allow 'Apache Full'
sudo ufw enable
```
---
## Usage
Now, your PHP/Frontend can access the service at:
`http://your-domain.com/api/export-word`
(which maps to `http://localhost:3000/export`)
**Payload:**
```json
{
"width": 800,
"height": 600,
"backgroundDataUrl": "...",
"objects": [...]
}
```