Skip to main content

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
PartPackageRuns asListens onConfigData
Apacheapache2service apache2, user www-dataport 80/etc/apache2/pages in /var/www/html
PHPphp, libapache2-mod-php, php-mysqlinside Apache(no port)/etc/php/8.2/apache2/php.ini
MariaDBmariadb-serverservice mariadb, user mysqlport 3306 + socket /run/mysqld/mysqld.sock/etc/mysql//var/lib/mysql
phpMyAdminphpmyadminPHP 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.

When a web page that uses the database fails, test the layers in this order and stop at the first one that fails:

TestCommandGood result
1. MariaDB runningsystemctl is-active mariadbactive
2. You can log inmysql -u Nypuser -p -e "SHOW DATABASES;"a list of databases
3. Apache runningsystemctl is-active apache2active
4. Apache serves pagescurl -I http://localhostHTTP/1.1 200 OK
5. PHP worksopen http://localhost/info.php (a file with <?php phpinfo(); ?>)purple-and-grey PHP info page
6. PHP can reach MariaDBphp -m | grep -i mysqlmysqli, 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:

QuestionWhat it changes
Switch to unix_socket authenticationYes: root logs in only with sudo mariadb (no password). No: root keeps password login. See MariaDB users.
Change the root passwordSets MariaDB's root password. This is not the Linux pi password.
Remove anonymous usersDeletes accounts with an empty user name. Leaving them causes confusing "Access denied" errors.
Disallow root login remotelyRoot can only log in from the Pi itself.
Remove test databaseDeletes the test database that anyone could use.
Reload privilege tablesApplies 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