How to use Match in PHP 8

In PHP 8, the match expression was introduced as a more concise and powerful alternative to the switch statement. The match expression allows you to perform pattern matching against a value and execute code based on the matched pattern.

Here’s the basic syntax of the match expression:

match ($value) {
    pattern_1 => expression_1,
    pattern_2 => expression_2,
    // more patterns and expressions...
    pattern_n => expression_n,
    default => expression_default
};

Here’s how it works:

  • The $value is the expression that you want to match against.
  • pattern_1, pattern_2, and pattern_n are the patterns you want to match against the value. These can be literal values, constants, or expressions.
  • expression_1, expression_2, and expression_n are the expressions that will be executed if the corresponding pattern matches.
  • default is an optional keyword that specifies the default case when none of the patterns match. expression_default is the expression executed in the default case.

The match expression evaluates the value and compares it against each pattern in order. If a pattern matches, the corresponding expression is executed, and the match expression completes. If none of the patterns match, the default case (if provided) is executed. If no default case is provided and no pattern matches, a MatchError is thrown.

Here’s an example to illustrate the usage of the match expression:

$result = match ($value) {
    0 => 'Zero',
    1 => 'One',
    2, 3 => 'Two or Three',
    4, 5, 6 => 'Four, Five, or Six',
    default => 'Other'
};

In this example, the value of $value is matched against different patterns, and the corresponding expressions are executed based on the match. The result is assigned to the variable $result.

The match expression in PHP 8 provides a more expressive and concise way to perform pattern matching, making your code easier to read and maintain.

How to disable window move with alt + left mouse button in XFCE

If you go to the Xfce menu and choose settings > settings manager > window manager tweaks > accessibility, you can select another key instead of alt to move the windows or choose none at all. This should be just what you want.

You can also use xfconf-query to set easy_click to none (or another key) on the command-line:

xfconf-query -c xfwm4 -p /general/easy_click -s none

In some programs, like Photoshop Alt + left mouse key used to modify some data and it is very important to disable window move with alt + left mouse button in XFCE for correct work of programs.

Ventoy – open source tool to create bootable USB drives

Ventoy is an open source tool to create a bootable USB drive for ISO/WIM/IMG/VHD(x)/EFI files.

With ventoy, you don’t need to format the drive again and again, you just need to copy the ISO/WIM/IMG/VHD(x)/EFI files to a USB flash drive and load them directly.

You can copy many files at the same time, and ventoy will give you a boot menu to select them.

Official WebSite: https://www.ventoy.net/en/index.html

How to enable remote connections to MySQL server

Enabling remote connections to a MySQL server involves a few steps:

1. Configure MySQL Server

By default, MySQL server is configured to listen to the loopback IP address 127.0.0.1, which means it only accepts connections from the local machine. To allow remote connections, you need to modify the MySQL configuration file (my.cnf or my.ini or 50-server.cnf), typically located in /etc/mysql/ or /usr/local/mysql/etc/ or /etc/mysql/mariadb.conf.d.

Look for the following line in the configuration file:

bind-address = 127.0.0.1

And change it to:

bind-address = 0.0.0.0

This allows the MySQL server to listen on all available network interfaces.

2. Grant remote access to MySQL user

By default, MySQL server creates a user root with full administrative privileges, but it only allows access from the local machine. To enable remote access for this user, you need to grant it permission to connect from a remote IP address.

Log in to the MySQL server as root user and execute the following command:

GRANT ALL ON . TO 'root'@'%' IDENTIFIED BY 'yourpassword';

This grants the user root access to all databases and tables from any IP address (%). Replace ‘yourpassword‘ with a secure password of your choice.

3. Restart MySQL server

After modifying the configuration file and granting remote access to the user, you need to restart the MySQL server to apply the changes.

On Ubuntu or Debian, use the following command:

sudo service mysql restart

On CentOS or Fedora, use:

sudo systemctl restart mysqld

4. Open MySQL port on firewall

If you have a firewall running on the MySQL server, you need to open the port that MySQL server is listening on (usually port 3306) to allow incoming connections.

On Ubuntu or Debian, use the following command to open the port:

sudo ufw allow 3306/tcp

On CentOS or Fedora, use:

sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload

Once you have completed these steps, you should be able to connect to the MySQL server remotely using the root user and the password you set in step 2.

However, note that allowing remote access to the MySQL server can pose a security risk if not properly secured.

Therefore, it is recommended to only allow connections from trusted IP addresses and to use a secure password.

Disable exec, shell_exec, system, popen and Other PHP Functions To Improve Security

PHP has a lot of functions which can be used to crack your server if not used properly. You can set list of functions in php.ini using disable_functions directive. This directive allows you to disable certain functions for security reasons. It takes on a comma-delimited list of function names.

Open a terminal application or login to your server over the ssh session using ssh command. Open php.ini file using a text editor such as vim command or nano command:

sudo nano /etc/php/8.0/fpm/php.ini

Find disable_functions and set new list as follows:

disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

We also recommend to disable allow_url_include and allow_url_fopen for security reasons:

allow_url_fopen=Off
allow_url_include=Off

Restart PHP with command:

systemctl restart php8.0-fpm

Install Wireguard on Debian 11

First update package list and upgrade your operating system:

sudo apt update && sudo apt upgrade

To install Wireguard use command:

sudo apt install wireguard

Now check if Wireguard installed correct:

sudo modprobe wireguard

If this command dose not show anything, Wireguard installed correct. If You get error like this: modprobe: FATAL: Module wireguard not found in directory /lib/modules/4.19.0-12-686, install this package:

sudo apt install linux-headers-$(uname --kernel-release)