# Everything about virtual private server


Learn virtual private server.

<!--more-->

## 1 Make user in UBUNTU

In, local machine create ssh keys to access the vps with ssh key

Create new public and private key

```bash
ssh-keygen
```

{{< admonition type=note title="Note" open=true >}}
Note: You can give directory location `<filename>` to save private and public key.
{{< /admonition >}}

See public key

```bash
cat ~/.ssh/<filename>.pub
```

- Copy content to newly created vps, Now you can access vps without using password.

### 1.1 Create a new user

```bash
sudo adduser <username>
```

### 1.2 Add user to "sudo" group

Give sudo permissions to new user

```bash
sudo usermod -aG <username>
sudo usermod -aG sudo <username>
```

Check sudo access

```bash
sudo cat /var/log/auth.log
```

## 2 Server Setup

Login with new user

```
ssh <username>@IP_ADDRESS
```

Create a new directory if it doesn't exist

```bash
mkdir -p ~/.ssh
```

Create authorized_keys file and paste public key

```bash
sudo nano ~/.ssh/authorized_keys
```

Change file permissions

```bash
sudo cat /var/log/auth.log
```

Disable root login, Find `PermitRootLogin` and set it `no`

```bash
sudo vi /etc/ssh/sshd_config
```

Note: Do it with new user's access.

Restart ssh daemon

```bash
sudo service sshd restart
```

Disable root login

```bash
sudo vi /etc/ssh/sshd_config
```

## 3 Nginx setup

Install nginx

```bash
sudo apt install nginx
```

Start nginx

```bash
sudo service nginx start
```

Firewall

```bash
sudo ufw allow 'Nginx HTTP'
```

Check status of ngnix

```bash
systemctl status nginx
```

Navigate to nginx

```bash
cd /etc/nginx/sites-available
```

Update default

```bash
sudo nano default
```

> Setup Github Action

Add sudo permission for action

```bash
sudo visudo -f /etc/sudoers.d/<username>
```

Put this line of script

```bash
<username> ALL=(ALL) NOPASSWD: /usr/sbin/service nginx start,/usr/sbin/service nginx stop,/usr/sbin/service nginx restart
```

Reboot the system

```bash
sudo reboot
```

> Install node with latest version
> [How To Install Node.js on Ubuntu 20.04](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04)

Install npm, serve and pm2

```bash
# Install npm
sudo npm i -g npm

# Install pm2
sudo npm i -g pm2

# Install serve
sudo npm i -g serve
```

Run server with pm2

```bash
# pm2 task creation for backend
pm2 start npm --name "backend" -- run start

# pm2 task creation for react app
pm2 serve --name "admin" build <port> --spa
```

Check Status and Log

```bash
# pm2 status
pm2 status

# pm2 log
pm2 log
```

nginx

```bash
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        # listen 80 default_server;
        # listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/app/_work/<project>/<project>;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name example.com <ip_address>;

        location /api/ {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                proxy_pass http://localhost:4000;
                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;
        }
        location / {
                proxy_pass http://localhost:5000;
                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;
        }
        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}
```

If you want new configuration file for nginx, create one and write configuation

```bash
sudo ln -s /etc/nginx/sites-available/<new-nginx-conf-file> /etc/nginx/sites-enabled/<new-nginx-conf-file>
```

