This tutorial will guide you through the process of deploying multiple MERN apps to a single digital ocean ubuntu VPS droplet Reference
$ sudo apt update
$ sudo apt install nodejs
$ nodejs -v
$ npm -v
In your droplets home directory create a folder called apps
$ mkdir apps
$ cd apps
$ git clone yourproject.git
cd yourproject
npm install
npm start (or whatever your start command)
# stop app
ctrl+C
sudo npm i pm2 -g
# Make sure you are in the root folder of your app then run
pm2 start app (or whatever your file name)
# Other pm2 commands
pm2 show app
pm2 status
pm2 restart app
pm2 stop app
pm2 logs (Show log stream)
pm2 flush (Clear logs)
# To make sure app starts when reboot
pm2 startup ubuntu
sudo ufw enable
sudo ufw status
# (Port 22)
sudo ufw allow ssh
#(Port 80)
sudo ufw allow http
#(Port 443)
sudo ufw allow https
sudo apt install nginx
I am assuming that the react front end is contained in a folder called "build" in your projects root directory
#From within your project ROOT directory
cp -Rv build /var/www/html
mv build mydomain.com
chown -R www-data:www-data /var/www/html/mydomain.com
sudo nano /etc/nginx/sites-available/mydomain.com.conf
I assume that the backend is running on port 5000 as denoted in the config block below -> "proxy_pass http://localhost:5000;"
and that the front end accesses the backend like this https://www/mydomain.com/backend/your-api-endpoints
server {
listen 80;
listen [::]:80;
#Path to the React front end
root /var/www/html/mydomain.com;
#the main html file
index index.html;
#Enter the IP address of your droplet
server_name <ENTER THE IP ADDRESS OF YOUR DROPLET>;
#STORE the nginx access logs for this app here
access_log /var/log/nginx/mydomain.com.access.log;
#Store the error logs for this app here
error_log /var/log/nginx/mydomain.com.error.log;
location / {
# Without this line routing in your Single Page APP will not work
try_files $uri $uri/ /index.html =404;
}
#REST API BACKEND CONFIG
location /backend {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:5000;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
Ctrl + O
Ctrl + X
14. Enable the new server block file by creating a symbolic link from the file to the sites-enabled directory
sudo ln -s /etc/nginx/sites-available/mydomain.com.conf /etc/nginx/sites-enabled/
sudo nginx -t
If there are no errors, the output will look like this:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl restart nginx
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
sudo certbot --nginx -d mydomain.com -d www.mydomain.com
sudo certbot renew --dry-run
#FIRST DELETE THE CONF FILE FROM SITES-ENABLED AND NOT SITES-AVAILABLE
#ONCE AGAIN DELETE THE CONF FILE IN SITES-ENABLED
cd /etc/nginx/sites-enabled
rm mydomain.com.conf
#and then after you have finished modifying the conf file in SITES-AVAILABLE run this once again
sudo ln -s /etc/nginx/sites-available/mydomain.com.conf /etc/nginx/sites-enabled/
Did you follow step 14, the symbolic link may not have been created, try running the step 14 command again, also check this folder "/etc/nginx/sites-enabled/" for the .conf file