How to Install LAMP (Linux, Apache, MySql & PHP) on Azure with Ubuntu 18.04

cloud
Published on June 24, 2020

This tutorial discusses installation of LAMP stack in an Azure virtual machine. The following topics are included :

  • Launch a virtual machine on Azure, running Ubuntu 18.04
  • Connect to Azure virtual machine from Windows / Mac / Linux
  • Updating package list on virtual machine
  • Install Apache 2.4
  • Install MySQL 8.0
  • Install PHP 7.4 & common PHP extensions
  • Install phpMyAdmin 4.9
  • Connect Virtual Machine via FTP

Step 1 - Launching a Virtual Machine

  1. Log in to https://portal.azure.com/

  2. Click on Virtual machines option.

  3. Click on button Create virtual machines.

  4. The next page would present a form where we need to enter details of the VM.

  5. Select Subscription & Resource group (create new one if required).

  6. Enter Virtual machine name

  7. Choose Image as Ubuntu Server 18.04 LTS

  8. Choose Size of the VM (alternately click Select size to see the list of all plans available).

  9. Choose Authentication type as Password

  10. For the sake of this tutorial, we recommend to leave the Username as default (AzureUser). Further steps in the tutorial will need this username.

  11. Enter Password and confirm it. Further steps in the tutorial will need this password.

  12. For Select inbound ports, add the HTTP rule also.

  13. Selected options would look something like the below :

  14. Click on Review + create button at the bottom of the page (we are going with default for rest of the other options).

  15. In the next page after Azure validates the entered information, click on the Create button to start creating the VM.

  16. In a couple of minutes, the VM should be created.

  17. In the VM settings, its Public IP address can be seen. Further steps in the tutorial will need this public IP address.

Now that the virtual machine is created, we need to connect to it from our system.

Step 2.1 - Connecting to Azure Virtual Machine - From Windows

We can use PuTTY application to connect to the VM. PuTTY is included in Windows by default.

We will need the public IP address of the VM, Username & Password to connect via PuTTY.

  1. Open PuTTY (Start > All Programs > PuTTY > PuTTY)

  2. In the Category panel on the left, choose Session.

  3. For the Host Name field, enter the public IP address of the VM.

  4. Port is 22

  5. For Connection type choose SSH

  6. Click on the Open button, and login with the username & password.

Step 2.2 - Connecting to Azure Virtual Machine - From Linux / Mac

A secure remote connection with our virtual machine is established using SSH.

We will need the public IP address of the VM, Username & Password to connect via SSH.

  1. The connection is initiated using the following command :

    sudo ssh AzureUser@vm-ip-address

    Default username is AzureUser.

    Replace vm-ip-address with the public IP address of the virtual machine.

  2. If a warning is prompted, type yes.

  3. Enter the VM password

  4. When the connection has been successfully established, the terminal of the virtual machine will be shown.

Step 3 - Update Package List

The package list needs to be updated by running the following command. This will ensure the latest version of applications are fetched while installing.

sudo apt-get update

Step 4 - Install Apache

  1. Apache is installed by executing the following command.

    sudo apt-get install apache2
  2. A successful installation of Apache can be tested by visiting the public IP Address of the virtual machine

    The Apache welcome page will be shown if the installation is successful.

Step 5 - Install MySQL

  1. MySQL is installed by executing the following command :

    sudo apt-get install mysql-server
  2. The database is then secured by executing the following command :

    sudo mysql_secure_installation
  3. We would then be asked whether we need to setup VALIDATE PASSWORD plugin ? We selected no.

  4. Set the password for the root MySQL account.

  5. Remove anonymous users? We selected yes.

  6. Disallow root login remotely? We selected no.

  7. Remove test database and access to it? We chose yes.

  8. Reload privilege tables now? We again chose yes.

  9. A successful MySQL installation can be checked by executing the following command:

    sudo mysql -u root -p

    To login into MySQL, enter the password and we will be greeted by the mysql prompt.

    Type exit to get out of MySQL, back to installing the next application.

Step 6 - Install PHP with Common Extensions

  1. PHP is installed along with some of the commonly used extensions by executing the following command :

    sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip
  2. The Apache service needs to be restarted after installing PHP using the following command:

    sudo service apache2 restart

Step 7 - Install phpMyAdmin

  1. phpMyAdmin is installed using the following command:

    sudo apt-get install phpmyadmin
  2. In the next screen, apache2 is selected as the webserver.

    IMPORTANT : Note that, space-bar needs to be pressed to select the option and tab key is used to move to the Ok button.

    Correctly selected Apache will look like :

  3. In the next screen for dbconfig-common option, choose Yes.

  4. The password for phpmyadmin is set and confirmed in the next prompt and after.

    (This password is set for the phpmyadmin MySQL user. This is different from the root user that came up while installing MySQL)

  5. Successful installation of phpMyAdmin can be checked by visiting the /phpmyadmin URL path in the public IP address of the virtual machine.

    Enter phpmyadmin as username, and the earlier created password to log in to phpMyAdmin.

Step 8 - Connect Virtual Machine via FTP

In this step we are going to set up FileZilla to connect to our Azure virtual machine.

  1. Open Filezilla. Under File > Site Manager setup the credentials needed to upload files.

    • Protocol is SFTP.

    • Host is public IP Address of virtual machine.

    • Logon Type is Normal.

    • User is the virtual machine username (by default it is AzureUser unless it was changed while creating the VM).

    • Password is the same as the one that was set while creating the VM.

  2. If FileZilla prompts about the server's unknown host key, click Yes.

  3. FileZilla will then connect to Azure virtual machine.

  4. We will be taken to default directory - /home/ubuntu. We can enter /var/www/html to go to the web server directory.

  5. To be able to upload files using FileZilla we need to change the ownership and access modes of the server directory.

    This is done by SSHing into Azure virtual machine and typing the following commands.

    sudo chown -R AzureUser /var/www/html
    sudo chmod -R 755 /var/www/html
    
  6. The installation can be tested by creating a file using FileZilla and entering the following simple code:

    <?php echo phpinfo(); ?>

    Navigate to the created file from the browser to get output like the one shown below.

    This implies that the setup is working perfectly.

Setup is Finished

Congratulations! We have a working Azure Virtual Machine instance with LAMP stack. In case of any issues, delete the VM and create a new one.

Comments
Loading Comments