LEMP STACK Project Implementation

LEMP STACK PROJECT IMPLEMENTATION

The main aim for this project is to explain the DevOps concepts and processes using a LEMP web stack. Some developers use this set of framework and tools to develop software products . We would be carrying out this project in the AWS platform

LEMP is an acronym of sets of technologies used to develop a technical software product.

Linux

NGINX

MySQL

PHP

(P could also stand for Python or Perl )

NGINX is pronounced engineX where the acronym “E” originated from

Pre-requisite for the projects is the following.

  1. Fundamental Knowledge of Installing and downloading software

  2. Basic Understanding of Linux Commands

  3. AWS account login with EC2 instance

  4. Internet connection

IMPLEMENTATION STEPS:

  1. Ensure you login with your details to your AWS console via the aws.amazon.com**

  2. Click on the EC2 link to create instances.

image

iii)Click on launch instance dropdown button and select launch instance .

image

image

iv)Fill in all relevant details to the LEMP project such as :

Type in the name and additional tag to the project (lemp) .Select ubuntu from the quick start option .Also note that the Amazon machine image selection varies from user to user

Select Ubuntu server 20.04 LTS (HVM),SSD Volume Type (Free Tier )

image

v)The instance type selected in the configuration is the t2 micro -free tier.

Click on the “Create new key pair” link.

Ensure the Checkbox remains unchanged on the “Create security group”.

image

vi)Type in the key pair name, chose the default key pair type and private key file format (rsa and .pem) and clicked the “Create key pair button”

image

vii) The .pem file was downloaded successfully

image

viii) I have deliberately chosen default settings to allow SSH traffic from anywhere as well as the storage volume given by AWS.

Then we proceed to launch our instance finally.

image

Instance successfully launched and click to view Instance details with the IP address.

image

image

Click the “Connect” button and copy the ssh client details we would be using on the git bash console.

image

Open git bash on visual studio code or whichever console is convenient to use. We are using git bash here with Visual Studio Code

image

Type YES to connect.

image

You have successful connected to the EC2 instance launched on AWS via ssh

Type clear to have a clear console and proceed to updating the lists of packages in the package manager.

sudo apt update

image

Then we run apache2 installation and click yes to complete installation

sudo apt install nginx

image

Type YES to continue NGINX installation.

image

We have to verify that Nginx is running in our Operating System and press the

Ctl + C button to get the ubuntu root.

sudo systemctl status nginx

image

To proceed by launching the web server in the AWS Cloud, we need to navigate back to the security group on the platform to add a new rule for TCP port 80 which is the default for web browsers. Once done we can access the web page on internet.

Click on security button.

image

And click the security group link.

image

Click on “Edit inbound rules “in order to add a new rule for port 80

image

Add a new rule.

image

Type in the port range and click “Anywhere ipv4”

image

Click the “Save rules” Button

image

Inbound rule successfully modified

image

Open any browser of your choice and access the URL http://3.95.228.188:80

image

Nginx default page successfully displayed.

From the LEMP stack, we have implemented with Linux and now have Nginx ready .

Next step would be to get the MySQL installed .

MYSQL INSTALLATION

Now that our web server is running, we need a relational database uses within the PHP environment hence we install MySQL server

Type “Y” and press Enter

sudo apt install mysql-server

image

When installation is finished, Log in to connect to the MySQL server as the administrator user root so that you can have access to the sudo command.

sudo mysql

image

It is important to set up a password for the user root using mysql_native_password as a default authentication method. Please note, Password not revealed for security purpose Exit MySQL

image

Interactive script is started and all modifications are answered with a Y/N response .

Root user password was set Validate password: No

Change password: No

Remove anonymous user: No

sudo mysql_secure_installation

image

Disallow remote login: No

Remove test data base and access to it: No Reload Privilege tables: Yes

image

Verify login details to ensure all details were inputted correctly and exiting MySQL

sudo mysql -p

image

MySQL server is now installed and secured .

Next, we proceed to the PHP installation which is the final component of the LEMP STACK

PHP INSTALLATION

PHP is the component that would process the codes to display dynamic content to the end user. Nginx requires an external program to handle the PHP processing and this act as a bridge between PHP interpreter and the web server .This enhances the overall performance for most PHP web based site .It is called the PHP fastCGI process manager)

Hence ,we would need to install 2 packages namely : 1)php-fpm 2) php-mysql .

sudo apt install php-fpm php-mysql

image

Installation continues.

image

At this point the PHP component is installed but we need to configure the Nginx to use the PHP processor

CONFIGURING NGINX TO USE PHP PROCESSOR

We need to create server block to encapsulate the Nginx configuration details and it can host more than one domain on a single server .Therefore we would create a direct structure with the /var/www for the domain website .

Root web directory created for our domain below.

sudo mkdir /var/www/projectLEMP

``

image

Then proceed to open new nginx in sites-available directory using the vim editor

image

Put the edited file in an insert mode by typing “i” without quotes and add the bare-bones configuration files ,press ESC ,save and exit with “ :wq” command

image

Please observe the following functions the location block carries out: listen: Port Nginx listen to port 80

root: defines where the document root is served and stored by the website

index: defines how Nginx prioritize the index files. Index .html file gets the highest priority than the rest of the indexes.

Server-name: The domain name or Ip address the server block is responsible for .

Location: Includes try file directives that checks the files that correlates with the matching URI request. If not found, it will return Error 404

Location ~.php$: Handles the PHP processing and ensures it points Nginx to the fastcgi-php.conf files and declares what socket is associated with php-fpm

Location ~ /.ht : deals with .htaccess files that Nginx does not process by denying all directives and ensuring they are not served to users .

Now we have to activate our configuration by linking the config files from Nginx site –enabled directory and also test the configuration to know if the syntax are OK

sudo ln -s /etc/nginx/sites-available/projectLEMP

image

Test successful and syntax are okay.

Next step is disabling default nginx host that is currently configured to listen to port 80.

sudo unlink /etc/nginx/sites-enabled/default

image

Proceed to reload Nginx to apply all changes.

sudo systemctl reload nginx

image

Hence , Our new website is active but note that the /var/www/projectLemp is empty.

We should create a file in that location so that we can test that the blocks is working as expected .

image

Now go and launch your browser URL using the IP address. http://3.95.228.188:80**

image

Our LEMP stack is fully configured successfully.

Next would be to create a PHP Script to test that our Nginx can handle .php files within the new configured website.

TESTING PHP WITH NGINX

Our LEMP stack is set up and completely installed and fully operational. We would test to validate that Nginx can handle .php files off to our PHP processor.

Create a new file called info.php and paste the file

sudo vim /var/www/projectLEMP/info.php

image

Paste the simplest valid php code that would return information about your server

image

Let us access this page on our web browser with the endpoint /info.php [3.95.228.188/info.php] (http://3.95.228.188/info.php)****)

image

A webpage containing details information about our server should be successfully displayed .

Please note: After checking the relevant information about your php .Its best to remove the file created as it contains sensitive information about your PHP environment and your ubuntu server by the command below

sudo rm /var/www/your_domain/info.php

Next ,we would be retrieving data from MySQL database with PHP

RETRIEVING DATA FROM MYSQL DATABASE WITH PHP

We would be creating a test database with a simple To-do list and configure to access it ,so the Nginx would be able to query data from the database and display it .

A new user would be created with the “mysql_native_password” in order for it to connect to the MySQL database from PHP

Our new user: example_user

** Our new database: example_database

We would connect to the root account by the command below

sudo mysql -p

image

Then we create a new database called “example_database” and password.

image

We grant all permissions to the new user over the new “example_database” in order to give the new user full privileges.

We exit the shell and test if the new user has actually been granted permission to be able to login to the MySQL console again and display the database.

mysql -u example_user -p

image

image

Next, we create a test table named “todo_list” and input 4 different values on each section

image

image

We confirm that the data were successfully saved to our table below and exit the table.

image

image

Next, we create a PHP Script that would connect to the MySQL database for our content in out root directory using vim editor to input the PHP script in the todo_list.php

sudo vim /var/www/projectLEMP

image

Save and close file when done editing.

image

We can now access the page in the browser by visiting the IP address configured for the website follow by the endpoint /todo_list.php

[3.95.228.188/todo_list.php](

http://3.95.228.188/todo_list.php)

image

To-do table have been successfully displayed on the webpage and our PHP environment has connected and was able to interact with our MySQL server

Congratulations.

If this post was helpful, Would be great if you could click the clap button 👏below to show your support.

Thank you for reading💚👏