Skip to content →

Accessing & Installing Web Server (LEMP) on Amazon EC2 Instance

On my last post, I wrote about how to create and launch an Amazon EC2 instance on Amazon Web Services (AWS). We had created an Amazon EC2 instance under AWS Free Tierand launched it successfully. Now we will go through step-by-step guide to SSH into the instance and then install Nginx, MySQL & PHP (LEMP) – which will be enough for us to host a simple PHP application.

We can use the public IPv4 address or the public IPv4 DNS name to SSH into the instance that we just created.

Amazon EC2 - Instance Summary
Amazon EC2 – Instance Summary

Make sure to set access permission to the ports by which we will need to communicate under the security group inbound rules. For example, here we are allowing access from anywhere to port 22 via SSH/TCP; so that we can SSH into the instance.

It’s time to login to our instance via SSH using the .pem file – we have gotten it earlier while launching our instance.

We will need to change the permission of the file first by running the following command:

Replace the file name and location according to your environment.

Then we will run the SSH command to login:

You will need to replace the private key file name and server IP address as per your environment and information.

After successful login, you will see something like this on your terminal.

AWS EC2 - SSH into the instance
AWS EC2 – SSH into the instance

We have successfully got access to our instance and now we will install Nginx, MySQL and PHP to prepare a simple web server so that we can run and host our PHP application there.

First of all, we will install Nginx by running the following command:

This will install the Nginx server on our instance. Next we will install MySQL.

You will need to provide some information like MySQL root password while installing the MySQL server.

Next we will run sudo mysql_secure_installation to configure and set some basic settings for the MySQL server.

At last, we will install PHP (php-fpm) to our instance along with some other modules.

Next, we will need to configure the default virtual host so that we can access our web pages.

Run the following command:

Edit the file according to your need and you will be ready. We need to tell Nginx that all our PHP files should be run by php-fpm. Here’s the information from my file:

You need to restart Nginx by running the following command and then you can visit your Public DNS name or domain name address to see the default folder of your web server.

Hope this helps. If you have any questions, feel free to ask on the comment section.

 

Published in Coding

2 Comments

  1. Obayedul Islam Rabbi Obayedul Islam Rabbi

    Thanks for share with us

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.