Skip to content

Instantly share code, notes, and snippets.

@webmaster-z1lab
Last active October 17, 2022 18:14
Show Gist options
  • Save webmaster-z1lab/084d20d58c8a7459f2ff23bf6eda5d0a to your computer and use it in GitHub Desktop.
Save webmaster-z1lab/084d20d58c8a7459f2ff23bf6eda5d0a to your computer and use it in GitHub Desktop.
Setup a Ubuntu 20.04 Nodejs webserver (NuxtJS ready) with Nginx (PageSpeed build), Brotli compression, LetsEncrypt SSL, Yarn (NPM) and PM2.

Setup a Ubuntu 18.04 LTS NodeJS webserver (NuxtJS ready) with Nginx (PageSpeed build), Brotli compression, LetsEncrypt SSL, Yarn (NPM) and PM2.

Add brotli repository

apt install libssl-dev
cd /usr/local/src
git clone --recursive https://github.com/google/ngx_brotli.git

Install certbot

sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Build nginx with pagespeed

[https://www.linode.com/docs/web-servers/nginx/build-nginx-with-pagespeed-from-source/]

bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
--nginx-version latest

Add the build parameters

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --add-module=/usr/local/src/ngx_brotli --with-compat

Config the nginx initializer

sudo nano /lib/systemd/system/nginx.service
sudo systemctl enable nginx
sudo systemctl start nginx

Config the nginx server

sudo useradd --no-create-home nginx
sudo mkdir -p /var/cache/nginx/client_temp
sudo mkdir /etc/nginx/conf.d/
sudo mkdir /var/www/example.com #your domain
sudo chown nginx:nginx /var/www/example.com
sudo mv /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf.backup-default

Config the server block

sudo nano /etc/nginx/conf.d/example.com.conf

Config PageSpeed

sudo mkdir /var/cache/ngx_pagespeed/
sudo chown nginx:nginx /var/cache/ngx_pagespeed/
curl -I -X GET example.com #Test your server

Config Brotli compression

[https://www.howtoforge.com/tutorial/how-to-install-nginx-with-brotli-compression-on-ubuntu-1804/]

sudo nano /etc/nginx/nginx.conf

Run certbot

sudo certbot --nginx

Config git

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global credential.helper 'cache --timeout=3600' #cache your password

Set permission for cache Git credentials

sudo chown ubuntu ~/.cache/git/credential/

Install Node and NPM

cd ~
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt-get install -y nodejs

Install Yarn

[https://yarnpkg.com/lang/en/docs/install/#debian-stable]

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

Install & config PM2

[https://medium.com/@vipercodegames/nuxt-deploy-809eda0168fc]

sudo npm install pm2@latest -g
pm2 startup systemd
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu
pm2 save
sudo systemctl start pm2-ubuntu
systemctl status pm2-ubuntu
ps aux | grep pm2 | grep -v grep | awk '{print $2}' | xargs kill -9 #if startup error [https://github.com/Unitech/pm2/issues/3924]

Clone the repository to webserver dir and set the permissions

sudo mkdir -p /var/www/example.com/releases
sudo chown -R ubuntu /var/www/example.com
sudo git clone example.rep /var/www/example.com/releases/v1.0.0
sudo chown -R ubuntu /var/www/example.com/releases/v1.0.0
sudo chgrp nginx /var/www/example.com/releases/v1.0.0
sudo chmod g+rwx /var/www/example.com/releases/v1.0.0
ln -s /var/www/example.com/releases/v1.0.0 /var/www/example.com/current
cd /var/www/example.com
pm2 init
nano /var/www/example.com/ecosystem.config.js
pm2 start
pm2 save
module.exports = {
apps : [{
name : 'app.com', // App name that shows in `pm2 ls`
exec_mode : 'cluster', // enables clustering
instances : 1, // or an integer
cwd : './current', // only if using a subdirectory
script : 'npm', // The magic key
args : 'start'
}]
};
# /etc/nginx/conf.d/example.com.conf
server {
server_name example.com www.example.com;
listen 80;
listen [::]:80;
# ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://localhost:3000;
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;
}
#PageSpeed config
pagespeed on;
pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
}
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
client_max_body_size 100m;
index index.html index.htm;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;
server_names_hash_bucket_size 64;
ssl_protocols TLSv1.2 TLSv1.3;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://localhost:3000;
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;
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment