Skip to content

Senaith/Project-2-LEMP-Stack-Implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

Project-2

LEMP-Stack-Implementation

This project will demonstrate the implementation of LEMP stack on AWS EC2 server.

What is LEMP Stack?

LEMP refers to a collection of open-source software that is commonly used together to serve web applications. The term LEMP is an acronym that represents the configuration of a Linux operating system with an nginx (pronounced engine-x, hence the E in the acronym) web server, with site data stored in a MySQL database and dynamic content processed by PHP.

This project will give you a better understanding of what the LEMP stack is and how to implement it on Amazon Web Services (AWS). AWS is the biggest Cloud service provider, and it offers a free tier account that we will be able to utilize for our project. For the purpose of this project, we will be employing EC2 (Elastic Compute Cloud) service.

Setting up your Virtual Environment

In order to complete this project, we need to begin by setting up an AWS account and a virtual server with Ubuntu Server OS.

Step 1: Create free AWS account. Once you have created your AWS account, navigate to the login page and type in your credentials.

step1

Step 2: After signing-in to your AWS account, navigate to the top-right corner of your screen and select your preferred region. This should be the closest region to your physical location.

step2

Step 3: Proceed to the search bar and type in EC2. Select the EC2 service that appears on top.

step3

Step 4: Click on the orange 'Launch Instances' button that appeears on the top right side of your screen.

step4

Step 5: Choose the Ubuntu Server 20.04 LTS (HVM) as the Amazon Machine Image (AMI) from the list of AMIs provided.

step5

Step 6: Select t2.micro as the instance type and click REVIEW AND LAUNCH.

step6

Step 7: and then on the next page choose LAUNCH.

p1

Step 8: There will be a window asking you to create a key pair. Select the 'Create a new key pair' option from the drop down menu and then select "Download". Make sure you know the location the file was downloaded to and don't lose the .pem file. You will need this file in order to connect into your server from your local PC. After you downloaded the key pair, check the box for the acknowledgement, and then click on "Launch Instances".

p2

Step 9: You've successfully launched an EC2 instance!

p3

Click on 'View Instances' to see your EC2 instance.

p4

Connect your AWS EC2 server to your local PC via SSH

Step 1: On your local Linux machine, change your working directory to the location where your downloaded key pair .pem file exists. And use the 'ls' command to check if the file exists in that folder.

p0

Step 2: Use the following commang to change the premissions for the private key file (.pem), otherwise you can get an error 'Bad permissions'.

p7

Step 3: Get your public IP address from your instance.

p65

Step 4: Connect to your EC2 instance by running the following command.

p6

When connected, your ip-address will be shown on your terminal.

p8

Installing the Nginx Web Server

What is Nginx?

NGINX (pronounced "engine X", stylized as NGINX, nginx or NginX, is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

Step 1: Start off by updating your server’s package index.

p9

Step 2: Next, you can use the following command to get Nginx installed.

p10

You will be prompted to confirm your installation enter Y using your keyboard.

p11

Step 3: To verify that your installation was successful and Nginx is now running on your Ubuntu server run the follwing command on your terminal.

p17

The result should look like this if your installation is working perfectly.

p18

If it is green and running, then you did everything correctly - you have just launched your first Web Server in the Clouds!

Step 4: Before we can receive any traffic by our Web Server, we need to open TCP port 80 which is the default port that web browsers use to access web pages on the Internet. We have TCP port 22 open by default on our EC2 machine to access it via SSH, so we need to add a rule to EC2 configuration to open inbound connection through port 80:

Open your AWS Management Console and Click on your EC2 instance. Click on the 'Security' tab.

p12

Step 5: Click on the link under the 'Securty Groups'. You will open a page similar to this one.

p13

Next, click on 'Edit Inbound Rules' box found on the bottom right side of your screen.

Step 6: Click on 'Add Rule' and add the HTTP, TCP port 80 and allow source from anywhere by using 0.0.0.0

p14

p15

p16

Step 7: Our server is running and we can access it locally and from the Internet (Source 0.0.0.0/0 means 'from any IP address'). First, let us try to check how we can access it locally on our Ubuntu shell, run 'curl http://localhost:80' command.

p19

p20

Step 8: Now it is time for us to test how our Nginx server can respond to requests from the Internet. Open a web browser of your choice and try to access following url 'http://Public-IP-Address:80' Replace the 'Public-IP-Address' with the public IP address of you AWS EC2 server. You should be able to see this page displayed on your screen.

p21

Your web server is now correctly installed and accessible through your firewall.

Installing MySQL Server

Now that we have your Nginx server up and running, you need to install a Database Management System (DBMS) to be able to store and manage data for your site in a relational database. MySQL is a popular relational database management system used within PHP environments.

Step 1: Use ‘apt’ to acquire and install this software.

p22

Hit the Y key and ENTER when this prompt appears on your screen.

p23

Step 2: Next, run security script to remove insecure default settings and lock down access to your database system. Start the interactive script by running:

p24

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN. Answer Y for yes, or any other key to continue without enabling. I recommend not enabling this plugin for now and proceed by pressing N or any other key on your keyboard to go to the next step.

p25

p26

Your server will next ask you to select and confirm a password for the MySQL root user (The database root user is an administrative user with full privileges over the database system.)

Step 3: By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. You should remove this by typing 'Y' for each prompt that follows.

p27

Step 4: check whether you can log in to the MySQL console.

p28

To exit MySQL console, run:

p29

Your MySQL server is now installed and secured. Next, we will install PHP, the final component in the LEMP stack.

Installing PHP

You have Nginx installed to serve your content and MySQL installed to store and manage your data. Now you can install PHP to process code and generate dynamic content for the web server.

PHP is a script on the server-side used for the creation of Static or Dynamic Web sites or Web applications. PHP is a pre-processor for hypertext, which used to stand for home pages. The software used to build web applications is an open-source, server-side scripting language. We say a program designed for automated work by writing a script-based language (code lines). It is suitable for the output and construction of dynamic web pages for web applications, e-commerce applications, and database applications.

Let's begin the installation.

To start with, run these two commands at once on your terminal.

p30

Confirm Y for Yes when the prompt appears.

p31

You now have your PHP components installed. Next, you will configure Nginx to use them.

Configuring Nginx to Use PHP Processor

In this project, we will set up a domain called 'projectLEMP', but you can replace this with any domain of your choice.

Step 1: On Ubuntu 20.04, Nginx has one server block enabled by default and is configured to serve documents out of a directory at /var/www/html. While this works well for a single site, it can become difficult to manage if you are hosting multiple sites. Instead of modifying /var/www/html, we’ll create a directory structure within /var/www for the your_domain website, leaving /var/www/html in place as the default directory to be served if a client request does not match any other sites.

Create the directory for projectLEMP using 'mkdir' command

p32

Step 2: Next, assign ownership of the directory.

p33

Step 3: Open a new configuration file in Nginx’s sites-available directory using your preferred command-line editor. For this project we will use nano.

p34

This will create a new blank file where you can write the following configurations.

p35

Once you are done editing enter ctrl+X the press the Y key followed by the Enter key to exit the nano editor.

Step 4: Activate your configuration by linking to the config file from Nginx’s sites-enabled directory.

p36

Step 5: Test your configuration for syntax errors by running the following command.

p37

You should see something like this on your screen.

p38

Step 6: We also need to disable default Nginx host that is currently configured to listen on port 80.

p39

Step 7: Reload Nginx to apply the changes.

p40

Step 8: Congratulations! Your new website is now active!!! But the web root /var/www/projectLEMP is still empty. Create an index.html file in that location so that we can test that your new server block works as expected.

p41

Step 9: Now go to your browser and try to open your website URL using IP address. You should see a screen similar to this one on your browser.

p42

Testing PHP with Nginx

Your LEMP stack is now completely set up. Good job!!!

You can test it to validate that Nginx can correctly handle .php files off to your PHP processor. You can do this by creating a test PHP file in your document root. Open a new file called info.php within your document root in your text editor.

p44

This will open a blank file. Add the following text, which is valid PHP code, inside the file.

p45

Once you are done editing enter ctrl+X the press the Y key followed by the Enter key to exit the nano editor.

You can now access this page in your web browser by visiting the domain name or public IP address you’ve set up in your Nginx configuration file, followed by /info.php:

p46

After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment -and your Ubuntu server.

p47

You can always regenerate this file if you need it later.

Retrieving data from MySQL database with PHP

Let's create a test database (DB) with simple “To do list” and configure access to it, so the Nginx website would be able to query data from the DB and display it. We will create a database named example_database and a user named example_user, but you can replace these names with different values.

Step 1: connect to the MySQL console using the 'root' account.

p48

Step 2: To create a new database, run the following command from your MySQL console.

p50

Step 3: create a new user and grant him/her full privileges on the database you have just created.

p51

Step 4: Give this user permission over the database.

52

Step 5: Exit from the MySQL console and Test if the new user has the proper permissions by logging in to the MySQL console again using the custom user credentials.

p53

This will prompt you for the password used when creating the user.

54

Step 6: After logging in to the MySQL console, confirm that you have access to the database.

p56

This will give you the following output.

p57

Step 7: Next, we’ll go ahead and create a test table named todo_list. Run the following statement from your MySQL console.

56

Step 8: Insert a few rows of content in the test table. And repeat the next command a few times, using different VALUES.

57

Step 9: Exit the MySQL console after confirming that the data was successfully saved to your table.

58

Now you can create a PHP script that will connect to MySQL and query for your content.

Create a new PHP file in your custom web root directory using nano.

p66

p67

Save and close the file when you are done editing.

You can now access this page in your web browser by visiting the domain name or public IP address configured for your website, followed by /todo_list.php. If you see a page like this, showing the content you’ve inserted in your test table, your PHP environment is ready to connect and interact with your MySQL server.

68

Congratulations! You have successfully built a flexible foundation for serving PHP websites and applications to your visitors, using Nginx as web server and MySQL as database management system.

This brings us to the end of this project.

About

This project will demonstrate the implementation of LEMP stack on AWS virtual server.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published