LEMP stack + phpmyadmin with Ubuntu 20.04 on azure virtual machine

LEMP stack configuration with phpmyadmin on Ubuntu 20.04 azure virtual machine.

1. Create an ubuntu 20.04 virtual machine on azure

Create an ubuntu 20.04 virtual machine on azure
Ubuntu logo

For the first step we need to create first the ubuntu virtual machine, you  can follow the guide here:

  1. Log in to your azure portal account
  2. navigate to azure services and click virtual machine

    azure services

  3. Click create and select virtual machine

    virtual machine menu

  4. fill up the form

    form 1

  5. create a new resource group (you can link all services with this group)
  6. select ubuntu 20.04 for the image
  7. for size you can use the Standard D2s_v3
  8. use ssh as authentication type
  9. for inbound ports select "Allow selected ports"
  10. for the ports select HTTP, HTTPS, and SSH

    form 2

  11. click Review + create
  12. fill up the form and click create
  13. wait for azure to configure your vm
  14. and your done (Congratiolations !!!)

2. Install nginx

Install nginx
NGINX logo

Why choose LEMP stack instead of a LAMP one?

For those who are wondering what is the difference between a LEMP and a LAMP stack for the main part it's their web server LEMP uses NGINX (Engine-x) while LAMP uses APACHE. why choose NGINX then well for the first reason is preferrence, maybe for you it's easier to use APACHE because you are trained in that environment but as a preference of mine i use NGINX. second is NGINX is easier to configure and maintain than APACHE because most of nginx runned websites are repairable by just some line in nginx it self while APACHE referrences a lot in order to fix or maintain a certain aspect of the stack.

Let us first install NGINX (Engine -  X)

  1.  Make sure you have administrator previleges: sudo -s
  2.  run sudo apt update
  3.  then sudo apt install nginx

    When prompted, enter Y to confirm that you want to install Nginx. Once the installation is finished, the Nginx web server will be active and running on your Ubuntu 20.04 server.
  4.  enable ufw run : sudo ufw enable
  5.  then sudo ufw app list


    The output should be like this:
    To                         Action      From
    --                         ------      ----
    OpenSSH                    ALLOW       Anywhere
    Nginx HTTP                 ALLOW       Anywhere
    OpenSSH (v6)               ALLOW       Anywhere (v6)
    Nginx HTTP (v6)            ALLOW       Anywhere (v6)
  6.  now try http://server_domain_or_IP

3. Install MySQL

Install MySQL
MySQL logo

Now that you have a web server up and running, you need to install the database system to be able to store and manage data for your site. MySQL is a popular database management system used within PHP environments.

  1.  run sudo apt install mysql-server
    When prompted, confirm installation by typing Y, and then ENTER.
  2.  now sudo mysql_secure_installation
    This will ask if you want to configure the VALIDATE PASSWORD PLUGIN.
    Answer Y for yes, or anything else to continue without enabling.
    If you answer “yes”, you’ll be asked to select a level of password validation. Keep in mind that if you enter 2 for the strongest level, you will receive errors when attempting to set any password which does not contain numbers, upper and lowercase letters, and special characters, or which is based on common dictionary words.
  3.  For the rest of the questions, press Y and hit the ENTER key at each prompt. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.
  4.  try sudo mysql
  5. This will connect to the MySQL server as the administrative database user root, which is inferred by the use of sudo when running this command.
  6. To exit the MySQL console, type: exit

4. Install php-fpm

Install php-fpm
PHP logo

You have Nginx installed to serve your content and MySQL installed to store and manage your data. Now you can install PHP to process code and generate dynamic content for the web server.

To install the php-fpm and php-mysql packages, run: sudo apt install php-fpm php-mysql

5. Configuring Nginx to Use the PHP Processor

  1. Create the root web directory for your_domain as follows: sudo mkdir /var/www/your_domain
  2. Next, assign ownership of the directory with the $USER environment variable, which will reference your current system user:  sudo chown -R $USER:$USER /var/www/your_domain
  3. Then, open a new configuration file in Nginx’s sites-available directory using your preferred command-line editor. Here, we’ll use nanosudo nano /etc/nginx/sites-available/your_domain
  4. This will create a new blank file. Paste in the following bare-bones configuration:
    server {
        listen 80;
        server_name your_domain www.your_domain;
        root /var/www/your_domain;
    
        index index.html index.htm index.php;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
         }
    
        location ~ /\.ht {
            deny all;
        }
    
    }

  5. Now go to your browser and access your server’s domain name or IP address, as listed within the server_name directive in your server block configuration file:
    http://server_domain_or_IP

6. Install phpmyadmin

Install phpmyadmin
phpMyAdmin logo

You can install phpMyAdmin by using APT to download the phpmyadmin package from the default Ubuntu repositories.

  1. run sudo apt update
  2.  then sudo apt install phpmyadmin
  3. During the installation process, you will be prompted to choose a web server (either Apache or Lighttpd) to configure. phpMyAdmin can automatically make a number of configuration changes to ensure that it works correctly with either of these web servers upon installation. However, because you are using Nginx as a web server you shouldn’t choose either of these options. Instead, press TAB to highlight the  and then press ENTER to continue the installation process.
    Next, you’ll be prompted whether to use dbconfig-common for configuring the application database. Select . This will set up the internal database and administrative user for phpMyAdmin. You will be asked to define a new password for the phpmyadmin MySQL user, but because this isn’t a password you need to remember you can leave it blank and let phpMyAdmin randomly create a password.
  4. run sudo ln -s /usr/share/phpmyadmin /var/www/your_domain/phpmyadmin
  5. try server_domain_or_IP/phpmyadmin

 As for my personal experiece on this part there is always an error so try using the method provided by digitalocean for the most part it works all the time but it is advisable to connect to your server via ssh using putty because the pop-ups do not appear on most console applications. and further configuration can be done if you proceed with the digitalocean referrence.

Drake, M., & Heidi, E. (2021, June 18). How To Install and Secure phpMyAdmin with Nginx on an Ubuntu 20.04 Server. DigitalOcean. https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-20-04-server