# Manual Deployment

Guide for deploying NCN Network manually on servers without containers.

***

## Prerequisites

* Linux server (Ubuntu 22.04 LTS recommended)
* Root or sudo access
* [Environment setup](/nc/neurochainai-guides/development/environment-setup.md) completed
* Network access between servers

***

## System Preparation

### Install Dependencies

```bash
# Update system
sudo apt update && sudo apt upgrade -y

# Install build dependencies
sudo apt install -y \
  build-essential \
  pkg-config \
  libssl-dev \
  protobuf-compiler \
  git \
  curl

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
```

### Install Python (for Compute Node)

```bash
sudo apt install -y python3 python3-pip python3-venv

# Create venv
python3 -m venv /opt/ncn/venv

# Install ML dependencies
/opt/ncn/venv/bin/pip install torch transformers numpy scipy
```

***

## Build Binaries

```bash
# Clone repository
git clone https://github.com/neurochainai/ncn-network-v2-rs.git
cd ncn-network-v2-rs

# Build release binaries
cargo build --release

# Copy binaries to /opt
sudo mkdir -p /opt/ncn/bin
sudo cp target/release/gateway_node /opt/ncn/bin/
sudo cp target/release/compute_node /opt/ncn/bin/
sudo cp target/release/p2p_registry_node /opt/ncn/bin/
```

***

## Configure Services

### Create Configuration Directory

```bash
sudo mkdir -p /opt/ncn/config
sudo mkdir -p /opt/ncn/data
sudo mkdir -p /opt/ncn/logs
sudo mkdir -p /opt/ncn/models
```

### P2P Registry Configuration

Create `/opt/ncn/config/registry.env`:

```bash
# Listening addresses
GRPC_LISTEN_ADDR=0.0.0.0:50050
P2P_LISTEN_ADDR=/ip4/0.0.0.0/tcp/8828

# Validator settings
VALIDATOR_PRIVATE_KEY=<your-validator-key>
MIN_VALIDATORS_FOR_CONSENSUS=3

# Blockchain sync
VALIDATOR_SYNC_ENABLED=true
FORKNET_RPC_URL=https://testnet-rpc-1.forknet.io
CHAIN_ID=828

# Logging
RUST_LOG=info
```

### Gateway Configuration

Create `/opt/ncn/config/gateway.env`:

```bash
# Listening addresses
GRPC_ADDR=0.0.0.0:50051
HTTP_ADDR=0.0.0.0:8080
WS_ADDR=0.0.0.0:9000

# Registry connection
REGISTRY_GRPC_ADDR=http://127.0.0.1:50050

# Wallet
GATEWAY_WALLET_PRIVATE_KEY=<your-gateway-key>
GATEWAY_WALLET_ADDRESS=<your-gateway-address>

# Blockchain
RPC_URL=https://testnet-rpc-1.forknet.io
CONTRACT_ADDRESS=0x4361115359E5C0a25c9b2f8Bb71184F010b768ea
CHAIN_ID=828

# Payment settings
REQUIRE_PAYMENT=true
ENABLE_PAYMENT_VERIFICATION=true

# Logging
RUST_LOG=info
```

### Compute Node Configuration

Create `/opt/ncn/config/compute.env`:

```bash
# Gateway connection
GATEWAY_ADDR=http://127.0.0.1:50051

# Node identity
COMPUTE_NODE_PRIVATE_KEY=<your-compute-key>
NODE_WALLET_ADDRESS=<your-compute-address>

# Model configuration
MODEL_PATH=/opt/ncn/models

# Execution settings
SANDBOX_MODE=strict
EXECUTION_TIMEOUT_SECS=300
MAX_MEMORY_MB=8192

# Python
PYTHON_PATH=/opt/ncn/venv/bin/python3
PYTHON_VENV_PATH=/opt/ncn/venv

# Logging
RUST_LOG=info
```

***

## Create Systemd Services

### P2P Registry Service

Create `/etc/systemd/system/ncn-registry.service`:

```ini
[Unit]
Description=NCN P2P Registry Node
After=network.target

[Service]
Type=simple
User=ncn
Group=ncn
WorkingDirectory=/opt/ncn
EnvironmentFile=/opt/ncn/config/registry.env
ExecStart=/opt/ncn/bin/p2p_registry_node
Restart=always
RestartSec=10

# Logging
StandardOutput=append:/opt/ncn/logs/registry.log
StandardError=append:/opt/ncn/logs/registry.log

# Security
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/opt/ncn/data /opt/ncn/logs

[Install]
WantedBy=multi-user.target
```

### Gateway Service

Create `/etc/systemd/system/ncn-gateway.service`:

```ini
[Unit]
Description=NCN Gateway Node
After=network.target ncn-registry.service
Wants=ncn-registry.service

[Service]
Type=simple
User=ncn
Group=ncn
WorkingDirectory=/opt/ncn
EnvironmentFile=/opt/ncn/config/gateway.env
ExecStart=/opt/ncn/bin/gateway_node
Restart=always
RestartSec=10

# Logging
StandardOutput=append:/opt/ncn/logs/gateway.log
StandardError=append:/opt/ncn/logs/gateway.log

# Security
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/opt/ncn/data /opt/ncn/logs

[Install]
WantedBy=multi-user.target
```

### Compute Node Service

Create `/etc/systemd/system/ncn-compute.service`:

```ini
[Unit]
Description=NCN Compute Node
After=network.target ncn-gateway.service
Wants=ncn-gateway.service

[Service]
Type=simple
User=ncn
Group=ncn
WorkingDirectory=/opt/ncn
EnvironmentFile=/opt/ncn/config/compute.env
ExecStart=/opt/ncn/bin/compute_node --gateway-addr ${GATEWAY_ADDR} --model-path ${MODEL_PATH}
Restart=always
RestartSec=10

# Logging
StandardOutput=append:/opt/ncn/logs/compute.log
StandardError=append:/opt/ncn/logs/compute.log

# Security (less restrictive for sandbox)
NoNewPrivileges=false
ProtectSystem=false

[Install]
WantedBy=multi-user.target
```

***

## Create Service User

```bash
# Create system user
sudo useradd -r -s /bin/false ncn

# Set ownership
sudo chown -R ncn:ncn /opt/ncn
```

***

## Start Services

```bash
# Reload systemd
sudo systemctl daemon-reload

# Enable services
sudo systemctl enable ncn-registry ncn-gateway ncn-compute

# Start services
sudo systemctl start ncn-registry
sudo systemctl start ncn-gateway
sudo systemctl start ncn-compute

# Check status
sudo systemctl status ncn-registry
sudo systemctl status ncn-gateway
sudo systemctl status ncn-compute
```

***

## Firewall Configuration

```bash
# UFW example
sudo ufw allow 50050/tcp  # Registry gRPC
sudo ufw allow 8828/tcp   # Registry P2P
sudo ufw allow 50051/tcp  # Gateway gRPC
sudo ufw allow 8080/tcp   # Gateway HTTP
sudo ufw allow 9000/tcp   # Gateway WebSocket

# Or iptables
sudo iptables -A INPUT -p tcp --dport 50050 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8828 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 50051 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 9000 -j ACCEPT
```

***

## Log Management

### View Logs

```bash
# Registry logs
sudo tail -f /opt/ncn/logs/registry.log

# Gateway logs
sudo tail -f /opt/ncn/logs/gateway.log

# Compute logs
sudo tail -f /opt/ncn/logs/compute.log

# Or via journalctl
sudo journalctl -u ncn-gateway -f
```

### Log Rotation

Create `/etc/logrotate.d/ncn`:

```
/opt/ncn/logs/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 640 ncn ncn
    postrotate
        systemctl reload ncn-gateway ncn-registry ncn-compute 2>/dev/null || true
    endscript
}
```

***

## Health Checks

### Check Services

```bash
# Service status
sudo systemctl status ncn-registry ncn-gateway ncn-compute

# Health endpoints
curl http://localhost:8080/health

# gRPC connectivity
grpcurl -plaintext localhost:50050 list
grpcurl -plaintext localhost:50051 list
```

***

## Updating

```bash
# Stop services
sudo systemctl stop ncn-compute ncn-gateway ncn-registry

# Update code
cd /path/to/ncn-network-v2-rs
git pull

# Rebuild
cargo build --release

# Copy new binaries
sudo cp target/release/gateway_node /opt/ncn/bin/
sudo cp target/release/compute_node /opt/ncn/bin/
sudo cp target/release/p2p_registry_node /opt/ncn/bin/

# Restart services
sudo systemctl start ncn-registry ncn-gateway ncn-compute
```

***

## Troubleshooting

### Service Won't Start

```bash
# Check logs
sudo journalctl -u ncn-gateway -n 50

# Check permissions
ls -la /opt/ncn/

# Verify binary
/opt/ncn/bin/gateway_node --help
```

### Connection Issues

```bash
# Check ports
sudo netstat -tlnp | grep -E "(50050|50051|8080)"

# Test connectivity
nc -zv localhost 50051
```

***

## Next Steps

* [Production Deployment](/nc/neurochainai-guides/deployment/production.md) - Production hardening
* [Monitoring](/nc/neurochainai-guides/operators/monitoring.md) - Set up monitoring
* [Troubleshooting](/nc/neurochainai-guides/troubleshooting/troubleshooting.md) - Common issues


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.neurochain.ai/nc/neurochainai-guides/deployment/manual.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
