just trying to consolidate all the stuff i end up googling everytime i deploy something on ubuntu. credit to DO and all other articles i reference from.
# Update sources
sudo apt-get update
# Install Git
sudo apt-get install git
# Install Nginx
sudo apt-get install nginx
# Install Certbot (For SSL with Let's encrypt)
# add sources for Certbot before this
sudo apt-get install certbot python-certbox-nginx
# Install NodeJS (v13) (Change _13.x to whatever 14,15 or 100.
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt-get install -y nodejs
# By now you have node, npm, git & certbot ready
# Now some process management
npm -g pm2
cd /var/source-code.git
git init --bare
cd hooks && touch post-receive
echo
"
#!/bin/sh
git --work-tree=/var/www/livefolder --git-dir=/home/source-code.git checkout -f
cd /var/www/livefolder
npm i
npm run build
pm2 restart process.json
" > post-receive
chmod +x post-receive
git remote add live ssh://user@server:/var/source-code.git
git add . && commit -m "pushing live"
git push live master
sudo chown -R $USER:$USER /var/www/website-folder
sudo chmod -R 755 /var/www
sudo apt-get install nginx
nano /etc/nginx/sites-available/default
add this :
server {
listen 80;
server_name livedomain.com;
location / {
proxy_pass http://localhost:3001;
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;
}
}
sudo service nginx restart
This assumes your express.js somewhere says :
app.listen("localhost",3001);
sudo npm install pm2 -g
Start Process & Restart it on file change (Deployment) more
pm2 start app.js
Sample Watch File: process.json
{
"apps" : [{
"script" : "index.js",
"watch" : true,
"ignore_watch" : ["logs","node_modules","values"],
"env": {
"NODE_ENV": "production",
}
}]
}
Setup procedure :
pm2 start process.json
Set it up for Starting on server restart
pm2 startup
Save the current proccesses to see them start
pm2 save
useful commands :
pm2 list
pm2 show [id]
pm2 logs
pm2 monit
Further readings: