LevButkovskiy / nginx-guide

Guide for setup Nginx server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Server guide

Guide for setup Nginx Ubuntu server

Contents

System requirements

  • Ubuntu 20.04

Software requirements

  • HTTP
    • nginx 1.20.1
  • SSL
    • snapd 2.51.4
    • certbot 1.18.0
  • Project
    • Node JS LTS 14.17.6

Nginx

Install Nginx

  1. sudo apt-get update - Check Ubuntu updates
  2. sudo apt install nginx - Install nginx
  3. sudo systemctl start - Start nginx
  4. sudo systemctl enable nginx- Start nginx with server (Auto launch)
  5. Done Result of install

Nginx settins

  1. Move to /etc/nginx/sites-available/
  2. Create %hostname%-ru.conf
  3. In file type:
server {
	listen 80;
	server_name %hostname%.ru www.%hostname%.ru;

	root /var/www/%hostname%.ru/public_html;

	index index.html index.htm;

	location / {
		try_files $uri $uri/ =404;
	}
}
  1. In command line type: ln -s /etc/nginx/sites-available/%hostname%-ru.conf /etc/nginx/sites-enabled/ to create link between sites-available and sites-enabled
  2. Delete default file from /etc/nginx/sites-enabled/ (rm -rf /etc/nginx/sites-enabled/default)
  3. Type nginx -t To check syntax. Result should be: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  4. Move to /var/www/
  5. Create new directory mkdir /var/www/%hostname%/
  6. Create directory mkdir /var/www/%hostname%/public_html
  7. In new directory place project build
  8. In command line type: sudo systemctl restart nginx to restart nginx
  9. Done. Your project build on server

SSL (HTTPS)

Certbot

Certbot using for LetsEncrypt certificates

  1. sudo apt install snapd to install snapd (Using for certbot)
  2. sudo snap install core; sudo snap refresh core Install and update snap core
  3. sudo apt-get remove certbot, sudo dnf remove certbot, or sudo yum remove certbot
  4. sudo snap install --classic certbot Install certbot
  5. sudo ln -s /snap/bin/certbot /usr/bin/certbot Execute the following instruction on the command line on the machine to ensure that the certbot command can be run.
  6. sudo certbot --nginx Install ssl in all configurations
  7. sudo certbot renew --dry-run To authomaticaly renew certificates
  8. Done. Go to https://%hostname%.ru Result

P.S To set certs for new nginx configuration type sudo certbot --nginx

Clonning project from Git

Prepare folder

  1. Clear folder for your project from git rm -r /var/www/%hostname%/public_html/
  2. Cd to folder: ``` /var/www/%hostname%/
  3. Clone git repo git clone https://github.com/%user name%/%repository name% public_html
  4. Cd to public_html cd public_html
  5. Done

Installing node

  1. Check updates sudo apt update
  2. Install node js sudo apt install nodejs
  3. Install npm sudo apt install npm
  4. Install npm into your project npm install
  5. Build latest version of your project npm run build
  6. Edit /etc/nginx/sites-available/%hostname%-ru.conf
	root /var/www/%hostname%/public_html; 

Change to

	root /var/www/%hostname%/public_html/build; 
  1. Check nginx syntax nginx -t
  2. Restart nginx sudo systemctl resart nginx
  3. Done

P.S. To further get updates from git in /var/www/%hostname%/public_html type git pull and then npm install, npm run build

About

Guide for setup Nginx server