Sign In
Sign In

Cloud Service Provider
for Developers and Teams

We make it simple to get started in the cloud and scale up as you grow —
whether you have one virtual machine or ten thousand
By signing up you agree to the Terms of Service and Privacy Policy
99.9% Uptime
Our cloud service provides the ultimate in server dependability and stability
Money-back Guarantee
Experience our high-speed cloud services without any risk, assured by our money-back guarantee
Easy to Deploy
Manage your services with ease using our intuitive control panel, where deploying software is a matter of minutes
Reliable and Available
Select from 6 datacenter regions around the world based on latency or deploy across regions for redundancy

Robust cloud services for every demand

See all Products

Cloud Servers

Cutting-edge hardware for cloud solutions: powerful Intel and AMD processors, ultra-fast NVMe disks

Databases

We provide a cloud database ready to store everything you have. The best DBMSs are on deck: MySQL, Redis, Kafka, and more

App Platform

Just link your repo, pick a project to deploy, and Hostman will have it running in the cloud with just a couple of clicks from the dashboard

S3 Storage

A universal object storage compatible with the S3 protocol

Firewall

Multi-layered protection from vulnerability scanning, DDoS, and cyber-attacks

Kubernetes

Automate the management of containerized applications, from deployment and scaling to monitoring and error handling

Managed Backups

Our server and application backup feature allows for both on-demand and scheduled backup and one-click data restoration

Images

Create images for backup free of charge or deploy your own in the Hostman cloud

Hostman's commitment to simplicity
and budget-friendly solutions

1 CPU
2 CPU
4 CPU
8 CPU
Configuration
1 CPU, 1 GB RAM, 25 GB SSD
Hostman
DigitalOcean
Google Cloud
AWS
Vultr
Price
$4
$6
$6.88
$7.59
$5
Tech support
Free
$24/mo
$29/mo + 3% of
monthly charges
$29/mo or 3% of
monthly charges
Free
Backups
from $0.07/GB
20% or 30% higher
base daily/weekly fee
$0.03/GB per mo
$0.05/GB per mo
20% higher base
monthly/hourly fee
Bandwidth
Free
$0.01 per GB
$0.01 per GB
$0.09/GB first
10 TB / mo
$0.01 per GB
Live chat support
Avg. support response time
<15 min
<24 hours
<4 hours
<12 hours
<12 hours
Anup k.
Associate Cloud Engineer
5.0 out of 5

"Hostman Comprehensive Review of Simplicity and Potential"

It been few years that I have been working on Cloud and most of the cloud service...
Mansur H.
Security Researcher
5.0 out of 5

"A perfect fit for everything cloud services!"

Hostman's seemless integration, user-friendly interface and its robust features (backups, etc) makes it much easier...
Adedeji E.
DevOps Engineer
5.0 out of 5

"Superb User Experience"

For me, Hostman is exceptional because of it's flexibility and user-friendliness. The platform's ability to offer dedicated computing resources acr...
Yudhistira H.
Mid-Market(51-1000 emp.)
5.0 out of 5

"Streamlined Cloud Excellence!"

What I like best about Hostman is their exceptional speed of deployment, scalability, and robust security features. Their...
Mohammad Waqas S.
Biotechnologist and programmer
5.0 out of 5

"Seamless and easy to use Hosting Solution for Web Applications"

From the moment I signed up, the process has been seamless and straightforward...
Mohana R.
Senior Software Engineer
5.0 out of 5

"Availing Different DB Engine Services Provided by Hostman is Convenient for my Organization usecases"

Hostman manages the cloud operations...
Faizan A.
5.0 out of 5

"Hostman is a great fit for me"

Hostman is a great fit for me. What do you like best about Hostman? It was very easy to deploy my application and create database, I didn't have
Adam M.
5.0 out of 5

"Perfect website"

This website is extremely user friendly and easy to use. I had no problems so didn't have to contact customer support. Really good website and would recommend to others.
Anup K.
4.0 out of 5

"Simplifying Cloud Deployment with Strengths and Areas for Growth"

What I like best about Hostman is its unwavering commitment to simplicity...
Naila J.
5.0 out of 5

"Streamlined Deployment with Room for Improvement"

Hostman impresses with its user-friendly interface and seamless deployment process, simplifying web application hosting...

Trusted by 500+ companies and developers worldwide

Deploy a cloud server
in just a few clicks

Set up your сloud servers at Hostman swiftly and without any fees, customizing them for your business with a quick selection of region, IP range, and details—ensuring seamless integration and data flow

Code locally, launch worldwide

Our servers, certified with ISO/IEC 27001, are located in Tier 3 data
centers across the US, Europe, and Asia
🇺🇸 San Francisco
🇺🇸 San Jose
🇺🇸 Texas
🇺🇸 New York
🇳🇱 Amsterdam
🇳🇬 Lagos
🇩🇪 Frankfurt
🇵🇱 Gdansk
🇦🇪 Dubai
🇸🇬 Singapore

Latest News

SQL

Indexes in SQL: Creation, Types, and How They Work

When working with SQL (Structured Query Language), you may encounter an object called an "index." In this article, we'll discuss indexes and practical examples of working with them. An index is an object created for one or more columns in a database table to enhance performance, specifically to speed up searching and retrieving the necessary data from the database. To understand what an index is, consider a real-life analogy — a book. A book has a table of contents or an index, which helps us quickly find the section we need. Without an index, we'd have to search for the section manually, which would take much longer. Database indexing works similarly by enabling fast data retrieval. How Indexes Work An index in SQL is a structure built for table columns and view objects. It consists of keys constructed from one or more columns in a table. The keys are stored in a balanced tree structure (a tree-like structure designed for quick data access). This structure starts from a root node at the top of the hierarchy and continues to leaf nodes at the bottom. The key advantage of indexes is the speed of data lookup, achieved because the index is built from a structure (a balanced tree) optimized for searching. It's also important to note that whenever new data is added, or old data is removed, the tree structure of the index is recalculated. This means the more data and indexes stored in a database, the more trees need to be recalculated. For example, if a table has five indexes and 10,000 records, each new entry will trigger the recalculation of all five indexes. Types of Indexes There are several types of indexes: Unique Index: Ensures all values are unique. Adding a duplicate value will cause an error. Non-Unique Index: Allows duplicate values. Simple Index:Built on a single column. Composite Index: Built on multiple columns, and the column order matters. B-tree Index: Represented by a root node and leaf nodes. Partial Index: Created from a subset of table rows based on a specific condition. Clustered and Non-Clustered Indexes There are also clustered and non-clustered indexes. Let's examine these more closely using PostgreSQL 15 as an example. Clustered Index A clustered index in SQL sorts the data rows in a table and stores the data within the index's leaves. The key feature of a clustered index is that all the values are sorted in a specific order, either ascending or descending. Data in a table is physically sorted only if the table has a clustered index. With a clustered index, data is physically organized on a disk, speeding up data retrieval when accessed sequentially. However, reorganizing the data can be costly and may require rebuilding. Unlike other indexes, a clustered index is created for the entire table, not just one or more columns. A table can have only one clustered index. Non-Clustered Index A non-clustered index is applied to non-key columns and does not physically order the data. Instead, it stores the index separately from the actual data. The leaves of a non-clustered index contain only the columns included in the index. This means additional operations are required to retrieve the necessary data. Non-clustered indexes can't be sorted like clustered ones, but multiple non-clustered indexes can be created for a table. You can also add unique constraints to a non-clustered index. Comparison of Clustered and Non-Clustered Indexes Clustered Index Non-Clustered Index Sorts and physically stores data according to the sort rule Does not physically sort the data; uses pointers to access data Data is stored in the leaf nodes of the index Does not store data in the leaf nodes Occupies more disk space Occupies less disk space Faster access to data Slower access to data No additional disk space needed Requires additional disk space for index storage Increases performance for data retrieval Applied only to columns used in joins or queries Creating Clustered and Non-Clustered Indexes Here are examples using PostgreSQL 15. Let’s create a table named movies that contains information about films. The table includes two columns: id (the unique identifier for each film) and title (the film's name). The SQL code to create this table is as follows: CREATE TABLE movies (    id SERIAL PRIMARY KEY,    title TEXT NOT NULL); To create an index for the id column, use: CREATE INDEX cluster_id ON movies (id); You can then create a clustered index using the following command: CLUSTER movies USING cluster_id; This clustered index speeds up data retrieval. To create a non-clustered index for the title column: CREATE INDEX non_clustered_index ON movies (title); Database Structure Example Consider a table orders that stores information about orders in an online store. We'll create this table with several necessary columns: CREATE TABLE orders ( order_id INT PRIMARY KEY, client_id INT, client_name VARCHAR(100) NOT NULL, client_address VARCHAR(255) NOT NULL, client_city VARCHAR(100) NOT NULL, client_country VARCHAR(100) NOT NULL, client_ip_address inet ); Creating SQL Indexes To create an index, use the CREATE INDEX command. The general syntax is: CREATE INDEX <index_name> ON <table_name> (<column1>, <column2>); For example, to create an index for the order_id column in the orders table: CREATE INDEX index_for_order ON orders (order_id); You can create indexes for multiple columns: CREATE INDEX index_for_order ON orders (order_id, client_id); To create a unique index: CREATE UNIQUE INDEX index_for_order ON orders (order_id); To create a partial index: CREATE INDEX clients_ip ON orders (client_ip_address)WHERE NOT (client_ip_address > '10.26.74.1' AND client_ip_address < '10.26.74.2'); Deleting and Modifying Indexes You can delete indexes with the DROP INDEX command: DROP INDEX index_for_order; To rename an index: ALTER INDEX index_for_order RENAME TO new_index_for_order; Best Practices for Indexes While indexes are useful, there are rules to follow for their effective use: Avoid indexes in small tables. Avoid indexes in tables with frequent data changes (inserts/updates). Avoid indexes on columns that will undergo complex data selection queries. Avoid indexes on columns that frequently contain NULL values. Use indexes on columns that are frequently searched. Conclusion In this article, we explored what an SQL index is and how it is used in databases. Proper use of indexing in SQL can significantly improve the performance of queries in your database.
12 September 2024 · 6 min to read
Linux

How to Switch Users in Linux

Computers store vast amounts of information, ranging from documents and photos to work projects and sensitive data. Now, imagine several people using the same computer. Each of them needs their own space, settings, and files. This raises the question: Is it possible to quickly switch users in Linux so everyone can work with their own data without disturbing others? Linux distributions offer the ability to change users instantly. In this guide, we'll look at several ways to do this, ensuring that all users on your system have their own workspace. Switching Users in Linux Switching user accounts in Linux can be useful at any time. This is not only important for system security (access control) but also for improving work efficiency. Below are some scenarios where you might need to switch users in Linux: Shared system use: When multiple users share one computer, switching users allows each person to maintain their personal settings, files, and workspace, ensuring a personalized and secure experience. Development and testing: Software developers may use account switching to test their applications in different environments and conditions. Granting temporary access: If guests or temporary workers need access to the system, switching users provides a convenient and secure way to use the system without creating permanent accounts. System administration: For system administrators managing and maintaining the system, switching accounts provides access to different user environments, which is essential for various administrative tasks. In this guide, we'll cover three ways to switch users in Linux. The first two methods use a graphical user interface. The third option describes an alternative method using the terminal, which can be particularly useful for those working on a cloud server or who prefer command-line tools. This way, you can choose the option that best suits your preferences and needs. Option 1: Switching Users at System Startup Linux distributions, by default, allow switching users at system startup. If automatic login is enabled, you need to disable it in the settings for the specific user (for Ubuntu: "Settings" → "Users"). Start your Linux machine and wait for it to load. A screen will appear showing a list of available user accounts. To select the account, click on it. Enter the password and press "Enter". The system will begin to load. If you don't see the user you need in the list, use the "Not listed?" button. In the new window, enter the username you want to log in as and press "Enter". Then, enter the password for the account. If everything is correct, the system will start loading. If a person already has an active session and doesn't want to restart the system to switch users, there's another option that allows switching without shutting down or rebooting the machine. Option 2: The "Switch User" Option This method involves using the "Switch User" button, which opens a window for selecting a user account without restarting the system. This option is typically available in the user menu in the top-right corner of the screen. If you don't find it, consult the official documentation for your Linux distribution. To switch users in Linux using this option, click on the area in the top-right corner of the screen, as shown below. In the drop-down menu, select "Power Off / Log Out," and then click "Switch User…". After following the steps, a list of available users will appear. All subsequent actions are the same as those described in the previous section. After covering the graphical interface methods, let's move on to the third option, which may interest those who prefer working through the terminal. This method is also suitable for those managing a server with only a terminal interface. Option 3: Using a Terminal Command If you're working on a Linux server or prefer using the terminal, this section of the guide is for you. Here, we'll discuss the Linux su command and explain how to use it step by step. The syntax of the command is shown below: su [options] [username] Open the terminal on your Linux system. You can use hotkeys (for example, Ctrl + Alt + T) or find the terminal in the applications menu. If you're working on a server, use the available connection tools, such as SSH. To switch to another user in Linux, type the following into the terminal: su - username The - option starts a new shell for the user, similar to how it would look if the user logged in from the start. After running the command, the system will ask for the account password. Enter it and press "Enter." Note that password characters won't appear on the screen as you type. If you need to log in as the superuser, run su without parameters: su Similarly, enter the superuser password. When you're done working as the superuser or another account, type: exit This command will terminate the session and return you to the previous user. Conclusion In this guide, we covered three different ways to switch users in Linux, giving you the flexibility to choose based on your preferences and use case. Switching between accounts efficiently is important for ensuring security, privacy, and convenience when using a Linux system.
11 September 2024 · 5 min to read
MySQL

How to Calculate Age From Date of Birth Using TIMESTAMPDIFF in MySQL

Calculating age from a date of birth (DOB) is a common requirement in many applications. In MySQL, this can be efficiently achieved using the TIMESTAMPDIFF function. This tutorial will guide you through the process of calculating age using TIMESTAMPDIFF, handling edge cases, and integrating the query into applications. Prerequisites Before diving into the tutorial, ensure you have: A MySQL database set up and accessible. Basic knowledge of SQL queries and MySQL functions. A table with a date of birth column to work with. Overview of TIMESTAMPDIFF Function The TIMESTAMPDIFF function calculates the difference between two dates based on the specified unit of time (e.g., years, months, days). For calculating age, you will use TIMESTAMPDIFF to find the difference in years between the current date and the date of birth. TIMESTAMPDIFF(unit, datetime1, datetime2) unit: The unit of time for the result (e.g., YEAR, MONTH, DAY). datetime1: The first date (usually the date of birth). datetime2: The second date (usually the current date). Writing the Basic TIMESTAMPDIFF Query To calculate age from a date of birth, use the following query: SELECT TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS ageFROM users; In this query: YEAR specifies that the result should be in years. date_of_birth is the column containing the date of birth. CURDATE() returns the current date. Handling Edge Cases When calculating age, consider the following edge cases: Leap Years Leap years do not significantly affect age calculations, as TIMESTAMPDIFF accurately accounts for these in its calculations. Birthdays on February 29 For individuals born on February 29, TIMESTAMPDIFF will handle their age calculation correctly, but be aware of potential issues if you use functions that do not recognize leap years. Different Date Formats Ensure that the date format stored in the database matches MySQL's date format (YYYY-MM-DD). If you encounter format issues, use the STR_TO_DATE function to convert strings to date formats. Practical Examples and Use Cases Here are some practical examples of using TIMESTAMPDIFF: Example 1: Calculate Age for a Specific User SELECT TIMESTAMPDIFF(YEAR, '1990-05-15', CURDATE()) AS age; This query calculates the age of someone born on May 15, 1990. Example 2: Age Calculation for All Users SELECT name, TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS ageFROM users; This query retrieves names and ages of all users from the users table. Integrating the Query in Applications To integrate this query into an application: In a PHP Application: $query = "SELECT TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age FROM users";$result = mysqli_query($conn, $query); In a Python Application:  query = "SELECT TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age FROM users"  cursor.execute(query) Ensure that your application handles database connections securely and efficiently. Performance Considerations The TIMESTAMPDIFF function is optimized for performance, but be mindful of the following: Indexes: Ensure that the date_of_birth column is indexed to speed up queries. Query Optimization: For large datasets, consider optimizing queries to improve performance. Troubleshooting Common Issues Here are some common issues and their solutions: Incorrect Results Issue: Age calculation is incorrect. Solution: Ensure that dates are correctly formatted and the date_of_birth column contains valid date values. Query Errors Issue: Syntax or execution errors. Solution: Verify that the SQL syntax is correct and that you are using valid MySQL functions. Conclusion Calculating age from a date of birth using TIMESTAMPDIFF in MySQL is straightforward and efficient. By following the steps outlined in this tutorial, you can accurately determine age and handle various edge cases. Integrate these calculations into your applications and optimize performance for the best results.
10 September 2024 · 4 min to read
Node.js

How to Install Node.js and NPM on Ubuntu 24.04

The popular JavaScript runtime Node.js enables server-side programming with JavaScript. NPM, a package manager for Node.js projects, helps with dependency management. This guide will show how to install NPM and Node.js on Ubuntu 24.04. Prerequisites System running in Ubuntu 24.04 Root access or user with sudo privileges Installing Node.js and npm from the Default Ubuntu Repository Update the package lists to ensure to have the most recent information on package versions and dependencies. Run the command below:  sudo apt update && sudo apt upgrade -y Node.js is normally available from Ubuntu's default repository. Install it by running the following command: sudo apt install nodejs npm  -y Installing Node.js and npm via the NodeSource Repository Add the NodeSource repository for Node.js:  curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -  Replace setup_20.x with the desired version. Different version can be found on nodesource.com. Use the following command to install Node.js after adding the NodeSource repository: sudo apt install nodejs -y Verifying the Node.js and npm Installation Verify the following versions of Node.js and npm to make sure they were installed correctly. Run the below command. node -v npm version Installing Specific Node.js Versions with NVM  With the help of the robust utility Node Version Manager (NVM), devops may easily manage several Node.js versions on a single machine. This is very helpful when switching between several project needs. To install NVM, download and run the installation script from the NVM repository using the following command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash After running the scripts, source the user profile and add NVM to the shell session. Add the following lines to the user's home directory (~/.bashrc, ~/.zshrc, or the corresponding shell profile script). Create it using nano editor: nano ~/.bashrc 3. Add the following content: export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" Run the command below so changes will take effect. source ~/.bashrc With NVM installed, install the specific versions of Node.js. In this case, to install Node.js version 16, run the command below: nvm install 16 Switch to a specific version of Node.js that is installed, using the command below. nvm use 16 Managing Node.js Projects Several essential procedures and best practices are involved in managing Node.js projects in order to ensure the effectiveness, maintainability, and scalability of the application. This is a tutorial to help to efficiently manage your Node.js projects. Launch the terminal, navigate to the project creation path, and make a folder named after the project you are creating. mkdir my_project Initiate the Node project by running the command npm init. Provide the required details (marked in red). All of the answers in this example will be default. The file package.json will result from this. npm init Install any required dependencies, such as nodemon and express. The package-lock.json file and the node_modules folder will be created as a result. npm i express nodemon To initialize git in the project, use the git init command. This will include the file .gitignore. git init Make a file called Readme.md that will have all of the project's information. touch Readme.md Make a file with the .env extension that will hold the project's credentials and sensitive data. touch process.env To launch the program, create a file with the name app.js or index.js. touch app.js Make two folders: Public (which contains resources and static files) and src (which contains controllers, models, routes, and views). mkdir Public src Check each and every folder and file that was generated. This is how a typical structure might look like. For the NODE JS application, it is best practice to establish a project structure, divide files based on their function, and place those files in the appropriate directories. To make it simple to confirm the existence and logic of any given file or folder, unify the application's naming conventions and include business logic in the controllers folder, for instance. ls -lrt Best Practices for Node JS Project Structure For production systems, set up logging and monitoring with tools like Datadog or New Relic. Plan routine maintenance activities including performance reviews, security audits, and dependency updates. Put in place a backup plan for important configurations and data. Check for security flaws in your dependencies and code on a regular basis. Troubleshooting Common Issues There are some frequent problems that a user could run into when installing npm and Node.js. These troubleshooting instructions should help you to address the majority of typical problems that arise when installing npm and Node.js. The steps for troubleshooting these issues are listed below: When attempting to install Node.js or npm globally (i.e., using sudo), users get permission-related issues that prevent them from finishing the installation process. After installing nvm, the command is not recognized. The error nvm Command Not Found will be encountered. Make sure that the shell's configuration file (.bashrc, .bash_profile, .zshrc, etc.) has nvm sourced, and then the command source ~/.bashrc has been use to reload it. The npm version is out of date or does not correspond with the Node.js version after installing Node.js. Use nvm install <version> to install a particular Node.js version, which will include the matching npm version, and manually update npm by running npm install -g npm.  Conclusion In conclusion, an important initial step in creating new web applications and utilizing server-side JavaScript is installing Node.js and npm. Although installing software is usually simple, there are a few frequent problems that can arise, such as permissions conflicts, environment setup problems, or version mismatches. One can successfully overcome these problems by configuring npm to be compatible with your network environment, modifying system settings for global installations, and managing Node.js versions with tools like nvm. Do not forget to update npm and Node.js frequently to take advantage of the newest features and security updates. It will have a strong base for developing and implementing Node.js-powered, scalable applications with correct setup and troubleshooting.
09 September 2024 · 6 min to read
Ubuntu

How to Install Python and Pip on Ubuntu 24.04

Python is one of the most popular programming languages, widely used for web development, data analysis, artificial intelligence, and more. Pip, the package installer for Python, allows you to install and manage additional Python libraries and tools. Ubuntu 24.04, being a robust and user-friendly Linux distribution, makes it relatively simple to install both Python and Pip. This guide will walk you through the process step-by-step. Prerequisites Before you begin, make sure you have the following: A system running Ubuntu 24.04 with administrative privileges (sudo access). A stable internet connection. Basic knowledge of terminal commands. Updating the Package List Before installing any software, it's essential to update your system's package list to ensure you're downloading the latest versions. To do this, open your terminal and run the following command: sudo apt update This command updates the list of available packages and their versions but does not install or upgrade any packages. Installing Python Ubuntu 24.04 comes with Python pre-installed with version 3.12. However, you might need a specific version or the latest version. To install Python, use the following command: sudo apt install python3 This command installs the latest version of Python 3 available in the Ubuntu repositories. Verifying the Python Installation Once the installation is complete, you should verify that Python is installed correctly. To check the installed version of Python, run: python3 --version You should see output displaying the version of Python installed, for example, Python 3.12.3. This confirms that Python is successfully installed on your system. Installing Pip Pip is the package manager for Python, which allows you to install additional libraries and packages. To install Pip for Python 3, run the following command: sudo apt install python3-pip This command installs Pip and its dependencies. Verifying the Pip Installation After Pip is installed, verify the installation by checking its version: pip3 --version You should see output similar to pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12), confirming that Pip is installed and linked to the correct Python version. Setting Up Virtual Environments Virtual environments are useful for managing dependencies for different projects separately. To create a virtual environment, you first need to install the venv module if it's not already installed: sudo apt install python3-venv Next, create a virtual environment in your project directory: mkdir project && cd project python3 -m venv myenv Replace myenv with the desired name of your virtual environment. To activate the virtual environment, use: source myenv/bin/activate Your terminal prompt will change, indicating that you are now working within the virtual environment. To deactivate, simply run: deactivate Troubleshooting Common Issues Python Command Not Found: If the python3 command is not recognized, ensure that Python is installed and properly linked by running sudo apt install python3 and verifying the installation again. Pip Command Not Found: If the pip3 command is not recognized, re-install Pip using sudo apt install python3-pip and verify its installation. Permission Denied Errors: If you encounter permission errors when installing packages with Pip, consider using pip3 install package-name --user or ensure you are using sudo when necessary. Outdated Pip Version: If you need the latest version of Pip, upgrade it using pip3 install --upgrade pip. Conclusion Installing Python and Pip on Ubuntu 24.04 is a straightforward process that enables you to start developing and managing Python projects quickly. With Python and Pip installed, you can now explore the vast ecosystem of Python libraries and tools. Setting up virtual environments further enhances your ability to manage project-specific dependencies, keeping your development environment clean and organized. By following this guide, you’ve laid a solid foundation for Python development on your Ubuntu system.
06 September 2024 · 4 min to read
JSON

Using the JQ Command to Process JSON on the Command Line

JSON (JavaScript Object Notation) has become the go-to format for data interchange, thanks to its lightweight structure and ease of use. However, working with JSON data directly from the command line can be challenging, especially when dealing with large datasets or needing to perform complex operations. Enter JQ—a powerful command-line tool designed to make JSON processing simple and efficient. Whether you’re a developer looking to manipulate JSON data on the fly or a sysadmin needing to extract specific information from an API response, JQ can significantly enhance your workflow. What is JQ? JQ is a lightweight and flexible command-line processor for JSON data. It allows you to slice, filter, map, and transform structured data with ease. Unlike traditional text-processing tools such as grep, awk, or sed, which are designed for flat text, JQ is specifically tailored for JSON, enabling you to navigate and manipulate nested structures effortlessly. With its powerful and expressive query language, JQ can handle a variety of tasks, from simple data extraction to complex transformations, making it an indispensable tool for anyone working with JSON. Installation of JQ JQ is available on most platforms, including Linux, macOS, and Windows. Here’s how you can install it on your system: Linux: Most Linux distributions include JQ in their package repositories. You can install it using your package manager: sudo apt-get install jq      # Debian/Ubuntu sudo yum install jq          # RHEL/CentOS macOS: If you use Homebrew, you can install JQ with: brew install jq Windows: You can download a precompiled binary from the JQ website and add it to your system's PATH, or use a package manager like Chocolatey: choco install jq Basic Usage of JQ Once JQ is installed, you can start using it to process JSON data. The basic syntax for using JQ is: jq <filter> <json-file> You can also pipe JSON data directly into JQ from other commands or files. For example, to pretty-print JSON data from a file: jq . data.json Parsing JSON Data Parsing JSON data with JQ is straightforward. The . filter is used to access the entire JSON structure. To extract specific elements, you can specify the keys or use indexing for arrays. Assume data.json contains the following content : { "employees": [ {"name": "John", "age": 30, "department": "Sales"}, {"name": "Jane", "age": 25, "department": "Marketing"}, {"name": "Doe", "age": 35, "department": "Development"} ] } Extract the entire list of employees: jq '.employees' data.json Extract the name of the first employee: jq '.employees[0].name' data.json Filtering JSON Data JQ’s powerful filtering capabilities allow you to extract only the data you need. You can use comparison operators, logical operators, and functions to filter JSON data effectively: Filter employees older than 30: jq '.employees[] | select(.age > 30)' data.json Display the names and departments of all employees: jq '.employees[] | {name: .name, department: .department}' data.json Modifying JSON Data JQ is not just for reading JSON data—it also allows you to modify it. You can change values, add or remove elements, and even transform the entire structure. Unless you redirect JQ output to a file, the output is just displayed on the console. Update an employee’s department: jq '.employees[0].department = "Customer Support"' data.json Add a new field to each employee: jq '.employees[] += {"status": "active"}' data.json Remove the age field from all employees: jq '.employees[] | del(.age)' data.json Combining JQ Commands One of the strengths of JQ is the ability to chain multiple commands together to perform complex transformations in a single command. You can use the pipe (|) operator to pass the output of one JQ command as input to another: Filter employees in the Sales department and update their status: o   First, let's change the department of the first employee and save the result back into the data.json file: jq '.employees[0].department = "Customer Support"' data.json > temp.json && mv temp.json data.json o   Now, add a "status": "active" field to all employees and save the changes: jq '.employees[] += {"status": "active"}' data.json > temp.json && mv temp.json data.json  After updating the JSON data with the "status": "active" field, you can now filter and extract the names of all active employees: jq '[.employees[] | select(.status == "active") | .name]' data.json Practical Examples To illustrate the power of JQ, here are a few practical examples (data.json has the initial content): Extract all department names: jq '.employees[].department' data.json  Calculate the average age of employees: jq '[.employees[].age] | add / length' data.json Convert the list of employees to CSV format: jq -r '.employees[] | [.name, .age, .department] | @csv' data.json Common Use Cases JQ is widely used in various scenarios, such as: API Data Processing: Extracting and transforming data from API responses. Configuration Management: Parsing and modifying JSON configuration files. Log Analysis: Filtering and analyzing JSON-formatted logs. Data Transformation: Converting JSON data into different formats or structures. Conclusion JQ is a versatile and powerful tool for anyone who works with JSON data regularly. From simple data extraction to complex transformations, JQ’s expressive command language and ease of use make it an essential part of the modern command-line toolkit. By mastering JQ, you can significantly streamline your data processing tasks and enhance your productivity, whether you’re developing software, managing systems, or analyzing data.  
06 September 2024 · 5 min to read
Apache

How to Disable Directory Browsing on Apache

When using an Apache server, directory browsing creates a security issue since it allows unauthorized users to view the organization and contents of your website's directories. Directories without an index file, such as index.html or index.php, may by default allow users to view their contents through Apache. Directories without an index file, such as index.html or index.php, may by default allow users to view their contents through Apache. This feature is considered by many as a terrible practice for production servers, despite the fact that it can be helpful in some development contexts. In order to improve the safety features of the website and prevent unauthorized access to vital information, one should take the easy but crucial action of disabling directory browsing on the website's server. This tutorial will show how to prevent directory surfing on an Apache web server, so that directories stay hidden from the attackers.  Prerequisites A cloud server with installed and running Apache Access to the Apache Server  Understanding Directory Browsing in Apache When no specified file (like index.html or index.php) is present to be displayed as the default page, the web server can show the contents of a directory. This is known as directory browsing in Apache. Users who have directory browsing enabled can view a list of files and folders within a directory by navigating to a URL that matches a directory path on the server.  Depending on how the server is configured, directory browsing may or may not be enabled by default for Apache. The Apache configuration files' Options directive manages this behavior. The Indexes option there establishes whether directory browsing is permitted. Directory browsing will be available for the designated directories if the Indexes option is included in the configuration. Directory browsing presents a number of risks in a production setting, even though it may be helpful for some types of file repositories or during development. Users may end up with access to private information—like configuration files, backup files, or temporary files—that were not intended to be shared with the public. Leaving a directory structure exposed could lead to the discovery of security flaws like outdated script versions, incorrect setups, or files containing known vulnerabilities. Directory browsing could be used by unauthorized users to obtain information for a more focused attack on the system or to download things that need to be kept secret. Disabling Directory Browsing in the Main Configuration File It's simple to disable directory browsing in the Apache main configuration file by changing the Options directive to make sure the Indexes option is disabled. This directive determines how directories behave in a number of ways, such as whether directory browsing is permitted. By modifying the primary Apache configuration file, you can turn off directory browsing as follows: Depending on your operating system, the primary Apache configuration file is normally found in one of the following directories: Debian/Ubuntu /etc/apache2/apache2.conf CentOS/RHEL /etc/httpd/conf/httpd.conf Make a backup on the configuration file above by running the command below. sudo cp -rp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backup Open the file and edit it with a text editor such as Vim or Nano. nano /etc/apache2/apache2.conf Find the Directory Configuration Block. There are multiple blocks in the configuration file. These blocks specify configurations for particular server folders. The root directory is the highest-level directory on the server, and the settings in the block are applicable to it. Look for a block that is similar to the following:Within this block look for the line that contains Options (box in red).  It may include the Indexes option, which enables directory browsing. Remove the Indexes option from the Options directive to disable the directory browsing. This is how the modified directive should appear. Save and exit on the editor.  For changes to take effect, restart the Apache service. Run the command below. Debian/Ubuntu sudo systemctl restart apache2 Centos/RHEL sudo systemctl restart httpd Verify that directory browsing has been disabled in order to validate the modifications. Then, use a web browser to access a directory on the server without an index file. Users should get a "403 Forbidden" response instead of a directory listing, indicating that directory browsing is no longer permitted. Disabling Directory Browsing in Virtual Host Files With Apache, different websites may be operated; each with its own configuration, on a single server by using virtual hosts. One can change the configuration in the virtual host files to prevent directory browsing for particular websites or virtual hosts. Here's how to make this happen: Depending on your operating system, the primary Apache configuration file is normally found in one of the following directories: Debian/Ubuntu /etc/apache2/sites-available/ CentOS/RHEL /etc/httpd/conf.d/ or /etc/httpd/sites-available/ Every virtual host will have a configuration file of its own, usually called example.com.conf or after the domain or website it serves. Make a backup on the configuration file above by running the command below. sudo cp  /etc/apache2/sites-available/example.com.conf  /etc/apache2/sites-available/example.com.conf.backup Open the file above and edit it with a text editor such as Vim or Nano. nano /etc/apache2/sites-available/example.com.conf Find the Directory Configuration Block. There are multiple blocks in the configuration file. These blocks specify configurations for particular server folders. The root directory is the highest-level directory on the server, and the settings in the block are applicable to it. Look for a block that is similar to the following:Within this block look for the line that contains Options (box in red).  it may include the Indexes option, which enables directory browsing. Remove the Indexes option from the Options directive to disable the directory browsing. This is how the modified directive should appear. Save and exit on the editor.  For changes to take effect, restart the Apache service. Run the command below. Debian/Ubuntu sudo systemctl restart apache2 Centos/RHEL sudo systemctl restart httpd Verify that directory browsing has been disabled in order to validate the modifications. Then, use a web browser to access a directory on the server without an index file. Users should get a "403 Forbidden" response instead of a directory listing, indicating that directory browsing is no longer permitted. Disabling Directory Browsing Using .htaccess Files With Apache, the.htaccess file is a strong configuration tool that lets users change server settings for individual directories. One can change or create a.htaccess file in the directory where they wish to apply this setting if they don't have access to the main Apache configuration file or if they want to block directory browsing for a particular directory. Using a.htaccess file, users can prevent directory browsing as follows: To disable directory browsing, navigate to the desired directory. This could be any subdirectory inside the site itself or the document root of the website (/var/www/example.com, for example). cd /var/www/example.com If there's already a .htaccess file in the directory, one can edit it with a text editor. Otherwise, one can make a fresh .htaccess file sudo nano .htaccess Add the line below on the file then save and exit. Options -Indexes Apache needs to be set up to permit .htaccess overrides in the appropriate directory in order for the .htaccess file to function. The AllowOverride directive in the virtual host or Apache configuration files controls this. Verify that the virtual host file or directory block in the Apache configuration permits overrides, as shown below. Verify that directory browsing has been disabled in order to validate the modifications. Then, use a web browser to access a directory on the server without an index file. Users should get a "403 Forbidden" response instead of a directory listing, indicating that directory browsing is no longer permitted. Troubleshooting Common Issues Although it is usually simple to disable directory browsing on Apache, there are certain issues that make the setup not work as intended. Users can solve typical problems while deactivating directory browsing on Apache by checking the list of solutions provided below. Directory browsing remains enabled even after making changes to the. htaccess file or Apache settings. To make the modifications take effect, make sure to restart the Apache server. Use the command that is right for your operating system. Debian/Ubuntu sudo systemctl restart apache2 CentOS/RHEL sudo systemctl restart httpd Check to see if the Apache user is allowed to view the modified configuration files or the .htaccess file. These files may not be readable by Apache due to incorrect file permissions. Perform the following if issue has been encountered. sudo chmod 644 /path/to/.htaccess Debian/Ubuntu sudo chown www-data:www-data /path/to/.htaccess   CentOS/RHEL sudo chown apache:apache /path/to/.htaccess Users get a "403 Forbidden" error for all requests after disabling directory browsing, even when trying to access files that are supposed to be available. To enable Apache to serve files, make sure the directory permissions are set appropriately. Run the command to fix it. sudo chmod 755 /var/www/example.com Make sure the AllowOverride directive in the Apache configuration is set to All, or at the very least, to allow the Options directive, especially if users are using a.htaccess file. After changing the configuration to deactivate directory browsing, Apache either fails to start or restarts with issues. Before restarting Apache, run a syntax check on the configuration files to identify any errors. sudo apachectl configtest For more details on the reason why Apache is not starting, review the Apache error logs. Debian/Ubuntu sudo tail -f /var/log/apache2/error.log  CentOS/RHEL sudo tail -f /var/log/httpd/error_log CONCLUSION One of the most important things one can do to secure the web server is to disable directory browsing on Apache. The directories' contents can be kept hidden from unauthorized users, lowering the possibility of vulnerable information and sensitive data being exposed. Using.htaccess files, virtual host file adjustments, and changes to the main Apache configuration file are some of the methods available to stop directory browsing. There is flexibility in each option based on the server configuration and access level. Knowing how to limit directory browsing at different levels is important for those who oversee a single site or a number of virtual hosts. It guarantees that the server is safe and properly setup. Even though the procedure is normally simple, be ready to troubleshoot common problems to make sure the modifications are implemented correctly, such as wrong permissions, conflicting setups, or syntax errors. These actions will help to safeguard information, improve the security of the Apache server, and give users a safer environment. Maintaining a strong and secure online infrastructure includes routinely checking and updating the server's settings, as well as turning off pointless functions like directory browsing.
05 September 2024 · 9 min to read
Ubuntu

How To Install and Secure Redis on Ubuntu 20.04

Redis is a powerful in-memory key-value store that is used as a database, cache, and message broker. Due to its speed and flexibility, Redis is popular in many applications that require real-time data processing. In this guide, we will walk you through the process of installing Redis on Ubuntu 20.04, configuring it for optimal performance, and securing it against unauthorized access. Prerequisites Before starting, ensure that your server meets the following requirements: Ubuntu 20.04: This guide assumes you are working on a system running Ubuntu 20.04. A user with sudo privileges: You’ll need administrative rights to install and configure Redis. Basic command-line knowledge: Familiarity with the Linux command line is essential. Installing Redis on Ubuntu 20.04 To begin the installation of Redis on Ubuntu 20.04, follow these steps: Update your system’s package list sudo apt update Install Redis Install Redis from the Ubuntu repository: sudo apt install redis-server -y Redis will be installed and configured to run automatically after installation. Verify the installation To check if Redis is installed and running, use: sudo systemctl status redis Redis should be running. If not, start it with: sudo systemctl start redis Configuring Redis By default, Redis works out of the box, but some configurations can optimize its performance and security: Edit the Redis configuration file Open the Redis configuration file located at /etc/redis/redis.conf: sudo nano /etc/redis/redis.conf Optimize memory usage Redis can be configured to use specific memory limits by adjusting the maxmemory directive: maxmemory 256mbmaxmemory-policy allkeys-lru This setting limits Redis to use a maximum of 256MB of RAM and removes the least recently used (LRU) keys when the limit is reached. Bind Redis to the local interface For security, ensure Redis is only accessible from the local machine: bind 127.0.0.1 ::1 Save your changes After making these adjustments, save and close the file (CTRL + X, then Y, and Enter). Restart Redis For the changes to take effect, restart Redis: sudo systemctl restart redis Securing Redis: Authentication and Firewall Redis has minimal security by default. Follow these steps to enhance its security: Set up Redis authentication In the Redis configuration file (/etc/redis/redis.conf), find the requirepass directive and set a strong password: requirepass your_secure_password Configure the firewall If Redis must be accessible from outside the local machine, set up UFW (Uncomplicated Firewall) to allow connections only from trusted sources: sudo ufw allow from trusted_IP to any port 6379 from trusted_IP : Specifies the source IP address from which traffic is allowed. to any: Refers to the destination, meaning any of the system’s IP addresses. port 6379: Specifies the port number to which the access is granted (the Redis port). To enable the firewall: sudo ufw enable Always ensure Redis is only accessible from authorized IP addresses. If ufw is not installed, you can simply install with: sudo apt update && sudo apt install ufw -y Enabling Redis Persistence Redis offers two types of persistence: RDB (Redis Database Backup) and AOF (Append-Only File). Enable RDB persistence RDB snapshots are created at specified intervals. In /etc/redis/redis.conf, configure the snapshot settings: save 900 1save 300 10save 60 10000 Enable AOF persistence AOF logs every write operation. To enable AOF, in the same configuration file: appendonly yes Restart Redis to apply changes sudo systemctl restart redis Setting Up Redis as a Service Redis can be managed as a service, allowing you to control it with systemd. Enable Redis to start on boot: sudo systemctl enable redis Start, stop, and check the status of Redis: Start Redis: sudo systemctl start redis Stop Redis: sudo systemctl stop redis Check the status: sudo systemctl status redis Monitoring and Managing Redis Proper monitoring ensures Redis is running efficiently: Use the Redis CLI for real-time monitoring Redis includes a command-line interface (redis-cli) for monitoring: redis-cli -a $password monitor This command authenticates using the specified password and directly starts monitoring. But for security reasons, we need to avoid passing the password directly on the command line in production environments, as it may be visible to other users on the system. Instead, you can enter the redis-cli interactive mode and authenticate like this: Start the redis-cli without a password: redis-cli Authenticate with the AUTH command: AUTH your_secure_password Then, run the monitor command: monitor Use Redis’ built-in metrics Get a summary of Redis operations using: redis-cli info Set up third-party monitoring tools Tools like Prometheus, Grafana, or RedisInsight offer advanced monitoring and visualization. Backing Up and Restoring Redis Data Backing up Redis data is crucial to avoid data loss: Manual RDB backup        Copy the Redis dump file: cp /var/lib/redis/dump.rdb /path/to/backup/directory Restore from backup      Stop Redis, replace the current RDB file with your backup, and start Redis again: sudo systemctl stop redis cp /path/to/backup/dump.rdb /var/lib/redis/ sudo systemctl start redis Troubleshooting Common Issues If you encounter issues, here are some common problems and their solutions: Redis won’t start Check the Redis logs at /var/log/redis/redis-server.log for error messages. Memory issues If Redis runs out of memory, adjust the maxmemory setting or add more RAM to your server. Connection issues Ensure that Redis is bound to the correct IP address and the firewall rules are correctly configured. Conclusion Installing and securing Redis on Ubuntu 20.04 is a straightforward process that, when done correctly, provides a reliable and fast database solution for your applications. By following this guide, you have set up Redis, configured it for optimal performance, secured it against unauthorized access, and implemented essential monitoring and backup procedures. With Redis now properly configured and secured, your applications can leverage its speed and efficiency with confidence.
03 September 2024 · 5 min to read
Docker

Installing Bitwarden in Docker

Bitwarden is a free, open-source password manager that stores sensitive information in an encrypted vault. It is written in C# using .NET Core and ASP.NET Core, and its database is based on the T-SQL/SQL Server. Bitwarden is a cloud service accessible through various client applications, making it cross-platform: web, desktop (Windows, macOS, Linux), mobile apps, browser extensions (Chrome, Firefox, Safari, Edge, Opera, Vivaldi, Brave, Tor), or through the command line interface. One key motivation for using Bitwarden is avoiding third-party password managers where sensitive data is stored on external servers. Instead, you can deploy Bitwarden on your secure server. Bitwarden is based on a group of containers, each containing a separate functional component of the manager, such as the database or web server. Therefore, installing and running Bitwarden requires a containerization system, which is Docker. Here's a brief list of Bitwarden's features: Open-source Built on the 256-bit AES encryption standard to protect user data Supports two-factor authentication Password auditing and verification system Biometric authentication support Ability to host the server locally Cross-platform client applications on all popular platforms Prerequisites This tutorial uses commands for UNIX-like operating systems, specifically Debian/Ubuntu, as they are often used for deploying server applications. Before installing and configuring Bitwarden, ensure that all necessary system packages are installed and updated: sudo apt updatesudo apt upgrade If your cloud server is new, it's recommended to install some basic tools: sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common Next, ensure that you have Docker and Docker Compose for managing Bitwarden containers. You can install them on Ubuntu/Debian using this guide. Step 1. Install Docker and Its Components First, add Docker's GPG key, which is used for signing packages: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - Then, add the Docker repository to obtain the latest version: sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" Update the system again: sudo apt update Finally, install Docker and Docker Compose: sudo apt install docker-ce docker-ce-cli containerd.io docker-compose Step 2. Secure Your Bitwarden Setup To enhance security, create a dedicated user with limited permissions. Create a directory for Bitwarden: sudo mkdir /opt/bitwarden Create the Bitwarden user: sudo adduser bitwarden Assign directory ownership to the Bitwarden user: sudo chmod -R 700 /opt/bitwardensudo chown -R bitwarden:bitwarden /opt/bitwarden Allow the Bitwarden user to run Docker commands: sudo usermod -aG docker bitwarden After setting permissions, switch to the Bitwarden user: su bitwarden Navigate to the installation directory: cd /opt/bitwarden Step 3. Install Bitwarden in Docker and Start the Server Even if you're not familiar with Docker, Bitwarden's developers have provided an installation script that automates the process. Download it: curl -Lso bitwarden.sh https://go.btwrdn.co/bw-sh && chmod 700 bitwarden.sh Now run it: ./bitwarden.sh install The script will prompt you for various configuration details, such as: Domain name for Bitwarden Database name Whether to use Let's Encrypt for a free SSL certificate Additionally, you will need to enter an installation ID and key, which you can generate on Bitwarden's official site. Step 4. Set Up a Mail SMTP Server Although optional, setting up an SMTP server is recommended for administrator login functionality. If you are an experienced Docker user, you might want to consider the fairly simple mail server, docker-mailserver, which is well-suited for most Bitwarden tasks. In the simplest scenario (which slightly contradicts the logic of deploying a local server for security purposes), you can use public mail servers, such as Gmail. In that case, you should specify the following email parameters: globalSettings__mail__replyToEmail=your_email@gmail.comglobalSettings__mail__smtp__host=smtp.gmail.comglobalSettings__mail__smtp__username=your_emailglobalSettings__mail__smtp__password=your_passwordglobalSettings__mail__smtp__ssl=trueglobalSettings__mail__smtp__port=587globalSettings__mail__smtp__useDefaultCredentials=falseglobalSettings__disableUserRegistration=true Once these parameters are set, Bitwarden notifications will be sent to your Gmail. However, make sure to enable the "allow less secure apps" option in your Gmail account. Otherwise, Google's email service might block messages from your password manager. If you use a different mail service, find out its settings (specifically, the SMTP server host address) and specify them similarly in the global.override.env file. That said, there is a chance that the SMTP configurations you provide may not work correctly, preventing messages from reaching your email server, whether it's yours or a third-party one. Therefore, it might be wise to use an online SMTP checker to verify and fine-tune the correct settings in case any issues arise. Step 5. Start the Server and Verify Operation Start the server using the same script: ./bitwarden.sh start Access the server via a web browser using its IP or domain name. To access the admin panel, append /admin to the server address. The page will ask you to enter an email address—the same one whose details you added to the global.override.env file. The mail server will send a link to this address for passwordless access to the admin panel. From there, you can use any Bitwarden client applications that are available. For example, you can download the desktop client, enter your server's address, log in to Bitwarden, and use the manager to securely store your confidential data. Stopping and Removing the Server Sometimes, various issues may accumulate on the server side, requiring you to reinstall the manager. To do this, first, completely remove Bitwarden using the same script: ./bitwarden.sh stop Then, simply delete the manager's directory: rm -r ~/bwdata And perform a reinstallation if necessary: ./bitwarden.sh install Conclusion Installing Bitwarden is straightforward, with developers automating most of the deployment process. After the installation, you can secure your sensitive data with this robust password manager, leveraging DNS, SSL, and SMTP as needed. You can find many useful tips on using Bitwarden for practical tasks in the official documentation.
30 August 2024 · 6 min to read
Kafka

Installing and Configuring Kafka on Windows, Ubuntu, and Other Operating Systems

A message broker is a software that acts as an intermediary for sending messages between different applications. It functions like a server that receives messages from one application (called the producer) and routes them to one or more other applications (called consumers). The main purpose of a message broker is to ensure robust and reliable communication between different systems without requiring these systems to always be available or directly connected. This allows applications to work asynchronously, providing fault tolerance and the ability to operate in real time. A message broker can accept messages from multiple sources and efficiently route them to the appropriate receiver. Depending on the required business logic, messages can be grouped into topics or queues. There are many different message brokers, each with its own features and advantages. In this article, we'll focus on Kafka. Apache Kafka is a fast and scalable message broker capable of handling millions of messages per second. It is particularly valued for its fault tolerance and ability to store data for extended periods. Originally developed by LinkedIn, Kafka is now the most popular open-source solution in the message broker space and is licensed by the Apache Software Foundation. It is widely used to build real-time data pipelines and streaming applications. Moving and processing data streams between systems or applications is a critical task, and Kafka excels in helping users handle data streams in real-time with minimal latency. As a distributed system, Kafka is divided across multiple servers, which can store and process data streams in parallel. This distribution allows Kafka to provide real-time data processing for many different sources, ensuring reliability and resilience against system failures. In this article, we will explore how to install and configure Kafka on Windows, Ubuntu, and macOS,  so that you can take full advantage of it for your projects.  System Requirements Apache Kafka is designed to maximize the efficiency of the hardware it runs on. However, there are some general recommendations to keep in mind when setting up a system to work with this broker: Processor (CPU): Kafka usually doesn't require a lot of processing power since most operations are performed using direct disk access (zero-copy). However, the number of CPU cores can impact throughput. Memory (RAM): Having at least 8GB of RAM is recommended, but the final amount will depend heavily on the data load and the number of parallel operations. Disk Space: Kafka efficiently uses the file system and direct disk writing. It is preferable to use an SSD with high read/write speeds. It's also recommended that a separate disk be used to isolate its operations from other processes. Network: Kafka actively uses the network for data transmission. A stable connection with high bandwidth is recommended. Operating System: Apache Kafka generally runs on Unix-like systems such as Linux, but it does not restrict users from choosing other operating systems. Java: Since Kafka is written in Java, you will need the Java Development Kit (JDK) version 8 or higher. While Linux gives Kafka a key advantage in performance and scalability, the broker works well on both Windows and macOS. We'll discuss the pros and cons of each solution a bit later, but for now, let's proceed with the installation. Installing and Configuring Kafka on Windows The process of Kafka installation on Windows is straightforward but requires some care. Here's a step-by-step guide: Download and Install Java Development Kit (JDK): Apache Kafka runs on Java, so the first step is to install the development tools if they are not already installed. You can download the JDK from Oracle's official website. After installation, verify its functionality by entering the following command in the command prompt (cmd): java -version Download Apache Kafka: You can download Apache Kafka for Windows from the project's official website (look for Binary downloads). It is recommended that you choose the latest stable version (at the time of writing, this is 3.7.0). However, the installation process does not vary significantly between versions, so you can apply this guide to other versions as well). Unpacking: After downloading the archive, unpack it and move it to a convenient location. After unpacking the distribution, you will see various folders such as: bin: This folder contains executable files used to start and manage the distributed messaging system. The /windows subfolder contains special versions of files intended for use on Windows OS. config: This folder contains Kafka configuration files, including zookeeper.properties and server.properties, which can be edited for more precise setup. libs: This folder contains all the libraries needed to run Kafka. logs: This folder contains work logs, which can be useful for troubleshooting issues and finding dependencies between components. site-docs: This folder contains documentation for the Kafka version you installed, which can be helpful for beginners. LICENSE and NOTICE: These files contain the license agreement and legal notices. Basic Configuration of Data and Logging Directories: By default, log files and the data directory are saved in the /tmp folder, which can lead to performance, security, and data management issues. It is recommended to change the default paths to custom ones: Navigate to /config/server.properties and open the file in any text editor (e.g., VSCode). Find the log.dirs field (you can use the search function by pressing Ctrl+F). Change the default path /tmp/kafka-logs to a permanent path, e.g., c:/kafka/kafka-logs. Save the file and close it. Perform similar actions for the data directory: Navigate to /config/zookeeper.properties and open the file in any text editor. In the dataDir parameter, change the default path to a custom one. An example of a permanent path is shown in the screenshot below. The basic setup is now complete. This is enough to start the Zookeeper and Kafka servers and verify that the system is working. Starting Zookeeper and Kafka Servers: To start, navigate to the folder with the unpacked archive and open the command prompt. To start Zookeeper, use the following command: .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties Note that our Zookeeper is running on port 2181, which is the default port for this service. If you encounter the error "The input line is too long. The syntax of the command is incorrect", move the Kafka folder closer to the disk's root. During the startup of zookeeper-server-start.bat, the CLASSPATH is called multiple times, leading to the overflow of the variable. The cmd.exe environment supports no more than 8191 characters. Open a new terminal window to start the Kafka server and use the following command: .\bin\windows\kafka-server-start.bat .\config\server.properties Verifying Functionality: To verify that everything is working, try creating a topic using the following command: .\bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 --topic TestTopic Note that port 2181 matches the open port for Zookeeper. To visualize, create another topic called NewTopic. Now check what topics exist with the following command: .\bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092 You can interact with the topic in a new command prompt by creating and reading several messages. To do this, enter the following command in a new window: .\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic TestTopic After the command starts, you can send any messages. To start receiving messages, enter the following command in a new console window: .\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic TestTopic --from-beginning As shown in the screenshot, we received messages on the TestTopic. This is a simple functionality that helps you understand and familiarize yourself with Kafka tools. However, things may go wrong during installation and testing. Here are some common issues that may arise: JDK Issues: Ensure you have installed the JDK, not just the JRE. Also, make sure the version is 8 or higher. Environment Variable Check: After installing Java, ensure that the JAVA_HOME variable is set correctly and that the path to the bin directory is included in the system path. Firewall and Antivirus Issues: Sometimes, third-party antivirus programs or firewalls can block Kafka. If you encounter connection issues, try disabling them temporarily. Ports: By default, Zookeeper listens on port 2181, and Kafka on 9092. Make sure these ports are free, or reassign the default ports for these services. Starting Zookeeper Before Kafka: Before starting Kafka, make sure Zookeeper is already running. If not, start Zookeeper. Improper Kafka Shutdown: If Kafka shuts down improperly, some data may still be left in the temporary folder. If you start encountering difficulties during startup, try clearing the temporary files. Installing and Configuring Kafka on Ubuntu The steps for installing Kafka on Ubuntu are quite similar to those for other Linux distributions. The main differences lie in each operating system's package managers and minor specifics. These steps also resemble the installation process for Windows, so you can refer to that section even if you're using Linux. Downloading and Installing the Java Development Kit (JDK) As mentioned earlier, Apache Kafka runs on Java, so the first step is to install the JDK. However, before doing so, it's recommended to update your package list and upgrade the package versions with the following commands: sudo apt updatesudo apt upgrade In Linux systems, the installation can be easily done via the terminal by entering the following commands: sudo apt install default-jresudo apt install default-jdk Downloading and Extracting Kafka You can download Apache Kafka from the official website. Select the latest stable version of the product. Use the wget utility from the console to download it: wget https://downloads.apache.org/kafka/3.7.0/kafka_2.13-3.7.0.tgz To extract the downloaded file, use this command: tar xzf kafka_2.13-3.7.0.tgz Note that the version of Kafka might be different when you read this, so the command, particularly the numbers in the link, might look different. After the above steps, you should have a folder with the product next to the archive. Navigate to this folder using: cd kafka_2.13-3.7.0 Verifying Functionality The remaining steps are similar to what was done for Windows, so it's recommended to read the instructions starting from the third point in that section. To start Zookeeper, enter the following command: bin/zookeeper-server-start.sh config/zookeeper.properties Then, in a new terminal window, start Kafka: bin/kafka-server-start.sh config/server.properties This covers the basic installation and configuration. You can configure various parameters for production environments such as multiple backups, network configuration, data partitioning, and more, but this process is more labor-intensive and complex. Common Issues with Kafka Installation on Ubuntu and other Linux distributions Permission Issues: Sometimes, permission problems arise in Linux when accessing certain files or directories. To bypass this, you can use sudo before commands that cause issues. However, be cautious as sudo gives full admin access, which might lead to security issues. Java Memory Errors: If you encounter Java memory problems while working with Kafka, try increasing the maximum memory allocated for the JVM using the -Xmx flag. You'll need to add this flag to the configuration file in /bin/kafka-server-start.sh. However, ensure that you leave enough memory for other processes on the system. Increasing the maximum JVM memory can slow down the system if JVM starts using all available resources. Version Management: Version issues can arise when working with Linux. Always check the version of Kafka and all related tools, such as Zookeeper, to ensure compatibility. Proper Shutdown of Kafka and Zookeeper: To shut down Kafka and Zookeeper on Linux, you can use the following commands: kafka-server-stop.shzookeeper-server-stop.sh It's recommended that these services are always stopped properly to avoid data loss. Logging Issues: Kafka generates a large number of logs. Ensure you have enough disk space and that log rotation is enabled. Port and File Limits: Make sure you have permission to open the necessary number of files or sockets. Linux has system limits that can be adjusted if needed. Installing and Configuring Kafka on MacOS Homebrew is a package manager that simplifies software installation on MacOS. Homebrew doesn't require admin rights to install software, making it convenient and reducing security risks. If you don't have Homebrew installed, you can install it by entering the following command in the terminal: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" In this case, you will need Homebrew to install Kafka and its dependencies. Updating Homebrew If you already have Homebrew installed, it's a good idea to update it to the latest version with: brew update Installing the Java Development Kit (JDK) To install JDK, you can use the Homebrew we just installed. Enter the following command in the terminal: brew install openjdk Installing Kafka Install Kafka with the following command: brew install kafka Starting Kafka and Zookeeper First, start Zookeeper, then Kafka. Replace /usr/local/bin with the path to the executable files for Kafka and Zookeeper if they are located elsewhere: zookeeper-server-start /usr/local/etc/kafka/zookeeper.propertieskafka-server-start /usr/local/etc/kafka/server.properties For simplicity, we launched Zookeeper and Kafka in standalone mode on a local machine. To create a full-fledged distributed network on multiple machines, adjust the configuration files accordingly. Key parameters to modify include: Partitions: These allow parallel data processing. The number of partitions determines how many streams can process data simultaneously within a topic. Replicas: Copies of existing partitions ensure fault tolerance. The number of replicas determines how many copies of each partition will be stored in the cluster. Broker Information: A complete list of all servers that will participate in the cluster. Common Issues When Installing Kafka on MacOS JDK Version: Ensure that JDK version 8 or higher is installed. If not, you might encounter an error when trying to launch Kafka. Environment Variables: Kafka may not work if environment variables are incorrectly set or not set at all. For instance, you must set the KAFKA_HOME environment variable to the directory path. Other environment variables like JAVA_HOME might also be necessary for proper operation. File Paths and Permissions: Kafka might not find the necessary files or fail to start if it doesn't have read and write permissions for certain directories. You might need to change permissions or move some files. Homebrew Issues: Ensure Homebrew is correctly installed and updated to the latest version. Sometimes, installation via Homebrew can lead to version conflicts or dependency issues. Dependency Issues: The system requires Zookeeper to function. Always start Zookeeper before Kafka. Ports: Kafka and Zookeeper use specific ports (9092 and 2181, respectively) by default. If other applications use these ports, Kafka won't be able to start. Configuration: Errors in Kafka configuration files or incorrectly set parameters can cause issues when attempting to start Kafka. Installing and Configuring Kafka in Docker Docker is a platform for developing, delivering, and running applications in containers. Containers allow you to package an application with all its environment and dependencies into a single package that can be easily distributed and installed on any system. Installing Kafka in Docker is a great way to quickly and easily start working with the system. Here are some simple steps for installation: Install Docker Download Docker from the official website in a way that suits your OS. Run an Instance Use this command to start a Kafka instance: docker run -p 9092:9092 apache/kafka:3.7.0 Note that your Kafka version may differ from the one in the example. You can verify the functionality of Kafka in a manner similar to the Linux installation section. Choosing an OS for Deploying Kafka As we've established, Kafka can be installed on all major operating systems, as well as in Docker. Depending on the specific situation and needs, each option has its own advantages and disadvantages. If you're deciding which OS to use for deploying Kafka, here are the pros and cons of each system: Windows Pros: Ease of Use: Windows remains one of the most popular operating systems with extensive documentation and community support. Integration: It integrates very well with other Microsoft products and services. Cons: Windows is not always the best choice for deploying server applications; you will likely encounter compatibility and performance issues. While PowerShell and WSL (Windows Subsystem for Linux) can simplify operations, these systems may not always be optimal for working with Linux applications. Kafka and Zookeeper are usually tested and used on Unix-like systems, which might lead to more bugs and issues. macOS Pros: Simple Installation: Installation is straightforward with minimal difficulties. User-Friendly Tools: Convenient tools for installing and managing software. Unix-Based System: Makes it easier to work with most tools. Cons: Resource-Intensive: If your Mac lacks sufficient resources, it may slow down operations. Compatibility Issues: Possible compatibility issues between macOS versions and Kafka could lead to critical errors. Linux Pros: Open Source Support: Since Linux is open-source and supported by a large community, there are almost always ways to solve any problem. Efficient Resource Use: Linux consumes fewer system resources, making it more efficient for running Kafka. Preferred for Server Applications: Linux-based operating systems are often the preferred choice for server applications. Cons: Technical Skills Required: More technical skills are needed for setup and management compared to Windows and macOS. GUI Installation Challenges: There may be difficulties when installing and configuring a GUI. Docker Pros: Portability: Docker containers can run on any operating system, simplifying broker deployment in various environments. Isolation: Docker provides isolation between applications, meaning Kafka's operation won't affect other applications. Reproducibility: Docker allows you to create configurations that are easy to replicate, simplifying updates and deployments. Integration with Other Tools: Docker interacts well with popular solutions, simplifying Kafka container management and scaling. Cons: Complexity: Docker adds an extra layer of complexity to the broker installation. Data Management: The broker stores all messages on disk, and managing this in a containerized environment can be challenging. Performance: As with any containerized system, the broker's performance may be limited by the container's resources, requiring fine-tuning of Docker. Management: Managing and monitoring a broker in a container can be complex, especially in large systems. You may need automation tools like Kubernetes and Prometheus. Overall, Linux is the most common choice for working with Apache Kafka, especially for servers and workstations. However, the choice of operating system will depend directly on your preferences and requirements. Running Kafka in the Cloud We've covered the process of installing Kafka on different operating systems, but this process can be time-consuming due to potential errors. If you want to avoid the hassle of installation and configuration, consider our solution. Hostman offers a flexible and scalable cloud solution for launching a Kafka instance in just a few minutes. You don't need to install or configure any software; just select a region and configuration. Hostman ensures stability and performance for your Kafka project, thanks to professional support and high-performance infrastructure. This allows you to fully focus on developing and scaling your project without worrying about the technical side of things. Try Hostman today and discover the benefits of working with reliable and high-performance cloud hosting. Conclusion In this guide, we have covered how to install Kafka on Ubuntu, Windows, and other operating systems. Apache Kafka is a robust, reliable, and scalable message broker that offers high throughput, fault tolerance, and low latency. Here are some reasons why Kafka is a great choice for a messaging environment: High Throughput: Apache Kafka can handle millions of messages per second, making it an excellent choice for applications that process large volumes of real-time data. Fault Tolerance: Kafka provides recovery from failures and ensures high data availability through its replication mechanisms. Scalability: Kafka can easily scale by adding more nodes to the cluster without disrupting the service. Long-Term Data Storage: Unlike most other message brokers, Kafka supports long-term data storage. You can configure the retention period in Kafka, and the data will be stored until it expires. Distributed System: Kafka is inherently a distributed system, meaning messages can be consumed in any order and across multiple channels. Integration with Multiple Systems: Kafka can be easily integrated with various systems, such as Hadoop, Spark, Storm, Flink, and many others. Fast Processing: Apache Kafka provides low latency, making it an excellent choice for applications requiring real-time data processing. Publish-Subscribe Topology: Kafka allows data sources to send messages to topics, and recipient applications to subscribe to topics of interest. All these advantages make Kafka one of the most popular and reliable message brokers on the market.
29 August 2024 · 18 min to read

Answers to Your Questions

What is Hostman used for, and what services do you offer?

Hostman is a cloud platform where developers and tech teams can host their solutions: websites, e-commerce stores, web services, applications, games, and more. With Hostman, you have the freedom to choose services, reserve as many resources as you need, and manage them through a user-friendly interface.

Currently, we offer ready-to-go solutions for launching cloud servers and databases, as well as a platform for testing any applications.

 

  • Cloud Servers. Your dedicated computing resources on servers in Poland and the Netherlands. Soon, we'll also be in the USA, Singapore, Egypt, and Nigeria. We offer 25+ ready-made setups with pre-installed environments and software for analytics systems, gaming, e-commerce, streaming, and websites of any complexity.

  • Databases. Instant setup for any popular database management system (DBMS), including MySQL, PostgreSQL, MongoDB, Redis, Apache Kafka, and OpenSearch.

  • Apps. Connect your Github, Gitlab, or Bitbucket and test your websites, services, and applications. No matter the framework - React, Angular, Vue, Next.js, Ember, etc. - chances are, we support it.

Can I have confidence in Hostman to handle my sensitive data and cloud-based applications?

Your data's security is our top priority. Only you will have access to whatever you host with Hostman.

Additionally, we house our servers in Tier IV data centers, representing the pinnacle of reliability available today. Furthermore, all data centers comply with international standards: 

  • ISO: Data center design standards

  • PCI DSS: Payment data processing standards

  • GDPR: EU standards for personal data protection

What are the benefits of using Hostman as my cloud service provider?

User-Friendly. With Hostman, you're in control. Manage your services, infrastructure, and pricing structures all within an intuitive dashboard. Cloud computing has never been this convenient.

 

Great Uptime: Experience peace of mind with 99.99% SLA uptime. Your projects stay live, with no interruptions or unpleasant surprises.

 

Around-the-Clock Support. Our experts are ready to assist and consult at any hour. Encountered a hurdle that requires our intervention? Please don't hesitate to reach out. We're here to help you through every step of the process.

 

How does pricing work for your cloud services?

At Hostman, you pay only for the resources you genuinely use, down to the hour. No hidden fees, no restrictions.

Pricing starts as low as $4 per month, providing you with a single-core processor at 3.2 GHz, 1 GB of RAM, and 25 GB of persistent storage. On the higher end, we offer plans up to $75 per month, which gives you access to 8 cores, 16 GB of RAM, and 320 GB of persistent storage.

For a detailed look at all our pricing tiers, please refer to our comprehensive pricing page.

Do you provide 24/7 customer support for any issues or inquiries?

Yes, our technical specialists are available 24/7, providing continuous support via chat, email, and phone. We strive to respond to inquiries within minutes, ensuring you're never left stranded. Feel free to reach out for any issue — we're here to assist.

Can I easily scale my resources with Hostman's cloud services?

With Hostman, you can scale your servers instantly and effortlessly, allowing for configuration upsizing or downsizing, and bandwidth adjustments.

Please note: While server disk space can technically only be increased, you have the flexibility to create a new server with less disk space at any time, transfer your project, and delete the old server

What security measures do you have in place to protect my data in the cloud?

Hostman ensures 99.99% reliability per SLA, guaranteeing server downtime of no more than 52 minutes over a year. Additionally, we house our servers exclusively in Tier IV data centers, which comply with all international security standards.

 

How can I get started with Hostman's cloud services for my business?

Just sign up and select the solution that fits your needs. We have ready-made setups for almost any project: a vast marketplace for ordering servers with pre-installed software, set plans, a flexible configurator, and even resources for custom requests.

If you need any assistance, reach out to our support team. Our specialists are always happy to help, advise on the right solution, and migrate your services to the cloud — for free.

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us