megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Configure XDebug in Visual Studio Code

For the past 6 months, I've been writing code in C# 11 and .NET 7 Core and of course, using the amazing Visual Studio 2022 IDE was quite an amazing experience especially when it comes to debugging your code.

Recently, I had to switch back to classic ol' PHP and now, I wanted a similar debugging experience in Visual Studio Code.

Using XDebug and if configured correctly, it'll be quite useful in your debugging journey instead of using var_dump() or dd() your variables everytime.

Prerequisites

The only thing you need here is Visual Studio Code and ensure that it's the latest version. I'm writing this from a Windows Machine using the latest version of WAMP Server.

1. Download PHP Debug

The official XDebug team has released an extension that could be installed in your editor. So, here you go, first download it.

2. Install XDebug

In the previous step, we just installed the extension for the editor but that's just an adapter used between the editor and XDebug. Whereas, XDebug is a PHP extension that needs to be installed on your local system.

I'm not going to show you the installation process as this documentation explains that well enough and you can follow it based on your operating system of choice.

3. PHP Configuration

After you're done with installing the extension, open your php.ini file and add the following line in this file:

zend_extension=path/to/xdebug

Now, it's time to enable remote debugging. Depending on your XDebug version:

For XDebug v3.x.x:

xdebug.mode = debug
xdebug.start_with_request = yes

For XDebug v2.x.x:

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000

If you are looking for more specific options, please see read the documentation on remote debugging. Please note that the default port in XDebug v3.x.x has been changed from 9000 to 9003.

Once done, restart your PHP service and check if the configuration has taken effect. You can do this by creating a simple test.php with a phpinfo(); statement in there and look for your XDebug extension under the Loaded Configuration File section.

4. Visual Studio Code Configuration

In your editor, hold CTRL + SHIFT + P and type "Debug: Add Configuration" and it'll prompt you to select your language of preference and this case, of course, you should "PHP".

Upon selecting, it'll automatically generate a launch.json file in your .vscode folder in your project folder.

There'll be three different configurations in your file:

  • Listen for XDebug: This setting will simply start listening on the specified port (by default 9003) for XDebug.
  • Launch currently open script: It will launch the currently opened script as a CLI, show all stdout/stderr output in the debug console and end the debug session once the script exits.
  • Launch Built-in web server: This configuration starts the PHP built-in web server on a random port and opens the browser with the serverReadyAction directive.

Now, to test if it works, hit F5 and add a breakpoint in your PHP code and if it lands on your breakpoint, that means it works fine.

Hope you found this article useful!

References

Generate CSR with multiple subdomains using OpenSSL

I have done this many times but I never got to write about it and I felt that I should write about it after I helped a friend of mine with this.

Prerequisites

It doesn't matter which operating system you're going to do this on as long as you have openssl installed in your system, you should be fine.

All you have to do is just create a simple configuration file and give it a name like multiple-domains.cnf:

[req]
default_bits       = 2048
distinguished_name = req_distinguished_name
req_extensions     = req_ext

[req_distinguished_name]
countryName         = [COUNTRYCODE]
stateOrProvinceName = [STATE]
localityName        = [LOCALITY]
organizationName    = [ORGANIZATION]
commonName          = website.com

[req_ext]
subjectAltName = @alt_names

[alt_names]
DNS.1 = website.org
DNS.2 = wesbite.ae
DNS.3 = wesbite.co.uk

Save the file and copy-paste this into your command prompt or terminal:

openssl req -out <your-csr-name>.csr -newkey rsa:2048 -nodes -keyout <your-private-key-name>.key -config multiple-domains.cnf

Once executed, it should be generated for you and if not, verify your configuration file for any typos.

Hope this helps you out!

List tables that don't have primary keys in a MySQL database

Whenever I restore a database dump on a MySQL InnoDB Cluster, most of the time, the error I face is related to missing PRIMARY KEY attribute because as per MySQL's official documentation:

Every table that is to be replicated by the group must have a defined primary key, or primary key equivalent where the equivalent is non-null unique key.

What I would usually do is, run a simple query to determine the list of the tables that don't the PRIMARY KEY attribute:

select tab.table_schema as database_name,
       tab.table_name
from information_schema.tables tab
left join information_schema.table_constraints tco
          on tab.table_schema = tco.table_schema
          and tab.table_name = tco.table_name
          and tco.constraint_type = 'PRIMARY KEY'
where tco.constraint_type is null
      and tab.table_schema not in('mysql', 'information_schema', 'performance_schema', 'sys')
      and tab.table_type = 'BASE TABLE'
      and tab.table_schema = 'your_database_name_here'
order by tab.table_schema,
         tab.table_name;

Hope you found this tip useful!

Playing around with ImageMagick

ImageMagick is not a new tool for me but I haven't really talked about how it comes in handy whenever I wanted to resize and/or compress images efficiently using the terminal instead of using some image conversion service found for free online.

To get started, make sure you install ImageMagick on your machine. This is the command I use for Fedora Linux:

sudo dnf install ImageMagick

For example, if you wanted to compress an image's quality to 80%, here's you can do it:

convert -quality 80% src.png dest.png

Great! What if you want to do it for hundreds of images in a directory, then you can easily do something like this:

find . -type f -name '*.png' -exec bash -c 'convert -quality 80% "$1" "${1%}_compressed.png" ' bash  {} \;

You can do the same to resize the images while keeping original aspect ratio intact:

convert -resize 450x450 src.png dest.png

And similar to the one above, you can do the same for multiple images like so:

find . -type f -name '*.png' -exec bash -c 'convert -resize 450x450 "$1" "${1%}_resized.png" ' bash  {} \;

I have just scraped the surface of this image manipulation tool and if you are interested to know more, read the official manpage on how to use it.

Hope you found this tip useful!

Install Cisco Anyconnect Client on Fedora Linux

Cisco Anyconnect Client is a SSL VPN Client with VPN functionalites that enables an organization to secure its endpoints. Although, I use it a lot at office, I have only used it on Windows 10 so far.

One day, when I was on my vacation, I had to connect to a server remotely but this time, I tried doing it via Linux and it worked well!

Download Cisco AnyConnect Client for Linux

Fortunately, the client is available for Windows, macOS and Linux. You can go to the Downloads page and download the tarball archive for Linux.

After downloading, you can extract the tarball file:

tar -xvf anyconnect-linux64-4.10.00093-predeploy-k9.tar.gz

Install Cisco AnyConnect Client for Linux

Now that the file has been extracted, you can navigate to the vpn directory in anyconnect-linux64-* directory:

cd anyconnect-linux64-*/vpn/

Execute the vpn_install.sh script:

sudo ./vpn_install.sh

Follow the instruction steps and you're ready to use it.

Connect to VPN via Terminal

The GUI version was crashing on both KDE and GNOME desktop environments. Whereas, it was a smooth experience when connecting via the Terminal.

For easy access, you can create an alias in your .bashrc file:

alias anyconnectvpn="/opt/cisco/anyconnect/bin/vpn"

Save and restart your terminal for the changes to take effect.

After that, you can connect to VPN like this:

anyconnectvpn -s connect <IP_ADDRESS>

Once executed, you'll be prompted to enter your credentials and if all goes fine, you should be connected.

Disconnect VPN via the Terminal

You can execute the following command to disconnect:

anyconnectvpn -s disconnect <IP_ADDRESS>

Configuring your VPN connection

Sometimes, it can be tedious to write these commands all the time especially when you're in a rush, this can really get in your way.

To solve that, you can easily store your credentials in a file like .my_vpn_creds in your home directory:

y
<username>
<password>

Save the file and create another file named my_vpn_connect.sh:

#!/bin/bash

VPN_SERVER="<IP_ADDRESS>"

echo "Connecting to VPN.."
/opt/cisco/anyconnect/bin/vpn -s  < ~/.my_vpn_creds connect ${VPN_SERVER}

Save the file and make the file executable:

sudo chmod +x my_vpn_connect.sh

Now, you'll be connected to the VPN automatically by just executing this script:

./my_vpn_connect.sh

This saved me a lot of time and I hope it does the same for you as well! 🤗

Hope you found this article useful!

Setup a virtual environment for Python development

virtualenv is a tool used that allows you to install and isolate Python packages that are specific to your project rather than installing them globally on your system, which might, at some point, break other tools or services in your system.

In this simple guide, I'll show you how to install and create your own Python virtual environment for development.

Before you begin, please make sure that you have installed Python 3 or greater on your system.

Install virtualenv

Using the pip tool, you can execute the following command:

sudo pip install virtualenvwrapper

Then you need to add the following lines to your .bashrc startup file (found in the home directory).

These lines determine where your virtual environments should live, the location of the script installed and the location of the development project directories:

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 '
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

Next, restart the startup file:

source .bashrc

Once done, it'l execute a bunch of scripts for a few minutes and once the installation is successful, you can start using the mkvirtualenv command to create your virtual environment.

Creating a virtual environment

Really, it's quite trivial to create a virtual environment like this:

mkvirtualenv my_virtual_environment

Once done, you'll notice that your terminal looks something like this:

(my_virtual_environment) user@desktop:~$

This indicates that you are inside the virtual environment and can begin installing your Python packages and begin developing your application.

Using the virtual environment

There are a few commands that I'd like to share (and you should also know) while working with a virtual environment:

  • deactivate — get out of the current virtual environment
  • workon — list available virtual environments on the system
  • workon name_of_environment — activate the specified virtual environment
  • rmvirtualenv name_of_environment — remove the specified virtual environment

Conclusion

If you are a Python developer or write code in Python, then virtual environments are super handy and disposable especially while testing and working on different projects.

Unless, your project is package-dependant, you don't really need to isolate your development environment.

Hope you found this article useful!

Install TeamViewer on Fedora Linux 36/37

If you are using Fedora Linux or any other Linux distribution and want a short guide on how to install TeamViewer on your workstation, then this article is for you.

Usually, I use it access remote servers or my computer at work but I myself never did it on a Linux workstation. It worked out for me, so it should work for you too!

1. Download TeamViewer RPM Package

You can find the latest release on TeamViewer's official site.

Make sure that you have install wget on your system to easily download it via the Terminal:

wget https://download.teamviewer.com/download/linux/teamviewer.x86_64.rpm

2. Download minizip module

During installation, it failed for me as I was missing a certain libminizip.so module. So, I looked up online and found the module and downloaded it on my workstation:

wget https://rpmfind.net/linux/fedora/linux/updates/36/Everything/x86_64/Packages/m/minizip-compat-1.2.11-33.fc36.x86_64.rpm

Once the download is complete, you can install it:

rpm -i minizip-compat-1.2.11-33.fc36.x86_64.rpm

3. Install TeamViewer

Now, the final step is to install the RPM package for TeamViewer:

rpm -i teamviewer_15.37.3.x86_64.rpm

Hope you found this short guide useful!

Connect to a Remote Windows machine from a Linux machine

Last week, my Windows machine was down and I was struggling to connect to a remote server as I'm using Fedora Linux at home.

I tried installing and using GNOME Connections but it crashed and didn't work well for me. I found another tool named rdesktop which was stable and worked well for my case.

Installing it is as easy as typing the following command using the dnf package manager:

sudo dnf install rdesktop

Now, you can connect to your remote desktop machine using the following command:

rdesktop -d <domain> -u <username> -p - <ipaddress>

Upon execution of this command, you'll be prompted to enter the password and after that, you're all good to go!

Hope you found this tip useful!