This document describes how to deploy the application to production. We make use of Next.js as a framework, which has a built-in server.
To export the application as static HTML run the following command:
npm run export
The server is setup using PM2. This is a process manager for Node.js applications. It allows us to run the application in the background and restart it when it crashes.
To install PM2 run the following command:
npm install pm2 -g
To start the application run the following command:
pm2 start npm --name "<project>" -- start
This will start the application in the background. To view the logs run:
pm2 logs <project>
To stop the application run the following command:
pm2 stop <project>
To restart the application run the following command:
pm2 restart <project>
To update the application run the following command:
pm2 stop <project>
pm2 start npm --name "<project>" -- start
The script for deployment with Exonet could look like:
#!/bin/sh
git pull
npm ci
npm build
npm start
sudo supervisorctl restart <project>
Deployment of .next
packages is not needed, because the application is running in production mode.
To make the application available on port 80 we make use of a reverse proxy. This is a server that forwards requests to the application. We use Nginx as a reverse proxy.
To install Nginx run the following command:
brew install nginx
To configure Nginx open the following file:
sudo nano /usr/local/etc/nginx/nginx.conf
Add the following configuration to the http
block:
server {
listen 80;
server_name <domain>;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
Replace <domain>
with the domain of the application.
To start the reverse proxy run the following command:
sudo nginx
To enable HTTPS we make use of Let's Encrypt. This is a free, automated, and open certificate authority. It allows us to generate a SSL certificate for the application.
To install Let's Encrypt run the following command:
brew install certbot
To configure Let's Encrypt open the following file:
sudo nano /usr/local/etc/nginx/nginx.conf
Add the following configuration to the http
block:
server {
listen 80;
server_name <domain>;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
location ~ /.well-known/acme-challenge {
allow all;
root /usr/local/var/www;
}
}
Replace <domain>
with the domain of the application.
To generate a certificate run the following command:
sudo certbot certonly --webroot -w /usr/local/var/www -d <domain>
Replace <domain>
with the domain of the application.