From 5698071d4717cd1f0e1d502b0d76e1d3afd604bb Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Fri, 8 Nov 2024 17:20:38 +0100 Subject: [PATCH] docs: add guide on deployment with PM2 + NGINX --- app/(root)/blog/[slug]/page.jsx | 1 + app/docs/[[...slug]]/page.jsx | 1 + components/PrismLoader.jsx | 1 + constants/docLinks.js | 6 +++ docs/deployment/index.md | 3 +- docs/deployment/pm2-nginx.md | 82 +++++++++++++++++++++++++++++++++ docs/deployment/svrjs.md | 2 +- docs/getting-started.md | 7 +-- 8 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 docs/deployment/pm2-nginx.md diff --git a/app/(root)/blog/[slug]/page.jsx b/app/(root)/blog/[slug]/page.jsx index e2d8e84..6cccb23 100644 --- a/app/(root)/blog/[slug]/page.jsx +++ b/app/(root)/blog/[slug]/page.jsx @@ -16,6 +16,7 @@ import "prismjs/components/prism-yaml"; import "prismjs/components/prism-markdown"; import "prismjs/components/prism-json"; import "prismjs/components/prism-perl"; +import "prismjs/components/prism-nginx"; import "./_styles/prism-twilight.css"; import "./_styles/prism.twilight.min.css"; diff --git a/app/docs/[[...slug]]/page.jsx b/app/docs/[[...slug]]/page.jsx index e6d61aa..88a3beb 100644 --- a/app/docs/[[...slug]]/page.jsx +++ b/app/docs/[[...slug]]/page.jsx @@ -19,6 +19,7 @@ import "prismjs/components/prism-markdown"; import "prismjs/components/prism-markup-templating"; import "prismjs/components/prism-json"; import "prismjs/components/prism-perl"; +import "prismjs/components/prism-nginx"; export async function generateMetadata({ params }) { const obtainedParams = await params; diff --git a/components/PrismLoader.jsx b/components/PrismLoader.jsx index 73382b6..9ed3377 100644 --- a/components/PrismLoader.jsx +++ b/components/PrismLoader.jsx @@ -11,6 +11,7 @@ import "prismjs/components/prism-yaml"; import "prismjs/components/prism-markdown"; import "prismjs/components/prism-json"; import "prismjs/components/prism-perl"; +import "prismjs/components/prism-nginx"; import "prismjs/components/prism-markup"; import "prismjs/components/prism-markup-templating"; import "prismjs/components/prism-handlebars"; diff --git a/constants/docLinks.js b/constants/docLinks.js index cf3fd03..b52195f 100644 --- a/constants/docLinks.js +++ b/constants/docLinks.js @@ -39,5 +39,11 @@ export default [ target: "_self", sub: true, label: "Deploy with SVR.JS" + }, + { + href: "/docs/deployment/pm2-nginx", + target: "_self", + sub: true, + label: "Deploy with PM2 + NGINX" } ]; diff --git a/docs/deployment/index.md b/docs/deployment/index.md index ef91b72..5516cd9 100644 --- a/docs/deployment/index.md +++ b/docs/deployment/index.md @@ -2,4 +2,5 @@ MERNMail provides multiple options for deployment. Here is the list of the options: -- [SVR.JS](/docs/deployment/svrjs) \ No newline at end of file +- [SVR.JS](/docs/deployment/svrjs) +- [PM2 + NGINX](/docs/deployment/pm2-nginx) \ No newline at end of file diff --git a/docs/deployment/pm2-nginx.md b/docs/deployment/pm2-nginx.md new file mode 100644 index 0000000..86bd1cf --- /dev/null +++ b/docs/deployment/pm2-nginx.md @@ -0,0 +1,82 @@ +# Deploy with PM2 + NGINX + +PM2 is a process manager for Node.JS applications with a built-in load balancer. NGINX is a popular web server and reverse proxy software. This tutorial will guide you on how to deploy MERNMail application with PM2 and NGINX on GNU/Linux systems. + +## 1. Clone the Repository + +First, change your working directory to your home directory, and clone the MERNMail repository: + +```bash +cd ~ +git clone https://git.svrjs.org/mernmail/mernmail.git +cd mernmail +``` + +## 2. Install Dependencies and Build the Application + +Navigate to the project directory, install the necessary dependencies, and build the MERNMail application: + +```bash +npm install +npm run build +``` + +## 3. Configuration + +Copy the `.env.example` file to a `.env` file in the root directory of the project. The configuration options can be found in the [Configuration](/docs/configuration) page. + +## 4. Install and Set Up PM2 + +Install and set up PM2: + +```bash +npm install -g pm2 +pm2 start npm --name "MERNMail" --start +pm2 startup +``` + +After running the `pm2 startup` command, run the command shown in the output to the terminal to start the PM2 daemon when the OS boots up. + +## 5. Install and Set Up NGINX + +Install NGINX using `sudo apt install nginx` on Debian-based systems, `sudo pacman -S nginx` on Arch-based systems, or `sudo dnf install nginx` on Red Hat-based systems. + +After installing NGINX, open your preferred text editor, and save these contents to the `/etc/nginx/sites-available/mernmail` file (replace `/home/user` with path to your home directory; also check the port the MERNMail application listens to; this configuration causes NGINX to only listen to port 80): + +```nginx +server { + listen 80; + server_name _; + + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"; + add_header x-content-type-options "nosniff"; + add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'sha256-VA8O2hAdooB288EpSTrGLl7z3QikbWU9wwoebO/QaYk=' 'sha256-+5XkZFazzJo8n0iOP4ti/cLCMUudTf//Mzkb7xNPXIc=' 'sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=' 'sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo='; style-src 'self' 'unsafe-inline'; frame-src 'self' data:"; + add_header Referrer-Policy "strict-origin-when-cross-origin"; + add_header Permissions-Policy "geolocation=(), camera=(), microphone=(), fullscreen=*"; + add_header Feature-Policy "geolocation 'none', camera 'none', microphone 'none', fullscreen *"; + + location /api/ { + proxy_pass http://localhost:3000/api/; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } + + location / { + root /home/user/mernmail/frontend/dist; + try_files $uri $uri/ =404; + } +} +``` + +After creating the NGINX configuration file, run these commands to enable the new NGINX configuration: +```bash +sudo rm /etc/nginx/sites-enabled/* +sudo ln -s /etc/nginx/sites-available/mernmail /etc/nginx/sites-enabled/mernmail +``` + +## 6. Restart NGINX + +Restart NGINX using either `sudo systemctl restart nginx` or `sudo /etc/init.d/nginx restart` command. \ No newline at end of file diff --git a/docs/deployment/svrjs.md b/docs/deployment/svrjs.md index e01c202..571bba3 100644 --- a/docs/deployment/svrjs.md +++ b/docs/deployment/svrjs.md @@ -57,7 +57,7 @@ sudo runuser svrjs -s /bin/bash -c 'cd /var/lib/mernmail && npm install && npm r ## 4. Configure MERNMail -Copy the `.env.example` file to a `.env` file in the MERNMail root directory (like `/var/lib/mernmail`). The configuration options can be found in the [/docs/configuration](Configuration) page. +Copy the `.env.example` file to a `.env` file in the MERNMail root directory (like `/var/lib/mernmail`). The configuration options can be found in the [Configuration](/docs/configuration) page. For additional security, you can set the permissions for the `.env` file using this command: ```bash diff --git a/docs/getting-started.md b/docs/getting-started.md index a23a6cc..f4d80c4 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -24,17 +24,18 @@ git clone https://git.svrjs.org/mernmail/mernmail.git cd mernmail ``` -### 2. Install Dependencies +### 2. Install Dependencies and Build the Application -Navigate to the project directory and install the necessary dependencies: +Navigate to the project directory, install the necessary dependencies, and build the MERNMail application: ```bash npm install +npm run build ``` ### 3. Configuration -Copy the `.env.example` file to a `.env` file in the root directory of the project. The configuration options can be found in the [/docs/configuration](Configuration) page. +Copy the `.env.example` file to a `.env` file in the root directory of the project. The configuration options can be found in the [Configuration](/docs/configuration) page. ### 4. Running the Application