This repository contains information about deploying the acbs-database (backend and frontend) on a server.
To deploy the acbs-database on a server, you will need:
- Node.js
- Express.js
- MongoDB
- Nginx
Node.js is the application runtime, Express.js is a backend web application framework for Node.js, MongoDB is a NoSQL database used for data storage. Nginx web server is used as a reverse proxy because, although Express.js can act as an internet-facing web server and even support SSL/TLS out of the box, it is better to use the Nginx reverse proxy for performance optimization, as well as for increased security, scalability, and flexibility of the application.
First, you'll need to install Node.js and MongoDB (including mongosh). Then, you'll need to create the acbs-database directory and go into it:
mkdir acbs-database
cd acbs-databaseIn this directory, you need to create two nested directories for the frontend and backend:
mkdir acbs-database-backend
mkdir acbs-database-frontendAnd then clone the acbs-database frontend and backend git repositories into these directories:
git clone https://github.com/webstraycom/acbs-database-backend.git acbs-database-backend
git clone https://github.com/webstraycom/acbs-database-frontend.git acbs-database-frontendThen go to the acbs-database-backend directory and install the required dependencies:
cd acbs-database-frontend
call npm install
cd ..Then go to the acbs-database-frontend folder and also install the required dependencies:
cd acbs-database-backend
call npm install
cd ..To run the project, use the npx nodemon app.js command, for example, to run the backend:
cd acbs-database-backend
npx nodemon app.jsBy default, the backend runs on port 3000 and the frontend on 3001, but you can change the port to any other by changing this line in the app.js file:
app.listen(3000);Congratulations! The main part is complete, but to complete the installation, we'll need to set up a reverse proxyusing the Nginx web server.
First, we'll need to install the Nginx web server. Go to the installation page and download the latest stable build of Nginx for Windows.
Then unzip the downloaded Nginx archive into the acbs-database directory, and then create the nginx/sites-enabled folder. In this directory, create two files – example.conf and api.example.conf.
The example.conf file should contain the following:
server {
listen 80;
server_name example.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:3001;
}
}This configuration file is used to have the web server process requests to port 80 (default) of example.com and redirect them to the application running on port 3001.
The api.example.conf file, in turn, should contain the following:
server {
listen 80;
server_name api.example.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:3000;
}
}The api.example.conf configuration file, in turn, is used to have the web server process requests to port 80 of the api.example.com subdomain and redirect them to the application running on port 3000.
In order for your websites to be accessible via https, you need to issue SSL certificates for them.
The easiest way to obtain and renew certificates from Let's Encrypt is to use the Certbot tool.
To install Certbot and issue certificates, follow these steps:
- Download
Certbotfrom the provided link and install it on your system. - Start the certificate issuance process with the following command:
certbot certonly --webroot -w C:\Certbot\webrootThen enter the names of the domains for which you want to issue an SSL certificate, separated by commas or spaces:
example.com, api.example.comWait for the SSL certificates to be issued, and then configure nginx to automatically restart when certificates are renewed.
You can do this by adding the following line to the Certbot certificate renewal configuration file (for example, the file is located in C:\Certbot\renewal\example.com.conf):
[renewalparams]
deploy_hook = C:\nginx\nginx.exe -s reload
Repeat this for all files in the renewal folder.
To ensure all redirects and paths are configured correctly, use the following command. It doesn't issue actual certificates or consume any limits:
certbot renew --dry-runTo actually update the certificates (which is done automatically by the Windows Task Scheduler), use the following command:
certbot renewThen modify the nginx configuration files for the domains by adding another server block to listen for requests on port 443.
For example, for the domain api.example.com, another server block should look like this:
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate C:\Certbot\live\api.example.com\fullchain.pem;
ssl_certificate_key C:\Certbot\live\api.example.com\privkey.pem;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:3000;
}
}You can also set up automatic redirection from http to https by making the following changes to the first server block:
server {
listen 80;
server_name api.example.com;
return 301 https://$host$request_uri;
}The line return 301 https://$host$request_uri; in the example above returns a permanent redirect (HTTP 301) to the https version of the same URL.
After all the changes have been made, your api.example.conf file should look like this:
server {
listen 80;
server_name api.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate C:\Certbot\live\api.example.com\fullchain.pem; # managed by Certbot
ssl_certificate_key C:\Certbot\live\api.example.com\privkey.pem; # managed by Certbot
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:3000;
}
}Great! You've just issued SSL certificates and configured your nginx web server to serve them. This will make your websites accessible via the secure https protocol.
After all the previous points have been completed, go to the nginx/conf folder and edit the nginx.conf file so that it looks like this:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
include "/acbs-database/nginx/sites-enabled/*.conf";
sendfile on;
keepalive_timeout 65;
}Congratulations! You've just successfully configured nginx reverse proxy for your domain and subdomain.
All that's left for you to do is start the web server by running the following commands:
cd acbs-database/nginx
nginx.exeYour web server will now act as a reverse proxy, accepting requests for each domain on the port specified in its configuration file (in this case, 80 or 443) and forwarding them to applications running on the ports also specified in the configuration files (in this case, 3000 and 3001).