Zum Hauptinhalt
Mr. Helland
  • Startseite
  • Kalender
  • Mehr
Deutsch
Deutsch English Español Français Tiếng Việt Русский العربية 简体中文
Sie sind als Gast angemeldet
Anmelden
Mr. Helland
Startseite Kalender
Alles aufklappen Alles einklappen
  1. Servers Networks
  2. 1️⃣ Intro to Linux
  3. 07: Installing a Preprocessor and Database Engine

07: Installing a Preprocessor and Database Engine

Abschlussbedingungen
Abgabe einreichen
Fällig: Montag, 9. März 2026, 23:59

Target Icon Learning Target

  • Install PHP and MySQL on the Raspberry Pi
  • Connect PHP to Nginx
  • Verify that dynamic web pages work on the network


Backpack Icon Resources

1️⃣ Control Computer (Windows PC)

    • Runs Windows
    • Used for:
      • SSH connection
      • Web browser testing
    • This computer stays Windows the entire time.

2️⃣ Raspberry Pi Server

Already has:

    • Ubuntu 22.04 installed
    • Nginx installed
    • Custom Nginx site configuration working


Pencil Icon Instructions

Part 1: Connect to Your Raspberry Pi

Step 1:
Open Windows Terminal:

    1. Click Start
    2. Type Windows Terminal or Powershell
    3. Press Enter

Step 2: 
Connect to your Raspberry Pi using SSH:

ssh username@IP_ADDRESS

Common Pitfalls

    • Wrong IP address
    • Raspberry Pi is not powered on
    • Typing the wrong username


Part 2: Install PHP

PHP allows your web server to create dynamic pages.

    • Static page - Stays the same. The server just sends the file to the browser.
    • Dynamic page - When someone visits the page, the server runs the PHP code first, creates the HTML, and then sends the result to the browser.

By using dynamic pages, what the viewer sees will change based on things like user input, database information, or the time and date.

Note: The browser never sees the PHP code — only the finished result.

Step 1: 
Update package lists:

sudo apt update

Step 2:
Install PHP and PHP-FPM:

sudo apt install php-fpm php-mysql -y

php-fpm is required for Nginx to process PHP files.

Step 3:
Verify PHP is installed

php -v

You should see the PHP version number.
Write this down.

Common Pitfalls

    • Forgetting php-fpm (PHP will not work with Nginx without it)
    • Forgetting sudo
    • Typos in the package name


Part 3: Install MySQL (MariaDB)

A database server stores information in organized tables made of rows and columns, similar to a spreadsheet.
Each row is one record (like one student), and each column stores one type of data (like a name or grade).

PHP can connect to a database server like MySQL to read or update this information.
For example, a login page checks usernames and passwords in the database, or a grade page pulls the correct grades for a student.

Using a database keeps large amounts of information organized, searchable, and much faster to access than storing everything in separate files.

 
About MariaDB

MariaDB is a drop-in replacement for MySQL. Ubuntu will install MariaDB by default since it tends to be stable, fast and fully-compatible even when hardware is limited.

Step 1:
Install the database server:

sudo apt install mariadb-server -y

Step 2:
Secure the installation from remote attackers:

sudo mysql_secure_installation

Recommended answers:

    • Set root password? → Yes
    • Remove anonymous users? → Yes
    • Disallow root login remotely? → Yes
    • Remove test database? → Yes
    • Reload privilege tables? → Yes

Step 3:
Verify the database is running:

sudo systemctl status mariadb

Look for:

Active: active (running)

Common Pitfalls

    • Forgetting the root password you just created
    • Skipping mysql_secure_installation
    • MariaDB service not running


Part 4: Configure Nginx to Process PHP

Nginx does not automatically process PHP files.

Step 1: Run the PHP version command

While connected to the Raspberry Pi through SSH...

Run this command:

php -v

You should see something like:

PHP 8.1.x (cli)

The first number (for example, 8.1) is the version of PHP installed on your system.
Write this down.

Step 2: Open your site configuration

sudo nano /etc/nginx/sites-available/mysite

Find the section that handles location blocks.

Add this block:

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

Note: Be sure you have used the correct version number for PHP where it says X.X.

Save and exit nano.

Step 3: Test Nginx configuration

sudo nginx -t

If successful, you will see:

syntax is ok

Step 4: Restart Nginx

sudo systemctl restart nginx

Common Pitfalls

    • Typing "php-v"  instead of "php -v"
    • Using the wrong PHP version number in the socket path
    • Editing the wrong site file
    • Forgetting to test with nginx -t before restarting
    • Missing semicolons in the config file


Part 5: Create a PHP Test File

Step 1: Go to your web root

cd /var/www/mysite

Step 2: Create a test file

sudo nano info.php

Add this:

 <?php phpinfo(); ?> 

Save and exit.

Step 3: Test from Windows

Open your browser and go to:

http://RASPBERRY_PI_IP_ADDRESS/info.php

If successful, you will see a PHP information page.

Common Pitfalls

    • Putting the file in the wrong folder
    • Forgetting to restart Nginx in earlier steps
    • Seeing a download prompt instead of a PHP page (means PHP-FPM is not connected correctly)


Part 6: Test Database Login

Test MariaDB from the terminal:

sudo mysql -u root -p

Enter your password.

If successful, you will see:

MariaDB [(none)]>

Type the following to leave MariaDB:

exit;

Common Pitfalls

    • Wrong password
    • MariaDB service not running
    • Typing mysql instead of mariadb commands incorrectly


Final Check

If all steps worked:

    • Nginx serves static HTML pages
    • PHP processes dynamic pages
    • MariaDB runs as your database server

You now have a full LEMP stack:

    • L for Linux
    • E for Engine X (nginx)
    • M for MySQL (MariaDB)
    • P for PHP
This is how the majority of real-world web servers are built.

Fun Fact:

In the past, we used a LAMP stack. This used the Apache web server engine. Nginx has replaced Apache in most larger and mission critical web servers due to its exceptional performance and versatility.

◄ 06: Hosting a Static Website
PT1: Unit 1 Practice Test ►
Sie sind als Gast angemeldet (Anmelden)
Laden Sie die mobile App
Powered by Moodle