Skip to content

Instantly share code, notes, and snippets.

@nebuIr
Last active April 8, 2024 18:13
Show Gist options
  • Save nebuIr/c769f26bffd41db2667d1f9de9f8ce5a to your computer and use it in GitHub Desktop.
Save nebuIr/c769f26bffd41db2667d1f9de9f8ce5a to your computer and use it in GitHub Desktop.
WSL2 Guide

WSL2 Guide for Debian

Install Debian

Set default WSL version to 2: wsl --set-default-version 2

Install the Debian Distribution from the Windows Store.

Optional:

Set another user as the default user: Debian config --default-user <username>

Alternatively add the following lines to /etc/wsl.conf:

[user]
default=<username>

Install Basics

  • Update packages: sudo apt update && sudo apt upgrade -y
  • Install additional packages: sudo apt -y install screen git wget curl gnupg

Install Zsh & Oh My Zsh

sudo apt -y install zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Install Theme for Zsh

Install Powerlevel10k:

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Get compatible fonts from ryanoasis/nerd-fonts e.g. JetBrainsMono

Set the Zsh theme ZSH_THEME="powerlevel10k/powerlevel10k" in ~/.zshrc.

Start using powerlevel10k by restarting the shell and follow the configuration instructions. You can use the following example configuration by pasting it: yyyy3121111121n1y

Install Antibody

sudo curl -sfL git.io/antibody | sudo sh -s - -b /usr/local/bin

Install Antibody plugins

An extensive list of plugins can be found at unixorn/awesome-zsh-plugins.

Add a list of your desired plugins to ~/.zsh_plugins.txt.

Here's a list of the plugins I use:

b4b4r07/emoji-cli
bobthecow/git-flow-completion
chrissicool/zsh-256color
supercrabtree/k
wting/autojump
zsh-users/zsh-autosuggestions
zsh-users/zsh-syntax-highlighting

Add the following lines at the end of ~/.zshrc in order for Antibody to load the plugins:

source <(antibody init)
antibody bundle < ~/.zsh_plugins.txt

Requirements of of the plugins I use

Install dependencies: sudo apt -y install fzf jq

Add export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border' at the end of ~/.zshrc

Install LAMP

sudo apt -y install software-properties-common apache2 mariadb-server

Start services: sudo service apache2 start && sudo service mysql start

sudo mysql_secure_installation

sudo apt -y install lsb-release apt-transport-https ca-certificates 
sudo curl -sSL -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

If necessary: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <PUBKEY>

sudo apt update && sudo apt -y upgrade && sudo apt -y autoremove

sudo apt -y install php7.4 libapache2-mod-php7.4 php7.4-{bcmath,bz2,intl,gd,mbstring,mysql,zip,curl,dom}
sudo apt -y install php8.0 libapache2-mod-php8.0 php8.0-{bcmath,bz2,intl,gd,mbstring,mysql,zip,curl,dom}
sudo apt -t buster-backports install php-twig

Set PHP version:

sudo a2dismod php<old-version>
sudo a2enmod php<new-version>
sudo service apache2 restart
sudo update-alternatives --set php /usr/bin/php<new-version>
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
sudo mkdir /var/www/html/phpmyadmin
sudo tar xvf phpMyAdmin-latest-all-languages.tar.gz --strip-components=1 -C /var/www/html/phpmyadmin
sudo cp /var/www/html/phpmyadmin/config.sample.inc.php /var/www/html/phpmyadmin/config.inc.php
sudo chmod 660 /var/www/html/phpmyadmin/config.inc.php
sudo chown -R www-data:www-data /var/www/html/phpmyadmin
sudo systemctl restart apache2

Add blowfish secret inside config.inc.php:

$cfg['blowfish_secret'] = '<my_secret_passphrase>';

Optional: Enable login with root:

sudo mysql -u root
use mysql;
update user set plugin='' where User='root';
flush privileges;
exit;

Additional packages

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

sudo mv composer.phar /usr/local/bin/composer

Install nvm: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

Add the following lines to ~/.zshrc:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Restart the shell

Install the latest node version: nvm install node

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev

wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz

tar -xf Python-3.9.2.tgz

cd Python-3.9.2

./configure --enable-optimizations

make -j 2

sudo make altinstall

Add virtual hosts

Set symbolic link for sites:

sudo mv /var/www/html /var/www/html.bak && \
sudo ln -s /mnt/b/Projects /var/www/html

Create config file: sudo nano /etc/apache2/sites-available/<HOSTNAME>.conf

Paste the following content with the appropriate values:

<VirtualHost *:80>
    ServerName **HOSTNAME**
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/**PATH**

    <Directory /var/www/html/**PATH**>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site: sudo a2ensite <HOSTNAME>.conf

Optional:

Enable all available sites:

find /etc/apache2/sites-available/ -type f -and -not -name "*default*" -exec a2ensite {} \;

Reload apache2: sudo service apache2 reload

Enable X Server

Add these lines to ~/.zshrc:

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1

Recommended X Server until native support arrives: VcXsrv or X410

Git config

Example config inside ~/.gitconfig:

[core]
    excludesfile = /path/to/.gitignore_global
    autocrlf = true
[user]
    name = <name>
    email = <email>
    signingkey = <id>
[commit]
    gpgsign = true

Generate GPG Key

gpg --full-generate-key

Get ID: gpg --list-secret-keys --keyid-format LONG

Add SSH connections

Generate SSH key: ssh-keygen Copy SSH key to server: ssh-copy-id <user>@<host>

Note: Reset known host for <host> if necessary:

ssh-keygen -f "~/.ssh/known_hosts" -R "<host>"

Example config for SSH connection inside ~/.ssh/config:

Host github github.com
    HostName github.com
    IdentityFile ~/.ssh/id_rsa_github
    User <user>

Shrink WSL2 Disk

Default VHDX Location for Debian is:

"C:\Users\user\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\ext4.vhdx"
  1. Ensure that WSL2 is not running: wsl --terminate <Distro>
  2. Launch Diskpart: diskpart
  3. Specify VHDX file path: select vdisk file=<path to VHDX file>
  4. Compact WSL2: compact vdisk

Note: If it is not working, because the file is still in use, try: wsl --shutdown

Defaults:

Used font: JetBrainsMono NF

"defaults": {
    "useAcrylic" : true,
    "acrylicOpacity" : 0.6,
    "backgroundImageOpacity" : 0.2,
    "backgroundImageStretchMode" : "uniformToFill",
    "fontFace": "JetBrainsMono NF",
}

Example Custom icon & background:

"icon": "B:/WSL/debian.png",
"backgroundImage" : "B:/WSL/terminal_bg_debian.png",

Add SSH to profiles:

"commandline": "wsl -d Debian ssh <user>@<host>"

Export & Import

WSL

Run the following commands inside PowerShell or CMD:

Export: wsl --export <Distro> "C:\Path\to\WSL.tar"

Import: wsl --import <Distro> "C:\Path\to\WSL" "B:\Path\to\WSL.tar"

SQL

Export: mysqldump -u root -p --all-databases > alldb.sql

Import: mysql -u root -p < alldb.sql

Files

Export: tar -czvf html.tar.gz /var/www/html

Verify: ls -l html.tar.gz

Import: tar -xzvf html.tar.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment