Nearby Shops API Installation Guide for Ubuntu 16.04

In this article we will learn how we can install Nearby Shops API on ubuntu 16.04

 

A word of Caution for Developers

Currently the API Authenticates using Basic Auth and Lacks TLS / SSL Security. When planning to run a production version of the app please add TLS encryption before you start a production server.

 

Enough of the Introduction and Let us now begin with the step by step installation Guide for Nearby Shops API Backend

Step 1 : Install OpenJDK 8

Our API Runs on Java therefore we need JDK on our machine. We can install Oracle JDK also. But we recommend you to install OpenJDK-8 the open-source version because by using Oracle JDK you might get trapped into licensing issues.

To stay on the safe side of licensing issues prefer to install the open source version.

To install OpenJDK 8

Type these commands on your terminal

sudo apt-get udpate
sudo apt-get install openjdk-8-jdk

in order to verify jdk has been installed type type “java -version”

java -version

The above command gives the following output which indicates that Java 8 has been installed

openjdk version “1.8.0_151”
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

 

Step 2 : Install Postgres Database Latest Version

We use postgres as our primary database therefore we need to install Postgres. Currently Postgres 10 is the latest version of Postgres therefore we will install Postgres 10.

The instructions to install Postgres 10 are given at the following link

http://yallalabs.com/linux/how-to-install-and-use-postgresql-10-on-ubuntu-16-04/

If these instructions do not work for you. You can always find a tutorial on Postgres Installation by searching on Google.

 

Step 3 : Create Database , add User and Grant Privileges to user in Postgres DB

We need to create new database and add new user with password. We will grant all privileges to use the database to the new user we have created.

After that please write and note the database name, username and password somewhere because we will require them in future.

The following article provides very good overview of what command will be required.

https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e

Make sure you don’t use “postgres” as username and “password” as password. They are popular defaults and hackers look for them. Therefore using them as username and password is going to make your postgresql Installation vulnerable to hacking attacks.

 

Step 4 : Install GIT Version Control System

Nearby Shops API is hosted on Github and we will be cloning a repository from Github. For cloning the repo we will require GIT. Therefore if GIT is not on your system. Please install GIT.

 

Step 5 : Install Maven 

Maven is a build System for Java. It assembles the source code and generates the Executable Jar file.

We are going to generate an Executable Jar file from the latest Source code therefore we will require Maven also.

To install maven type the following

sudo apt-get update
sudo apt-get install maven

To verify that maven has been installed type

mvn -version

This will give the following / or similar output on your terminal which verifies that maven is installed.

Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: “linux”, version: “4.4.0-112-generic”, arch: “amd64”, family: “unix”

 

Step 6 : Clone the Repository and Create the API Jar File

We will create a folder inside a home folder.

Go into your home directory

cd /
mkdir nearbyshops
cd nearbyshops

Head over to the github and get the repository link for cloning the repo.

Now clone the repository by typing

git clone https://github.com/SumeetMoray/Nearby-Shops-API-New.git

CD inside the cloned repository folder. ( See below for commands)

When you type “ls” on your command line inside the cloned repository folder. You will see various files and folders.

root:/nearbyshops# ls
Nearby-Shops-API

root:/nearbyshops# cd Nearby-Shops-API

root:/nearbyshops/Nearby-Shops-API# ls
keystore.jks LICENSE pom.xml README.md src target nearbyShopsAPI.iml

When you are inside the “Nearby-Shops-API” directory which contains the pom.xml file. You can now type the following commands to generate the jar file

cd /nearbyshops/Nearby-Shops-API
mvn install

maven looks for a pom.xml file in order to generate the Jar file.

When you run the mvn install it is going to take at least 5 minutes because maven will download all the dependencies. So keep patience.

“mvn install” command downloads all the dependencies and generates the fat jar (uber jar). The fat jar is a jar which is self contained and does not need to download any repositories to run.

after all the process is complete you can find the jar file generated inside the “target” folder.

 

 

Step 7 : Move the Jar file and Configuration file into main folder

Head inside the target folder you will see two jar files. We need to copy the jar file (the one with higher size) into the “/nearbyshops” folder

To determine the size of files you can type the command “ls -lh” on the terminal to see which Jar file has higher size.

root:# mv /nearbyshops/Nearby-Shops-API/target/taxi-referral-api-1.0.jar /taxiReferralService/nearby-shops-api-1-feb-18.jar

the api will need a configuration file so we will also need the configuration file. So use the following command to copy the configuration file.

root:# mv /nearbyshops/Nearby-Shops-API/api_config_example.properties /nearbyshops/api_config.properties

 

Step 8 : Edit the configuration file by adding port number, postgres username and password and database name

In the step 3 we created new Database and added a new user with username and password. We will now have to edit the configuration file and provide the database name and username and password.

JDBC URL contains the Database Name. So we need to update JDBC URL with the database name of the database we have created. You can take a look at JDBC URL format here https://jdbc.postgresql.org/documentation/80/connect.html

jdbc:postgresql://host:port/database

If your host is localhost port is 5432 and database name is nearbyshopsdb then your jdbc url would be  “jdbc:postgresql://localhost:5432/nearbyshopsdb”

Type the following commands to edit the configuration file.

 

cd /nearbyshops
sudo nano api_config.properties

update the configuration file with the new JDBC URL and new Username and Password

 

 

Step 9 : Test the jar file by running it

After you have edited the configuration file you can now run the Jar file and see whether it runs successfully.

Note : Before running the Jar file ensure that you have a configuration file with correct name. The of configuration file should be “api_config.properties”

 cd /nearbyshops
 java -jar ./nearby-shops-api-1-feb-18.jar

 

Step 10 : Create systemd Unit script for auto restart the java process after being terminated

Sometimes the java process can terminate unexpectedly. Therefore we need a mechanism which automatically starts the process if it gets terminated and it will also start the process on Server Reboot / Restart.

To solve this issue we will use SystemD init system and write a small script which will tell SystemD to restart our Java process if it gets terminated unexpectedly.

 

Please read this article which will tell you how you can setup the auto restart mechanism for Java process using SystemD init system

http://nearbyshops.org/2017/02/27/auto-restart-java-process-termination-system-reboot-ubuntu-16-04-using-systemd/

 

Step 11 : Configure Domain Name for the API

You should assign a domain name to your API Server because when the api server is changed and its IP address changes we will not have to update the API URL at the client side. Updating the IP address for the domain will solve our problem.

Watch the following video which tells you how to configure a domain name on Digital Ocean Server.

The domain name for your rest API service is the “Service URL” of the service. Remember this URL and provide this URL whenever asked.

 

Step 12 : Customize the Android apps and update the Service URL to connect client apps with new self hosted nearby-shops-api

 

 

 

Step 13 : Log in as admin through the Service Provider App

Download the service provider android app from the downloads section of this website.

If you are downloading the service provider app from the direct download link and not from the google play store then you will need to change the security setting on your android device to unable installation from unknown sources.

After you change this setting go ahead and install the service provider app. Type the service URL for your service which you configured using the above step.

Now login to your app using these credentials

Username : username

Password : password

these are auto generated by the System. And needs to be used when accessing your system for the first time.

 

Step 14 : Create MSG91 API key to enable SMS and OTP 

 

Step 15 : Create One SIgnal api key to enable push notifications

 

Step 16 : Get started as an Admin of the Nearby Shops app

 


 

If you have any questions and queries feel free to ask in the comment. Have any difficulty in understanding anything let me know in the comments below ?

 

free open source software to help you start your own bussiness with almost zero investment

Most of the entrepreneurs are not aware that there are lot of free and open source software available in the market which migh help you start your own bussiness in almost zero investment. These software’s can help you save your costs and save your money which you may want to invest into building your own propritiary software. You can start your bussiness in almost zero investment.

WordPress

Website : https://wordpress.org

If you are starting your bussiness it is very much unlikely that you dont need a website. A website is a basic and essential need of any bussiness. Although wordpress is already a very much popular and doesnt need a mention but still i must mention this in order to bring more completeness to this list.

You can get your self-hosted wordpress website up and running in just around 5 $ per month using Digital Ocean Cloud hosting platform.

Discourse Forum Software

website : https://discourse.org

Building a community is one of the most essential requirements to promote and provide support for your customers. Discourse is a best and visually appealing forum software that you can find. Starting your own forum is one of the best ways to support your customers in the existing bussiness.

You can also use the forum software to start your own forum which can later take the forum of a bussiness. If your forum becomes popular it can help you start your bussiness using your forum.

You can get your discource forum up and running in just about 10 $ per month on digital ocean cloud hosting platform. Which is a very low investment to start your own forum and bussiness.

Nearby Shops e-commerce sofware

website : https://nearbyshops.org

Nearby Shops helps you build your own e-commerce service similar to Instacart or Amazon-now where multiple sellers can register and sell their goods. The software is available open source and completely free to use. It comes with android apps and has support for mobile apps also.

As a service provider you dont have to do lot of work. You only have to review the registrations from the shop owner and resolve the disputes if any. You can get your service up and running on Digital Cloud hosting platform for just 10 $ per month. This is almost zero investment to start your own bussiness.

free TLS / SSL Certificates from Letsencrypt.org

website : letsencrypt.org

Is a conventional setup you might be expected to buy SSL certificates for your online bussiness. But if will give you a great relief that letsencrypt.org gives you free SSL certificates and you dont have to pay anything to secure you web properties.

Tutorials already exist how you can use SSL certificates from Letsencrypt to secure your wordpress and discource forum.

Local e-commerce rising on the cards

Every idea has its own proper time when its eventually gets implemented and enter into people’s lives.

Before youtube was developed it didnt require a lot of common sense to understand that there is a need for a video sharing service in the market. But the video sharing platforms didnt really emerged untill we saw the rise in the internet bandwidth which grew upto the levels where video sharing service could sustain and operate.

This happend in around year 2005 when youtube was born. The first time in the history of human kind internet bandwidth was raised enough to support the sharing of videos.

We can see that there is a proper timing behind every concept and an idea to enter into the market.

Similarly when we think of local e-commerce it should not really need a lot of common sense to figure out that local e-commerce is required and it would simplify our lives. But what needs to be figured is when is the appropriate timing ? for local e-commerce to go mainstream into people’s lives ?  But before discussing that let us have a little discussion on the history of local e-commerce.

Even in the year 2000′ the attempt’s were made help people buy fruits and vegetables from local shops using a website. But it didnt succeeed. Because local e-commerce required features which were not present in Desktop computers.

Speaking further it would actually be quite an aweful experince to buy fruits and vegetables from your laptop and PC and for a reason which i cannot express into words the concept of buying fruits and vegetables from your desktop appears to be little inappropriate. May be because nobody carries laptops and PC’s with them all the time.

The local e-commerce appears to have certain special needs and some extra challanges to overcome compared to the conventional e-commerce. It needs to be location based. The shops / stores / sellers need to be sorted according the distance and should also get filtered out when the custormers goes outside the delivery range of the shop.

The emergence of Smartphones has somehow solved these problems. Because it is very trivial for smartphones to have GPS these days and it is getting more common for apps to use GPS and location.

More number of people have access to smartphones compared to the number of people who used the Desktops. This difference is substancial in the third world developing economies like India.

Therefore in a developing economy like India the local e-commerce would not be successful unless the smartphone comes to the rescue. The cost of buying a smartphone is substancially lower than buying a desktop. Therefore, people have adopted the smartphones in large numbers despite India largely being a country of poor people.

Smarphones have also helped bring the benefits of software and technology into the lives of even the most underpreviliged people.

There were few more factors. The cost of development and complexity of development. The e-commerce marketplace software can be quite a challange to develop and it would be a big IT project. But fortunately the field of software development has matured. The emergence of REST API’s and lots of good Quality libraries in the ANdroid ecocystem has made the development of SOftware very simple and easy.

These are some of the factors by which we can predict that we will see the rise local -ecommerce and just like adoption of services like Youtube the local e-commerce will become a commonplace in people’s lives.

The year 2005 was the year when the world was transitioning into adopting video sharing as something which is widespread and something which would become a part of our lives.

2017 would be the year where we might see local e-commerce rising really fast and growing rapidly into our economy and also entering into our lives.

Upgrade Postgres Database to 9.6 on Ubuntu 16.04

In this tutorial we will see a step by step guide for how to upgrade postgres database without losing data using the pg_upgrade tool provided with postgres. Although these instructions are specifically written for version 9.6 but you can customize it to work with any other version.

 

Step 1 : Install postgres 9.6

Install postgres 9.6 using these instructions

Link : https://tecadmin.net/install-postgresql-server-on-ubuntu/

 

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
$ wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

 

$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib

 

Step 2 : Upgrade cluster using pg_upgrade utility

Run pg_lsclusters your 9.5 and 9.6 main clusters should be “online”.

pg_lsclusters 
Ver Cluster Port Status Owner    Data directory               Log file
9.5 main    5432 online postgres /var/lib/postgresql/9.3/main /var/log/postgresql/postgresql-9.5-main.log
9.6 main    5433 online postgres /var/lib/postgresql/9.5/main /var/log/postgresql/postgresql-9.6-main.log

There already is a cluster “main” for 9.6 (since this is created by default on package installation). This is done so that a fresh installation works out of the box without the need to create a cluster first, but of course it clashes when you try to upgrade 9.5/main when 9.6/main also exists. The recommended procedure is to remove the 9.6 cluster with pg_dropcluster and then upgrade with pg_upgradecluster.

 

Stop the 9.6 cluster and drop it.

sudo pg_dropcluster 9.6 main --stop

Upgrade the 9.5 cluster to the latest version.

sudo pg_upgradecluster 9.5 main

when you type the above command you might get an error stating you “Unable to stop postgres ” or “Postgres server did not shut-down”

So we will need to stop the postgres server before we type the above command. To stop the Postgres server please type the following command.

sudo systemctl stop postgresql@9.5-main

OR (You can also type the following command as an alternative)

sudo pg_ctlcluster 9.5 main stop –force

after stopping the server please type the upgrade command again

sudo pg_upgradecluster 9.5 main

Your 9.5 cluster should now be “down”.

pg_lsclusters 
Ver Cluster Port Status Owner    Data directory               Log file
9.5 main    5433 down   postgres /var/lib/postgresql/9.5/main /var/log/postgresql/postgresql-9.5-main.log
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log

Check that the upgraded cluster works, then remove the 9.5 cluster.

sudo pg_dropcluster 9.5 main

The above instructions are taken from this link

Link : https://gist.github.com/SumeetMoray/c8a0343822b4a07c72b90daf08fb125b

 

 

Other Useful Links :

https://www.safaribooksonline.com/library/view/postgresql-9-administration/9781849510288/ch04s03.html

https://www.postgresql.org/docs/9.6/static/pgupgrade.html

Auto restart Java Process after termination or System Reboot on Ubuntu 16.04 using SystemD

There are cases when we are running a Jar Process on our server and the process terminates unexpectedly. This process contains our Rest API therefore such unexpected termination of our service can shutdown our API Server. This is very sad because now people can no longer access and use your app.

The solution to this problem is the setup a mechanism which monitors the running Java Process and restarts the process automatically when its terminated unexpectedly. We will also set another mechanism which starts a process automatically on server restart.

We can solve this problem using SystemD init system which is available in Ubuntu 16.04.

You can learn more about SystemD init system by reading these articles

https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files?utm_medium=btb_algo

https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples

 

We will have to write a small script which manages and tells SystemD how to manage process execution for your process and configure it to restart your process when terminated.

 

Step 1 : Navigate to /etc/systemd/system

SystemD service unit files are located at this directory so we will cd into /etc/systemd/system

cd /etc/systemd/system

 

Step 2 : Create a blank unit file

sudo touch nearbyshopsapi.service

Type this command in your terminal. It will create a new file with name “nearbyshopsapi.service”. This is a blank file now and it does not have anything inside it. So in the next step we will open this file and add some instructions inside it.

 

Step 3 : Add instructions in your unit file

The following is an example of Sample Unit file.

 


[Unit]

Description=nearbyshops_app: Nearbyshops Java API

After=syslog.target network.target

[Service]

WorkingDirectory=/nearbyshops
ExecStart=/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar     /nearbyshops/nearby-shops-api-1-feb-18.jar
Type=simple

Restart=always

[Install]

WantedBy=multi-user.target

 

Copy and paste this example and replace the directory with the directory containing your jar file and also the replace the name of the jar file with the name of your jar file.

 

Step 4 : Start the Service 

Type the following command which reloads all the unit file. You need to type this command whenever you make any changes in the unit files.

  • sudo sytemctl daemon-reload

Start the service using the following command

sudo systemctl start nearbyshopsapi.service

 

Step 5: Check whether the service is started

sudo systemctl status nearbyshopsapi.service

this command will show you the status of the service and whether it is running or failed to run.

Step 6 :  Enable the service to start automatically at Sytem Restart / Reboot

sudo systemctl enabled nearbyshopsapi.service

this command will enable the service and this will allow the service to restart automatically at system Boot or system start. This will start the service automatically when the system is rebooted or restarted.

you can check whether you have successfully enabled the service or not by typing the following command

sudo systemctl is-enabled nearbyshopsapi.service

 

Step 7 : Check whether the service starts at system Restart

Type the following command to restart your Ubuntu server

sudo shutdown -r now

After the server is restarted and running. You need to check the status of the service to determine whether it is running or not.

sudo systemctl status nearbyshopsapi.service

Type the above command to check the status of the service. The service should be running.

 

Step 8 : Check whether the service restarts at Unexpected Termination

sudo systemctl status nearbyshopsapi.service

The above command will give you the process number. We will use this number to kill this process and check to see if the process restarts automatically or not.

sudo kill -9 process_number

Use the above command to kill the process.

Now check the status of the service by typing the following command. Is the Service Still Running ? If the answer is yes . This verifies that our mechanism is working successfully and the SystemD is monitoring and restarting the process automatically when terminated.

sudo systemctl status nearbyshopsapi.service

 

You will need to study various SystemD and systemCtl commands to manage things in a better way. For this purpose see this reference which lists various SystemCtl commands to manage the process execution.

http://nearbyshops.org/systemd-linux-terminal-commands/

Nearby Shops open Source android app for grocery shopping

Nearby Shops an open source android app for grocery shopping is available on Github at this link.

Backend (Server Side)

Nearby Shops API github Link

https://github.com/SumeetMoray/Nearby-Shops-API

 

Front End (Client Side)

Nearby Shop End User Android app Github Link

https://github.com/SumeetMoray/Nearby-Shops-End-User-Android-app

Nearby Shops Distributor Android app Github Link

https://github.com/SumeetMoray/Nearby-Shops-Distributor-Android-app

Nearby Shops Service Provider app Github Link

https://github.com/SumeetMoray/Nearby-Shops-Service-Provider-Android-app

 

 

Screenshots

 

Shop Home

Shop and its Delivery Range

Free up port 80 on Linux Server

There could be a situation where port 80 on your server is already occupied by some other process and therefore you are not able to start a new process / application on port 80. In this situation you may want to close the process running on port 80.

For closing the process on port 80 we use the following commands

  • nmap localhost

    nmap is a utlity which lists all the opened ports and the process running on those ports. Using the above command would let you know which ports are occupied on your machine.

 

You can kill the process running on the port 80 by using the following commands

fuser -k <port>/tcp

In the place of port put the value 80 to close the port 80 on the machine.

 

Links and References

  1. http://www.webupd8.org/2009/07/how-to-close-port-in-linux.html

Nearby Shops Source Code is Now Released to the Public !

We have just released Source Code for the Nearby Shops Software to the Public.

The Source Code is Available at the following Github Repositories.

https://github.com/SumeetMoray/Nearby-Shops-API

 

Nearby Shops API Installation Guide for Digital Ocean

In this article we will learn how we can install Nearby Shops API on ubuntu 16.04 server using Digital Ocean Cloud Hosting Service.

 

A word of Caution for Developers

You can use the current setup for testing and review purpose or just for playing with it or experimenting with it but before you decide to use this software in Production Environment please note that right now it lacks some of the very essential features and security. And when you use this software in Production you should understand that you bear the risks associated. because you have been warned !

The following essential features have not been implemented althought we are in the process of implementing them soon.

TLS / SSL security, e-mail integration, Google and facebook sign-in are some of the features which is currently absent but we are going to implement these features soon. Pull requests which add these features into the API are welcome.

Currently the API Authenticates using Basic Auth but i believe we will soon upgrade it and replace it with a token based authentication System.

 

Enough of the Introduction and Let us now begin with the step by step installation Guide for Nearby Shops API Backend

Step 1 : Create an API Jar File from the source code

We need to compile the source code and create a Jar file for the REST API. Let us see how do we do that.

Download the API source code from the github and create the Uber Jar file using maven.  Maven is a popular build tool used in Java Ecosystem. If you dont have maven installed on your machine you can install it by using  “sudo apt-get install maven”  . Before typing the following commands on your terminal make sure you have git and maven both installed on your machine.

cd /
mkdir nearbyShopsAPI
cd /nearbyShopsAPI

git clone https://github.com/SumeetMoray/Nearby-Shops-API.git
mvn install

“mvn install” would download all the dependencies and create an Uber Jar (a jar file which contains all of its dependencies). After this command executes successfully. You will see two jar files in the target folder which lie inside the project folder. THe jar file with higher  size  will be the Uber Jar. Its size will be around 15 MB approx.

You will move this Uber Jar file the server using scp command.

Pro tip : 

If you want to customize port and Postgres DB username and password. You can do it before you generate a Jar file by editing the “Main.java” and “JDBCContract.java” which resides inside the Source Folder.

 

A word of caution for using NGINX server

You might consider using the NGINX server. But this approach has some pitfalls so if you are considering using the NGINX server please understand that it has some issues. The push notifications utilizes SSE (Server Sent Events) mechanism and they stop working when we use NGINX proxy.

Therefore unless you are willing to bear the disabling of push notifications on android app. Dont consider using the NGINX server.

If you find any solution to keep the push notifications enabled while using the NGINX Server please let us know and share your solution with us.

 

Step 2 : Create Server or Droplet

Digital Ocean offers a intuitive and easy to understand user-interface for creating a server. Please create a server with a following configuration.

Ubuntu 16.04 with minimum 512 MB ram or above !

You can also watch the following video which contains instructions for creating and initial setup of a Server

 

Step 3 : Login to the server using SSH and Enable it

The tutorial for how to setup your server is given at the following links. Please go through these links and setup your server.

https://www.digitalocean.com/community/tutorials/how-to-connect-to-your-droplet-with-ssh

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04

 

More instructions are given in this video

 

 

Step 4 : Install OpenJDK 8

Nearby shops is a developed using java and its a java application. So you will need to install JDK in order to run it. But our personal recommendation is to install OpenJDK 8 and prefer to use Open JDK over Oracle JDK.

On this date we are hearing lot of licensing issues and cases where oracle is charging and suing people for using Java. Therefore its always safe to use OpenJDK over Java.

You can install open-jdk 8 by using following commands.

sudo apt-get udpate
sudo apt-get install openjdk-8-jdk

Link : http://askubuntu.com/questions/464755/how-to-install-openjdk-8-on-14-04-lts

Step 5 : Install PostgresDB

You can install postgresDB’s latest version by typing the following commands in the terminal. For more detailed information on how to install postgresql please refer this tutorial.

Link : https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

Step 6 : Add User to PostgresDB

Installing the PostgresDB is not enough to get access to the PostgresDB. We also need to create a user with username and password. In order to do that please type the following commands in your terminal.

We have to create a user with username ‘postgres’ and password ‘password’ because app uses this setting to connect with the postgresDB.

In future we are planning to have a configuration file where you can set your own username and password. But for now please use the following settings.

 

sudo -u postgres psql
ALTER USER postgres PASSWORD 'password';

// do not forget the semicolon at the end of ALTER USER Statement
press ctrl + D to come out

 

Step 7 : Copy NearbyShops API jar file to the Folder on the Server

Download the latest API Jar file available on the downloads page.

http://nearbyshops.org/downloads/

We now have to copy the Jar file to the server. The jar file is an uber Jar – a jar which contains all of its dependencies.

In order to copy the file first we should create a folder with name of your choice. All of our files will be copied in this folder.


mkdir /nearbyshops
cd /nearbyshops
scp -r /path/to/jar/filename.jar root@0.0.0.0:/nearbyshops

Note : Please note that you need to replace 0.0.0.0 with 
the IP address of your own server. 


Reference : Use the following command to copy the files
scp -r /path/to/my/files root@0.0.0.0:/path/on/remote/droplet

 

For Example:

Consider the name of the Jar file is “nbs_api.jar” you can now run this jar file to ensure that everything is working fine.

Type “Java -jar nbs_api.jar” in the remote terminal in order to execute the jar file and get the api up and running. You would start seeing the api log. Please read this log, you should see “HTTP Server created” or something similar to that.

You can test whether your api is successfully running or not by typing

“http://[Server-IP]:[Server-Port]/api/ServiceConfiguration”

on your browser. Please replace server-ip and server-port with IP of your server and port of your server. This is a link to the default server api configuration. You should now see a JSON ouput something similar to the following. If you see this it is an indication that your API is connected to Postgres Successfully and started running.

{
“serviceID”: 1,
“serviceName”: “DEFAULT_CONFIGURATION”,
“pincode”: 0,
“serviceType”: 1,
“serviceLevel”: 1,
“latCenter”: 0,
“lonCenter”: 0,
“serviceRange”: 0,
“created”: “2017-02-12T11:13:24+0530”,
“updated”: “2017-02-12T11:13:24+0530”,
“rt_distance”: 0
}

 

Step 8 : Create systemd Unit script for auto restart the java process after being terminated

Sometimes the java process can terminate unexpectedly. Therefore we need a mechanism which automatically starts the process if it gets terminated and it will also start the process on Server Reboot / Restart.

To solve this issue we will use SystemD init system and write a small script which will tell SystemD to restart our Java process if it gets terminated unexpectedly.

 

Please read this article which will tell you how you can setup the auto restart mechanism for Java process using SystemD init system

http://nearbyshops.org/2017/02/27/auto-restart-java-process-termination-system-reboot-ubuntu-16-04-using-systemd/

 

Step 9 : Configure Domain Name

You should assign a domain name to your API because the Server can be change and in such a situation IP of your service will change. This is not acceptable becasue client apps will not be able to find your server then.

Watch the following video which tells you how to configure a domain name on Digital Ocean Server.

The domain name for your rest API service is the “Service URL” of the service. Remember this URL and give this URL whenever asked.

 

Step 10 : Log in as admin through the Service Provider App

Download the service provider android app from the downloads section of this website.

If you are downloading the service provider app from the direct download link and not from the google play store then you will need to change the security setting on your android device to unable installation from unknown sources.

After you change this setting go ahead and install the service provider app. Type the service URL for your service which you configured using the above step.

Now login to your app using these credentials

Username : username

Password : password

these are auto generated by the System. And needs to be used when accessing your system for the first time.

 

Step 11 : Add a service Configuration

After you have logged in as an Administrator. Go to the Service Configuration. and fill up all the details to update the service configuration. This configuration will be used by “Service Discovery Service” to access your service and define the visibility of your service by its location.

 

Step 12 : After you have updated the Service Configuration. Submit the Service to the Service Discovery Service

After you have updated your service Configuration. You should submit your service URL to the “Service Discovery Service”.

“Service Discovery Service” is like a location based directory of services. It shows you the services which are available in the given location.

SDS is used by End-User app and Distributor app to find and configure the service.

 


 

If you have any questions and queries feel free to ask in the comment. Have any difficulty in understanding anything let me know in the comments below ?

 

Enable remote access to Postgres Database

Step  1 :  Modify postgresql.conf

Change the address PostgreSQL listens to

Using Nano (or vim/emacs/whatever), edit your postgresql.conf file. We’re specifically looking for a line that says listen_addresses. We’re going to modify where PostgreSQL is listening on.

Uncomment the following Line in the file postgresql.conf located at the /etc/postgres/9.5/main

File Location : /etc/postgresql/9.5/main/postgresql.conf

Please replace 9.5 with the version number of the Postgres you are using

# /etc/postgresql/9.5/main/postgresql.conf

listen_addresses='*'

 

Step 2 : Modify pg_hba.conf

Allow remote connections to actually reach your PostgreSQL instance

Next we’re going to edit our pg_hba.conf file and configure it to allow remote connections. At the very end of the file, add the following line:

# /etc/postgresql/9.5/main/pg_hba.conf

host all all  0.0.0.0/0 md5

 

This is a rule that basically tells PostgreSQL to allow anyone to access the instance as long as they have proper credentials.

If you want to whitelist your IP only you can use your network/mask instead of 0.0.0.0/0.

 

Step 3 : Restart your PostgreSQL instance.

That’s it, you’re done. Restart it.

sudo service postgresql restart

Now you can use a tool like PgAdmin or RubyMine to access your remote database.

 

Links and References

Link : https://www.digitalocean.com/community/questions/remote-connect-to-postgresql-with-pgadmin

Link : https://coderwall.com/p/cr2a1a/allowing-remote-connections-to-your-postgresql-vps-installation

Link : https://bosnadev.com/2015/12/15/allow-remote-connections-postgresql-database-server/