Learn about the Deployment of Web application using AWS
Amazon EC2 allows individuals to deploy various applications. Using Amazon EC2, you can launch as many or as few virtual servers as per the given circumstances. Hence, in this blog, I will be discussing on deploying a PHP application using Amazon EC2 instance.
What are the pre requisitions before you go through this blog?
Before you dive into this blog, please make sure that you have created your own Amazon EC2 instance and be connected to it to view the stepwise procedure for deploying a PHP application. Adding to that, you should also have your PHP application in the GitHub repository. Likewise, familiarity with the Linux environment can help to provide a better understanding of the blog content. However, I will further elaborate on all the required Linux commands.
Note: If you have not created your Amazon EC2 instance yet, you can go through my previous blog on the stepwise procedure for creating an Amazon EC2 instance and connecting to the Amazon EC2 instance with the link provided below.
What is Amazon EC2 and how to launch an EC2 instance: Step by step guide?
Amazon Elastic Cloud Compute (EC2) is a web service that allows you to deploy your applications in the cloud. Amazon…
Note: If you are willing to connect to your Amazon EC2 using PuTTy, you can open the link provided below.
How to use PuTTy to connect to your Amazon EC2 instance?
PuTTY is an SSH and Telnet client, which was developed for the Windows platform by Simon Tatham. Here, in this blog, I…
Note: You should also have your PHP application in your GitHub repository. So, if you have not created your PHP application, then for the demo, you might as well use the application provided in the GitHub repository below.
Stepwise procedure to deploy a PHP Application using Amazon EC2 Instance
Step 1: First of all connect to your Amazon EC2 instance as shown in the image below.
Step 2: Provide command as ‘sudo apt-get update’ in your SSH where you have successfully established a connection with your developed Amazon EC2 instance.
Note: ‘sudo’ also known as ‘superuser’ grants root or Administrator privileges to the command which is to be executed. In all the conditions, root-level access might not be required. However, for the execution of some commands, it might require the admin level of access and in these types of situations, ‘sudo’ can be used. Hence, if you are using ‘sudo’ in front of any command, it will be executed with high privilege.
Here, ‘sudo apt-get update’ comprises of ‘sudo’ as it requires superuser privilege to update the packages. Likewise, ‘apt-get update’ command is used to update all the list of available packages from the remote repositories. In simple terms, ‘sudo apt-get update’ goes to the remote repositories to check for the updates of packages and in case if a newer version is found then, your package is now updated or installed to the newer version.
Step 3: Now, provide command as ‘sudo apt-get install apache2’.
Note: Before apache is installed first of all let’s grasp the knowledge regarding Apache. So, what is apache? Apache is an open-source and free web server software that is maintained and developed by the Apache Software Foundation. Today, Apache is used to power the website which allows website owners to serve their contents on the web.
Hence, after updating the local repositories, you should provide command as ‘sudo apt-get install apache2’ for installing the apache2 package in your Amazon EC2 instance.
Step 4: After the command ‘sudo apt-get install apache2’ is executed, you are asked if you want to continue. Here, type ‘y’ as shown in the image below in order to provide the confirmation to carry out the installation.
Step 5: Now, execute the command i.e. ‘sudo apt-get install php libapache2-mod-php’.
Since the web application is developed in PHP, it is also one of the required components in the Amazon EC2 instance. So, without PHP, the code which is applied for presenting the dynamic content of the website cannot be viewed. Therefore, to install PHP in EC2 instance, ‘sudo apt-get install php libapache2-mod-php’ command is executed.
Step 6: After the command, ‘sudo apt-get install php libapache2-mod-php’ is executed, you are required to confirm the installation. Therefore, you are required to enter ‘y’ as shown in the image below for confirming the installation.
Step 7: Here, ‘sudo apt install php-dev libmcrypt-dev php-pear’ command is executed as shown in the image below.
‘sudo apt install php-dev libmcrypt-dev php-pear’ command installs all the required packages that are required for installing ‘mcrypt’.
Step 8: After the execution of command ‘sudo apt install php-dev libmcrypt-dev php-pear’, you are required to confirm the installation by typing ‘y’ as shown in the image below.
Step 9: Here, the command ‘sudo pecl channel-update pecl.php.net’ is executed to install ‘mcrypt’ PHP module in the EC2 instance with Ubuntu OS.
Note: ‘mcrypt’ is used as a replacement of the old crypt() package and crypt(1) command, with the extension. ‘mcrypt’ facilitates developers to use a wide range of encryption functions. Furthermore, ‘php-mcrypt’ extension serves as an interface between PHP and mcrypt.
Step 10: Finally, ‘sudo pecl install mcrypt-1.0.1’ is executed to install the ‘mcrypt’.
Step 11: Here, you are required to confirm the installation. So, to confirm the installation, type ‘y’ as shown in the image below.
Step 12: ‘sudo service apache2 restart’ command should be executed to restart the ‘apache’ server.
Step 13: Copy ‘Public DNS (IPv4)’ of your EC2 instance as shown in the image below and open it in the ‘New tab’ of the browser. For instance: ‘ec2–34–204–60–105.compute-1.amazonaws.com’ is the Public DNS of the selected EC2 instance which is shown in the image below.
Step 14: After you open the Public DNS in the new tab, scroll down until you find the ‘Document Roots’. Here, you can see that the default Ubuntu document root is ‘/var/www/html’.
Step 15: Now, execute the command as ‘cd /var/www/html’ to go to the html directory.
Note: If you are willing to learn some basic shell commands, you can further read my blog provided below.
Some Basic Shell commands
Before we dive into Git commands, it would be better for us to become familiar with some of the basic shell commands…
Step 16: Here, first of all ‘ls’ command is executed to view all the flies and directories that are present in html directory. Then, ‘sudo rm index.html’ command is executed to remove the ‘index.html’ file. After the execution of ‘sudo rm index.html’ command, from the image provided below, you can see that the ‘index.html’ file is removed.
Step 17: After the ‘index.html’ file is removed, go to your web browser where you had opened the Public DNS of your EC2 instance. Here, you are required to ‘Refresh/Reload’ the link to obtain the output as shown in the image below.
Step 18: Now, go for further testing to verify if all the installation is as per the requirement. Here, you can now develop a PHP file for testing purposes and provide command as ’sudo nano test.php’ to develop a ‘test.php’ to provide PHP codes.
Step 19: After the execution of command ’sudo nano test.php’, a text editor appears as shown in the image below. Here, in the text editor you can provide code as:
echo “Hello World!”;
Note: The above PHP code displays ‘Hello World!’ as your output.
Step 20: Now, press ‘Ctrl + X’ to exit. Then, enter ‘y’ to save the updates that had been made in the file. Finally, press the ‘Enter’ key to save changes in ‘test.php’ file.
Step 21: After you have saved your test.php file, go to your Public DNS of your EC2 instance and ‘Refresh/Reload’ the link. Then, you can see the currently developed ‘test.php’ as shown in the image below. Now, you are required to click on ‘test.php’ as presented in the image.
Step 22: Now, you can see that ‘Hello World!’ is displayed in the browser after you have opened your ‘test.php’ file.
Step 23: After your testing is successful, go to your GitHub repository which comprises of application developed using PHP. Here, you are required to copy the link to ‘Clone’ your repository with HTTPS.
Step 24: Now, in your SSH, execute the command as ‘sudo git clone https://github.com/hrishavtandukar/PHPTrialApplication.git’ to clone your GitHub repository.
Note: As per the circumstances you might as well have to provide your GitHub username and password in this stage.
Step 25: Finally, you need to update the URL to view the output of the cloned PHP application. Here, you are required to add in the file location of your PHP application in the Public DNS URL which you had previously opened in the browser. For instance: In the current case scenario, the URL of Public DNS is to be updated from ‘http://ec2-34-204-60-105.compute-1.amazonaws.com/test.php’ to ‘http://ec2-34-204-60-105.compute-1.amazonaws.com/PHPTrialApplication/demo.html’.
Note: In the current scenario, the folder name and the file name are provided respectively in the Public DNS URL i.e. PHPTrialApplication/demo.html.