Skip to content

Instantly share code, notes, and snippets.

@abobija
Last active February 25, 2024 08:07
Show Gist options
  • Star 79 You must be signed in to star a gist
  • Fork 39 You must be signed in to fork a gist
  • Save abobija/228e36441d31ae17ecc36edc9a47ceb5 to your computer and use it in GitHub Desktop.
Save abobija/228e36441d31ae17ecc36edc9a47ceb5 to your computer and use it in GitHub Desktop.
LAMP stack on WSL2 (Ubuntu 20.04) - Apache, MySQL, PHP, PhpMyAdmin

LAMP stack on WSL2 (Ubuntu 20.04) - Apache, MySQL, PHP, PhpMyAdmin

Apache

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

PHP

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

MySQL

sudo apt-get 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-get 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.

@joaquin-alvarez
Copy link

Great tutorial !! Thank You!

@shameed
Copy link

shameed commented Oct 14, 2021

Thanks you for the steps :)

@abobija
Copy link
Author

abobija commented Oct 14, 2021

No problem. Glad it helps.

@VasylKyryliuk
Copy link

This is the only working tutorial I've seen for WSL2. Thank you so much.

@dreaddymck
Copy link

The above comment convinced me to try and it indeed works. Many thanks.

@grievercr
Copy link

I ran into an issue on 22.04LTS where PhpMyAdmin was giving me an error
root mysqli::real_connect(): (HY000/2002): Permission denied and Connection for controluser as defined in your configuration failed

I found a solution here https://stackoverflow.com/questions/41881123/mysqli-real-connect-hy000-2002-no-such-file-or-directory
By modifying /etc/phpmyadmin/config.inc.php $cfg['Servers'][$i]['host'] = '127.0.0.1'; I was able to get it to work and continue the guide.

@FrozenBlueSky
Copy link

The solution of @vectorcr leads to the point. But at my system the change was better placed in /etc/phpmyadmin/config-db.php as this file stores all the database connection parameters.
Just change: $dbserver='localhost'; to: $dbserver='127.0.0.1';

@raymundopizano
Copy link

Error:
$sudo mysql_secure_installation
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

@sayeed13
Copy link

Error: $sudo mysql_secure_installation ... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

1 - I killed the mysql_secure_installation process

2 - I logged into mysql using:

sudo mysql

of course, sudo asks my system root password. Once I provided the right root password, I'm connected on mysql as root mysql user.

3 - I use my mysql session to run ALTER USER:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'my-secret-password';

4 - exit

mysql> exit
Bye

5 - Run sudo mysql_secure_installation command, and complete steps of securing Mysql.

@raymundopizano
Copy link

Error: $sudo mysql_secure_installation ... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

1 - I killed the mysql_secure_installation process

2 - I logged into mysql using:

sudo mysql

of course, sudo asks my system root password. Once I provided the right root password, I'm connected on mysql as root mysql user.

3 - I use my mysql session to run ALTER USER:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'my-secret-password';

4 - exit

mysql> exit Bye

5 - Run sudo mysql_secure_installation command, and complete steps of securing Mysql.

Thanks!

@SuhaibAln
Copy link

I tried the Allow remote root login command but i got this
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

@eransariel
Copy link

Thank you!

phpmyadmin needed to be added to the apache configuration as follows:
sudo nano /etc/apache2/apache2.conf

Add the phpmyadmin config to the file:
Include /etc/phpmyadmin/apache.conf

then restart apache:
sudo service apache2 restart

@Jiculi
Copy link

Jiculi commented Jan 22, 2023

Thank you

@davidrobertbrt
Copy link

Watch out for permissions if you change the document root from /var/www/html to ~/www

Make sure to add apache2 user (usually www-data for Ubuntu) to the personal group of the current user

sudo adduser www-data $(whoami)
sudo service apache2 reload

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

@davidrobertbrt
Copy link

I tried the Allow remote root login command but i got this mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Try using MariaDB instead of MySQL from Oracle. I found out that the mysql_secure_installation script is broken.

@AudioMegistus
Copy link

AudioMegistus commented May 8, 2023

NOTES --
MySQL:
1.
You may see:
"su: warning: cannot change directory to /nonexistent: No such file or directory"
after running sudo service mysql restart

  • don't worry this is fixed later with sudo usermod -d /var/lib/mysql mysql

when you run mysql_secure_installation, you may see:
“Failed! Error: SET PASSWORD has no significance for user ‘root’@’localhost'”

  • First open a second terminal and run:
    sudo killall -9 mysql_secure_installation
  • then run:
    sudo mysql
  • type the following at the mysql> prompt:
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD';
  • press enter
  • type "exit"
  • run sudo mysql_secure_installation again and use the password you just set

Allow remote login:
sudo mysql -u root -pMyPassword -e "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User = 'root'; FLUSH PRIVILEGES;"
gave the following error/warning:
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
what worked:
sudo mysql -u root -p [press enter, then enter password]

  • at the mysql> prompt, type:
    UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User = 'root'; FLUSH PRIVILEGES;
  • Press enter, type "exit"

--- accessing phpmyadmin via web browser ---

  • copy the phpmyadmin.conf to apache's conf-available directory:
    sudo cp /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf

  • enable the configuration:
    sudo a2enconf phpmyadmin

  • restart apache2:
    sudo service apache2 restart

  • change the $dbserver value in config-db.php
    sudo nano /etc/phpmyadmin/config-db.php
    --- change:
    $dbserver='localhost';
    to
    $dbserver='127.0.0.1';

  • restart apache2:
    sudo service apache2 restart

  • go to localhost/phpmyadmin

  • you can now login with user: root and the password you set during the phpmyadmin setup

@EleaFederio
Copy link

Thank you

@m-pokrovskii
Copy link

cannot make work virtual hosts. localhost/site works though. But site.dev doesn't

@jorge-maikel-sierra
Copy link

Me encanto este paso a paso si se toma el tiempo para entender todos los comandos es lo que que e podido encotrar en interner

@Stiy27
Copy link

Stiy27 commented Aug 2, 2023

Segui todos os passos do guia, no entanto não consegui logar como root e com a senha que configurei.
Então, para resolver esse problema, editei o arquivo CONFIG.INC.PHP em /etc/phpmyadmi, removendo o comentário em , removi isso -> "//", $cfg['Servers'][$i]['AllowNoPassword'] = TRUE;

Assim consegui logar como ROOT sem o uso de senha e criei outro usuário no banco de dados para o phpmyadmin, logo após voltei a colocar o comentário: //$cfg['Servers'][$i]['AllowNoPassword'] = TRUE;

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