# Voitekk Website Deployment Guide

## 1. Project Overview

This repository contains the source code for the Voitekk corporate website. It is a modern, server-rendered web application built with Next.js and React, designed for high performance and a seamless user experience. The project features a modular file structure for easy maintenance and scalability.

This guide provides detailed instructions for deploying the website on a production Ubuntu server using Apache2 as a reverse proxy to handle the Node.js process.

---

## 2. Supported Ubuntu Versions

This deployment guide has been tested and is supported on the following Ubuntu LTS (Long-Term Support) versions:
- **Ubuntu 20.04 LTS (Focal Fossa)**
- **Ubuntu 22.04 LTS (Jammy Jellyfish)**
- **Ubuntu 24.04 LTS (Noble Numbat)**

---

## 3. Prerequisites

Before you begin the installation, ensure your server meets the following requirements:

- A clean Ubuntu server instance (20.04, 22.04, or 24.04).
- Root or `sudo` access to the server.
- A static IP address for your server.
- (Optional but Recommended) A registered domain name pointing to your server's static IP address.

### Required Software & Modules

The installation script will handle installing most dependencies, but it's important to know what is required:

- **Node.js**: The application runs on the Node.js runtime. We recommend using version 20.x or later.
- **Apache2**: The web server that will act as a reverse proxy, directing traffic to the Next.js application.
- **Apache Modules**: `mod_rewrite`, `mod_proxy`, `mod_proxy_http`, `mod_proxy_wstunnel`, and `mod_headers` must be enabled.
- **Git**: For cloning the project repository.
- **UFW (Uncomplicated Firewall)**: For securing the server.

### File Permissions

The installation process involves setting specific ownership and permissions for the project files to ensure the web server can run the application securely. All project files will be owned by the `www-data` user and group, which is the standard user for Apache on Ubuntu.

---

## 4. Step-by-Step Installation Guide

Follow these steps carefully to deploy the Voitekk website.

### Step 1: Update System Packages

First, connect to your server via SSH and update the package list to ensure you have the latest information on available packages and their versions.

```bash
sudo apt update && sudo apt upgrade -y
```

### Step 2: Install Node.js, Git, and other Dependencies

We need to install Node.js to run the Next.js application. We will use the official NodeSource repository to get a modern version.

```bash
# Install curl and other basic dependencies
sudo apt install -y curl git
# Add the NodeSource repository for Node.js 20.x
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# Install Node.js
sudo apt install -y nodejs
```

### Step 3: Install and Configure Apache2

Install the Apache2 web server.

```bash
sudo apt install -y apache2
```

Next, enable the necessary Apache modules for the reverse proxy to function correctly.

```bash
sudo a2enmod proxy proxy_http proxy_wstunnel rewrite headers
sudo systemctl restart apache2
```

### Step 4: Clone and Build the Application

Clone the project from its Git repository into the `/var/www` directory.

```bash
sudo git clone https://github.com/your-repo/voitekk-website.git /var/www/voitekk
```
*Note: Replace `https://github.com/your-repo/voitekk-website.git` with your actual repository URL.*

Navigate into the project directory and install the Node.js dependencies using `npm`.

```bash
cd /var/www/voitekk
sudo npm install
```

After the dependencies are installed, create a production build of the Next.js application.

```bash
sudo npm run build
```

### Step 5: Set Ownership and Permissions

Change the ownership of the project directory to the `www-data` user and group so that Apache can manage the application files securely.

```bash
sudo chown -R www-data:www-data /var/www/voitekk
```

### Step 6: Configure Email for Lead Capture Form

To enable the lead capture form to send emails, you must configure the email server settings in an environment file.

Create a `.env.local` file in the root of the project directory:
```bash
sudo nano /var/www/voitekk/.env.local
```

Add the following environment variables to the file with your email provider's details.

```ini
# Email Server Configuration
EMAIL_SERVER_HOST=smtp.example.com
EMAIL_SERVER_PORT=587
EMAIL_SERVER_USER=your-email@example.com
EMAIL_SERVER_PASSWORD=your-email-password
EMAIL_FROM=sender-email@example.com
EMAIL_TO=recipient-email@example.com
```
Save the file (Ctrl+O, Enter) and exit (Ctrl+X).

### Step 7: Set Up PM2 for Process Management

PM2 is a process manager for Node.js applications that will keep your website running and restart it automatically if it crashes.

```bash
sudo npm install -g pm2
```

Start the Next.js application with PM2.

```bash
cd /var/www/voitekk
sudo pm2 start npm --name "voitekk" -- start
```

Now, configure PM2 to automatically start on server reboots.

```bash
sudo pm2 startup systemd -u www-data --hp /home/www-data
sudo pm2 save
```

### Step 8: Configure Apache Virtual Host

Create a new Virtual Host configuration file for your site. Replace `your-domain.com` with your actual domain name.

```bash
sudo nano /etc/apache2/sites-available/voitekk.conf
```

Paste the following configuration into the file. This configures Apache to listen on port 80 and forward all requests to the Next.js application running on port 3000.

```apache
<VirtualHost *:80>
    ServerName your-domain.com
    ServerAdmin webmaster@localhost
    
    ProxyPreserveHost On
    ProxyRequests Off
    
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

    # Required for Next.js live updates and WebSocket connections
    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://localhost:3000/$1 [P,L]

    ErrorLog ${APACHE_LOG_DIR}/voitekk-error.log
    CustomLog ${APACHE_LOG_DIR}/voitekk-access.log combined
</VirtualHost>
```
*Note: The default port for Next.js is `3000`. If you have configured it to run on a different port, update the `localhost` URLs accordingly.*

### Step 9: Enable the Site and Reload Apache

Disable the default Apache site, enable your new site configuration, and restart Apache to apply the changes.

```bash
sudo a2dissite 000-default.conf
sudo a2ensite voitekk.conf
sudo systemctl restart apache2
```

---

## 5. Verification

### Check Service Status
Verify that the Apache and PM2 services are active and running without errors.

```bash
# Check Apache status
sudo systemctl status apache2

# Check the status of the Next.js app managed by PM2
sudo pm2 status
```
Both services should show an "active (running)" status.

### Access the Website
Open your web browser and navigate to your server's IP address or domain name (e.g., `http://your-domain.com`). You should see the Voitekk website.

### Common Troubleshooting
- If Apache fails to start, check the syntax of your configuration file: `sudo apache2ctl configtest`.
- If the site is not loading, check the Apache error logs for clues: `sudo tail -f /var/log/apache2/voitekk-error.log`.
- To see logs from the Next.js application itself, use PM2: `sudo pm2 logs voitekk`.

---

## 6. Optional Enhancements

### Enable HTTPS with Let's Encrypt

It is highly recommended to secure your site with HTTPS. You can get a free SSL certificate from Let's Encrypt using the Certbot tool.

```bash
# Install Certbot for Apache
sudo apt install -y certbot python3-certbot-apache

# Obtain and install an SSL certificate
# Replace your-domain.com with your actual domain
sudo certbot --apache -d your-domain.com
```
Follow the on-screen prompts. Certbot will automatically update your Apache configuration and set up auto-renewal.

### Configure Firewall with UFW

Secure your server by enabling the UFW firewall and allowing traffic only on necessary ports.

```bash
# Allow SSH, HTTP, and HTTPS traffic
sudo ufw allow 'OpenSSH'
sudo ufw allow 'Apache Full'

# Enable the firewall
sudo ufw enable
```
Press `y` to confirm. Your firewall is now active.

---

## 7. Maintenance and Updates

### Updating the Website

To update the site with the latest changes from the Git repository, follow these steps:

```bash
# Navigate to the project directory
cd /var/www/voitekk

# Pull the latest code
sudo git pull origin main

# Install any new dependencies
sudo npm install

# Rebuild the application
sudo npm run build

# Restart the application with PM2
sudo pm2 restart voitekk
```

### Restarting Services
If you ever need to manually restart Apache or the application:

```bash
# Restart Apache
sudo systemctl restart apache2

# Restart the Next.js application
sudo pm2 restart voitekk
```
