Skip to content

Commit

Permalink
some unresolved conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
PurnenduMIshra129th committed Dec 29, 2024
1 parent fb166f6 commit 42af787
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 61 deletions.
90 changes: 36 additions & 54 deletions example/linux/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,29 @@ This guide provides step-by-step instructions for setting up the Talawa API serv

## Prerequisites

- **fnm** (Fast Version Manager)
- **fnm** (Fast Node Manager)
- **Node.js** (version specified in your Talawa API's `package.json`)
- **tsx** (TypeScript execution environment, install globally with `npm install -g tsx`)
- A Linux system with **systemd**
- **Root access** or `sudo` privileges for service installation
- **Dedicated system user** `talawa` for running the service (security best practice)
- **MongoDB** installed and running (required for Talawa API)
- **Redis** installed and running (required for Talawa API)
- Most important ! Add the working directory named `TALAWA_API_HOME` as global variable in `~/.bashrc`(eg.`export TALAWA_API_HOME="/path/to/your/talawa-api`) . Always give fallback sometimes it may not load.
- Proper file permissions on `/path/to/your/talawa-api` directory means `TALAWA_API_HOME` . Where your talawa-api is installed.
- Proper file permissions on `/path/to/your/talawa-api`
- For development:
- Ensure `.env` file sets `NODE_ENV=development`.
- Run the service manually to verify functionality.
- Ensure `.env` file sets `NODE_ENV=development`
- Run the service manually to verify functionality
- For production:
- Build the app to generate the `dist` folder.
- Ensure `.env` file sets `NODE_ENV=production`.
- Build the app to generate the `dist` folder
- Ensure `.env` file sets `NODE_ENV=production`
- **Log file setup**:
- Ensure a log file exists at `/var/log/talawa-api.log` with appropriate permissions and ownership.
- Verify Node.js version in your system matches the version required by `package.json`.
- Install `jq` for parsing JSON data (`sudo apt install jq` or equivalent).
- Ensure a log file exists at `/var/log/talawa-api.log` with appropriate permissions and ownership
- Verify Node.js version in your system matches the version required by `package.json`
- Install `jq` for parsing JSON data (`sudo apt install jq` or equivalent)

## Steps

### 1. Make `TALAWA_API_HOME` Path as Environment variable

- Find `/path/to/your/talawa-api` :
```bash
pwd
```
- Add `export TALAWA_API_HOME="/path/to/your/talawa-api"` in the `~/.bashrc`(for bash users) file or `~/.zshrc`(for zsh users). You can use any text editor like (`nano,vim,emacs,vi`).This command will open the file then add `export TALAWA_API_HOME="/path/to/your/talawa-api"`:
```bash
nano ~/.bashrc
```
- Refresh the shell to load the variable
```bash
source ~/.bashrc
```
- Verify the Path:
```bash
echo $TALAWA_API_HOME
```

### 2. Create a Dedicated System User
### 1. Create a Dedicated System User

- Create a user named `talawa` for running the service:
```bash
Expand All @@ -57,49 +37,50 @@ This guide provides step-by-step instructions for setting up the Talawa API serv
id talawa
```

### 3. Create the Systemd Service File
### 2. Create the Systemd Service File

- Create the `talawa-api.service` file in the `/etc/systemd/system/` directory with root privileges.
- Cheack following placeholders:
- `ExecStart` (path to your `Talawa-api.sh` script. Means `/path/to/your/talawa-api/example/linux/systemd/Talawa-api.sh`).
- `WorkingDirectory` (root directory of your Talawa API project `/path/to/your/talawa-api`).
- Refer to the example in `/path/to/your/talawa-api/example/linux/systemd/talawa-api.service` for guidance.
- here you can't use `TALAWA_API_HOME` as global variables are not accessed by systemd so you have to manually add it.
- Copy talawa-api.service then paste it inside `/etc/systemd/system/`
- Make sure `talawa-api.service` should have owned by root.
- Create the `talawa-api.service` file in the `/etc/systemd/system/` directory with root privileges
- Check following placeholders:
- `ExecStart` (path to your `Talawa-api.sh` script: `/path/to/your/talawa-api/example/linux/systemd/Talawa-api.sh`)
- `WorkingDirectory` (root directory of your Talawa API project: `/path/to/your/talawa-api`)
- `ReadOnlyPaths` (root directory of your Talawa API project: `/path/to/your/talawa-api`)
- `User, Group` (make sure to create user named `talawa`)
- Refer to the example in `/path/to/your/talawa-api/example/linux/systemd/talawa-api.service` for guidance
- Copy `talawa-api.service` then paste it inside `/etc/systemd/system/`
- Make sure `talawa-api.service` is owned by root

### 4. Set Up the `Talawa-api.sh` Script
### 3. Set Up the `Talawa-api.sh` Script

- Edit the script to specify:
- **Project directory** (e.g., `/path/to/your/talawa-api/talawa-api` means `TALAWA_API_HOME`)
- **Project directory** (e.g., `/path/to/your/talawa-api/talawa-api`)
- **Log file path** (e.g., `/var/log/talawa-api.log`)
- Ensure that the development (`src/index.ts`) and production (`dist/index.js`) paths are correctly set.
- Make sure `Talawa-api.sh` should be executable
- Ensure that the development (`src/index.ts`) and production (`dist/index.js`) paths are correctly set
- Make sure `Talawa-api.sh` is executable and owned by user `talawa`. Log file should also be owned by user `talawa`

### 5. Configure the Environment
### 4. Configure the Environment

- Ensure the `.env` file exists in the project directory and contains the appropriate configuration.
- Ensure the `.env` file exists in the project directory and contains the appropriate configuration
- Add the following environment variables:
- `NODE_ENV=development` or `NODE_ENV=production`.
- `NODE_ENV=development` or `NODE_ENV=production`

### 6. Verify Log File and Permissions
### 5. Verify Log File and Permissions

- Create the log file if it does not exist:
```bash
sudo touch /var/log/talawa-api.log
sudo chown talawa:talawa /var/log/talawa-api.log
sudo chmod 664 /var/log/talawa-api.log
```
- Ensure the log file owner matches the service user (e.g., `talawa`).
- Ensure the log file owner matches the service user (e.g., `talawa`)

### 7. Install Dependencies
### 6. Install Dependencies

- Install required Node.js version with `fnm`:
```bash
fnm install <node_version>
fnm use <node_version>
```
Replace `<node_version>` with the version specified in `package.json` (`engines.node`).
Replace `<node_version>` with the version specified in `package.json` (`engines.node`)
- Install dependencies:
```bash
npm install
Expand All @@ -113,7 +94,7 @@ This guide provides step-by-step instructions for setting up the Talawa API serv
sudo apt install jq
```

### 8. Enable and Start the Service
### 7. Enable and Start the Service

1. Reload the systemd configuration:
```bash
Expand All @@ -128,7 +109,7 @@ This guide provides step-by-step instructions for setting up the Talawa API serv
sudo systemctl start talawa-api.service
```

### 9. Verify the Installation
### 8. Verify the Installation

- Check the status of the service:
```bash
Expand Down Expand Up @@ -157,12 +138,13 @@ This guide provides step-by-step instructions for setting up the Talawa API serv
```bash
chmod +x /path/to/Talawa-api.sh
```
- Adjust `LimitNOFILE` and security-related settings in the `talawa-api.service` file as needed for your environment.
- Adjust `LimitNOFILE` and security-related settings in the `talawa-api.service` file as needed for your environment
- For production, ensure the `dist` folder exists by running:
```bash
npm run build
```
- If you encounter any issues, refer to the logs in `/var/log/talawa-api.log` or use `journalctl`.
- If you encounter any issues, refer to the logs in `/var/log/talawa-api.log` or use `journalctl`
- Don't try to create a global variable to store paths for use in both systemd service and script files. Global variables (like `/path/to/your/talawa-api`) will not work properly as systemd services run in a separate environment. While there are various suggested solutions (using `/etc/environment`, `/etc/default/`, or `Environment` and `EnvironmentFile` directives), these approaches can complicate service execution and are not recommended

### Additional Steps for Troubleshooting

Expand Down
6 changes: 3 additions & 3 deletions example/linux/systemd/Talawa-api.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
# Description: Talawa API startup script

# Use environment variable for project path, with a default fallback(eg./path/to/your/talawa-api replace with original path if TALAWA_API_HOME will not set then it will use default path.Important ! always use fallback path)
PROJECT_DIR="${TALAWA_API_HOME:-/path/to/your/talawa-api}"
# Don't use environment variables in this script, as when the script will run by systemd, it will not have access to the environment variables of the user.I have tried setting the environment variables in the systemd service file but it didn't work. So, directly use the absolute paths in the script.)
PROJECT_DIR="/path/to/your/talawa-api"
LOG_FILE="/var/log/talawa-api.log"
DEV_PATH="src/index.ts"
PROD_PATH="dist/index.js"
Expand Down Expand Up @@ -127,7 +127,7 @@ if [ ! -f ".env" ]; then
echo "Error: '.env' file not found. Exiting." | tee -a "$LOG_FILE"
exit 1
fi
echo ".env file found in '$pwd' directory. Proceeding..." | tee -a "$LOG_FILE"
echo ".env file found in '$(pwd)' directory. Proceeding..." | tee -a "$LOG_FILE"

# Load environment variables from .env file securely
NODE_ENV=$(grep '^NODE_ENV=' .env | cut -d '=' -f2)
Expand Down
8 changes: 4 additions & 4 deletions example/linux/systemd/talawa-api.service
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ After=network.target
# 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.Here you can't use TALAWA_API_HOME from shell (~/.bashrc) as systemd will not load any variable. That's why we have to manually add it.
ExecStart="${TALAWA_API_HOME}/example/linux/systemd/Talawa-api.sh"
# The command to start the service. This points to the Talawa-api.sh script.Here you can't use TALAWA_API_HOME from shell (~/.bashrc) as systemd will not load any variable. That's why we have to manually add it.Always use absolute path.
ExecStart="/path/to/your/talawa-api/example/linux/systemd/Talawa-api.sh"

# The working directory for the service.User have to export it.
WorkingDirectory=${TALAWA_API_HOME}
WorkingDirectory=/path/to/your/talawa-api

# 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'.Create a user named `talawa` for better understanding and Security
# The user to run the service as. You can find your username by running 'whoami'.Create a user named `talawa` for better understanding and Security.First create a user and group named 'talawa' for better Security.
User=talawa

# The group to run the service as. Usually, this is the same as the username.
Expand Down

0 comments on commit 42af787

Please sign in to comment.