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”
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
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.
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
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
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
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 ?