DEV Community

Cover image for My first experience with the LAMP stack
Marko
Marko

Posted on

My first experience with the LAMP stack

I carried out this work as part of a university internship. It main task was to set up a web server with own resume, a login window and a connected mysql database.
I want to share my first experience with the LAMP stack and perhaps help someone with this topic.

Brief theory

A software stack is a set of layered tools, libraries, programming languages, and technologies used to create, manage and run an application. The stack consists of software components that support the application in various ways, such as visualization, database, networking, and security.

LAMP stack architecture:

  • Linux is an open source operating system. Resides at the first level of the LAMP stack and supports other components at higher levels.

  • Apache is an open source web server that forms the second layer of the LAMP stack. The Apache module stores website files and communicates with the browser using HTTP, an Internet protocol for transmitting website information in plain text.

  • MySQL is an open source relational database management system and the third layer of the LAMP stack. The LAMP model uses MySQL to store, query, and manage information in relational databases.

  • PHP Last in the stack is a scripting language that allows websites to run dynamic processes. A dynamic process involves information in software that is constantly changing. It is used to allow the web server, database, and operating system to process requests from browsers in a consistent manner.

Preparation

Installing Apache on Ubuntu.

Happens using the commands:
sudo apt-get update – used to download package information from all configured sources
sudo apt-get install apache2 – used to install Apache itself

Installing MySQL

sudo apt-get install mysql-server – used to install mysql

Installing PHP

sudo apt install php – used to install PHP

To check the results of installing the LAMP stack, let's create a test file:

nano /var/www/html/info.php

nano - text editor (you can use any one of your choice)
Add

<! ?php phpinfo();? >

1

After this, restart apache2 with the command - service apache2 restart

Enter http://my-ip-address/info.php into our web browser

This should appear:

2

Creating a Web Server
By default, Apache comes with a built-in base site. You can change its contents in /var/www/html or edit the settings.

Virtual Host file located in Virtual Host which is located in /etc/apache2/sites-enabled/000-default.conf

You can change the algorithm for processing incoming requests or support several web resources on one web server using virtual host support.

Let's create an example.html file for example.

sudo mkdir /var/www/test/ - create a folder for your web server

nano /var/www/test/ example.html – create a file example.html

Let's write simple code in it:

<html>
<head>
<title> LAMP on Ubuntu! </title>
</head>
<body>
<p> I'm running this website on an Ubuntu Server!
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

3

VirtualHost configuration

VirtualHost is a directive in the Apache web server configuration file, designed to map IP addresses, domains and directories available on the server, as well as manage the sites available on the server. The tag specifies the IP addresses and ports that are used on the server.

We will use the default configuration:

cd /etc/apache2/sites-available/
sudo cp 000-default.conf test.conf
sudo nano test.conf

Edit for yourself:

4

8090 is the port on which my web server will listen.
As part of the practical assignment, we had to create our resume on a web server.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CV</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .resume {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h1, h2, h3 {
            color: #333;
        }
p {
            margin-bottom: 10px;
        }
        ul {
            list-style-type: none;
            padding-left: 0;
        }
        ul li::before {
            content: '\2022';
            color: #007bff;
            font-weight: bold;
            display: inline-block;
            width: 1em;
            margin-left: -1em;
        }
    </style>
</head>
<body>
<div class="resume">
    <h1>Name and surname</h1>
    <p>City, Country</p>
    <p>Email: .....@gmail.com</p>
    <p>Mobile phone: </p>

    <h2>Education</h2>
    <ul>
            <h3> School</h3>
            <p>Primary, middle and high school</p>
            <p>Semptember - June </p>

            <h3>University </h3>
            <p>Speciality: </p>
            <p>Semptember - June </p>

    </ul>
<h2>Skills</h2>
    <ul>
        <li>Programming on  HTML, C++, Python, PHP</li>
        <li>Use of database MySQL</li>
        <li>Knowledge of English at Intermediate level</li>
        <li>Knowledge of Windows and Linux Ubuntu at the administration level</>
</li>
    </ul>
            </ul>

    <h2>Personal qualities</h2>
    <ul>
        <li>Stress resistance</li>
        <li>Teamwork</li>
        <li>Punctuality</li>
    </ul>

</div>

</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Web server activation
sudo a2ensite test.conf

Database creation

CREATE DATABASE my_database;
USE my_database;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100)
);
Enter fullscreen mode Exit fullscreen mode

Adding users using the command

INSERTT INTO ysers (name, email) values ​​('Elena', 'ElenaCon@gmail.com');

Exit the editor using the command – exit.

We give all privileges to our host using the commands:

GRANT ALL PRIVILEGES ON my_database. * TO 'Your@localhost';
FLUSH PRIVILEGES;

Creating a login window
We create two files: one is the site itself for authorization (site.php)
The second one is for connecting the authorization site to the database (connect.php)
nano /var/www/html site.php
nano /var/www/html connect.php

Site.php code:

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Authorization</title>
</head>
<body>

<h2>Authorization form</h2>

<form action="connect.php" method="post">
    <div>
        <label for="name">User name:</label>
        <input type="text" id="name" name="name" required>
    </div>
    <div>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
    </div>
<button type="submit">Login</button>
</form>

</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Code for connect.php:

<?php

$host = "localhost";
$username = ".....";
$password = ".....";
$database = "my_....";

$conn = new mysqli($host, $username, $password, $database);

if ($conn->connect_error) {
    die("Error " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];

    $sql = "SELECT * FROM users WHERE name='$name' AND email='$email'";
    $result = $conn->query($sql);

if ($result->num_rows > 0) {

        header("Location: http://you_ip_address:(port)");
}       else {
        echo "Error";
}

}


$conn->close();
?>

Enter fullscreen mode Exit fullscreen mode

Install the PHP extension to create a web server with a database using the command:

php –m | grep mysqli
apt-get install php-mysql

After calling these commands, PHP will act as a web server that can accept HTTP requests and process them.

We start the web server using the command:
http://you_ip_address:(any port)

Result:
Authorization page:

5

CV page:

6

I didn’t have any major difficulties creating my first web server. There were only moments that really slowed me down because they didn’t work out the first and second times, such as the correct operation of the database or the correctly written codes for the site.php and connect.php files. Overall, I really enjoyed studying this topic and now, as a result, I have my own web server, which can be used for new experiments, such as in my first article

Top comments (1)

Collapse
 
kwnaidoo profile image
Kevin Naidoo

Well done! this is a good start. If I may, here are some critical points:

  • It's important not to do this "email='$email'". This is a security hole because hackers can inject dangerous SQL into your code via forms. Rather look at parameterized queries.
  • Use PDO instead of "mysqli", it's very similar but is the preferred way in most modern PHP applications. PDO also has the benefit of being database agnostic, so you can switch between databases fairly easily without any code changes.

Not so major, just a minor typo: "INSERTT"

Hopefully this helps, and happy coding!