How to install in ubuntu
Hosting our Sparrow API on Ubuntu, This guide provides step-by-step instructions for self-hosting our Sparrow API on an Ubuntu server.
Before you begin
Prerequisites
- Ubuntu server version (22.04.2)
- Sparrow API codebase
- Node.js
- MongoDB
- Kafka
Install MongoDB Community Edition
From a terminal, install gnupg
and curl
if they are not already available:
sudo apt-get install gnupg curl
To import the MongoDB public GPG key, run the f`ollowing command:
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
Create a list file for MongoDB
Create the list file /etc/apt/sources.list.d/mongodb-org-7.0.list
for version Ubuntu version.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Reload local package database
Issue the following command to reload the local package database:
sudo apt-get update
Install the MongoDB packages
Install the latest stable version of MongoDB.
sudo apt-get install -y mongodb-org
Start MongoDB
Start the mongod
process by issuing the following command:
sudo systemctl start mongod
If you receive an error similar to the following when starting mongod
:
Failed to start mongod.service: Unit mongod.service not found.
Run the following command first:
sudo systemctl daemon-reload
Then run the start command above again.
Verify that MongoDB has started successfully.
sudo systemctl status mongod
Optionally ensure that MongoDB will start following a system reboot by issuing the following command:
sudo systemctl enable mongod
Stop MongoDB
As needed, stop the mongod process by issuing the following command:
sudo systemctl stop mongod
Restart MongoDB
Restart the mongod
process by issuing the following command:
sudo systemctl restart mongod
Follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log
file.
Install Kafka
Since Apache Kafka is written in Java, Installation of Java is a prerequisite. So, log in to server and refresh the local package index.
sudo apt update
Next, install OpenJDK which is a free and open-source implementation of the Java Standard Edition Platform. Here, we are installing OpenJDK 11 which is an LTS release.
sudo apt install openjdk-11-jdk -y
Once installed, verify the version of Java as shown.
java -version
Install Apache Kafka
To download it, use the wget
command-line utility.
wget https://downloads.apache.org/kafka/3.5.1/kafka_2.13-3.5.1.tgz
Next, extract the tarball file using the tar
command-line tool.
tar xvf kafka_2.13-3.5.1.tgz
Once extracted, a folder called kafka_2.12-3.5.0 is created. Next, move this folder to the /usr/local directory and rename it kafka.
sudo mv kafka_2.13-3.5.1 /usr/local/kafka
Create Kafka and ZooKeeper Systemd Unit files
In this step, we will create systemd unit files for Kafka and ZooKeeper services. This will allow to easily manage the services using the systemctl command.
Let’s start by creating the Zookeeper systemd file using the nano editor as shown.
$ sudo nano /etc/systemd/system/zookeeper.service
Paste the following lines of code which define Zookeeper’s systemd service.
[Unit]
Description=Apache Zookeeper server
Documentation=http://zookeeper.apache.org
Requires=network.target remote-fs.target
After=network.target remote-fs.target
[Service]
Type=simple
ExecStart=/usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties
ExecStop=/usr/local/kafka/bin/zookeeper-server-stop.sh
Restart=on-abnormal
[Install]
WantedBy=multi-user.target
Save the changes and exit.
Next, create Kafka’s systemd file
$ sudo nano /etc/systemd/system/kafka.service
Paste the following lines of code which define Kafka’s systemd service.
[Unit]
Description=Apache Kafka Server
Documentation=http://kafka.apache.org/documentation.html
Requires=zookeeper.service
[Service]
Type=simple
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
ExecStart=/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties
ExecStop=/usr/local/kafka/bin/kafka-server-stop.sh
[Install]
WantedBy=multi-user.target
Be sure to save the changes and exit
sudo systemctl start zookeeper
sudo systemctl start kafka
Confirm if the services are running. For Zookeeper, run:
sudo systemctl status zookeeper
For Apache Kafka service , execute:
$ sudo systemctl status kafka
Install Node.js
Ensure that Node.js is installed on Ubuntu server. You can do this by running the following commands:
sudo apt update
sudo apt install nodejs
Install npm & pnpm
npm (Node Package Manager) should be installed along with Node.js. Verify its installation by running:
npm -v
pnpm install
Set Up Environment Variables
Application requires environment variables for MongoDB and Kafka connection URLs. Create a .env
file in your project directory and add the necessary variables:
MONGODB_URI=mongodb://username:password@mongodb_host:27017/database_name
KAFKA_URL=kafka://kafka_host:9092
Make sure to replace username
, password
, mongodb_host
, database_name
, and kafka_host
with your actual MongoDB and Kafka connection details.
Start Your Sparrow API
pnpm start:dev
Accessing Sparrow API swagger
http://localhost/api/docs