Tuesday, November 19, 2019

How to generate SSH public and private key pair, connect to an external server, configuring an nginx server and deploy an angular application

  1. Generate Public and Private SSH key pair in git bash Command Prompt/ Terminal.
  2. Connect to a Remote Server in CMD.
  3. Install NPM – Node Package Manager in a Server.
  4. Deploy or Update an Angular Application in a Server.
  5. Configure an nginx server.

Prerequisites

Interface: CMD or Terminal (Windows or Ubuntu/Linux Systems)

Prerequisites to Ubuntu/Linux or Windows Systems:
Git bash CLI installed
Windows: Download git bash installation binary package(msi) from its official site and install it like other programs. You may set path to its bin folder so that you can also access the git from CMD of windows. How to set path in windows.

Ubuntu/ Linux: Just execute following commands in terminal. It might ask you for password authentication. You do not have to set path here.
sudo apt-get update
sudo apt-get install git-core
sudo git –version                // to verify that git installed successfully

Generate Public and Private SSH key pair in git bash Command Prompt/ Terminal

Git bash consists of the SSH package by default.

Windows

   Run git bash as administrator. Right click on git bash icon, run as administrator and ok to accept warning popped up. or you may use CMD also with git command if the path set.
  1. Go to your User folder in C:/Users/<User Account>
  2. Create a directory named .ssh if not already exists. You can check existence of the director with ‘dir’ command which lists down everything inside the user directory.
  3. Generate public-private key pairs with ssh-keygen command from the directory ‘.ssh’. It will ask you for a name for the key pair. Enter any meaningful name related to the server. With or without extension does not matter. Then it asks for a pass phrase, provide if you need the feature or just hit enter otherwise.
  4. Now, the key pair has been generated. You can see them in explorer with the command “explorer .”. It would have two files, the one with the name you just provided and another with .pub extension to the name. if you add -o argument to the command it will save generated private key in a form more resistant to brute force attack.
  5. You can check with the private and public key that created inside the .ssh folder with ‘explorer .’ command.
 1. cd c:/users/<user>

 2. mkdir .ssh       
//ignore this line if the dir already exists.

 3. cd .ssh
 4. ssh-keygen -o
Enter file in which to save the key (C:\Users\<user>/.ssh/id_rsa):
                                                                             SERVER1KEY
Enter passphrase (empty for no passphrase):   ¿
Enter same passphrase again:                         ¿
Your identification has been saved in new.
Your public key has been saved in new.pub.

 5. explorer .

Ubuntu/ Linux

  1. To checkout .ssh folder, cd ~/.ssh.
  2. You can check if any ssh key already exist with command ‘ls’ from .ssh folder.
  3. You do not have to execute the ssh-keygen command from .ssh folder in Ubuntu systems. Executing the ssh-keygen command would create new key inside folder .ssh and also create .ssh folder if already not exists. 
  4. You can also copy public key with the command “cat ~/.ssh/id_rsa.pub”.
 >  cd ~/.ssh
 >  ls

 >  ssh-keygen -o
Enter file in which to save the key (/home/<user>/.ssh/id_rsa): NEW_KEY
Enter passphrase (empty for no passphrase):   ¿
Enter same passphrase again:                         ¿
Your identification has been saved in NEW_KEY.
Your public key has been saved in NEW_KEY.

 >  cat ~/.ssh/id_rsa.pub
l8JP99FWl8iyjNa1XUleoxTNkSP9mrZMpPC8fY.ZnbdDWtw7gyibMQpc
Z8DBrqszpFjwtezrAZXJABFcSdqMFm1Q=rQcEEn/75WKa3bbAQyAdlv
ATmaHE7VAoriJAhoQ8BrXpI/p+GcTG10z2dOHUoV@sWvivT3ecjC3po
ZWp3Ap/SnKkMlTri5BhEOauPrwc6syaHs6z9h/oL3nK48nDXdUcPsVM
1/biIBMFOEP6u3AR4AQV56omKWjbttaaRlljx+rlw=AX/TwRX1v2aVJV
8y/HVYry1l92ISFlwa4PdNyb1Nw+jaYlKUAkQA09E3wk1Ax7fLTxnAZ7
FnnpJXI+QAmDlrMFlfmXrNbgIIs0LkhRbD4vH+HfSLoFhm0wA81q7NA

You have successfully generated the SSH public-private key pair.

   Now you need to send your public key (the one with .pub extension) to supervisor or the person who owns the server account and ask to add your public key to the server authentication list. They will add it and provide you with the following details.

Host name: Ip address (###.###.###.###) or a domain name (www.server-url.com)
User name: Assigned by the supervisor.

Connect to a Remote Server in CLI

You can connect to the server directly via a CLI/ Terminal. If you often connect to the server, creating a config file make it easy and save time.
  1. Go to the .ssh directory.
  2. Create a text file and save it as config with no extension. (All file types and then ‘config’ as name barely).
Content of the text file:Update fields highlighted in red with your server information and save the file. The port field is optional and you do not include if it is not provided. Notice that field names are followed by a space and by corresponding values then.
Host <ANY_NAME> // will be used to connect to the server
    HostName <ip or domain name>
    User <user name assigned by the supervisor>
    IdentityFile ~/.ssh/<the key name given when generating the key pair>
    IdentitiesOnly yes
    Port <SERVER_PORT>         //It is optional and 8080 by default.
    ServerAliveInterval 60

Now you are done with the configuration and just connect to go.
  1. Run Git or CMD as administrator.
  2. Just type following command to connect to the server.
SSH ANY_NAME                  

   That is all. Now, you will have connected to the server in CLI mode. Any operation allowed by the supervisor can be done through it.

Install NPM – Node Package Manager in a Server

   NPM is distributed and installed with Node.js. We can download and install if it is a Microsoft server simply. But we often met with Ubuntu servers. We are using curl so that we would install NodeJS version of our choice. Invoke following commands in the terminal will be installing NodeJS of version v13.0.1 and corresponding NPM of version 6.12.0.

 >  sudo apt-get install curl
 >  curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash –
 >  sudo apt-get install nodejs
 To check whether installed that it installed properly and not a must
 >  sudo npm -v
          // Output will be like v13.0.1
 >  sudo npm -v
          // Output will be like 6.12.0

Now, you are done and have installed the NPM successfully.

Deploy or Update an Angular Application in a Server

   Once you have connected to a server, you will be in the home directory of the system. It is assumed that you have your project in a git repository. It is also assumed that you are using a nginx server. If you are using Apache server instead, setting up the server is easy, just copy the built outcome script in step 3 into the WWW directory by using the command “cp".

Newly Deploy an Angular APP to a server

Install NPM – Node Package Manager if not installed previously (discussed above) and You can check whether NPM exists with the command “npm -v”.
  1. Clone the project into the server. It might ask you to authenticate with user name and password. Provide them and go. If cloning is successful, it means you project source code is now on the server. You can check with the command “ls” which will list everything inside home directory and also your newly clone project.
  2. Install the application: It is to download and setup all dependencies of the project. i.e: getting node modules directory fulfilled.
  3. Build the angular application: It will optimize code and build all scripts into a form that can be run without angular CLI. 
  4. Configure NGINX file. Next lesion will be about configuring nginx. Look at there for it.
 >  git clone <remote-url>                       //You may need to sign in
Optional; listing everything inside home directory and the project
 >  ls

 >  cd <project_name>
 >  npm install

 >  ng built –prod

Update an angular app in a server

   This is almost same as new deployment but you do not need to clone project and configure the nginx. But you need to pull instead to adopt updates in the repository to the server.
  1. Pull the project: It would bring changes committed last to the repository to the server. Important: You may need to handle conflicts but I will not teach you that here.
  2. Install the application: It is required since repository may have some dependency changes in the “package.json” file.
  3. Build the angular application.
  4. If it is Apache server, copy the distribution folder and merge the one in the WWW directory with it.
 >  cd <project_name>
 >  git pull                           //You may need to sign in

 >  npm install

 >  ng built –prod
 Similar following command to copy compiled distribution
 if you use Wamp server
 >  cp ~<project_name>/dist/<project_name>  ~wamp64/www

Configure an nginx server

   I am not going to teach all the things related to configuring an nginx website configuration file in detail. I am just considering the server section of site-enable file which is basically required to configure to make your site running. If you really want in more depth, you can go with this link How to Configure NGINX or Full ExampleConfiguration at Nginx official site. Also, if your server is not installed with nginx, read Install Nginx on Ubuntu.
   Nginx server would have created a configuration file called “default.conf” inside /etc/nginx/sites-enabled/ and linked with the “nginx.conf” file (/your-app/config/nginx.conf). You may create any number of conf files as you want and define them in the “nginx.conf” file. But one is enough for a basic configuration.
1.     server {
2.         listen         80 default_server;                                       //ipv4
3.         listen         [::]:80 default_server;                                 //ipv6
4.         server_name    sample.com www.sample.com;
5.         root           /var/www/sample.com;
6.         index          index.html index.htm;
7.         try_files    $uri  $uri/  /index.html;
8.     }
  1. Search for the server section in the file. There can be one or more server sections defining roots to web applications in the server. But there will be only one in a fresh server on which the nginx just been installed. 
  2. There are 6 important fields in the section but you may be wanted to modify few where others will be kept as default. The server_name (line 4), root (line 5), index (line 6) and try_files must be ensured that they are correct.
Server_name: The browser determines that with which server it is connected.
Root: This field defines in which directory the server should be looking for application to be served at the server name.
Index: You will be defining the startup point of the web application. It can be combination of index.htm, index.php, index.html or index.
Try_files: This must be defined to avoid application retrieval failure in refresh when we are looking for sub-pages of it. Example: www.neckoverpain.com/this-is-my-page-1

It might be like following in some of the nginx versions.

 location / {
         try_files $uri $uri/ /index.html;

Tags: Linux Server, Ubuntu Server, connect to a server, install nodeJS Ubuntu, install node package manager in Ubuntu, update the server..

No comments:

Post a Comment

Whats New

How redundant uploads can be avoided in social media apps?

People spend more time on social media, and it influences the most and changes to societies. They want to share photos and videos often when...

You may also like to read

x

emailSubscribe to our mailing list to get the updates to your email inbox...

Delivered by FeedBurner