Making a local MicroK8s environment available externally (Part 3 - NGINX and phpMyAdmin)
Tags: nginx, phpmyadmin, devops, microk8s
Published: 2022-06-15 11:03:12

If you're following along with the steps to get our MicroK8s environment up and running this part isn't strictly necessary, but getting [phpMyAdmin](https://www.phpmyadmin.net/) up and running can make the administration of our MySQL installation easier.

As part of this, we'll also look at installing the [NGINX](https://www.nginx.com/resources/wiki/) webserver and enable this through the built in firewall in Ubuntu.

If you haven't already `SSH` into our VM using windows terminal, in my case using the following command.
```
ssh pete@pl-k8s-vm-1
```
## Install NGINX

To act as our webserver for hosting phpMyAdmin we're going to install NGINX.

This should be pretty quick and easy, simply run the following,
```
sudo apt update
sudo apt install nginx
```
## Enable Firewall access

Now we've installed NGINX run the following
```
sudo ufw app list
```

This will provide us with a list of applications registered with the firewall that we can allow access to.  In this instance we'll just enable `Nginx HTTP` as we don't require `https` access at this point.

...oh, and while we're here we'll also allow OpenSSH access as well.

Run the following commands.
```
sudo ufw allow 'Nginx HTTP'
sudo ufw allow OpenSSH 
```
Once this is done we can check the status to make sure we're allowed access through the firewall with the following command.
```
sudo ufw status
```
This should return something like the following.

![UFW Status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ay1j0s63iniepalqnbnl.png)

Once this is done we can check that the webserver is up and running by launching a web browser on our host machine and simply inputting our VMs hostname, in my case `http://pl-k8s-vm-1`, and hopefully you should see the welcome to nginx page.

![Welcome to NGINX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3byrhr3mdb3a3l8kx86j.png)

## Installing PHP

Before we can install `phpMyAdmin` we need to go ahead and install `PHP` and then setup our `NGINX` webserver to serve php pages.

Run the following command to install PHP.
```
sudo apt install php-fpm php-mysql
```
After this we need to tell `NGINX` to process our php pages so run the following to edit the config.
```
sudo nano /etc/nginx/sites-enabled/default
```
Within the editor find the line referencing the index files that are processed:

![NGINX Index Before](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9uhfdzdqosvjnj7rv1sc.png)

And add
```
index.php
```
to the list:

![NGINX Index After](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6v37scmei396w4rkjfj.png)

In the same config file find the reference to `PHP scripts to FastCGI server`:

![NGINX PHP Before](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nijfi4it3vio3u1a167i.png)

And add the following.
```
location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
```

![NGINX PHP After](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6aiwiww9efulvj9hh25v.png)

Exit out of nano `CTRL+X, Y, ENTER`, and the get `NGINX` to reload its config with the following command.
```
sudo systemctl reload nginx
```
Once this is done lets quickly test if `PHP` is being correctly processed by `NGINX`.

Run the following command to create a demo php file in the route of the webserver.
```
sudo nano /var/www/html/index.php
```
And then add the following line of code.
```
<?php phpinfo(); ?>
```

![NGINX Index PHP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppuy259ej6nas52tyqbq.png)

Exit out of nano `CTRL+X, Y, ENTER`, reload the url we originally used to test our `NGINX` install, in my case `http://pl-k8s-vm-1/`, and hopefully you should see the PHP Info page:

![NGINX phpInfo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/404pv7z9cek897ts4aff.png)

## Install phpMyAdmin

We're almost there, just `phpMyAdmin` left to setup.

Run the following command.
```
sudo apt install phpmyadmin
```
The installation will run and you'll be presented by a wizard with options to choose from.  The first one will ask you to select which webserver to use, now `NGINX` won't be in the list, but we won't let that stop us, choose `apache2` instead by pressing `SPACE`, `TAB` then `ENTER`.

![phpMyAdmin Install 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fsdozomzjo6tyrlc5xj1.png)

Next we'll be asked to configure phpMyAdmin:

![phpMyAdmin Install 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/id3wdjb7ppldn61i235h.png)

press `ENTER` on `YES` and in the next screen set a password of your choice for phpMyAdmin to use to connect to the database server.

**Password validation**

If during the `MySQL` installation you choose to enable a 'Validate Password plugin' you may receive `password does not satisfy the current policy requirement` errors when trying to create the phpmyadmin user.  If this is the case the easiest way I found to fix it was to choose the option to ignore the error then log into `MySQL` with the following command.
```
mysql -u root -p
```
Run the following.
```
UNINSTALL COMPONENT "file://component_validate_password";
```
and exit out.

You can then run the following to remove and then re-install `phpMyAdmin`.

```
sudo apt remove phpmyadmin
sudo apt install phpmyadmin
```
This time your password should be accepted.  If you then want to re-enable the password validation you can log back into `MySQL` and run the following.
```
INSTALL COMPONENT "file://component_validate_password";
```

**Continuing the phpMyAdmin install**

Once the install has completed we want to create a link from the phpmyadmin share to the root of our `NGINX` webserver by running the following command.
```
sudo ln -s /usr/share/phpmyadmin /var/www/html
```
After this we should be able to access `phpMyAdmin` by going to the following address `http://pl-k8s-vm-1/phpmyadmin`

![phpMyAdmin Login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8gaubooi6mz4pdj2pdl2.png)

We can then use the account we setup in Step 2, mine was `pluser` with a password of `5ecurePassw0rd!`, to access our `MySQL` installation with `phpMyAdmin`.

![phpMyAdmin Home](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x4ttez1e4dh1zy0naaf.png)

## Next Steps

I think we've got everything in place now so it's about time we got something up and running in our new Kubernetes environment.  Lets head over to Part 4 to get our docker containers up and running.

Pete

[![Buy Me A Coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whmy70aj858tvqekv5n7.png)](https://www.buymeacoffee.com/panachesoftware)