# Mawrd Production Deployment Guide

This guideline will help you to deploy mawrd.com to the production phase, including MongoDB setup, environment variable handling, and email configuration.

---

## **1. Server Setup and Next.js Deployment**

### Setting Up the Server
1. **Prepare the Server**:
   - Ensure your Linux server is up-to-date:
     ```bash
     sudo apt update && sudo apt upgrade -y
     ```
   - Install Node.js and npm:
     ```bash
     curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
     sudo apt install -y nodejs
     ```
   - Verify installation:
     ```bash
     node -v
     npm -v
     ```

2. **Install Git**:
   ```bash
   sudo apt install git -y
   ```

### Upload the Project Files

If your repository is private, you have the following options:

#### Option 1: Use SSH Key Authentication
1. Add your server's SSH key to your GitHub account:
   - On the server, generate an SSH key:
     ```bash
     ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
     ```
   - Copy the public key to GitHub:
     ```bash
     cat ~/.ssh/id_rsa.pub
     ```
     Add this key to your GitHub account under **Settings > SSH and GPG keys > New SSH Key**.

2. Clone the repository using SSH:
   ```bash
   git clone git@github.com:<your-username>/<your-repository>.git
   ```

#### Option 2: Use a Personal Access Token (PAT)
1. Create a PAT on GitHub:
   - Go to **Settings > Developer Settings > Personal Access Tokens > Tokens (classic)**.
   - Generate a new token with `repo` permissions.

2. Clone the repository using the token:
   ```bash
   git clone https://<your-username>:<your-token>@github.com/<your-username>/<your-repository>.git
   ```

#### Option 3: Upload Files Manually
1. Archive your project locally:
   ```bash
   tar -czvf mawrd.tar.gz mawrd/
   ```

2. Copy the archive to the server using SCP:
   ```bash
   scp mawrd.tar.gz user@server-ip:/path/to/deploy/
   ```

3. Extract the files on the server:
   ```bash
   tar -xzvf mawrd.tar.gz
   ```

After uploading the files using one of the methods above, proceed to the next step.

### Verify Project Structure
Ensure the project structure matches the following:
```
mawrd/
├── app/
│   ├── (admin)/
│   ├── api/
│   ├── auth/
│   ├── contact/
│   ├── submit-cv/
│   ├── favicon.ico
│   ├── globals.css
│   ├── layout.tsx
│   ├── not-found.tsx
│   ├── page.module.css
│   ├── page.tsx
├── components/
├── data/
├── node_modules/
├── public/
├── utils/
├── .env
├── .gitignore
├── eslint.config.mjs
├── middleware.ts
├── next-env.d.ts
├── next.config.js
├── package-lock.json
├── package.json
├── postcss.config.mjs
├── README.md
├── tailwind.config.ts
├── tsconfig.json
```

### Install Dependencies
1. Install the project dependencies:
   ```bash
   npm install
   ```

### Build the Project
1. Don't Build the project after installing dependencies (Finish the next steps first then build the project):
   ```bash
   npm run build
   ```

### Start the Application
1. Start the application:
   ```bash
   npm start
   ```

2. Optionally, use a process manager like `PM2` to keep the application running:
   ```bash
   sudo npm install -g pm2
   pm2 start npm --name "mawrd" -- start
   pm2 save
   pm2 startup
   ```

---

## **2. How to Add Environment Variables on a VPS Hosting**

### Edit the Shell Profile
1. Open the `.bashrc` or `.bash_profile` file:
   ```bash
   nano ~/.bashrc
   ```
2. Add your environment variables at the bottom:
   ```bash
   export MONGODB_URI="your-mongodb-uri"
   export NEXTAUTH_SECRET="your-nextauth-secret"
   export EMAIL_USER="noreply@mawrd.com"
   export EMAIL_PASS="your-email-password"
   export SMTP_HOST="smtp.mawrd.com"
   export SMTP_PORT=587
   export EmailNotify=true
   ```

### Source the Profile
Apply the changes:
   ```bash
   source ~/.bashrc
   ```

### For Systemd Services
If you're using a `systemd` service to run your app, add the variables to the service file:
1. Open the service file:
   ```bash
   sudo nano /etc/systemd/system/mawrd.service
   ```
2. Add the following under the `[Service]` section:
   ```bash
   Environment="MONGODB_URI=your-mongodb-uri"
   Environment="NEXTAUTH_SECRET=your-nextauth-secret"
   Environment="EMAIL_USER=noreply@mawrd.com"
   Environment="EMAIL_PASS=your-email-password"
   Environment="SMTP_HOST=smtp.mawrd.com"
   Environment="SMTP_PORT=587"
   Environment="EmailNotify=true"
   ```

3. Reload and restart the service:
   ```bash
   sudo systemctl daemon-reload
   sudo systemctl restart mawrd
   ```

---

## **3. MongoDB Setup for Production**

### Steps:
1. **Install MongoDB**:
   - Add the MongoDB public key:
     ```bash
     wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
     ```
   - Add the MongoDB repository:
     ```bash
     echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
     ```
   - Update the package database:
     ```bash
     sudo apt-get update
     ```
   - Install MongoDB:
     ```bash
     sudo apt-get install -y mongodb-org
     ```

2. **Start MongoDB**:
   ```bash
   sudo systemctl start mongod
   ```

3. **Enable MongoDB to Start on Boot**:
   ```bash
   sudo systemctl enable mongod
   ```

4. **Secure MongoDB**:
   - Enable authentication:
     ```bash
     sudo nano /etc/mongod.conf
     ```
     Under the `security` section, add:
     ```yaml
     security:
       authorization: enabled
     ```
     Save and exit.
   - Restart MongoDB:
     ```bash
     sudo systemctl restart mongod
     ```
   - Create an admin user:
     ```bash
     mongo
     use admin
     db.createUser({
       user: "admin",
       pwd: "securepassword",
       roles: [ { role: "root", db: "admin" } ]
     })
     exit
     ```

5. **Allow Remote Access (Optional)**:
   - Edit the bind IP in the configuration file:
     ```bash
     sudo nano /etc/mongod.conf
     ```
     Update the `bindIp` value:
     ```yaml
     net:
       bindIp: 0.0.0.0
     ```
     Restart MongoDB:
     ```bash
     sudo systemctl restart mongod
     ```
   - Use a firewall to restrict access:
     ```bash
     sudo ufw allow from <your-server-ip> to any port 27017
     ```

6. **Test the Connection**:
   - Connect using the MongoDB shell or a driver:
     ```bash
     mongo --host localhost -u admin -p --authenticationDatabase admin
     ```

7. **Connect DB to your project**:
   - Update the MONGODB configuration in environment variables:
        ```bash
MONGODB_URI="mongodb://localhost:27017/your-database-name"
     ```
---

## **4. Securing the `.env` File**

You should **never upload your `.env` file directly to the production server or version control system** (e.g., GitHub) since it contains sensitive credentials.

### Best Practices:
1. **Exclude `.env` from Version Control**:
   - Add `.env` to your `.gitignore` file so it isn’t pushed to your repository:
     ```plaintext
     # .gitignore
     .env
     ```

2. **Environment Variables on the Server**:
   - Use environment variables instead of relying on the `.env` file.
   - Most hosting platforms (e.g., Vercel, Heroku, AWS, etc.) allow you to define environment variables directly in their configuration settings.

3. **Encrypt .env if Necessary**:
   - If you must include a `.env` file on the server, encrypt it using tools like [git-crypt](https://github.com/AGWA/git-crypt) or manually decrypt it during deployment.

---

## **5. Additional Production Considerations**

### Use HTTPS
Ensure your `NEXTAUTH_URL` is updated to use HTTPS:
```env
NEXTAUTH_URL=https://your-domain.com
```

### Strong Secrets
- Ensure `NEXTAUTH_SECRET` is long, random, and strong. Use a password generator or tools like `openssl`.

### Email Notifications
- There is an environment variable called `EmailNotify`. Turn it on (`true`) if you want to send a confirmation email to the user when they submit a CV. You will need to define the following environment variables:
  ```env
  EMAIL_USER=noreply@mawrd.com
  EMAIL_PASS=your-email-password
  SMTP_HOST=smtp.mawrd.com
  SMTP_PORT=587
  ```
- If you do not want to notify the user via email, set `EmailNotify` to `false`.

---

## **6. Example Updated `.env` for Production**
```env
# MongoDB settings
MONGODB_URI="mongodb://localhost:27017/your-database-name"

# NextAuth
NEXTAUTH_SECRET="SemQmo8{[1(jr?j9w#'DeD^W4SG[?Q~j"
NEXTAUTH_URL=https://your-domain.com

# Admin credentials
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="admin123"

# Nodemailer Settings
EmailNotify=true  # Set to false to disable email notifications
EMAIL_USER=noreply@mawrd.com
EMAIL_PASS=your-email-password
SMTP_HOST=smtp.mawrd.com
SMTP_PORT=587
```

---

## **7. Summary Checklist**

- [ ] Replace `MONGODB_URI` with a cloud MongoDB connection string.
- [ ] Set up environment variables directly in your hosting platform (e.g., Vercel, Heroku, Your Hosting).
- [ ] Ensure HTTPS is used for `NEXTAUTH_URL`.
- [ ] Configure Mail Settings.
- [ ] Verify Project Structure.
- [ ] Install Dependencies, Build, and Start the Project.

By following these steps, you’ll ensure your application is secure, reliable, and ready for production!

