How To Setup VM On Azure For PHP Websites

In this article, we are going to see how to setup a Virtual machine on Azure for PHP & MySQL based websites.

Why Use VM?

You can host a Website using a Web app service on Azure right? VMs provides you your own mini Server, where you get full remote desktop access and you can do whatever you like. You don’t have to be limited to a Web based control panel. It’s your own baby Server on the Cloud, fully scalable and redundant.

Login to the Azure management portal.

Click New, Virtual machines and then Ubuntu Server 14.04 LTS as in the following screenshot,

 
Fill the Basic details for your VM

Name (Name of your Virtual Machine), User name (Required at time of Login ), Authentication type (Select Password), Password (Required at time of Login).

Other details are given in following screenshot:

Choose your Virtual Machine Size

You can select as per your requirement, if you have no idea how much size you are going to require. Don’t worry, you can change this after the setup. In this tutorial, I am using DS1 Standard.

 
Configure Optional features

If you want to change the domain name then click on the domain name otherwise, if you don’t want to change the default, leave it as it is. (You can’t use your custom domain here. Thus, don’t worry about it) Just click OK.



Summary

Don’t go deep into the details and just hit OK.



Now, check the Notification Window and wait until the setup is completed.



This will Pop up when the deployment is completed.



Now, click Virtual Machines (classic), <Your_VM_name> is shown below in the following screenshot: 
 

Now download PuTTY from this link.

Open PuTTY

Put the DNS and the port. Give a name to the session, click save and click open.

 

Click yes ( If this security alert appears), as shown below:

 

Login to your VM 

Enter the username and the password you have configured while creating the VM (Don’t worry if the password doesn’t show up. Just type it and click enter).



Congratulations! You are now into a Linux Server. Don’t be shocked, there’s no graphical client like Windows Remote Desktop.

Now, let’s do some required setups.

Lets move to the elevated command line. Use “ sudo su ” command. (* You can paste in the PuTTY Window by just clicking the right button of the mouse.

You see “root@<your_app_name>” so you are in root mode (Windows equivalent of an Administrator privilege).

Upgrade the system

Run “ do-release-upgrade ” command to check if there is any upgrade pending or not.

 

Setup automatic security patch installation by running “ unattended-upgrade ”.

Add a Swap file

Linux VM doesn’t come with Swap file. It means you only have the 768 MB RAM to play with. If an Application exceeds the RAM, it crashes. Hence, you need to add a swap file, and you get plenty of virtual memory to play with.

Run “ swapon –s  ” command to check if there’s any swap file or not. If none, it will look as shown below:

Let’s create one

Run “ dd if=/dev/zero of=/swapfile bs=1024 count=512k ” (This will create a 512 MB empty file).



Now, lets make that file the swap file :

Run “ mkswap /swapfile ” then “ swapon /swapfile ” command. Now, let’ s check if something happened or not. Run “ swapon –s ”

You can see now the swap file is there and turned on.

Wait, we still have a problem. When your VM will reboot, the swap will be gone. Hence, let’s make it permanent.

Run “ nano /etc/fstab ” command

Now, go to the end of the file and add the line, given below:

  1.   /swapfile        none       swap         sw       0        0


Press Ctrl X, then Y and enter. Yes, it is not Windows. It takes a while to get used to it.

After exiting, run this command, shown below:

  1. chown root:root /swapfile

  2. chmod 0600 /swapfile

This will set the proper permission to the swap file.

Now, let’s reboot and make sure the swap file is there.

Run “ reboot ” command .

Afterwards, the system is back and you have logged back in and run the “ sudo su ” command to get into the root access, run “ swapon –s ” to check; if the swap is working or not.

You can also use “ free ” to see how much RAM and swap are being used. It is a very handy command to check if your Server is suffering from  a memory issue.

 

Now, install LAMP Stack.

LAMP stands for Linux, Apache, Mysql and PHP. That’s the pre-requisite for hosting PHP website like WordPress to run .

Run “ apt-get install tasksel ” then “ tasksel ” command.

It will start a wizard to configure the LAMP Server and install all the stuff it needs.

User Arrow keys to navigate and the space to select.



You should be asked for the password for the MySQL root user account, as given below:



Once it is complete, you should have a LAMP stack ready and there will be a Web Server running that you can test.

You need to open the HTTP port 80 on your VM from Azure firewall, as shown below:



Wait at least 2-5 mins after Azure says it is done. I can not hit the site immediately after getting the complete message. I had to wait for some time.



Let’s create a test.php file to make sure PHP is working.

Move to Base directory “ cd /var/www/html ” then “ nano test.php ”



Afterwards, put this into the file, as shown below: 
  1.   <?php      phpInfo();       ?>

Ctrl+X, Y and enter to get out of nano.

Now Visit:  <Your_CloudApp_URL>/test.php

 
Now, let’s setup a mailing service.

You need to install SMTP Server in order to send an email from your Server. Postfix is the name of the Server. Here’s how to install this,

Run "apt-get install postfix".

 

Select “Internet Site”, as shown below:



You will be asked to put the fully qualified DNS for your Website. If you are finally planning to host your site on a domain like example.com,, put it here or you can put Azure DNS for now and later, add your final domain in the Postfix config file.



Let’s test sending an Email. First, install a mail utility to send an Email, shown below:

apt-get install mailutils

Once it is installed, test sending an Email, shown below:

  1.   echo "Test mail from postfix" | mail -s "Test Postfix" [email protected]
Conclusion

In this article, we have taught you to setup a Linux VM in Windows Azure. We have also learned how to install LAMP, configure and tune Apache and MySQL.

What's Coming

In the next post, I will show you how to configure your own domain, optimize your Server, detect and restart the Web Server on the failure, protect your Website from a Brute Force attack, and  configure FTP for VM.