Skip to content

Step 4: Project 2 LEMP stack implementation

monotiller edited this page May 10, 2022 · 2 revisions

Discussion page

Web stack implementation (LEMP)

I created a new EC2 instance and logged in

Installing the NGINX web server

NGINX was installed and is running

Running curl http://localhost:80

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Which means it worked. Visiting the IP address gives us this page:

Installing MySQL

MySQL was installed and I entered it to check it worked:

Installing PHP

PHP was installed

Configuring NGINX to use PHP processor

I made the directory sudo mkdir /var/www/projectLEMP, set the permissions sudo chown -R $USER:$USER /var/www/projectLEMP and then I used VIM to put the configuration in sudo vim -N /etc/nginx/sites-available/projectLEMP

#/etc/nginx/sites-available/projectLEMP

server {
    listen 80;
    server_name projectLEMP www.projectLEMP;
    root /var/www/projectLEMP;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

}

Then I activated the configuration with sudo ln -s /etc/nginx/sites-available/projectLEMP /etc/nginx/sites-enabled/ and then checked for syntax errors and was presented with the following message:

The default site was disabled with sudo unlink /etc/nginx/sites-enabled/default and then I restarted nginx sudo systemctl reload nginx. I set a message to be displayed with:

sudo echo 'Hello LEMP from hostname' $(curl -s http://169.254.169.254/latest/meta-data/public-hostname) 'with public IP' $(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) > /var/www/projectLEMP/index.html

And navigating to the IP address I see this:

Testing PHP with NGINX

I added the php test script:

<?php
phpinfo();

This displayed correctly:

I then removed it using sudo rm /var/www/projectLEMP/info.php

Retrieving data from MySQL database with PHP

I created a database for this part:

I then created a user for myself and gave it the relevant permissions

I was then able to log in with that user

I then ran SHOW DATABASES and got this

+--------------------+
| Database           |
+--------------------+
| akerp2             |
| information_schema |
+--------------------+
1 row in set (0.00 sec)

I entered in some data into the database:

I then created the php script

<?php
$user = "monotiller";
$password = "password";
$database = "akerp2";
$table = "todo_list";

try {
  $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
  echo "<h2>TODO</h2><ol>";
  foreach($db->query("SELECT content FROM $table") as $row) {
    echo "<li>" . $row['content'] . "</li>";
  }
  echo "</ol>";
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

Then visited the new webpage and saw this: