Windows 10 WSL Ubuntu Common Commands and hacks

In C:\Windows\System32\drivers\etc\hosts

127.0.0.1 test.tld
::1 test.tld
#::1 is really important sometimes
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinSock2\Parameters\AppId_Catalog08F7A3]
"AppFullPath"="C:\\Windows\\System32\\wsl.exe"
"PermittedLspCategories"=dword:80000000

Save the above as a .reg file and run it to update registry

Powershell script=> save as .ps1 file

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {   
  $arguments = "& '" + $myinvocation.mycommand.definition + "'"
  Start-Process powershell -Verb runAs -ArgumentList $arguments
  Break
}

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

#[Ports]

#All the ports you want to forward separated by coma
[email protected](80,443,3306);


#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";


#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}


netsh interface portproxy show v4tov4

check if your proxy is working

Most of the times you may not even need a proxy for apache2. However mysql inside WSL is extremely tricky for some reason. I have ALWAYS had issues until I changed the default port. Hence try this in your file:

sudo nano /etc/mysql/my.cnf
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
explicit_defaults_for_timestamp=true
bind-address = 0.0.0.0
user=root
pid-file     = /var/run/mysqld/mysqld.pid
socket       = /var/run/mysqld/mysqld.sock
port         = 3334

Now you should be able to connect to this mysql instance from some gui app on windows too like dbeaver with localhost on port 3334.

If you are victim of unpredictive WSL behavior, do leave a comment

S3 notes – how to make all s3 images public

aws configure --profile myproject
// to test if it works or not

aws s3 ls --profile myproject

aws s3 cp \
      --exclude "*" \
      --include "*.jpeg" \
      --content-type="image/jpeg"  \
      --metadata-directive="REPLACE" \
      --recursive \
	  --acl public-read \
       s3://myproject/ \
       s3://myproject/ \
	   --dryrun --profile myproject
aws s3 cp \
      --exclude "*" \
      --include "*.jpg" \
      --content-type="image/jpeg"  \
      --metadata-directive="REPLACE" \
      --recursive \
	  --acl public-read \
       s3://myproject/ \
       s3://myproject/ \
	   --dryrun --profile myproject
aws s3 cp \
      --exclude "*" \
      --include "*.jpeg" \
      --content-type="image/jpeg"  \
      --metadata-directive="REPLACE" \
      --recursive \
	  --acl public-read \
       s3://myproject/ \
       s3://myproject/ \
	   --dryrun --profile myproject
// not needed 
aws s3 sync . s3://my-bucket/path --acl public-read
	   

Dont forget to remove dryrun flag once you are sure.

prevent sticky header jumping in css or js

jQuery(document).ready(function(){var header=jQuery("header .fusion-header");var headerHeight=0; var shouldWrite=1;
jQuery(window).on("scroll",function(){
headerHeight=header.height();
console.log(headerHeight);
if(shouldWrite && headerHeight!==undefined){
jQuery("#headerjumpfixer").html(".fusion-header-wrapper.fusion-is-sticky{padding-top:"+headerHeight + "px}");
shouldWrite=0;}
});
jQuery(window).on("resize",function(){shouldWrite=1;});
});
<style id="headerjumpfixer">.fusion-header-wrapper.fusion-is-sticky{padding-top:88px}</style>

This unique date 02 02 2020 will come after 1010 years – WRONG MATH

In dd/mm/yyyy format, 2nd February 2020 (02 02 2020) is a palindrome

In really common beliefs, whatsapp forwards were filled with this number being so rare and unique. However it would be stupid to say that such a rare date will occur only after 1010 years.

Here is how:

In whole millennium (1000 years), first few years of first few centuries have many of such dates. Even if you consider MM/DD/YYYY format, you can find a lot of such numbers in first few centuries.

Here are all of such dates in this century (2000 – 2099) in DD/MM/YYYY format

10/02/2001
20/02/2002
01/02/2010
11/02/2011
21/02/2012
02/02/2020
12/02/2021
22/02/2022
03/02/2030
13/02/2031
23/02/2032
04/02/2040
14/02/2041
24/02/2042
05/02/2050
15/02/2051
25/02/2052
06/02/2060
16/02/2061
26/02/2062
07/02/2070
17/02/2071
27/02/2072
08/02/2080
18/02/2081
28/02/2082
09/02/2090
19/02/2091
29/02/2092

10 tips for better freelancing

How to improve yourself as a freelancer? Here are 10 points to take care of:

  1. Communication Skills – convincing power
  2. Make yourself a brand, your ethics will define your future in freelancing. This will help you in scaling also
  3. Consistency, Focus and Dedication
  4. Productivity is Gold, know your value, do math and target for a monthly number
  5. Read!! know clients requirements in depth, if it is small task, fix already without getting the project. This approach will consume time without paying, but would probably make you better
  6. keep challenging yourself, keep improving
  7. Stay away from template bids, no body wants to know which app you did 4 years ago if the project not about an app
  8. Keep your portfolio updated
  9. Take care of clock, predict and quote project timelines intelligently
  10. Exceed expectations of your Client, under-promise and over-deliver

SQL Notes

CREATE TABLE `clients` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(11) NOT NULL,
  `associate` varchar(100) NOT NULL,
  `revenue` int(12) NOT NULL,
  `profit` int(12) NOT NULL,
   PRIMARY KEY (`id`),
   FOREIGN key (`associate`) references `emp`(`emp_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

alter table `emp` add COLUMN `phone` int(12) not null after `name`
alter table `emp` add COLUMN `phone` int(12) null after `name`
alter table `emp` modify `phone` int(12) null
alter table `emp` drop column `phone`


ALTER TABLE `emp`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `emp_code` (`emp_code`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `emp`
--
ALTER TABLE `emp`
  MODIFY `id` int(4) NOT NULL AUTO_INCREMENT;
COMMIT;

SSL certification setup: Cloudflare.

This article will guide you to set your SSL certification using Cloudflare. Cloudflare is an internet security company which provides content delivery network service.

You can get free SSL certification as well as paid one from Cloudflare.

A breif discussion on SSL certification and https redirection.

So, Let’s start the process.

Steps:

Step1

Signup and Login to cloudflare. There you will get an option to add your site url. Do it and proceed.

Step2

Cloudflare will check your DNS records. You just click next and proceed.


Step3

Now choose your plan according to your needs. For this tutorial i preffered free plan. Paid plans will add some extra features and security to your site.

Step4

Now it will show you some details of your site to confirm. Just Click continue. And move on.

Step5

Cloudflare will give you two nameservers to replace with your default nameserver. Just login to your hosting provider and change it there. And then click continue.

If everything went good so far, cloudflare will activate security on your website. Click Re-check button to refresh cloudflare and it will show the status.

We are almost done. The last thing to do is – get your API key and paste it to wordpress plugin of cloudflare, and save API credentials.

That is it. Your site is now secured with a SSL certification from cloudflare. This certification is valid for 1 year.

https://www.cloudflare.com/

Magento File Directory

/app
The etc folder contains the di.xml configuration file
The di.xml file contains class mapping and interface preferences
Contains the design folder for themes.

/bin
The directory contains the magento file or a Command Line Interface script

/dev
Magento 2 testing and development tools are placed in this folder.

/lib
The internal and web subfolders are located here
The internal directory contains server-side libraries and several font files;
The web subfolder contains client-side libraries, jquery, modernizr, requirejs, prototype, scriptaculous

/pub
It should be set as your web root on the server
It contains all the publicly accessible files
index.php controller is also contained in the folder
pub also contains the error pages that will be displayed if your site is showing a 404 or 503 or another report page
Image uploads and imports will appear somewhere within pub/media.

/var
caches and generated classes are all located in the folder
the var folder includes cache, composer_home, generation, log, di, view_preprocessed, page_cache files

/vendor
the folder includes various packages that have been defined under composer.json
Marketplace modules and the core modules are all under vendor.
If you download something from the new Magento Marketplace.