Categories
Server

Add urldecode to your command line.

So, here’s the deal:

alias urldecode='sed "s@+@ @g;s@%@\\x@g" | xargs -0 printf "%b"'

This command is an alias—a shorthand that saves me from typing a long series of commands every time I need to decode a URL. Think of it as me saying, “Yo terminal, whenever I type urldecode, just run this whole thing for me.” But what does it actually do?

The Problem We’re Solving

You’ve seen those ugly URLs with %20 for spaces or %3A for colons? Yeah, decoding those by hand is no fun. So, instead of decoding each character one by one, I use this alias to automate it.

Here’s the breakdown:

  1. sed "s@+@ @g;s@%@\\\\x@g"
    • sed is a stream editor that we use to make text transformations.
    • First, this part: s@+@ @g replaces every + in the URL with a space (@ is just a delimiter here, replacing the default /). Because in URLs, + is used for spaces. Handy, right?
    • Then, we’ve got s@%@\\\\x@g, which is the magical part that takes all those %xx patterns (URL encoding) and transforms them into something printf can understand. Specifically, \\\\x turns each %xx into \x, which is the escape code format for hex in printf.
  2. xargs -0 printf "%b"
    • This takes the output from sed and passes it to printf. The xargs -0 part ensures that the input is handled correctly, particularly if there are null characters. It’s super useful if your URL is complex or has tricky characters in it.
    • printf "%b" is where the actual decoding magic happens. %b tells printf to interpret backslash escapes, so our \x becomes an actual decoded character. Ta-da!

So What Happens When You Run It?

When you run something like this:

echo "https%3A%2F%2Fexample.com%2Fsearch%3Fq%3Dnyc+coffee" | urldecode

It gets transformed from this gobbledygook:

https%3A%2F%2Fexample.com%2Fsearch%3Fq%3Dnyc+coffee

To this nice, human-readable format:

https://example.com/search?q=nyc coffee

Geeky Tips:

  • If you’re regularly working with URLs and encoded strings (maybe you’re doing some web scraping or API work?), this alias can save you tons of time.
  • You can add this to your ~/.bashrc or ~/.zshrc to have it always ready when you open a terminal. Just paste the line there and run source ~/.bashrc (or source ~/.zshrc).
Categories
Server

A Quick Comparison: Ubuntu, Debian, and CentOS in 2024

In the world of Linux distributions, choosing the right operating system can be tricky, especially with so many options available. Ubuntu, Debian, and CentOS have been three of the most widely used Linux distros for years, each offering something unique. Let’s take a look at how the latest versions of these distros compare in 2024.

1. Ubuntu

Latest Version: Ubuntu 24.04 LTS (April 2024)

Ubuntu is known for its user-friendliness and frequent updates. It’s one of the most popular Linux distributions globally, especially among desktop users and developers. Canonical, the company behind Ubuntu, focuses on delivering an OS that’s easy to install, highly polished, and reliable.

  • Ease of Use: Ubuntu shines in terms of ease of use, with a clean graphical interface (GNOME) and a large software repository. Its LTS (Long Term Support) versions are perfect for users who want a stable environment.
  • Release Cycle: Ubuntu follows a predictable release cycle, with new versions every six months and LTS releases every two years. The 24.04 LTS is supported for five years, providing security updates and bug fixes.
  • Target Audience: Ideal for desktop users, developers, and small to mid-sized businesses. It’s also a go-to for cloud deployments, thanks to its integration with tools like Juju and MAAS.
  • Software and Package Management: Ubuntu uses the APT package manager and supports both .deb packages and Snap packages. Snap has grown significantly, allowing for easier installation of newer software versions across multiple distros.
  • Community and Support: One of Ubuntu’s strengths is its active community and the large amount of documentation available. Users can also opt for paid support from Canonical if needed.

2. Debian

Latest Version: Debian 12 “Bookworm” (June 2023)

Debian is often regarded as the foundation for many Linux distributions, including Ubuntu itself. It’s known for its stability, making it a favorite for servers and other mission-critical systems.

  • Stability: Debian is rock solid. It prioritizes stability over having the latest and greatest software, making it an excellent choice for servers where reliability is more important than new features.
  • Release Cycle: Debian doesn’t have a fixed release schedule. New versions come out when they are ready, typically every two to three years. Debian 12 was released in June 2023 and, as always, will receive long-term support.
  • Target Audience: Best suited for system administrators, experienced users, and server environments. Debian’s stability makes it popular in enterprise environments, but its learning curve might be steeper for beginners.
  • Software and Package Management: Like Ubuntu, Debian uses the APT package manager. However, it doesn’t use Snap, so you’ll primarily rely on .deb packages and the extensive official repository.
  • Community and Support: Debian’s community is large and very involved in development. There’s plenty of documentation and community support available, but you won’t find the same level of commercial support as with Ubuntu unless you go through a third party.

3. CentOS (Stream)

Latest Version: CentOS Stream 9

CentOS used to be the go-to Linux distribution for enterprises and data centers looking for a free, community version of Red Hat Enterprise Linux (RHEL). However, things changed when CentOS Stream was introduced, shifting the distribution’s role from a downstream clone of RHEL to an upstream version—a rolling release.

  • Rolling Release Model: CentOS Stream now follows a rolling release model, meaning it gets updates before RHEL but can be less stable as it’s essentially a preview of what’s coming in RHEL. For users looking for a rock-solid system, this has made CentOS less appealing for critical production systems.
  • Target Audience: CentOS Stream is more suitable for developers and users who want to stay on the cutting edge of RHEL developments. It’s often used in testing and development environments before changes are incorporated into RHEL.
  • Package Management: CentOS uses the YUM/DNF package manager, similar to RHEL, and supports RPM packages. It’s closely tied to the Red Hat ecosystem, so you get compatibility with RHEL software and tools.
  • Community and Support: While the community around CentOS is still strong, the transition to CentOS Stream has led many to move to other RHEL-based distros like AlmaLinux or Rocky Linux for production use. CentOS Stream is supported by Red Hat, but not to the same level as RHEL itself.

Which One Should You Choose?

  • Ubuntu: Great for those who want a modern desktop experience, easy cloud integration, or frequent updates. Perfect for developers, startups, and small businesses.
  • Debian: Best for those who value stability and reliability over cutting-edge software. Ideal for servers, experienced users, and enterprises that need rock-solid performance.
  • CentOS (Stream): Suitable for developers working with the Red Hat ecosystem and those who don’t mind using a rolling release system. However, it’s no longer as ideal for production systems as it once was, making alternatives like AlmaLinux or Rocky Linux worth considering.

In summary, Ubuntu remains the easiest to use and most accessible to newcomers, while Debian is for those seeking long-term reliability, and CentOS Stream fits the niche of developers working closely with the RHEL ecosystem. Each distro has its strengths, so the best choice depends on your needs!

Categories
Server

How to use Vim for Global Text Replacement

If you’re working in Vim, one of the most powerful text editors, you might often need to replace multiple instances of a string throughout a file. Vim offers a handy way to perform global text substitutions with the following command:

%s/old-text/new-text/g

What Does This Command Do?

  • :: Enters command mode in Vim, allowing you to execute commands.
  • %: Refers to the entire file. It tells Vim to search through all lines of the file. Without %, Vim would only search the current line.
  • s: Stands for “substitute,” which is Vim’s command for search and replace.
  • old-text: The text string you want to search for and replace.
  • new-text: The text string you want to use as the replacement.
  • g: This flag stands for “global,” meaning Vim will replace all instances of old-text on each line. Without the g flag, only the first occurrence on each line would be replaced.

Example Use Case

Suppose you have a file with many occurrences of the word “apple” and want to change them all to “orange.” You would run:

%s/apple/orange/g

This command will go through the entire file and replace every occurrence of “apple” with “orange.”

Categories
Server

How to Use the wget Command to Retrieve Your Public IP Address

If you’ve ever needed to find your public IP address quickly from the command line, one handy method is using the wget command along with a simple web request. Here’s how you can do it:

wget http://ipinfo.io/ip -qO -

What Does This Command Do?

Let’s break down the components:

  • wget: A powerful command-line utility used for downloading files from the web.
  • http://ipinfo.io/ip: This URL returns your public IP address in plain text when accessed.
  • -qO -: The -q flag makes the output “quiet,” meaning it suppresses all non-essential messages from wget. The O - flag tells wget to send the output to the standard output (your terminal) instead of saving it to a file.
Categories
Server

Installing PHP 8.2, Nginx, and PHP-FPM on Debian 12: A Step-by-Step Guide

This guide will walk you through the installation and configuration of PHP 8.2, Nginx, and PHP-FPM on Debian 12. This setup is ideal for those running web applications that require modern PHP and the performance advantages of Nginx with PHP-FPM (FastCGI Process Manager).

Prerequisites

  • A server running Debian 12.
  • Root or a user with sudo privileges.
  • Basic familiarity with the command line.

Step 1: Update and Upgrade Your System

Before starting the installation process, it is essential to update your system’s package list and upgrade any outdated packages.

sudo apt update && sudo apt upgrade -y

This ensures your system is running the latest security patches and software updates.

Step 2: Install Nginx

Nginx is a powerful, high-performance web server and reverse proxy. To install Nginx on Debian 12, run the following command:

sudo apt install nginx -y

Once the installation is complete, enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

To verify that Nginx is running, you can check the service status:

sudo systemctl status nginx

Additionally, you can test Nginx by visiting your server’s IP address in a browser. You should see the default Nginx welcome page.


Step 3: Install PHP 8.2

Debian 12 does not have PHP 8.2 in its default repositories, so you’ll need to add an external repository to install it. The SURY repository is a popular and trusted source for the latest PHP versions.

First, add the necessary dependencies:

sudo apt install ca-certificates apt-transport-https software-properties-common -y

Then, import the SURY repository’s GPG key:

sudo wget -qO /etc/apt/trusted.gpg.d/sury.gpg https://packages.sury.org/php/apt.gpg

Now, add the PHP repository:

sudo add-apt-repository "deb https://packages.sury.org/php/ $(lsb_release -sc) main"

Update the package list to include the newly added repository:

sudo apt update

Finally, install PHP 8.2 along with commonly used extensions:

sudo apt install php8.2 php8.2-fpm php8.2-cli php8.2-mbstring php8.2-xml php8.2-mysql php8.2-curl php8.2-zip -y

Step 4: Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) is responsible for handling PHP requests from Nginx. After installing PHP-FPM, you need to ensure that the service is enabled and running:

sudo systemctl enable php8.2-fpm
sudo systemctl start php8.2-fpm

To verify that PHP-FPM is running:

sudo systemctl status php8.2-fpm

Step 5: Configure Nginx to Use PHP-FPM

Now that PHP-FPM is installed and running, you need to configure Nginx to handle PHP requests via PHP-FPM.

First, edit the Nginx server block configuration file. For simplicity, we’ll modify the default server block, but you can create custom server blocks for specific websites or applications.

Open the default Nginx configuration file:

sudo nano /etc/nginx/sites-available/default

Locate the location ~ \.php$ { block and modify it as follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Make sure to adjust the root directive (/var/www/html) if your website files are located elsewhere. The fastcgi_pass directive should point to the correct PHP-FPM socket.

Save and close the file. Then, test the Nginx configuration for syntax errors:

sudo nginx -t

If no errors are reported, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 6: Verify PHP-FPM with Nginx

To verify that PHP is correctly configured with Nginx and PHP-FPM, create a simple PHP info file.

sudo nano /var/www/html/info.php

Add the following PHP code:

<?php
phpinfo();
?>

Save and close the file.

Now, visit http://your_server_ip/info.php in a browser. If everything is set up correctly, you will see a page displaying detailed PHP information.


Step 7: Secure Your Installation

After confirming that PHP is working, it’s important to remove or restrict access to the info.php file as it exposes detailed information about your server configuration:

sudo rm /var/www/html/info.php

Additionally, consider further securing your Nginx and PHP-FPM setup by:

  • Setting up firewalls (e.g., ufw) to allow only necessary traffic.
  • Using SSL certificates (e.g., via Let’s Encrypt) to enable HTTPS for your websites.
  • Configuring fail2ban to protect against brute-force attacks.

Conclusion

You’ve successfully installed PHP 8.2, Nginx, and PHP-FPM on Debian 12. This setup provides a robust foundation for running PHP-based web applications with modern, efficient software. By following this guide, you’ve ensured that your web server is configured with the latest PHP version and optimized for performance with Nginx and PHP-FPM.

You can now start deploying your applications or further fine-tune the configuration to meet your specific requirements.

Categories
Server

Server Maintenance Tip: Keep Your Logs in Check!

One crucial aspect of server management is maintaining your log files.

Over time, log files can accumulate and consume valuable disk space, potentially impacting your server’s performance.

To avoid this, regularly monitor and clean up old logs.

Implementing tools like logrotate can help automate this process, ensuring that your logs remain manageable and your server continues to run smoothly!

Categories
Server

Monitor Your Server’s Performance for Optimal Operation

To keep your server running smoothly, regular monitoring of its performance and resource usage is crucial.

Here’s why and how you should incorporate this practice:

Why Monitoring Matters

Servers handle critical tasks, and any issues with performance can lead to slowdowns, crashes, or even data loss. Monitoring helps you keep track of key metrics like CPU usage, memory consumption, disk I/O, and network activity. This proactive approach allows you to identify and resolve potential problems before they become severe.

How to Monitor Your Server

  1. Real-Time Monitoring
    Tools like top or htop provide real-time insights into your server’s performance, showing active processes and their resource usage. These tools help you quickly spot any immediate issues.
  2. Resource Usage Analysis
    For more detailed information, consider using iostat to understand disk performance and vmstat for data on system memory and processes. These tools give you a deeper look into your server’s resource utilization.
  3. Automated Monitoring Solutions
    To enhance your monitoring strategy, implement automated tools such as Nagios, Zabbix, or Prometheus. These solutions continuously track your server’s health, alert you to anomalies, and help analyze performance trends over time.

By integrating these monitoring practices into your server management routine, you’ll ensure better reliability and performance, ultimately leading to a more stable and efficient server environment.

Categories
Server

How to Check the Status of Your ZFS Pool

Monitoring your ZFS pool is crucial for maintaining the health and performance of your storage system. Luckily, ZFS provides simple yet powerful commands to help you keep track of your pool’s status.

1. Check Overall Status

To get a comprehensive overview of your ZFS pool’s health, open your terminal and run:

zpool status

This command will display the status of all your pools, highlighting any issues with disk health, errors, or pool integrity. If everything is functioning well, you’ll see “healthy” in the output. Any warnings or errors will be clearly indicated.

2. View Pool Usage

Curious about how much space you’re using or have left? Use:

zpool list

This command provides a snapshot of your pool’s space utilization, including total size, used space, and available space. It’s a quick way to check if you’re nearing capacity.

3. Detailed Health Report

For a more detailed report, especially useful if you suspect issues or want to check the status of a scrub, use:

zpool status -v

This command includes verbose output, detailing individual disk errors, any ongoing scrubs, and more. It’s perfect for deep diagnostics and ensuring your pool is in top shape.

By regularly checking your ZFS pool with these commands, you can prevent potential problems and ensure your data remains safe and accessible.

Categories
Server

Missed your train?

No worries!, I have a solution for you!.

First, (On ubuntu) do;
apt-get install sl

Now, you can have a train pick you up anytime you want!


If you do sl -l you’ll call a train with all the passengers abord!
If you prefer a short version, just do sl or sl -h

Have fun!

Categories
Server

Unix: Count number of files in a folder

You have run out of inodes on your server and you have no idea where all those files are?

The following command will tell you the number of files of each directory so you can spot where the hell they are.

for i in /home/*; do echo $i; find $i | wc -l; done

When you see a directory with lots files, you can continue examining its subdirectories by calling again the command on it.

for i in /home/thisone/*; do echo $i; find $i | wc -l; done