Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FetureRequest/Add Talawa-api running on Linux as system daemon processs #2795

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions example/linux/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Talawa API Installation Guide

This guide provides step-by-step instructions for setting up the Talawa API service on a Linux system using systemd.

## Prerequisites

- Node.js (version 20.18.0 or current version the app is running on) (Ensure the Linux Node.js version matches the app version)
- nvm (Node Version Manager)
- tsx (TypeScript execution environment should be installed globally. Command : npm install -g tsx )
- A Linux system with systemd
- For development, first run manually to see if it is working. Check the `.env` file to ensure `NODE_ENV` is set to `development`.
- For production, first build the app to generate the `dist` folder, then set `NODE_ENV` to `production`.
- Ensure you provide the correct paths for each configuration to avoid errors.
- The service dynamically starts the development and production server based on the `.env` configuration, so verify it before making any changes.
- Always check the log files to identify errors.
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved

## Steps

1. **Create the Systemd Service File**:
- Create a file named `talawa-api.service` in the `/etc/systemd/system` directory. Ensure you have root privileges.

2. **Edit the `talawa-api.service` File**:
- Modify the `talawa-api.service` file as required (e.g., `ExecStart`, `WorkingDirectory`, `User`, `Group`). You can find how to edit these fields in the `talawa-api.service` file located in `example/linux/systemd`.

3. **Copy the Configuration**:
- Copy the text into the `talawa-api.service` file located in `/etc/systemd/system` and save it.

4. **Edit the `Talawa-api.sh` Script**:
- Edit the `Talawa-api.sh` script to set the project directory and log file path:

```bash
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
# Change to the project directory
cd /usr/local/talawa-api

# Define log file
LOG_FILE="/var/log/talawa-api.log"
```

PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
## Commands to Follow in Sequence

1. **Reload the Systemd Configuration**:
sudo systemctl daemon-reload

2. **Enable the Talawa API Service (required the first time):**:
sudo systemctl enable talawa-api.service

3. **Start the Talawa API Service:**:
sudo systemctl start talawa-api.service

4. **Check the Status of the Talawa API Service:**:
sudo systemctl status talawa-api.service

5. **View the Logs in Real-Time**:
sudo journalctl -u talawa-api.service -f

6. **Stop the Talawa API Service:**:
sudo systemctl stop talawa-api.service

7. **Restart the Talawa API Service:**:
sudo systemctl restart talawa-api.service

8. **Also, you can see direct log stored in /talawa-api/example/linux/talawa-api.log :**:
cat pathOfLogFile
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved



PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 52 additions & 0 deletions example/linux/systemd/Talawa-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
# filepath: eg: /home/purnendu/Development/talawa-api/example/linux/systemd/Talawa-api.sh

# Load nvm and use the correct Node.js version
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 20.18.0
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved

# Define the path to the tsx executable dynamically
TSX_PATH=$(which tsx)
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved

# Define paths for development and production
DEV_PATH="src/index.ts"
PROD_PATH="dist/index.js"

# Change to the project directory .give your path
cd /usr/local/talawa-api || exit 1

# Load environment variables from .env file
export "$(grep -v '^#' .env | xargs)"
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved

# Define log file give your path
LOG_FILE="/var/log//talawa-api.log"
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved

# Ensure the log file exists
sudo touch $LOG_FILE

# Debugging: Output environment variables
{
echo "NODE_ENV is: $NODE_ENV"
echo "Starting directory is: $PWD"

# Log the Node.js version
echo "Node.js version:"
node -v

# Log the current directory
echo "Current directory:"
pwd

# Check the value of NODE_ENV and execute the corresponding command
if [ "$NODE_ENV" == "development" ]; then
echo "Starting Talawa API in development mode..."
exec $TSX_PATH $DEV_PATH
elif [ "$NODE_ENV" == "production" ]; then
echo "Starting Talawa API in production mode..."
exec $TSX_PATH $PROD_PATH
else
echo "NODE_ENV is not set to a valid value. Please set it to 'development' or 'production'."
exit 1
fi
} >> $LOG_FILE 2>&1
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
39 changes: 39 additions & 0 deletions example/linux/systemd/talawa-api.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[Unit]
# Description of the service
Description=Talawa-API Service

# Ensure the service starts after the network is available
After=network.target

PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
[Service]
# The type of service. 'simple' means the service will start immediately.
Type=simple

# The command to start the service. This points to the Talawa-api.sh script.
ExecStart=/usr/local/talawa-api/example/linux/systemd/Talawa-api.sh

# The working directory for the service.
WorkingDirectory=/usr/local/talawa-api
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved

# Restart the service automatically if it stops.
Restart=always

# The delay before restarting the service.
RestartSec=5

# The user to run the service as. You can find your username by running 'whoami'.
User=purnendu

# The group to run the service as. Usually, this is the same as the username.
Group=purnendu

PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved
# Redirects the output and error to the systemd journal and console.
StandardOutput=journal+console
StandardError=journal+console

# Sets the maximum number of open files.
LimitNOFILE=1024
PurnenduMIshra129th marked this conversation as resolved.
Show resolved Hide resolved

[Install]
# Specifies the target to which the service should be added. 'multi-user.target' means the service will start in multi-user mode.
WantedBy=multi-user.target
Loading
Loading