---
# System prepended metadata

title: Installation Guide

---

# Installation Guide

### Welcome to Spotify ElGhalaba Installation Guide
Please follow the following instructions on a clean Linux Setup 
 > Note: We use `Ubuntu Server 18.04 x86_64` for the examples

1. Install `git` :-
    ```bash
    $ sudo apt update && sudo apt upgrade
    $ sudo apt install git
    ```
2. Install `docker` for containerizing the running processes :-
    ```bash
    $ curl -fsSL https://get.docker.com -o get-docker.sh
    $ sudo sh get-docker.sh
    $ sudo usermod -aG docker <your user>
    $ newgrp docker
    ```
    you can test the installation by
    ```bash
    $ docker -v
    # Docker version 19.03.8, build afacb8b7f0
    ```
3. Install `nginx` :-
    ```bash
   $ sudo apt install nginx
    ```
4. We will run our API container on port `8080`, front-end server on `4000` and `nginx` on port `80` as a reverse proxy. To do so, add the following config to `nginx.conf` :-
    ```yaml
    http {
        ....
        # Add this
        server {
            listen 80
            location /api {
                proxy_pass 127.0.0.1:8080/api
                client_max_body_size 100M
            }
            location * {
                proxy_pass 127.0.0.1:4000
            }
        }
    }
    ```
5. Get SSL Certificate :-
    We are going to use `certbot` to install a certificate  for nginx from LetsEncrypt.
    All you need is just to run this code.
    ```bash
    $ sudo apt-get update
    $ sudo apt-get install software-properties-common
    $ sudo add-apt-repository universe
    $ sudo add-apt-repository ppa:certbot/certbot
    $ sudo apt-get update
    $ sudo apt-get install certbot python3-certbot-nginx
    $ sudo certbot --nginx
    
    ```
    > Note: make sure your certificate is updated automatically every 90 days using the command `sudo certbot renew --dry-run`
7. Generate ssh keys to communicate with gitlab :-
    ```bash
    $ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (~/.ssh/id_rsa): # leave as is
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase): # Enter this 
    Enter same passphrase again: # repeat
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:gYoEmGJdS2ImAnoAmqd1JAWiK3bFYi/Y9wQjrXpM5Lg root@2d9ac453e081
    The key's randomart image is:
    +---[RSA 2048]----+
    |@o+*+o           |
    |O*++= ..         |
    |B ==.B. .        |
    | BBo*.o  .       |
    |++.B.o .S        |
    |o * o o          |
    | E o   .         |
    |  .              |
    |                 |
    +----[SHA256]-----+
    ```
    now the file is stored in `~/.ssh/id_rsa`. Now go there, open `id_rsa.pub` and copy its contents. Go to gitlab and add it to the allowed keys. **Make sure you have permissions**
    
7. Clone the two repos using ssh 
    ```bash
    $ git clone git@gitlab.com:spotify_el8alaba/front.git
    $ git clone git@gitlab.com:spotify_el8alaba/back.git
    ```
    Now you have two files in your `pwd` - let's say `~`. Time to dockerize !

> In the upcoming examples, we use `~` as `pwd` 

8. Starting a bridge Docker network and Installing MongoDB Database container :-
    First we need a Docker network to connect our Docker containers internally, It **MUST** be named **sp8** :
    ```bash
    $ docker network create sp8
    ```
    Make sure it's installed by listing the installed networks :
    ```bash
    $ docker network ls
    ```
    Now we need to run the DB continer inside our network using an image from DockerHub named `mongo:4-bionic` :
    ```bash
    $ docker container run -d --name db --network sp8 mongo:4-bionic
    ```
    Now you can see the db continer running :
    ```bash
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
    f6394cc87eec        mongo:4-bionic      "docker-entrypoint.s…"   7 weeks ago         Up About an hour    27017/tcp              db

    ```
    > NOTE : The connection string to this DB from any contianer **inside the `sp8` network** is `mongodb://db/<db-name>`

9. Before dockerization, make sure you adjust the environment variables of each application as mentioned in their `README` files.

10. Go to `~/back` and run :
    ```bash
    $ ./start-docker.sh
    ```
    > You  **MUST** run these commands inside the **root folder of each repo.**

    do the same with `~/front`. Now you have 3 running docker contaienrs. Run the following to verify :
    ```bash
    $ docker ps
    # You should see this output
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
    feb656893311        front:latest        "nginx -g 'daemon of…"   About an hour ago   Up About an hour    0.0.0.0:4000->80/tcp   front-prod
    0eee6021f074        back:latest         "docker-entrypoint.s…"   About an hour ago   Up 2 minutes        0.0.0.0:8080->80/tcp   back-prod
    f6394cc87eec        mongo:4-bionic      "docker-entrypoint.s…"   7 weeks ago         Up About an hour    27017/tcp              db
    ```
    Notice that the `back-prod` container runs on port `8080` by default while `front-prod` runs on port `4000`. If you wish to change this, you change the `EXPOSE` command of the `Dockerfile` but don't forget to also change the `docker run` command in `start-docker.sh`. However, we prefer to keep it as is and adjust their extenal env only.


### Thats it ! Happy Deployment !