Developer Blog

Tipps und Tricks für Entwickler und IT-Interessierte

WordPress asking for FTP credentials

If you’re asked to enter your FTP credentials when you try to install or update a plugin or theme in the WordPress dashboard, it’s because of a specific setting in the wp-config.php file. Follow the steps below to prevent WordPress from displaying this FTP prompt.

  1. Find the folder with your WordPress files and open the wp-config.php file.
  2. At the bottom of the wp-config.php file, above the line that says 
    /* That's all, stop editing! Happy publishing. */,
    add the following lines of code:
define('FS_METHOD', 'direct'); 
define('FS_CHMOD_DIR',0755); 
define('FS_CHMOD_FILE',0644);

Power Shell | Inside Powershell

Language

Switches

Basic Syntax

Let’s start by taking a look at a basic switch statement.

$i = 1;
switch ($i)
{
1 {
write-host “one”
break
}
2 {
write-host “two”
write-host “two”
break
}
default {
write-host “other”
break
}
}

Notice we don’t use the Case: keyword at all.

The default in PowerShell is to assume -eq is used between your input and typed case value. This means No Case Sensitivity and No Wild Cards:

switch(“hello”) #will match uppercase
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}
switch(“hello”) #will NOT match h*
{
“h*” {write-host “wildcard” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}

Expression Match

We have a long-hand switch statement that lets us use whatever we want (-eq, -gt, -le, -like, straight up boolean values, etc.)

Psudo-code:Switch($value) { {<bool expression>} {<code>} {<bool expression>} {<code>} default {<code>} }

Real:

switch(“hello”) #will match uppercase
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}
switch(“hello”) #will NOT match h*
{
“h*” {write-host “wildcard” -ForegroundColor Magenta;break}
“hello” {write-host “lowercase” -ForegroundColor green;break}
}

Jump Statements aren’t necessary

C# requires jump statements such as break, goto, or return. PowerShell does not!

This is one of the coolest features in PowerShell. We actually allow for continuous case- checks.

This means your switches can actually act more like a bunch of independent if statements. Notice the previous example, without any of the “break statements” and using a number that is less than 5, 10 and 15.

$num = 4
Switch($num)
{
{$num -lt 5} {write-host “less than 5!” -ForegroundColor Magenta}
{$num -lt 10} {write-host “less than 10!” -ForegroundColor green}
{$num -lt 15} {write-host “less than 15!” -ForegroundColor cyan}
default {write-host “greater than or equal to 15” -ForegroundColor yellow}
}

Loops and $_

It might be common for you to take a bunch of data, do a foreach loop through it and send each value through your switch:

$nums = 1..15
foreach($num in $nums)
{
Switch($num)
{
{$num -lt 5} {write-host “$num is less than 5!” -ForegroundColor Magenta}
{$num -lt 10} {write-host “$num is less than 10!” -ForegroundColor green}
{$num -lt 15} {write-host “$num is less than 15!” -ForegroundColor cyan}
default {write-host “$num is greater than or equal to 15” -ForegroundColor yellow}
}
}

However, PowerShell actually has a loop and $_ built right into your switch so we can chop off the foreach completely:

$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host “$_ is less than 5!” -ForegroundColor Magenta}
{$_ -lt 10} {write-host “$_ is less than 10!” -ForegroundColor green}
{$_ -lt 15} {write-host “$_ is less than 15!” -ForegroundColor cyan}
default {write-host “$_ is greater than or equal to 15” -ForegroundColor yellow}
}

This lets us write some really concise and convenient little code blocks. The nice thing is that if our list has 1 object it still gets handled fine, and if it’s an empty collection it will just skip the whole switch!

This, however, can lead to some confusion if you try to use “break” since our loop is also the whole switch statement:

$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host “$_ is less than 5!” -ForegroundColor Magenta;break}
{$_ -lt 10} {write-host “$_ is less than 10!” -ForegroundColor green;break}
{$_ -lt 15} {write-host “$_ is less than 15!” -ForegroundColor cyan;break}
default {write-host “$_ is greater than or equal to 15” -ForegroundColor yellow}
}

view rawLoopSwitchSample3.ps1 hosted with ❤ by GitHub

Uh-oh, not good.

We also have “continue” in PowerShell and this will stop our current iteration of our loop (or switch) so we can use the looping feature and make it like a bunch of elseifs:

$nums = 1..15
Switch($nums)
{
{$_ -lt 5} {write-host “$_ is less than 5!” -ForegroundColor Magenta;continue}
{$_ -lt 10} {write-host “$_ is less than 10!” -ForegroundColor green;continue}
{$_ -lt 15} {write-host “$_ is less than 15!” -ForegroundColor cyan;continue}
default {write-host “$_ is greater than or equal to 15” -ForegroundColor yellow}
}

Shortcuts

In addition to the looping, we provide you a few other handy short cuts.

If you just wanted a basic equality switch, but would want to use -ceq (case sensitivity), -like (wild cards), or -match (regex) we let you do that without writing an expression match via some parameters.

Notice, weirdly, the parameters must come between the word “switch” and the parenthesis, they won’t work at the end of the parenthesis.

switch -casesensitive (“hello”) #makes it a -ceq
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta}
“hello” {write-host “lowercase” -ForegroundColor green}
“h*” {write-host “wildcard” -ForegroundColor Magenta}
}
switch -wildcard (“hello”) #makes it a -like
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta}
“hello” {write-host “lowercase” -ForegroundColor green}
“h*” {write-host “wildcard” -ForegroundColor Magenta}
}
switch -wildcard -CaseSensitive (“hello”) #makes it a -clike
{
“HELLO” {write-host “Uppercase” -ForegroundColor Magenta}
“hello” {write-host “lowercase” -ForegroundColor green}
“h*” {write-host “wildcard” -ForegroundColor Magenta}
}
switch -regex (“hello”) #makes it a -match
{
“\w{5}” {write-host “5 characters” -ForegroundColor Magenta}
“\w{6}” {write-host “6 characters” -ForegroundColor green}
}

CommandLets

  • Get-Alias
  • New-Alias
  • Set-Alias
  • Export-Alias
  • Import-Alias 

Function

To see the definition of mkdir use

Get-Content Function:\mkdir

Daily Azure: Migrate a Storge Account

TL;DR

Migration is done via azcopy:

  • download souce container to local folder
  • upload local folder to destination container

Get AzCopy

Here is the script install-azcopy.ps1:

# Download and extract

#
$URI = "https://aka.ms/downloadazcopy-v10-windows"
$DST = "~\AppData\Local\Programs\AZCopy\"

Invoke-WebRequest -Uri $URI -OutFile AzCopy.zip -UseBasicParsing
Expand-Archive ./AzCopy.zip ./AzCopy -Force

# Move AzCopy
mkdir ~\AppData\Local\Programs\AZCopy
Get-ChildItem ./AzCopy/*/azcopy.exe | Move-Item -Destination "~\$DEST"

# Add AzCopy to PATH
$userenv = (Get-ItemProperty -Path 'HKCU:\Environment' -Name Path).path
$newPath = "$userenv
New-ItemProperty -Path 'HKCU:\Environment' -Name Path -Value $newPath -Force

# Clean the kitchen
del -Force AzCopy.zip
del -Force -Recurse .\AzCopy\

Copy Folder

param (
    $FOLDER = "",

    [Parameter(Mandatory=$false)]
    [string]$TYPE   = "latest",

    [Parameter(Mandatory=$false)]
    [switch]$LOGIN
)

if ($TYPE -eq "latest") {
    $SRC_ROOT="<latest-folder>"
    $DST_ROOT="latest"
} else {
    $SRC_ROOT="<history-folder>"
    $DST_ROOT="history"
}


$SRC_ACCCOUNT = "<source storage account>";
$DST_ACCCOUNT = "<destination storage account>";

$SRC_CONTAINER = "<source container>"
$DST_CONTAINER = "<destination container>"


$SRC_URL      = "https://${SRC_ACCCOUNT}.blob.core.windows.net/$SRC_CONTAINER/$SRC_ROOT/$FOLDER/"
$DST_URL      = "https://${DST_ACCCOUNT}.blob.core.windows.net/$DST_CONTAINER/$DST_ROOT/"

$TMP_FLDR     = "C:\TMP\Downloads"

Write-Host  "== Copy     '$FOLDER'"
Write-Host "       from  $SRC_URL"
Write-Host  "        to  $DST_URL"

#

if ($LOGIN) {
    $ENV:AZCOPY_CRED_TYPE = "OAuthToken";
    $ENV:AZCOPY_CONCURRENCY_VALUE = "AUTO";

    azcopy login
}

Write-Host  "== Download ======================================================"
Write-Host "       from  $SRC_URL"
Write-Host  "        to  $TMP_FLDR\$CONTAINER"

azcopy copy         $SRC_URL                                                                                      `
                    $TMP_FLDR                                                                                     `
                    --trusted-microsoft-suffixes=${SRC_ACCCOUNT}.blob.core.windows.net                            `
                    --overwrite=true                                                                              `
                    --check-md5=FailIfDifferent                                                                   `
                    --from-to=BlobLocal                                                                           `
                    --recursive                                                                                   `
                    --log-level=ERROR

# Upload
Write-Host  "== Upload   ======================================================"
Write-Host  "      from  $TMP_FLDR\$CONTAINER"
Write-Host  "        to  $DST_URL"

azcopy copy         $TMP_FLDR\$CONTAINER                         `
                    $DST_URL                                     `
                    --overwrite=true                             `
                    --from-to=LocalBlob                          `
                    --blob-type BlockBlob                        `
                    --follow-symlinks                            `
                    --check-length=true                          `
                    --put-md5                                    `
                    --follow-symlinks                            `
                    --disable-auto-decoding=false                `
                    --recursive                                  `
                    --log-level=ERROR

Call the script

First call should use -login to authenticate

.\copy.ps1 demo-folder-1 -login

Then, following commands dont need the login

.\copy.ps1 demo-folder-2

WordPress – Working with CLI

Working with CLI

Basic work with posts

Search post content.

wp post list --s="text" --fields=ID,post_title,url

Find posts with ping status “open”.

wp post list --ping_status="open"

And if you only want to get the post IDs:

wp post list --ping_status="open" --fields=ID

Update ping status to “closed” where the value is “open”.

wp post update $(wp post list --ping_status="open" --format=ids) --ping_status="closed";

Search and replace

Search and replace post content.

bashwp search-replace "old thing" "new thing" --all-tables

Post meta

List all metadata associated with a post.

bashwp post meta list 1422 --format=json

Get specific post meta for a post.

bashwp post meta get 1422 "meta_key" --format=json

Update post meta.

bashwp post meta update 1422 "meta_key" "meta_value"

Update post meta from a file.

bashwp post meta update 1422 "post_meta" < file.json --format=json

Delete post meta.

bashwp post meta delete 1422 "post_meta"

Delete all post meta by specific key. (With wp db query.)

bashwp db query "DELETE FROM `wp_postmeta` WHERE `meta_key` = 'NAME_OF_YOUR_META_KEY'"

List posts by meta key or value.

bashwp post list --fields=ID,post_title,url --meta_key="meta_key"
wp post list --fields=ID,post_title,url --meta_key="meta_key" --meta_compare="NOT EXISTS"
wp post list --post_type="post_type" --fields=ID,post_title,url --meta_key="meta_key" --meta_compare="NOT EXISTS"
wp post list --fields=ID,post_title,url --meta_key="_wp_page_template" --meta_value="page-templates/post-full-grid.php"

The meta_compare flag also accepts "LIKE" as a value, so that you can do:

phpwp post list --fields=ID,post_title,url --meta_key='meta_key' --meta_compare='LIKE' --meta_value='value'

Update meta where it’s missing.

bashwp post meta update $(wp post list --post_type="post_type" --meta_key="meta_key" --meta_compare="NOT EXISTS" --format=ids) "meta_key" "meta_value"

Update post meta for all posts in a category.

bashwp post meta update $(wp post list --category_name="category" --format=ids) "meta_key" "meta_value"

Save meta value based on an existing meta value. In this example I’m looping through all post with a specific post type and saving the URL of the featured image to the post’s meta.

bashfor id in $(wp post list --post_type="post_type" --fields=ID --meta_key="meta_key" --meta_compare="NOT EXISTS")
do
    wp post meta update $id "meta_key" $(wp post meta pluck $(wp post meta get $id _thumbnail_id) _wp_attachment_metadata file)
done

Show specific meta value for posts based on a category.

bashfor id in $(wp post list --post_type="post_type" --fields=ID --category="category name")
do
    wp post meta get $id "meta_key"
done

Export/import all meta.

bashwp post meta list 1422 --format=json > 1422_meta.json
wp post update 1422 --meta_input= < 1422_meta.json

Working with post terms

Move posts from one tag to another.

bashfor id in $(wp post list --tag=OLD_TAG --format=ids)
do
    wp post term remove $id post_tag OLD_TAG --by=slug
    wp post term add $id post_tag NEW_TAG --by=slug
done

Delete Posts

Delete all posts from a custom post type. (Adding --force will skip the trash and remove the posts completely.)

bashwp post delete $(wp post list --post_type='activity' --format=ids) --force

If you’re trying to delete too many posts at once, you might get an error like this:

bashPHP Fatal error: Allowed memory size of xxx bytes exhausted

Or perhaps this one:

bashArgument list too long

One way around this is to limit how many posts you’re deleting at once using the posts_per_page option, for example:

bashwp post delete $(wp post list --post_type='activity' --posts_per_page=50000 --format=ids) --force

Delete posts with specific meta key.

bashwp post delete $(wp post list --format=ids --meta_key="meta_key")

Delete posts with specific meta key and meta value.

bashwp post delete $(wp post list --format=ids --meta_key="meta_key" --meta_value="meta_value")

Delete posts where a specific meta key is missing.

bashwp post delete $(wp post list --format=ids --meta_key="meta_key" --meta_compare="NOT EXISTS")

Post revisions

List all revisions.

bashwp post list --post_type=revision

List revisions for a specific post.

bashwp post list --post_parent=1422 --post_type=revision

Get more info on revisions for a specific post.

bashfor id in $(wp post list --post_parent=1422 --post_type=revision --fields=ID)
do
    wp post get $id
done

Site Options

Change the site URL.

bashwp option update home 'https://example.com'
wp option update siteurl 'https://example.com'

Plugins

List active plugins.

bashwp plugin list --status=active

Activate/deactivate plugins.

bashwp plugin activate plugin-1 plugin-2 plugin-3
wp plugin deactivate plugin-1 plugin-2 plugin-3

Users

Update user password.

wp user update USER_ID --user_pass="newpassword"

WP CLI cheatsheet

check database size

Returns sizes of all database tables in a human friendly format.

Reference:https://developer.wordpress.org/cli/commands/db/size/

wp db size --human-readable --tables

 copy

check db status

Run health check on the database set in the wp-config.php file. It reports status of each database table separately in the output.

Reference:https://developer.wordpress.org/cli/commands/db/check/

wp db check

 copy

check for updates

Checks if WP-CLI is due for an update.

wp cli check-update

 copy

check version

Return WP CLI version.

wp cli version

 copy

configure wordpress

Creates config.php file. Requires mysql cli to be installed.

wp config create --dbname=yourdbname --dbuser=root --prompt=dbpassword

 copy

count all posts

This command searches for all posts in the database and returns the count number.

Reference:https://developer.wordpress.org/cli/commands/post/list/

wp post list --format=count 

 copy

download wordpress

Download latest version of WordPress core files to current directory.

wp core download

 copy

edit config

Opens wp-config.php file in the default text editor.

wp config edit

 copy

export

Exports WXR file containing XML formatted data about your site including posts, comments and information about images and other attachments. Site option settings are excluded.

Reference:https://developer.wordpress.org/cli/commands/export/

wp export

 copy

export single page

By combining the following flags you can export a single page from WordPress. First set the post type you want to export (page) then set the ID of the post, and finally limit the command to only fetch a single post. Skipping comments is optional.

Reference:https://developer.wordpress.org/cli/commands/export/

wp export --skip_comments --post_type=page --post__in=4995 --max_num_posts=1

 copy

generate salts

This command will generate authentication unique keys and salts in your wp-config.php file.

Reference:https://developer.wordpress.org/cli/commands/config/shuffle-salts/

wp config shuffle-salts

 copy

get media image sizes

List all defined image sizes, their width, height and the crop details.

Reference:https://developer.wordpress.org/cli/commands/media/image-size/

wp media image-size

 copy

import

Imports WXR files.

Reference:https://developer.wordpress.org/cli/commands/import/

wp import yoursiteexample.wordpress.2021-09-22.000.xml

 copy

import to sub-site

You can import WXR data files to specific site in the multi-site installation. The –url flag identifies the site you want to migrate data to. Skipping authors is optional.

Reference:https://developer.wordpress.org/cli/commands/import/

wp import yoursiteexample.wordpress.2021-10-15.000.xml --authors=skip --url=http://yoursite.com/subsite

 copy

install plugin

Install a plugin in your WordPress site. By passing –activate flag you can enable your new plugin in one command.

wp plugin install [pluginhandle] --activate

 copy

list all pages

This command is based on the “wp post list” used to return all posts but the “–post_type=page” flag changes the search scope to return only pages. By default the output if formatted as a table and includes page ID, title, slug, creation date, and the status.

Reference:https://developer.wordpress.org/cli/commands/post/list/

wp post list --post_type=page

 copy

list all post meta

Use post ID to return all related meta data. By default it returns data as a table but other formats are available, for example you can use this flag –format=json to get data as json.

Reference:https://developer.wordpress.org/cli/commands/post/meta/list/

wp post meta list <ID>

 copy

list custom post type

Returns a table of custom posts with a type specified using the –post_type flag.

Reference:https://developer.wordpress.org/cli/commands/post/list/

wp post list --post_type=movie

 copy

list database table columns

Lists all columns in a given database table. You will need to pass a table name as the argument. List of the default WordPress tables – wp_commentmeta, wp_comments, wp_links, wp_options, wp_postmeta, wp_posts, wp_term_relationships, wp_term_taxonomy, wp_termmeta, wp_terms, wp_usermeta, wp_users.

Reference:https://developer.wordpress.org/cli/commands/db/columns/

wp db columns {tabel name}

 copy

list database tables

Lists all tables in the database connected to your site.

Reference:https://developer.wordpress.org/cli/commands/db/tables/

wp db tables

 copy

list post types

Returns a table of all post types including custom posts.

Reference:https://developer.wordpress.org/cli/commands/post-type/

wp post-type list

 copy

list posts

Returns a table of all posts including ID, title, name, date and status.

Reference:https://developer.wordpress.org/cli/commands/post/list/

wp post list

 copy

list sites

If you’re running a multi-site WordPress installation you can return a handy table presenting blog ID, URL, and update dates of each site instance.

Reference:https://developer.wordpress.org/cli/commands/site/list/

wp site list

 copy

list taxonomies

Returns a table of all registered taxonomies including categories, post tags, navigation menus, themes, custom taxonomies and more.

Reference:https://developer.wordpress.org/cli/commands/taxonomy/list/

wp taxonomy list

 copy

maintenance-mode activate

Puts your site in a maintenance mode and returns “Briefly unavailable for scheduled maintenance. Check back in a minute.” message with a 503 server response.

wp maintenance-mode activate

 copy

maintenance-mode deactivate

Takes your site out of maintenance mode.

wp maintenance-mode deactivate

 copy

maintenance-mode status

Returns maintenance mode status of your site.

wp maintenance-mode status

open mysql cli

Connects to the database set in your wp-config.php file using MySQL CLI.

Reference:https://developer.wordpress.org/cli/commands/db/cli/

wp db cli

optimize database

Runs MySQL optimizer on the connected database.

Reference:https://developer.wordpress.org/cli/commands/db/optimize/

wp db optimize

regenerate thumbnails

Regenerate all thumbnail image sizes stored in the media library. The –yes flag confirms your choice.

wp media regenerate --yes

 copy

scaffold plugin

This command will generate basic files and structure for a new plugin. Use –skip-tests flag to omit php unit tests in the process.

Reference:https://developer.wordpress.org/cli/commands/scaffold/plugin/

wp scaffold plugin your-plugin-slug

 copy

scaffold theme

Generates un-styled main theme with all the necessary files and views to display content.

Reference:https://developer.wordpress.org/cli/commands/scaffold/_s/

wp scaffold _s your-theme-slug --theme_name="Theme Name" --author="Your Name"

 copy

search string in DB

You can search for a string in connected WordPress database. The –stats flag will append details about the search like matches found and tables searched.

Reference:https://developer.wordpress.org/cli/commands/db/search/

wp db search <string_to_find> --stats

update all plugins

Updates all installed plugins.

wp plugin update --all

update wp-cli

Update WP-CLI to the latest version.

wp cli update

wp-cli info

Prints information to the console about WP CLI and its environment such as version, OS, PHP, MySQL.

JQ – Cheatsheet

Installing jq

On Mac OS

brew install jq

Useful arguments

When running jq, the following arguments may become handy:

ArgumentDescription
--versionOutput the jq version and exit with zero.
--sort-keysOutput the fields of each object with the keys in sorted order.

Basic concepts

The syntax for jq is pretty coherent:

SyntaxDescription
,Filters separated by a comma will produce multiple independent outputs
?Will ignores error if the type is unexpected
[]Array construction
{}Object construction
+Concatenate or Add
Difference of sets or Substract
lengthSize of selected element
|Pipes are used to chain commands in a similar fashion than bash

Dealing with json objects

DescriptionCommand
Display all keysjq 'keys'
Adds + 1 to all itemsjq 'map_values(.+1)'
Delete a keyjq 'del(.foo)'
Convert an object to arrayto_entries &#124; map([.key, .value])

Dealing with fields

DescriptionCommand
Concatenate two fieldsfieldNew=.field1+' '+.field2

Dealing with json arrays

Slicing and Filtering

DescriptionCommand
Alljq .[]
Firstjq '.[0]'
Rangejq '.[2:4]'
First 3jq '.[:3]'
Last 2jq '.[-2:]'
Before Lastjq '.[-2]'
Select array of int by valuejq 'map(select(. >= 2))'
Select array of objects by value** jq ‘.[] | select(.id == “second”)’**
Select by type** jq ‘.[] | numbers’ ** with type been arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars

Mapping and Transforming

DescriptionCommand
Add + 1 to all itemsjq 'map(.+1)'
Delete 2 itemsjq 'del(.[1, 2])'
Concatenate arraysjq 'add'
Flatten an arrayjq 'flatten'
Create a range of numbersjq '[range(2;4)]'
Display the type of each itemjq 'map(type)'
Sort an array of basic typejq 'sort'
Sort an array of objectsjq 'sort_by(.foo)'
Group by a key – opposite to flattenjq 'group_by(.foo)'
Minimun value of an arrayjq 'min' .See also min, max, min_by(path_exp), max_by(path_exp)
Remove duplicatesjq 'unique' or jq 'unique_by(.foo)' or jq 'unique_by(length)'
Reverse an arrayjq 'reverse'

XAMPP – Setup HTTPS/SSL localhost on macOS

Original Post from here

Preparation

1. Check XAMPP SSL config

Open /Applications/XAMPP/xamppfiles/etc/httpd.conf and make sure your settings are the same as below:

LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
<IfModule ssl_module>
    <IfDefine SSL>
        Include etc/extra/httpd-ssl.conf
    </IfDefine>
</IfModule>

Test SSL config with this command:

sudo /Applications/XAMPP/xamppfiles/xampp enablessl

2. Backup httpd-ssl.conf

  • Go to: /Applications/XAMPP/xamppfiles/etc/extra
  • Copy httpd-ssl.conf & paste as httpd-ssl.conf.bak

3. Edit httpd-ssl.conf

  • Open /Applications/XAMPP/xamppfiles/etc/extra/httpd-ssl.conf
  • Change default port from 8443 to 443
#Listen 8443
Listen 443
  • Find this line and remove everything below it
## SSL Virtual Host Context

Setup HTTPS/SSL localhost

Open Terminal

4. Create SSL folder to store SSL files

cd
/Applications/XAMPP/xamppfiles/etc/
mkdir ssl
cd ssl

5. Create SSL CSR & private key

You can use any file name here (just replace localhost)

openssl req -new -newkey rsa:2048 -nodes -keyout localhost.key -out localhost.csr

Enter info as below. Note: change localhost to your localhost domain name (ex: www.domain.name)

Country Name (2 letter code) [AU]:VN
State or Province Name (full name) [Some-State]:HCM
Locality Name (eg, city) []:HCM
Organization Name (eg, company) [Internet Widgits Pty Ltd]:VN
Organizational Unit Name (eg, section) []:VN
Common Name (e.g. server FQDN or YOUR name) []:<strong>localhost</strong>
Email Address []:abc@gmail.com

There are 2 more information may be asked:

  • A challenge password
  • An optional company name

Leave this information BLANK (just Enter, no need to input anything)

6. Create .ext file

touch localhost.ext

Copy & paste below content to v3.ext file

Note: change localhost to your localhost domain name

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = localhost

7. Create SSL CRT

openssl x509 -req -sha256 -extfile localhost.ext -days 3650 -in localhost.csr -signkey localhost.key -out localhost.crt

8. Add SSL CRT to macOS Keychain

  • Double click on localhost.crt on Finder
  • Select Keychain: System then click Add
XAMPP - Setup HTTPS/SSL localhost on macOS
  • Find & double click on localhost cert in Keychain Access
  • Expand Trust, select When using this certificates: Always Trust
XAMPP - Setup HTTPS/SSL localhost on macOS
  • Close the window and click Update Settings

9. Config SSL in XAMPP virtual host

Open /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf

Update your config:

from

<VirtualHost *:80>
	ServerName localhost
	DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
</VirtualHost>

to

<VirtualHost *:443>
	ServerName localhost
	DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"

	SSLEngine on
	SSLCertificateFile "/Applications/XAMPP/xamppfiles/etc/ssl/localhost.crt"
	SSLCertificateKeyFile "/Applications/XAMPP/xamppfiles/etc/ssl/localhost.key"
</VirtualHost>

10. Open Chrome and access https://localhost.

WSL: Wrong Date

When checking the date in WSl, it display the wrong date

❯ date
Wed Jul 26 15:28:20 CEST 2023

So setup the correct date, you could run either

❯ sudo hwclock -s

or setup a timesync daemon (described here)

Add these lines to the /etc/wsl.conf (note you will need to run your editor with sudo privileges, e.g: sudo nano /etc/wsl.conf):

[boot]
systemd=true

And close out of the nano editor using CTRL+O to save and CTRL+X to exit. Now close your WSL distro windows and run wsl.exe --shutdown from PowerShell, then restart your WSL instance. Upon launch you should have systemd running.

Now install systemd-timesyncd:

sudo apt install systemd-timesyncd
sudo systemctl edit systemd-timesyncd

In the second step, when editing, add the two lines beginning with [Unit], as shown below:

Now start it:

sudo systemctl start systemd-timesyncd

Check status with:

timedatectl status
timedatectl timesync-status

Angular | Working with Angular Examples

General

On the Angular Website, there are a lot of examples.

You could download each of them and look around. In this post, i will show you to do this at once for all examples.

So, we will

  • get list of all examples
  • download zip files of examples
  • extract examples

Preparation

First, we define some variables for the Chrom Browser and the resulting output files.

# Mac OS
CHROME=/Applications/Chromium.app/Contents/MacOS/Chromium

# Windows WSL
CHROME='/mnt/c/Program Files/Google/Chrome/Application/chrome.exe'

LISTOFZIPS=listofzips

Download List of examples

Next, we will download the list of examples from https://angular.io/guide/example-apps-list.

This is not a static html file. The list will be created dynamically when viewing the page. So, only download the page with wget or curl does not help, because we need to run the JavaScript code in the browser to create the list.

Therefore, we will use Chrome in headless mode: Load the page and dump the contents of the page (dom) into a file

do_get_list() {
        "$CHROME" --headless --dump-dom https://angular.io/guide/example-apps-list | tr '>' '>\n ' >${LISTOFZIPS}.html

        echo "##. Download List of Examples"
        echo "    #Examples: ($(grep 'href="guide/' listofzips.html | wc -l))"
}

Then, we will extract all the links to the zip examples files.

A quick look into the html file shows, that the html code with the links looks like this:

We need the part in the href of the <a tag.

  • First, we will split the html file, so that every html tag will be on a separate line.
  • Then, we will cut out only the parts between apostrophes.
  • Then, we search all files containing “.zip” and the end of the line ($)
do_extract() {
        cat ${LISTOFZIPS}.html                                          |\
        tr ' ' '\n'                                                     |\
        cut -d \" -f2                                                   |\
        grep -e '.zip$'                                                 > ${LISTOFZIPS}

        echo "generated/zips/component-overview/component-overview.zip" >> ${LISTOFZIPS}

        echo "##. Extract zip files ($(wc -l ${LISTOFZIPS}))"
        echo "    #Examples: ($(wc -l ${LISTOFZIPS}))"
}

The result will be the list of URL’s for each example.

Download Examples

Next, for each of these example $ZIP, we will download the zip from the Angular Website with wget -q https://angular.io/$ZIP

do_download() {
        echo "##. Download Zips"

        (
                rm    -rf zip
                mkdir -p  zip
                cd        zip

                cat ../$LISTOFZIPS | while read ZIP
                do
                        echo "Download $ZIP"
                        if [ ! -f $ZIP ]; then
                                wget -q https://angular.io/$ZIP
                        fi
                done

                rm *.zip.1
        )
}

Extract ZIP Files

do_unzip() {
        echo "##. Unzip"

        rm    -rf src
        mkdir -p  src

        find zip -type f -exec basename {} \; | \
        while read FILE
        do
                FLDR=${FILE/.zip/}
                echo unzip zip/$FILE -d src/$FLDR
                unzip zip/$FILE -d src/$FLDR
        done
}

Upgrade all node modules to current version

We will need toe tool npm-check-updates for this.

Install it with

npm install -g npm-check-updates

ncu updates all packages to the latest version. Some Angular Examples require an earlier version of Typescript, so, after upgrade i changes the Typescript Version to 4.9.5

do_upgrade() {
	echo "##. Upgrade"

	(
		cd src
		ls | \
		while read FLDR
		do 
			echo "     $FLDR"
			(
				cd $FLDR
				ncu -u
				#     "typescript": "~4.9.3"
				sed -i '1,$s/"typescript": ".*"/"typescript": "~4.9.5"/' package.json
				# pnpm install
			)
		done
	)
}

Appendix

The code for the complete script is here:

#!/bin/bash

# Mac OS
CHROME=/Applications/Chromium.app/Contents/MacOS/Chromium

# Windows WSL
CHROME='/mnt/c/Program Files/Google/Chrome/Application/chrome.exe'

LISTOFZIPS=listofzips

do_get_list() {
        "$CHROME" --headless --dump-dom https://angular.io/guide/example-apps-list | tr '>' '>\n ' >${LISTOFZIPS}.html

        echo "##. Download List of Examples"
        echo "    #Examples: ($(grep 'href="guide/' listofzips.html | wc -l))"
}

do_extract() {
        cat ${LISTOFZIPS}.html                                          |\
        tr ' ' '\n'                                                                     |\
        cut -d \" -f2                                                           |\
        grep -e '.zip$'                                                         > ${LISTOFZIPS}

        echo "generated/zips/component-overview/component-overview.zip" >> ${LISTOFZIPS}

        echo "##. Extract zip files ($(wc -l ${LISTOFZIPS}))"
        echo "    #Examples: ($(wc -l ${LISTOFZIPS}))"
}

do_download() {
        echo "##. Download Zips"

        (
                rm    -rf zip
                mkdir -p  zip
                cd        zip

                cat ../$LISTOFZIPS | while read ZIP
                do
                        echo "Download $ZIP"
                        if [ ! -f $ZIP ]; then
                                wget -q https://angular.io/$ZIP
                        fi
                done

                rm *.zip.1
        )
}

do_unzip() {
        echo "##. Unzip"

        rm    -rf src
        mkdir -p  src

        find zip -type f -exec basename {} \; | \
        while read FILE
        do
                FLDR=${FILE/.zip/}
                echo unzip zip/$FILE -d src/$FLDR
                unzip zip/$FILE -d src/$FLDR
        done
}

do_upgrade() {
	echo "##. Upgrade"

	(
		cd src
		ls | \
		while read FLDR
		do 
			echo "     $FLDR"
			(
				cd $FLDR
				ncu -u
				#     "typescript": "~4.9.3"
				sed -i '1,$s/"typescript": ".*"/"typescript": "~4.9.5"/' package.json
				# pnpm install
			)
		done
	)
}


case "$1" in
        "get")       do_get_list;;
        "extract")   do_extract;;
        "download")  do_download;;
        "unzip")     do_unzip;;
        "upgrade")   do_upgrade;;
        *)

                echo "run $0 get|extract|download|unzip";;
esac

Podman | Getting Started

TL;DR

Take alook at the Podman Website

❯ brew install podman
❯ brew machine init
❯ podman machine start

Starting machine “podman-machine-default”
Waiting for VM …
Mounting volume… /Users:/Users
Mounting volume… /private:/private
Mounting volume… /var/folders:/var/folders

This machine is currently configured in rootless mode. If your containers
require root permissions (e.g. ports < 1024), or if you run into compatibility
issues with non-podman clients, you can switch using the following command:

podman machine set --rootful

API forwarding listening on: /Users/Shared/CLOUD/Programmier-Workshops/Kurse/Haskell/.podman/podman.sock

The system helper service is not installed; the default Docker API socket
address can’t be used by podman. If you would like to install it run the
following commands:

sudo /usr/local/Cellar/podman/4.4.2/bin/podman-mac-helper install
podman machine stop; podman machine start

You can still connect Docker API clients by setting DOCKER_HOST using the
following command in your terminal session:

export DOCKER_HOST='unix:///Users/Shared/CLOUD/Programmier-Workshops/Kurse/Haskell/.podman/podman.sock'

Machine “podman-machine-default” started successfully