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

SSH Login 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.

Allocate IP Address to EC2 Instance

First of all, we will need to allocate an IP address to our instance. To do that we will need to go to the “Elastic IPs” section and click on the “Allocate New Address” button.

Allocate New Elastic IP

You will be allocated an IP address. Next, we will need to associate the IP address with our instance.

Associate IP Address with EC2 Instance

This IP address will be you public IP address to access your instance or server. Now we will need to SSH into this IP address to have control of the instance or server.

Security Group on EC2 Instance

You may also need to modify/add a security group to determine the access permission of the instance. Make sure to set access permission to the ports by which we will need to communicate. 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 got 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.

SSH Login EC2 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 php-fpm to work with our Nginx server.

To do this, we need to open the php.ini file and edit it. We may run the following command to do this:

This command will open the file on nano editor.

We will need the find the line cgi.fix_pathinfo and uncomment it. Then set the value to 0.

While using nano editor, you can type CTRL+W to search within the file.

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.

 

Author: Muhammad Sumon Molla Selim

Homo sapiens by Born. Coder by Heart. Entrepreneur by Passion. Founder of Kodeeo & Lets Learn Coding. Software Engineer at GG, Bashundhara Group.

2 thoughts on “Accessing & Installing Web Server (LEMP) on Amazon EC2 Instance”

Leave a Reply