How LAMP fits together
Lab 5b installs LAMP: Linux, Apache, MariaDB, PHP. The lab sheet gives the install commands. This page explains what each part does and how they talk to each other, because most "it doesn't work" problems are one link in this chain being broken.
The chain
Browser ──HTTP port 80──> Apache ──runs──> PHP ──socket / port 3306──> MariaDB
│ │
files in /var/www/html data in /var/lib/mysql
| Part | Package | Runs as | Listens on | Config | Data |
|---|---|---|---|---|---|
| Apache | apache2 | service apache2, user www-data | port 80 | /etc/apache2/ | pages in /var/www/html |
| PHP | php, libapache2-mod-php, php-mysql | inside Apache | (no port) | /etc/php/8.2/apache2/php.ini | |
| MariaDB | mariadb-server | service mariadb, user mysql | port 3306 + socket /run/mysqld/mysqld.sock | /etc/mysql/ | /var/lib/mysql |
| phpMyAdmin | phpmyadmin | PHP pages inside Apache | (Apache's port) | /etc/phpmyadmin/ |
Flask is not part of LAMP: it is a separate Python web server on port 5000, next to Apache.
Test each link, from the bottom up
When a web page that uses the database fails, test the layers in this order and stop at the first one that fails:
| Test | Command | Good result |
|---|---|---|
| 1. MariaDB running | systemctl is-active mariadb | active |
| 2. You can log in | mysql -u Nypuser -p -e "SHOW DATABASES;" | a list of databases |
| 3. Apache running | systemctl is-active apache2 | active |
| 4. Apache serves pages | curl -I http://localhost | HTTP/1.1 200 OK |
| 5. PHP works | open http://localhost/info.php (a file with <?php phpinfo(); ?>) | purple-and-grey PHP info page |
| 6. PHP can reach MariaDB | php -m | grep -i mysql | mysqli, mysqlnd, pdo_mysql |
If 5 shows the PHP code as text or downloads it, PHP is not plugged into Apache. If 6 shows nothing, php-mysql is missing. Fixes are on Apache and PHP pages.
What the MariaDB setup questions mean
sudo mysql_secure_installation asks a series of questions. Your lab sheet says what to answer; this is what each one does, so you know what to undo if needed:
| Question | What it changes |
|---|---|
| Switch to unix_socket authentication | Yes: root logs in only with sudo mariadb (no password). No: root keeps password login. See MariaDB users. |
| Change the root password | Sets MariaDB's root password. This is not the Linux pi password. |
| Remove anonymous users | Deletes accounts with an empty user name. Leaving them causes confusing "Access denied" errors. |
| Disallow root login remotely | Root can only log in from the Pi itself. |
| Remove test database | Deletes the test database that anyone could use. |
| Reload privilege tables | Applies the changes now. |
You can run it again at any time to change an answer.
Reinstalling one part
If one part is badly broken, reinstall just that part. Your files and databases stay:
sudo apt install --reinstall apache2 # program files only; your /var/www/html stays
sudo apt install --reinstall mariadb-server # your databases in /var/lib/mysql stay
apt purge is different: it deletes the config too (and for MariaDB it asks whether to delete all databases). Only purge when you really want to start over, and dump your database first (backup).
Useful one-liners
sudo ss -tlnp | grep -E ':80|:3306|:5000' # who is listening on the web, database and Flask ports
systemctl status apache2 mariadb --no-pager # both services at once
sudo tail -n 20 /var/log/apache2/error.log # latest Apache and PHP errors