ssh <user_name>@<server_ip>
use this resource: https://nodejs.org/en/
N.B: if you do not use mongodb, skipe this section use this resource: https://docs.mongodb.com/manual/installation/
sudo systemctl enable mongod
this proccess may be need a few minute to execute
sudo systemctl start mongod
sudo npm install pm2 -g
npm install pm2 --save
pm2 init
change the ecosystem.config.js content to:
module.exports = {
apps: [
{
name: "your_app_name",
script: "server/server.js",
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: "1G",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
},
},
],
... other code
};add this to scritp to package.json
"start": "pm2 start ecosystem.config.js --env production",
"stop": "pm2 stop ecosystem.config.js --env production"
git clone <your_project_git_repository>
npm install
npm start
user this resource: https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#prebuilt_ubuntu
cd /etc/nginx/sites-available/
suod cp default api
sudo vim /etc/nginx/sites-available/api
add this to the location part in the server block
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
activat the api site
sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/api
restart the nginx service
sudo service nginx restart
for react:
npm install pm2 --save
add this line to package.json:
"start-production-server": "pm2 start ./node_modules/react-scripts/scripts/start.js --name <your-server-name>",
"stop-production-server": "pm2 stop <your-server-name>",
git clone <your_project_git_repository>
npm install
npm run build
npm run start-production-server
pm2 save
cd /etc/nginx/sites-available/
suod cp default front
sudo vim /etc/nginx/sites-available/front
add this to the location part in the server block
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
activat the api site
sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/api
restart the nginx service
sudo service nginx restart
use this resource: https://certbot.eff.org/lets-encrypt/ubuntufocal-nginx
sudo letsencrypt certonly -a webroot --webroot-path=/usr/share/nginx/website.com/ -d apidomain.com
- Add redirect to https in port 80 block
return 301 https://$host$request_uri;
- Add a new block for port 443
server {
server_name www.apidomain.com;
listen 443 ssl;
listen [::]:443 ssl;
root /usr/share/nginx/website.com;
index index.php index.html index.htm;
location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; #depend on your domain
ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; # depend on you domain
}
sudo letsencrypt certonly -a webroot --webroot-path=/usr/share/nginx/website.com/ -d frontdomain.com
- Add redirect to https in port 80 block
return 301 https://$host$request_uri;
- Add a new block for port 443
server {
server_name www.yourdomain.com;
listen 443 ssl;
listen [::]:443 ssl;
root /usr/share/nginx/website.com;
index index.php index.html index.htm;
location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; #depend on your domain
ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; #depend on your domain
}