By oldjoy
So I have a project structure that looks like this:
- client
- package.json
- server
- package.json
- package.json
In the package.json at root level, I have a npm script that starts both client and server for local development.
Now, I’m lost at deploying my client(React SPA) and server(Expressjs/Posgresql/redis) to Digital Ocean.
From what I’ve gathered with respect to deploying Nodejs to DO( I haven’t even explored deploying front-end yet :|), it seems common to do ‘git clone <nodejs_project.git>’ in droplet. But I’m clueless how to make that work with my current project structure above.
What do you guys think is the best practice here that work well with DO?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
Deploying a monorepo to Digital Ocean can be accomplished in a variety of ways, depending on your specific requirements. The approach you’ll need to take will depend on the specifics of your application, but generally speaking, you’ll need to configure your Digital Ocean Droplet to be able to serve both your client and server applications.
For the backend Node.js application, you could use a process manager like PM2 to keep the application running. The front-end application, which is built with React, will be bundled into a set of static files that can be served by a web server like Nginx.
Here are general steps to do that:
SSH into your Digital Ocean Droplet:
ssh root@your_droplet_ip
Install Node.js on your Droplet. You can use Node Version Manager (NVM) to help manage your Node.js versions:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install node
Install PM2 globally on your Droplet:
npm install pm2 -g
Install Nginx:
sudo apt-get update
sudo apt-get install nginx
Clone your git repository onto the server:
git clone your_repo.git
Navigate into your server directory and install dependencies:
cd your_repo/server
npm install
Start your server application with PM2:
pm2 start your_server_start_script.js
Navigate into your client directory and install dependencies:
cd ../client
npm install
Build your React application:
npm run build
Now, you need to configure Nginx to serve your static files. Open the default Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Edit the file so it looks something like this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /path/to/your/repo/client/build;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
Replace /path/to/your/repo/client/build with the actual path to your built React application.
Test Nginx configuration and restart Nginx:
sudo nginx -t
sudo service nginx restart
That should get your application up and running. Be sure to secure your server with a firewall and other necessary precautions. Look into setting up HTTPS with Let’s Encrypt as well.
Also note that the steps above are quite simplified and may vary depending on the exact setup and requirements of your application. Also, consider using environment variables for sensitive data like database credentials and keep these secure on your server.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.