<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: akintola abdulazeez oladele</title>
    <description>The latest articles on Forem by akintola abdulazeez oladele (@hayzedak).</description>
    <link>https://forem.com/hayzedak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1733666%2F2996b70e-ab24-4685-9c1a-49b47c0c0e53.jpg</url>
      <title>Forem: akintola abdulazeez oladele</title>
      <link>https://forem.com/hayzedak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hayzedak"/>
    <language>en</language>
    <item>
      <title>DevOpsFetch: A system monitoring and management tool.</title>
      <dc:creator>akintola abdulazeez oladele</dc:creator>
      <pubDate>Mon, 05 Aug 2024 09:20:58 +0000</pubDate>
      <link>https://forem.com/hayzedak/devopsfetch-a-system-monitoring-and-management-tool-36jf</link>
      <guid>https://forem.com/hayzedak/devopsfetch-a-system-monitoring-and-management-tool-36jf</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Welcome to &lt;strong&gt;&lt;em&gt;DevOpsFetch&lt;/em&gt;&lt;/strong&gt;, your new best friend in system monitoring and management. Picture this: You’re a DevOps engineer managing lot of servers, multitasking between Docker containers, Nginx configurations, and user activities. It’s a typical day, and you’re bombarded with requests for information—active ports, container statuses, and more. &lt;/p&gt;

&lt;p&gt;Instead of desperately typing away commands to gather this information, you can rely on DevOpsFetch to streamline the process. This script is designed to simplify your life by retrieving and logging various system information including active ports, Docker images and containers, Nginx domains, user logins, and system activities within a specified time range.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Script Does
&lt;/h2&gt;

&lt;p&gt;DevOpsFetch is a comprehensive script that automates the retrieval and logging of critical system information. Here’s a detailed breakdown of its functionality:&lt;/p&gt;

&lt;h3&gt;
  
  
  Log File Creation and Permissions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The script first checks if the log file (&lt;code&gt;/var/log/devopsfetch.log&lt;/code&gt;) exists.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it doesn’t exist, it creates the log file and sets the necessary permissions to allow writing.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOG_FILE="/var/log/devopsfetch.log"

#Create the log file if it doesn't exist and set permissions

if [ ! -f "$LOG_FILE" ]; then
    sudo touch "$LOG_FILE"
    sudo chmod 666 "$LOG_FILE"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Help Display:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Provides a help message (--help or -h) that shows usage instructions and available options.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;show_help() {
    echo "Usage: $0 [OPTIONS]"
    echo "Options:"
    echo "  -p, --port                List all active ports and services"
    echo "  -p &amp;lt;port_number&amp;gt;          Detailed information about a specific port"
    echo "  -d, --docker              List all Docker images and containers"
    echo "  -d &amp;lt;container_name&amp;gt;       Detailed information about a specific container"
    echo "  -n, --nginx               Display all Nginx domains and their ports"
    echo "  -n &amp;lt;domain&amp;gt;               Detailed configuration information for a specific domain"
    echo "  -u, --users               List all users and their last login times"
    echo "  -u &amp;lt;username&amp;gt;             Detailed information about a specific user"
    echo "  -t, --time &amp;lt;start&amp;gt; &amp;lt;end&amp;gt;  Display activities within a specified time range"
    echo "  --install                 Install dependencies and set up the service"
    echo "  -h, --help                Show this help message"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Logging Mechanism:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Defines a function (log_message) to log messages to the log file, ensuring all output is consistently recorded.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log_message() {
    echo "$1" | sudo tee -a "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Port Information:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lists all active ports and services (&lt;code&gt;--port&lt;/code&gt; or &lt;code&gt;-p&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides detailed information about a specific port (&lt;code&gt;-p &amp;lt;port_number&amp;gt;&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list_ports() {
    log_message "Listing all active ports and services..."
    netstat -tuln | sudo tee -a "$LOG_FILE"
}

port_details() {
    local port=$1
    log_message "Detailed information about port $port"
    netstat -tuln | grep ":$port" | sudo tee -a "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker Information:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lists all Docker images and containers (&lt;code&gt;--docker&lt;/code&gt; or &lt;code&gt;-d&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides detailed information about a specific container (&lt;code&gt;-d &amp;lt;container_name&amp;gt;&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list_docker() {
    log_message "Listing all Docker images..."
    docker images | sudo tee -a "$LOG_FILE"
    log_message "Listing all Docker containers..."
    docker ps -a | sudo tee -a "$LOG_FILE"
}

container_details() {
    local container_name=$1
    log_message "Detailed information about container $container_name..."
    docker inspect "$container_name" | sudo tee -a "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nginx Information:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Displays all Nginx domains and their ports (&lt;code&gt;--nginx&lt;/code&gt; or &lt;code&gt;-n&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides detailed configuration information for a specific domain (&lt;code&gt;-n &amp;lt;domain&amp;gt;&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;domain_details() {
    local domain=$1
    log_message "Detailed configuration for domain $domain..."
    grep -r "server_name $domain" /etc/nginx/sites-enabled/ | sudo tee -a "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  User Information:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lists all users and their last login times (&lt;code&gt;--users&lt;/code&gt; or &lt;code&gt;-u&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides detailed information about a specific user (&lt;code&gt;-u &amp;lt;username&amp;gt;&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list_users() {
    log_message "Listing all users and their last login times..."
    lastlog | sudo tee -a "$LOG_FILE"
}

user_details() {
    local username=$1
    log_message "Detailed information about user $username..."
    lastlog | grep "$username" | sudo tee -a "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Time-Range Activities:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Displays system activities within a specified time range (&lt;code&gt;--time &amp;lt;start&amp;gt; &amp;lt;end&amp;gt;&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filter_by_time_range() {
    local start_time=$1
    local end_time=$2
    log_message "Activities from $start_time to $end_time..."
    journalctl --since="$start_time" --until="$end_time" | sudo tee -a "$LOG_FILE"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Installs necessary dependencies and sets up the systemd service for DevOpsFetch (&lt;code&gt;--install&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ "$1" == "--install" ]; then
    log_message "Installing dependencies and setting up the service..."
    sudo apt-get update
    sudo apt-get install -y net-tools nginx docker.io
    sudo systemctl enable docker
    sudo systemctl start docker

    sudo tee /etc/systemd/system/devopsfetch.service &amp;gt; /dev/null &amp;lt;&amp;lt;EOL
[Unit]
Description=devopsfetch - DevOps System Information Retrieval Tool
After=network.target

[Service]
ExecStart=/home/hayzedak/HNG5/devopsfetch.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOL

    sudo systemctl daemon-reload
    sudo systemctl enable devopsfetch
    sudo systemctl start devopsfetch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Main Script Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Handles the different options provided by the user and calls the appropriate functions.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elif [ "$1" == "-p" ] || [ "$1" == "--port" ]; then
    if [ -n "$2" ]; then
        port_details "$2"
    else
        list_ports
    fi
elif [ "$1" == "-d" ] || [ "$1" == "--docker" ]; then
    if [ -n "$2" ]; then
        container_details "$2"
    else
        list_docker
    fi
elif [ "$1" == "-n" ] || [ "$1" == "--nginx" ]; then
    if [ -n "$2" ]; then
        domain_details "$2"
    else
        grep -r "server_name" /etc/nginx/sites-enabled/ | sudo tee -a "$LOG_FILE"
    fi
elif [ "$1" == "-u" ] || [ "$1" == "--users" ]; then
    if [ -n "$2" ]; then
        user_details "$2"
    else
        list_users
    fi
elif [ "$1" == "-t" ] || [ "$1" == "--time" ]; then
    if [ -n "$2" ] &amp;amp;&amp;amp; [ -n "$3" ]; then
        filter_by_time_range "$2" "$3"
    else
        echo "Please provide both start and end times in the format 'YYYY-MM-DD HH:MM:SS'"
    fi
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    show_help
else
    echo "Unknown option: $1"
    show_help
fi

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Installation and Configuration
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Fork or Clone This Repo&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fork or clone the DevOpsFetch repository to your local machine &lt;a href="https://github.com/Hayzedak/HNG5/tree/main" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can choose to place the script in the root directory &lt;code&gt;/usr/local/bin/devopsfetch.sh&lt;/code&gt; and set the ExecStart path to &lt;code&gt;/etc/systemd/system/devopsfetch.service&lt;/code&gt; or save the script to your desired location, such as &lt;code&gt;/home/hayzedak/HNG5/devopsfetch.sh&lt;/code&gt;, thereby making similar changes to the &lt;code&gt;ExecStart path&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Make the Script Executable&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To ensure the script can be executed, change its permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo chmod +x /path/to/devopsfetch.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Install Dependencies and Set Up the Service&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run the script with the --install option to install necessary dependencies and set up the systemd service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update the package list.&lt;/li&gt;
&lt;li&gt;Install net-tools, nginx, and docker.io.&lt;/li&gt;
&lt;li&gt;Enable and start the Docker service.&lt;/li&gt;
&lt;li&gt;Create a systemd service file for DevOpsFetch.&lt;/li&gt;
&lt;li&gt;Enable and start the devopsfetch service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Enable and Start the Service&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you need to enable and start the service manually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl daemon-reload
sudo systemctl enable devopsfetch
sudo systemctl start devopsfetch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Run the script with different options to retrieve specific system information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh [OPTIONS]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Options&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-p, --port&lt;/code&gt;: List all active ports and services.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p &amp;lt;port_number&amp;gt;&lt;/code&gt;: Detailed information about a specific port.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-d, --docker&lt;/code&gt;: List all Docker images and containers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-d &amp;lt;container_name&amp;gt;&lt;/code&gt;: Detailed information about a specific container.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-n, --nginx&lt;/code&gt;: Display all Nginx domains and their ports.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-n &amp;lt;domain&amp;gt;&lt;/code&gt;: Detailed configuration information for a specific domain.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-u, --users&lt;/code&gt;: List all users and their last login times.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-u &amp;lt;username&amp;gt;&lt;/code&gt;: Detailed information about a specific user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-t, --time &amp;lt;start&amp;gt; &amp;lt;end&amp;gt;&lt;/code&gt;: Display activities within a specified time range.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--install&lt;/code&gt;: Install dependencies and set up the service.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-h, --help&lt;/code&gt;: Show the help message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Examples&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;List all active ports and services:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --port
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-Get detailed information about a specific port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --port 80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;List all Docker images and containers:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --docker

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Get detailed information about a specific container:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --docker my_container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Display all Nginx domains and their ports:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Get detailed configuration information for a specific domain:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --nginx mydomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;List all users and their last login times:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Get detailed information about a specific user:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh -u username
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Display activities within a specified time range:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --time "2024-08-01 00:00:00" "2024-08-01 23:59:59"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Show the help message:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./devopsfetch.sh --help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Logging Mechanism
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Log File
&lt;/h3&gt;

&lt;p&gt;All output from the script is logged to &lt;code&gt;/var/log/devopsfetch.log&lt;/code&gt;. This log file is created and its permissions are set to ensure it can be written to by the script.&lt;/p&gt;

&lt;h3&gt;
  
  
  Viewing Logs
&lt;/h3&gt;

&lt;p&gt;To view the log file, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cat /var/log/devopsfetch.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  To continuously monitor the log file, use:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo tail -f /var/log/devopsfetch.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is a useful tool designed to make the life of a DevOps engineer easier by automating the retrieval and logging of important system information. Whether you're monitoring ports, managing Docker containers, configuring Nginx, or tracking user activities, it has you covered. Its simple yet effective logging mechanism ensures that all actions are recorded, providing a comprehensive audit trail for your system's activities.&lt;/p&gt;

&lt;p&gt;By automating these tasks, you can save valuable time and reduce the risk of human error, allowing you to focus on more critical aspects of your work. It is easily configurable and extensible, integrating seamlessly with your existing workflows.&lt;/p&gt;

&lt;p&gt;Feel free to reach out if you have any questions or need further assistance. Happy monitoring!&lt;/p&gt;

</description>
      <category>bashscript</category>
      <category>devops</category>
      <category>automation</category>
      <category>linux</category>
    </item>
    <item>
      <title>Messaging System with Flask App and RabbitMQ/Celery for Email Handling 🚀</title>
      <dc:creator>akintola abdulazeez oladele</dc:creator>
      <pubDate>Fri, 26 Jul 2024 10:04:05 +0000</pubDate>
      <link>https://forem.com/hayzedak/messaging-system-with-flask-app-and-rabbitmqcelery-for-email-handling-1id</link>
      <guid>https://forem.com/hayzedak/messaging-system-with-flask-app-and-rabbitmqcelery-for-email-handling-1id</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Welcome to the &lt;strong&gt;&lt;em&gt;Messaging System!&lt;/em&gt;&lt;/strong&gt;. This system is designed to handle two main tasks: sending emails and logging requests. It’s built using Flask for the web application and Celery for asynchronous task management, ensuring efficient and scalable processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python 3.12+&lt;/li&gt;
&lt;li&gt;Virtualenv&lt;/li&gt;
&lt;li&gt;RabbitMQ/Celery&lt;/li&gt;
&lt;li&gt;An SMTP email account&lt;/li&gt;
&lt;li&gt;Nginx&lt;/li&gt;
&lt;li&gt;Ngrok for external access&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Components:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;app.py&lt;/em&gt;&lt;/strong&gt;: This is the main entry point of our web application. It handles incoming HTTP requests, routes them appropriately, and interacts with the Celery tasks for sending emails or logging requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;tasks.py&lt;/em&gt;&lt;/strong&gt;: This file contains the Celery task for sending emails. It ensures that email sending is handled asynchronously, allowing the web application to remain responsive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Celery&lt;/em&gt;&lt;/strong&gt;: An asynchronous task queue/job queue that distributes tasks to multiple workers, making the system scalable and efficient.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  app.py 📄
&lt;/h3&gt;

&lt;p&gt;This file sets up the Flask web application. It includes routes to handle sending emails and logging requests.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Logging Setup&lt;/em&gt;&lt;/strong&gt;: Ensures the logging directory and log file exist.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Route Handling&lt;/em&gt;&lt;/strong&gt;: Defines a route (/) that can handle two types of requests: sendmail to send an email and talktome to log a message with a timestamp.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import logging
import time  # Import the time module
from flask import Flask, request
from tasks import send_email

app = Flask(__name__)

# Ensure logging directory exists
log_dir = "/var/log"
log_file = os.path.join(log_dir, "messaging_system.log")
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
if not os.path.exists(log_file):
    open(log_file, 'a').close()

logging.basicConfig(filename=log_file, level=logging.INFO)

@app.route('/')
def index():
    sendmail = request.args.get('sendmail')
    talktome = request.args.get('talktome')

    try:
        if sendmail:
            recipient_email = sendmail.replace('mailto:', '')
            send_email.delay(recipient_email)
            return "Email sent!"

        if talktome:
            logging.info(f"Talk to me request at {time.strftime('%Y-%m-%d %H:%M:%S')}")
            return "Logged time!"

        return "Welcome to the messaging system!"
    except Exception as e:
        logging.error(f"Error occurred: {e}")
        return "An error occurred. Check the logs for details.", 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  tasks.py 📄
&lt;/h3&gt;

&lt;p&gt;This file contains the Celery task for sending emails.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Celery Setup&lt;/em&gt;&lt;/strong&gt;: Configures Celery to use RabbitMQ as the message broker.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;send_email Task&lt;/em&gt;&lt;/strong&gt;: Defines a task to send an email asynchronously using SMTP.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from celery import Celery
from smtplib import SMTP_SSL
from email.mime.text import MIMEText

celery = Celery('tasks', broker='amqp://guest@localhost//')

@celery.task
def send_email(recipient_email):
    email_password = os.environ.get("EMAIL_PASSWORD")
    if not email_password:
        raise ValueError("EMAIL_PASSWORD not set")

    msg = MIMEText('This is a test email.')
    msg['Subject'] = 'Test Email'
    msg['From'] = 'akintola130@gmail.com'
    msg['To'] = recipient_email

    try:
        with SMTP_SSL('smtp.gmail.com', 465) as server:
            server.login('akintola130@gmail.com', email_password)
            server.sendmail('akintola130@gmail.com', recipient_email, msg.as_string())
        print("Email sent successfully!")
    except Exception as e:
        print(f"Failed to send email: {e}")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;1. Clone the Repository&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/hayzedak/HNG3.git
cd HNG3/messaging_system
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;2. Create and Activate a Virtual Environment&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m venv venv
source venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;3. Install Dependencies&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install Flask celery
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;4. Configure Environment Variable&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
Set the EMAIL_PASSWORD environment variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export EMAIL_PASSWORD=your_email_password
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to replace your_email_password with your actual email &lt;a href="https://myaccount.google.com/apppasswords" rel="noopener noreferrer"&gt;app password&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;5. Initialize RabbitMQ&lt;/em&gt;&lt;/strong&gt; 🐇&lt;br&gt;
Ensure that RabbitMQ is running on your system. You can start RabbitMQ with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start rabbitmq-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;6. Ensure Log Directory Exists&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
Ensure the &lt;code&gt;/var/log/messaging_system.log&lt;/code&gt; file exists and set the appropriate permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo touch /var/log/messaging_system.log
sudo chmod 666 /var/log/messaging_system.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;7. Configure Nginx&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
Install Nginx if it's not already installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt install nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an Nginx configuration file for your Flask application. For example, create &lt;code&gt;/etc/nginx/sites-available/messaging_system&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/messaging_system
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following configuration, remember to replace your_domain_or_ip with your actual IP address or domain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
    listen 80;
    server_name your_domain_or_ip;

    location / {
        proxy_pass http://your_domain_or_ip:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    error_log /var/log/nginx/messaging_system_error.log;
    access_log /var/log/nginx/messaging_system_access.log;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable the configuration by creating a symlink:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/messaging_system /etc/nginx/sites-enabled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test the Nginx configuration and restart Nginx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t
sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your Flask application should be accessible via your domain or IP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the Application 🚀
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Start the Flask Application&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember to activate the virtual environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Start the Celery Worker&lt;/em&gt;&lt;/strong&gt;: In another terminal, ensure you activate the virtual env.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;celery -A tasks worker --loglevel=info
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Exposing with ngrok 🌍
&lt;/h2&gt;

&lt;p&gt;To expose your local server to the internet using ngrok, follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Download and install ngrok from &lt;a href="//ngrok.com"&gt;ngrok.com&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start ngrok with the following command:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ngrok http 5000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Copy the generated ngrok URL and use it to access your Flask application from the internet.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To send an email, navigate to &lt;a href="http://your_ngrok_endpoint/?sendmail=mailto:recipient@example.com" rel="noopener noreferrer"&gt;http://your_ngrok_endpoint/?sendmail=mailto:recipient@example.com&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To log a request, navigate to &lt;a href="http://your_ngrok_endpoint/?talktome" rel="noopener noreferrer"&gt;http://your_ngrok_endpoint/?talktome&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Troubleshooting 🛠️&lt;/p&gt;

&lt;p&gt;If you encounter any issues, check the logs for more information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Flask application logs: &lt;code&gt;/var/log/messaging_system.log&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Celery worker logs: Run &lt;code&gt;sudo journalctl -u celery.service -f&lt;/code&gt; to view real-time logs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nginx logs: &lt;code&gt;/var/log/nginx/error.log&lt;/code&gt; and &lt;code&gt;/var/log/nginx/access.log&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By following these steps, you can set up and run your own messaging system with Flask and Celery. Enjoy! 🎉&lt;/p&gt;

</description>
      <category>python</category>
      <category>devops</category>
      <category>celery</category>
      <category>rabbitmq</category>
    </item>
    <item>
      <title>🚀 Deploying a 3-tier Application with Docker and Nginx Proxy Manager 🌐</title>
      <dc:creator>akintola abdulazeez oladele</dc:creator>
      <pubDate>Mon, 22 Jul 2024 08:11:31 +0000</pubDate>
      <link>https://forem.com/hayzedak/-deploying-a-3-tier-application-with-docker-and-nginx-proxy-manager-mj3</link>
      <guid>https://forem.com/hayzedak/-deploying-a-3-tier-application-with-docker-and-nginx-proxy-manager-mj3</guid>
      <description>&lt;p&gt;Welcome, tech enthusiast!. I’m excited to walk you through deploying a 3-tier application using Docker and Nginx Proxy Manager. If you've ever felt overwhelmed by the complexity of full-stack development, this guide is your friendly companion, designed to make the process smooth and straightforward. Let’s dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  📂 Project Structure
&lt;/h2&gt;

&lt;p&gt;Our project is neatly organized into two main directories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;frontend/&lt;/strong&gt;: Houses the ReactJS application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;backend/&lt;/strong&gt;: Contains the FastAPI application and PostgreSQL database integration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s the project structure for a quick reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/project-root
/frontend
Dockerfile
package.json
src/
public/
.env
/backend
Dockerfile
pyproject.toml
poetry.lock
app/
.env
docker-compose.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Initial Setup: Cloning the Repository
&lt;/h2&gt;

&lt;p&gt;First things first, let’s clone the project repository. Open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/Hayzedak/hng2.git
cd hng2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This repository serves as a demo application, perfect for interns or anyone looking to get their hands dirty with full-stack development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🖥️ Manual Deployment: Setting Up on Ubuntu&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 1: Install Prerequisites&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s ensure our system is up-to-date and ready for action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's install Git, PostgreSQL, Node.js, and npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install git postgresql nodejs npm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 2: Setting Up Poetry&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We’ll use Poetry for managing our Python dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -sSL https://install.python-poetry.org | python3 -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add Poetry to your system's PATH:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo 'export PATH="$HOME/.local/bin:$PATH"' &amp;gt;&amp;gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 3: Configuring PostgreSQL&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Set up PostgreSQL to match the configuration in the .env file located in the backend directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -u postgres psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE app;
CREATE USER app WITH ENCRYPTED PASSWORD 'changethis123';
GRANT ALL PRIVILEGES ON DATABASE app TO app;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands create a new database and user, and grant all privileges on the database to the user.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 4: Backend Setup&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the backend directory and install dependencies.&lt;/p&gt;

&lt;p&gt;Install necessary dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set up the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry run bash ./prestart.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the FastAPI server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry run uvicorn app.main:app --reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 5: Frontend Setup&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s get the frontend up and running.&lt;/p&gt;

&lt;p&gt;Install the necessary dependencies, and start the development server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update your .env files with your machine's IP address:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;VITE_API_URL=http://&amp;lt;your_ip&amp;gt;:8000&lt;/code&gt;&lt;br&gt;
&lt;code&gt;BACKEND_CORS_ORIGINS=http://&amp;lt;your_ip&amp;gt;:5173&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's test by going to the browser:&lt;/p&gt;

&lt;p&gt;http://:5173 =&amp;gt; Frontend&lt;/p&gt;

&lt;p&gt;http://:8000/api =&amp;gt; Backend&lt;/p&gt;

&lt;p&gt;http://:8000/docs =&amp;gt; Backend Docs&lt;/p&gt;

&lt;p&gt;http://:8000/redoc =&amp;gt; Backend Redoc&lt;/p&gt;

&lt;p&gt;http://:8080 =&amp;gt; Adminer&lt;/p&gt;

&lt;p&gt;http://:8090 =&amp;gt; Proxy Manager GUI&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;☁️ Deploying to EC2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Deploying our application to AWS EC2 involves a few additional steps.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 1: Clone Repository and Update .env Files&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;SSH into your EC2 instance and clone the repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/Hayzedak/hng2.git
cd hng2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update the .env files with your domain information:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;VITE_API_URL=https://&amp;lt;your_domain&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;BACKEND_CORS_ORIGINS=http://&amp;lt;your_domain&amp;gt;:5173, https://&amp;lt;your_domain&amp;gt;:5173&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 2: 🐳 Docker Setup&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Install Docker on your EC2 instance.&lt;/p&gt;

&lt;p&gt;Update the package list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install required packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add Docker's official GPG key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the Docker repository to APT resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update package list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install Docker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install docker-ce
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify Docker is properly installed and running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add user to Docker group:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo usermod -aG docker $USER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 3: Docker Compose Setup&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Install Docker Compose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')" /usr/local/bin/docker-compose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apply executable permission:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo chmod +x /usr/local/bin/docker-compose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify Docker Compose is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 4: Dockerfile Configuration&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s create Dockerfiles for our frontend and backend applications.&lt;/p&gt;

&lt;p&gt;Frontend Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Official Node.js runtime as a parent image
FROM node:20.15.0-alpine

# Set the working directory in the container
WORKDIR /app

# Copy the rest of the application code
COPY . /app
RUN npm install

# Make port 5173 available to the world outside this container
EXPOSE 5173

CMD ["npm", "run", "dev", "--", "--host"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Backend Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Official Python runtime as a parent image
FROM python:3.10

RUN set -xe

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 - --git https://github.com/python-poetry/poetry.git@master

ENV PATH="/root/.local/bin:$PATH"

# Confirm poetry version
RUN poetry --version

# Set up the working directory
WORKDIR /app

# Copy and install dependencies
COPY . /app

RUN poetry install

EXPOSE 8000

# Start the application
CMD ["bash", "-c", "poetry run bash ./prestart.sh &amp;amp;&amp;amp; poetry run uvicorn app.main:app --reload --host 0.0.0.0"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 5: Docker Compose Configuration&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Create a docker-compose.yml file to orchestrate our containers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.8'

services:
  nginx-proxy:
    image: jc21/nginx-proxy-manager:latest
    restart: always
    ports:
      - "80:80"
      - "8090:81"
      - "443:443"
    environment:
      DB_SQLITE_FILE: "/data/database.sqlite"
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt

  postgres-db:
    image: postgres:13
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: app
      POSTGRES_PASSWORD: changethis123
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

  backend-container:
    build: ./backend
    environment:
      DATABASE_URL: postgres://app:changethis123@postgres-db:5432/app
    depends_on:
      - postgres-db
    ports:
      - "8000:8000"

  frontend-container:
    build: ./frontend
    depends_on:
      - backend-container
    ports:
      - "5173:5173"

  adminer:
    image: adminer
    restart: always
    ports:
      - "8080:8080"
    environment:
      ADMINER_DEFAULT_SERVER: postgres-db

volumes:
  postgres_data:
    driver: local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 6: Build and Run Containers&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Run the following command to build and start your Docker containers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 7: Configure Nginx Proxy Manager&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Set up A records for your domain and sub-domains in Route53 on your AWS dashboard.&lt;/p&gt;

&lt;p&gt;Access Nginx Proxy Manager via &lt;code&gt;http://&amp;lt;your_domain:8090&amp;gt;&lt;/code&gt; and use the default credentials to log in.&lt;/p&gt;

&lt;p&gt;Configure proxy hosts for your frontend and backend on the same proxy. Map your domain name to the respective container service names and ports.&lt;/p&gt;

&lt;p&gt;Enable SSL certificates using Let's Encrypt and enforce SSL for secure connections.&lt;/p&gt;

&lt;p&gt;Configure the frontend to route API requests to the backend on the same domain, click on 'Advanced' on the frontend proxy host and paste this configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;location /api {
    proxy_pass http://backend:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

location /docs {
    proxy_pass http://backend:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

location /redoc {
    proxy_pass http://backend:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set up a different proxy host for your adminer and Nginx Proxy Manager. Map your subdomain name to the service name of respective containers and ports.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧪 Testing Your Application
&lt;/h2&gt;

&lt;p&gt;Congratulations! Your application is now deployed. You can access it via the following URLs:&lt;/p&gt;

&lt;p&gt;Frontend: &lt;code&gt;http://your_domain&lt;/code&gt;&lt;br&gt;
Backend: &lt;code&gt;http://your_domain/api&lt;/code&gt;&lt;br&gt;
Backend Docs: &lt;code&gt;http://&amp;lt;your_domain/docs&lt;/code&gt;&lt;br&gt;
Backend Redoc: &lt;code&gt;http://your_domain/redoc&lt;/code&gt;&lt;br&gt;
Proxy Manager GUI: &lt;code&gt;http://proxy.your_domain&lt;/code&gt;&lt;br&gt;
Adminer: &lt;code&gt;http://db.your_domain&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And there you have it! You've successfully deployed a 3-tier application with Docker and Nginx Proxy Manager. This journey might seem impossible at first, but with each step, you're building a robust understanding of full-stack development and deployment. Keep experimenting, keep learning, and most importantly, have fun!&lt;/p&gt;

&lt;p&gt;Feel free to reach out if you have any questions or need further assistance. Happy proxying!💻🎉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/t/nginx"&gt;#nginx&lt;/a&gt; &lt;a href="https://dev.to/t/linux"&gt;#linux&lt;/a&gt; &lt;a href="https://dev.to/t/docker"&gt;#docker&lt;/a&gt; &lt;a href="https://dev.to/t/ubuntu"&gt;#ubuntu&lt;/a&gt; &lt;a href="https://dev.to/t/postgres"&gt;#postgres&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Automating User and Group Management with Bash Script</title>
      <dc:creator>akintola abdulazeez oladele</dc:creator>
      <pubDate>Fri, 05 Jul 2024 08:51:47 +0000</pubDate>
      <link>https://forem.com/hayzedak/automating-user-and-group-management-with-bash-script-47ng</link>
      <guid>https://forem.com/hayzedak/automating-user-and-group-management-with-bash-script-47ng</guid>
      <description>&lt;p&gt;Managing user accounts and groups is a crucial job for system administrators, especially in environments where many new users are frequently added. Automating this process can save significant time and reduce the risk of human error. &lt;/p&gt;

&lt;p&gt;In this article, we will demonstrate how to automate user and group management using Bash script. This script will read a text file containing usernames and group names, create the users and groups as specified, set up home directories, generate random passwords, and log all actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Script Overview
&lt;/h2&gt;

&lt;p&gt;The script &lt;strong&gt;&lt;code&gt;create_users.sh&lt;/code&gt;&lt;/strong&gt; performs the following tasks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Reads a text file where each line contains a username and a list of groups, separated by a semicolon (&lt;strong&gt;&lt;code&gt;;&lt;/code&gt;&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creates a personal group for each user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creates user accounts with their respective personal groups.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adds users to additional groups as specified.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sets up home directories with appropriate permissions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generates random passwords for each user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Logs all actions to &lt;strong&gt;&lt;code&gt;/var/log/user_management.log&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stores the generated passwords securely in &lt;strong&gt;&lt;code&gt;/var/secure/user_passwords.txt&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Ensure you have the necessary permissions to create users, groups, and modify system files. The script needs to be executed with superuser privileges.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bash Script: &lt;a href="https://github.com/Hayzedak/HNG1/blob/main/create_users.sh"&gt;create_users.sh&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Preparing the Input File
&lt;/h2&gt;

&lt;p&gt;Create a text file named &lt;strong&gt;&lt;code&gt;user_list.txt&lt;/code&gt;&lt;/strong&gt; with the following format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;azeez;developers,admins
hng;developers
nora;admins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each line contains a username and a list of groups separated by a semicolon (&lt;strong&gt;&lt;code&gt;;&lt;/code&gt;&lt;/strong&gt;). Multiple groups are separated by commas (&lt;strong&gt;&lt;code&gt;,&lt;/code&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the Script
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Make the Script Executable:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo chmod +x create_users.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execute the Script:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo ./create_users.sh user_list.txt&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying the Script Execution
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Check the Log File:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo cat /var/log/user_management.log&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check the Password File:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo cat /var/secure/user_passwords.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Verify User Accounts:&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
&lt;code&gt;cut -d: -f1 /etc/passwd | grep -E 'azeez|hng|nora'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verify Group Membership:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;groups azeez
groups hng
groups nora
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Automating user and group management with a Bash script can enhance the efficiency and accuracy of administrative tasks. This script provides solution for creating users, managing group memberships, setting up home directories, and ensuring secure password handling. By following this guide, system administrators can save time and reduce errors, particularly in environments with frequent user account changes.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>devops</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
