From 42af787e6bce97ca7e2234b06a659a1cfa75fc39 Mon Sep 17 00:00:00 2001 From: Purnendu Date: Mon, 30 Dec 2024 02:57:49 +0530 Subject: [PATCH] some unresolved conversation --- example/linux/installation.md | 90 ++++++++++-------------- example/linux/systemd/Talawa-api.sh | 6 +- example/linux/systemd/talawa-api.service | 8 +-- 3 files changed, 43 insertions(+), 61 deletions(-) diff --git a/example/linux/installation.md b/example/linux/installation.md index b0ba33f55fe..7debb49f509 100644 --- a/example/linux/installation.md +++ b/example/linux/installation.md @@ -4,7 +4,7 @@ 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** @@ -12,41 +12,21 @@ This guide provides step-by-step instructions for setting up the Talawa API serv - **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 @@ -57,32 +37,33 @@ 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 @@ -90,16 +71,16 @@ This guide provides step-by-step instructions for setting up the Talawa API serv 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 fnm use ``` - Replace `` with the version specified in `package.json` (`engines.node`). + Replace `` with the version specified in `package.json` (`engines.node`) - Install dependencies: ```bash npm install @@ -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 @@ -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 @@ -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 diff --git a/example/linux/systemd/Talawa-api.sh b/example/linux/systemd/Talawa-api.sh index 2aa5f4b3ec6..d42bac68a49 100755 --- a/example/linux/systemd/Talawa-api.sh +++ b/example/linux/systemd/Talawa-api.sh @@ -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" @@ -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) diff --git a/example/linux/systemd/talawa-api.service b/example/linux/systemd/talawa-api.service index 846142e1f61..0293265d7bd 100644 --- a/example/linux/systemd/talawa-api.service +++ b/example/linux/systemd/talawa-api.service @@ -9,11 +9,11 @@ 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 @@ -21,7 +21,7 @@ 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.