How get Plantly to server
Here is a short description of the activities done to get the application to the server
DISCLAIMER: Please note that this is done for studying purposes, therefore most of the following comes either directly or non directly from:
- https://olegpahhomov.gitlab.io/archive/iti0203-2019/class-machine-setup/
- https://olegpahhomov.gitlab.io/iti0203-2020/class-machine/
- https://echo360.org.uk/section/a4fc9b59-a5f3-4125-af0b-6734b15b8640/home
Secondly, here is more redundant information than needed as it is intended to use as a reference for ourselves.
And it will be updated as the process develops!
So what we did: starting with backend
Server parameters:
- Operating system: Select Ubuntu Server 20.04 LTS (HVM), SSD Volume Type
- Instance Type: t3.micro
- Configure Instance Details: default values
- Storage: 16 GiB General Purpose SSD (gp2)
- Security: Select All traffic and source is 0.0.0.0/0, ::/0
To get aws server: Login with by substituting the ssh key location
ssh -i {ssh_key_location} ubuntu@ec2-13-48-49-138.eu-north-1.compute.amazonaws.com
- To ease the process for windows users, a "Linux subsystem for Windows" could be used
instead of messing around with putty.
More detailed description how to get it running is provided at:
https://docs.microsoft.com/en-us/windows/wsl/install-win10
- To ease the process for windows users, a "Linux subsystem for Windows" could be used
instead of messing around with putty.
More detailed description how to get it running is provided at:
Add necessary ssh keys to server:
- cd .ssh/ switch to ssh folder
- nano authorized_keys edit/add keys
- sudo service ssh restart
Check and add virtual memory:
- For checking: htop
- For adding 2Gb virtual memory:
- sudo fallocate -l 2G /swapfile
- sudo chmod 600 /swapfile
- sudo mkswap /swapfile
- sudo swapon /swapfile
- sudo swapon –show
- echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab
Update and upgrade server
- sudo apt-get update
- sudo apt-get upgrade
Install java virtual machine
- sudo apt-get install openjdk-11-jre openjdk-11-jdk
Preparation of the backend
- gitlab-ci.yml is added to the backend project based on https://olegpahhomov.gitlab.io/production/gitlab_ci/
- Basically it defines three steps: build, test and deploy. An example is given here
stages: - build - test - deploy before_script: - export GRADLE_USER_HOME=`pwd`/.gradle build plants: stage: build cache: paths: - .gradle/wrapper - .gradle/caches artifacts: paths: - build/libs tags: - plants script: - ./gradlew assemble test plants: stage: test tags: - plants script: - ./gradlew check deploy plants: stage: deploy only: refs: - main - feature/ci-cd tags: - plants script: - mkdir -p ~/api-deployment - rm -rf ~/api-deployment/* - cp -r build/libs/. ~/api-deployment - sudo service plants restart
Install gitlab runner
- sudo curl -LJO https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_amd64.deb
- sudo dpkg -i gitlab-runner_amd64.deb
Register a runner for the backend (linux runner):
- sudo gitlab-runner register
- Add url and token from gitlab, tag is "plants", executor is "shell"
Add a decent name for the jar built by gradle, add to gradle.build
bootJar { archiveFileName = 'plants-backend.jar' }
check the name in setting.gradle
Create service from the backend
- cd /etc/systemd/system/
- sudo touch plants.service
- add the following:
[Unit] Description=dashboard plants service After=network.target [Service] Type=simple User=gitlab-runner WorkingDirectory=/home/gitlab-runner/api-deployment ExecStart=/usr/bin/java -jar plants-backend.jar Restart=on-abort [Install] WantedBy=multi-user.target
- reload:
sudo systemctl daemon-reload - enable:
sudo systemctl enable plants - restart: sudo service plants restart
- give gitlab runner the rights to run it
- sudo visudo
- And at the end: gitlab-runner ALL = NOPASSWD: /usr/sbin/service plants *
So if everything went fine (doubtfully) the backend is visible at:
{Public IPv4 address of the server}:8080/api
Moving to frontend
Download dependencies
- add node to search pattern: sudo curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
- install nodejs: sudo apt-get install -y nodejs
Prepare the frontend
- add .gitlab-ci.yml analog to the https://olegpahhomov.gitlab.io/production/gitlab_ci/
stages: - build - deploy build plants: stage: build image: node:12-alpine cache: paths: - node_modules artifacts: paths: - dist tags: - plants script: - npm install - npm run build deploy plants: stage: deploy tags: - plants script: - mkdir -p ~/front-deployment - rm -rf ~/front-deployment/* - cp -r dist/. ~/front-deployment
- change "build" value under "scripts" in package.json to "ng build --prod"
- add to environment.prod.ts a value apiUrl: 'http://{Public IPv4 address of the server}/api'
Prepare a runner in server analog to the backend point 9 but in this case for front end repository
Install nginx to the server: sudo apt-get install nginx
Create a configuration file for nginx:
- cd /etc/nginx/sites-available
- sudo cp default plants
- cd /etc/nginx/sites-enabled
- sudo ln -s /etc/nginx/sites-available/plants /etc/nginx/sites-enabled/
- sudo rm default
Modify a proxy setup for the backend in nginx:
- cd /etc/nginx/sites-available
- sudo nano plants
- add:
location /api/ { proxy_pass http://localhost:8000; }
- restart: sudo service nginx restart
- backend should be seen now from {Public IPv4 address of the server}/api
Make nginx to show the front end:
- cd /etc/nginx/sites-available
- sudo ln -s /home/gitlab-runner/front-deployment/ /var/www/front-deployment
- sudo nano plants
- Change the line root/var/www/html to root/var/www/front-deployment/iti0203-plantly-frontend;
- Restart: sudo service nginx restart
- Frontend should be seen now from {Public IPv4 address of the server}
Add a rule for nginx in config file to forward index.html
- cd /etc/nginx/sites-available
- sudo nano plants
- add:
location / { index index.html index.htm; if (!-e $request_filename){ rewrite ^(.*)$ /index.html break; } }
So if everything worked then congratulations, this guide has some use after all, otherwise ...
Adding all kind of fancy things
Add a desirable url name for the application:
- go to freenom, type in desirable domain name, choose preferred option
- on checkout if it asks 'Use your new domain' choose 'Use DNS' paste your public ip4 values to the both boxes
If you want to tie it aws with double way then
- go to https://console.aws.amazon.com/route53/v2/home#Dashboard
- Choose DNS management Hosted zones
- Create new hosted zone, apply your new freenom domain name
- Afterwards go the hosted zone, click it, take the four domain name serve names and in freenom: edit domain and add the domain server names
For the https
- Install certbot sudo apt install certbot python3-certbot-nginx
- sudo nginx -t
- sudo systemctl reload nginx
- sudo certbot --nginx
- enter your domain name and preferences and Done
Use docker (in your own discretion)
- Install docker to server according to https://docs.docker.com/engine/install/ubuntu/
- Add Dockerfile to run jar file from build
FROM openjdk:11-jre-slim ENV LANG C.UTF-8 ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64 ENV PATH $PATH:$JAVA_HOME/bin ADD build/libs/plants-backend.jar plants-backend.jar ENTRYPOINT ["java", "-jar", "plants-backend.jar"] EXPOSE 8080
- P.S if you want to use external configuration (like external.yaml) when running i.e production line, then change docker file to
FROM openjdk:11-jre-slim ENV LANG C.UTF-8 ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64 ENV PATH $PATH:$JAVA_HOME/bin ADD external.yaml external.yaml ADD build/libs/plants-backend.jar plants-backend.jar ENTRYPOINT ["java", "-jar", "plants-backend.jar", "--spring.config.location=external.yaml"] EXPOSE 8080
- Add docker-compose.yaml
version: '3.7' services: plants-api: build: context: . dockerfile: Dockerfile image: plants-api:latest ports: - '8080:8080'
- for running locally use docker-compose up
- for stopping use docker-compose down
- for running it server by gitlab runner change in gitlab.ci.yml
- sudo service plants restart
to
- docker-compose build - docker-compose down - docker-compose up -d --force-recreate
- NB! If doing so either comment out or remove a line
ExecStart=/usr/bin/java -jar plants-backend.jar
in /etc/systemd/system/plants.service
Adding spring actuator:
- Follow the guidelines from https://spring.io/guides/gs/actuator-service/
- Dont't forget to add necessary ports to application.yaml (external.yaml):
management: endpoint: health: show-details: always endpoints: web: exposure: include: "*" server: port: 9000
- And if using docker compose then add
ports: - '9000:9000'
For adding Zabbix to monitor it.
- Make a new server as in point 1
- Follow the installation guide from: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-zabbix-to-securely-monitor-remote-servers-on-ubuntu-20-04 (Many thanks to the author for providing such a detailed installation guide)
- P.S Zabbix runs in http://13.53.140.245/ with default password and username from previous link.
Done: 29.10.2020, updated: 21.11.2020