Skip to content

Instantly share code, notes, and snippets.

@appel
Forked from abobija/wsl2-ubuntu-lamp.md
Last active September 24, 2024 19:34
Show Gist options
  • Save appel/03b831ea402345628f1eadd0bdc09579 to your computer and use it in GitHub Desktop.
Save appel/03b831ea402345628f1eadd0bdc09579 to your computer and use it in GitHub Desktop.
LAMP stack on WSL2 (Ubuntu 22.04.3 LTS) - Apache, MySQL, PHP

LAMP stack on WSL2 (Ubuntu 22.04.3 LTS) - Apache, MySQL, PHP

Apache

sudo apt update && sudo apt upgrade 
sudo apt install -y apache2

Install single PHP version

sudo apt install -y php libapache2-mod-php
sudo apt install -y php-curl php-gd php-json php-mbstring php-xml

Install multiple PHP versions

sudo add-apt-repository ppa:ondrej/php
sudo apt update -y
sudo apt libapache2-mod-fcgid -y
sudo apt install \
php7.0 php7.0-fpm php7.0-mysql php7.0-xml php7.0-mbstring libapache2-mod-php7.0 \
php7.1 php7.1-fpm php7.1-mysql php7.1-xml php7.1-mbstring libapache2-mod-php7.1 \
php7.2 php7.2-fpm php7.2-mysql php7.2-xml php7.2-mbstring libapache2-mod-php7.2 \
php7.3 php7.3-fpm php7.3-mysql php7.3-xml php7.3-mbstring libapache2-mod-php7.3 \
php7.4 php7.4-fpm php7.4-mysql php7.4-xml php7.4-mbstring libapache2-mod-php7.4 \
php8.0 php8.0-fpm php8.0-mysql php8.0-xml php8.0-mbstring libapache2-mod-php8.0 \
php8.1 php8.1-fpm php8.1-mysql php8.1-xml php8.1-mbstring libapache2-mod-php8.1 \
php8.2 php8.2-fpm php8.2-mysql php8.3-xml php8.3-mbstring libapache2-mod-php8.2 \
php8.3 php8.3-fpm php8.3-mysql php8.3-xml php8.3-mbstring libapache2-mod-php8.3 -y

sudo service php7.0-fpm start && \
sudo service php7.1-fpm start && \
sudo service php7.2-fpm start && \
sudo service php7.3-fpm start && \
sudo service php7.4-fpm start && \
sudo service php8.0-fpm start && \
sudo service php8.1-fpm start && \
sudo service php8.2-fpm start && \
sudo service php8.3-fpm start

Then restart Apache

sudo service apache2 restart

MySQL

sudo apt install -y mysql-server php-mysql
sudo service mysql restart
sudo mysql_secure_installation
    #> Validate password component: N
    #> New password: MyPassword
    #> Remove anonymous users: Y
    #> Disallow root login remotely: Y
    #> Reload privilege tables now: Y
sudo service mysql stop
sudo usermod -d /var/lib/mysql mysql
sudo service mysql start

Allow remote root login

sudo mysql -u root -pMyPassword  -e "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User = 'root'; FLUSH PRIVILEGES;"

PhpMyAdmin

sudo apt install -y phpmyadmin
    #> Use apache2
    #> Configure db with dbconfig-common: Yes
    #> Random password (leave password blank)
sudo service mysql restart && sudo service apache2 restart

Now you can login into phpmyadmin with username root and password MyPassword.


Additional (optionally)

Disallow root login remotely

sudo mysql -u root -pMyPassword  -e "UPDATE mysql.user SET plugin = 'auth_socket' WHERE User = 'root'; FLUSH PRIVILEGES;"

* If remotely login for root is disallowed then you need to create new MySql user, otherwise you will not be able to login into PhpMyAdmin.

LAMP Control Aliases

cd ~ && touch .bash_aliases
echo 'alias lampstatus="sudo service apache2 status ; sudo service mysql status"' >> .bash_aliases
echo 'alias lampstart="sudo service mysql start ; sudo service apache2 start"' >> .bash_aliases
echo 'alias lampstop="sudo service mysql stop ; sudo service apache2 stop"' >> .bash_aliases
echo 'alias lamprestart="lampstop ; lampstart"' >> .bash_aliases

Now you need to logout/login and then you can use lampstatus, lampstart, lampstop and lamprestart for controling LAMP stack.

Change document root of Apache2

In this example document root will be changed from /var/www/html to ~/www

cd ~
mkdir www
sudo sed -i "s;/var/www;$HOME/www;g" /etc/apache2/apache2.conf
sudo sed -i "s;/var/www/html;$HOME/www;g" /etc/apache2/sites-available/000-default.conf

Now you can go to ~/www and create index.html

Configure Apache2 for Wordpress module rewrite custom permalink structure

First with cd go into your wordpress installation folder and then:

sudo touch .htaccess
sudo chown -v :www-data .htaccess
sudo chmod -v 664 .htaccess
sudo sed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.conf
sudo a2enmod rewrite
sudo service apache2 restart

Now go to WpAdmin -> Settings -> Permalinks. Choose permalink structure and hit "Save Changes". Now .htaccess should be populated with wordpress rewrite rules and conditions.

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