There are a multiple approaches on how to deploy NextJs Apps on self hosted server. This repo, we will be using a server.js file with next module to handle the requests.
- A valid sub-domain to host the site
- You will need a valid pair of SSL cert & key files.
- Host Port to run the server.
- Node.js JavaScript runtime environment
- NextJS Version: 12 or 13. You can use an existing project or implement in on a basic installation
Copy and Paste the SSL Certicate & Key inside the certs folder located in the project's root directory.
- You can use any location but will need to update the path in the server.js file.
- It is crucial to set proper permissions of the certificate & key files to maintain security and prevent unauthorized access.
- Private Key ( *.pem, *.key): Set the permissions to -rw------- (600 in numeric notation).
- Certificate (*.crt, *.pem, or *.cert): Set the permissions to -rw-r--r-- (644 in numeric notation).
Edit the server.js file to set up the following variables in accordance to your needs
- subdomain Example: www.example.com
- port Example: 443 (ensure that the port is not being used by any other service)
- sslConfig (map the path to the cert/key files here)
- If you are running this sample template, just run
pnpm i
# or
npm install --save
# or
yarn
- If you are implementing on an existing project, install the ssl-root-cas & fs packages using your preferred package manager
pnpm i ssl-root-cas fs
# or
npm install ssl-root-cas fs --save
# or
yarn add ssl-root-cas fs
- If running this standalone example, ignore this step
- If you are implementing on an existing project, then add the following two scripts to your package.json file.
"scripts": {
"startStage": "NODE_ENV=development node server.js",
"startProd": "NODE_ENV=production node server.js"
},
Build your app by running -
pnpm build
pnpm run startProd
# or
npm run startProd
# or
yarn run startProd