Apache: where things are and how to fix it
Apache is the web server behind http://localhost, your PHP pages and phpMyAdmin. The lab sheet installs it; this page is for when it misbehaves.
Where Apache keeps things
| Path | What it is |
|---|---|
/var/www/html/ | The web root. http://localhost/x.php is the file /var/www/html/x.php. |
/etc/apache2/apache2.conf | Main config file |
/etc/apache2/sites-available/ | Website configs (000-default.conf is the default site) |
/etc/apache2/conf-available/ | Extra configs (phpMyAdmin can be added here) |
/etc/apache2/mods-available/ | Modules (php8.2, rewrite, ...) |
*-enabled/ folders | Links to the configs that are switched on |
/var/log/apache2/error.log | Errors, including PHP errors |
/var/log/apache2/access.log | Every page request |
Something in *-available does nothing until it is enabled. Debian has commands for that:
sudo a2enmod rewrite # enable a module (a2dismod to disable)
sudo a2enconf phpmyadmin # enable a config (a2disconf)
sudo a2ensite mysite # enable a site (a2dissite)
sudo systemctl reload apache2
Nothing changes until Apache is reloaded or restarted.
The golden rule: test before restart
A single typo in a config file stops Apache from starting, and then every page is down, including phpMyAdmin. Always:
sudo apache2ctl configtest # must print "Syntax OK"
sudo systemctl restart apache2
If configtest shows an error, it names the file and line number. Fix that line first.
AH00558: Could not reliably determine the server's fully qualified domain name is only a warning. It is safe to ignore. To silence it: add ServerName localhost at the end of /etc/apache2/apache2.conf.
reload or restart?
| Command | What it does | Use when |
|---|---|---|
sudo systemctl reload apache2 | Re-reads config, keeps running | After enabling a config/site |
sudo systemctl restart apache2 | Stops and starts | After enabling a module, or when reload is not enough |
If the config is broken, reload fails safely (the old config keeps running), but restart leaves Apache stopped.
Error codes in the browser
| Browser shows | Meaning | Check |
|---|---|---|
| This site can't be reached / connection refused | Apache is not running | systemctl status apache2, then sudo apache2ctl configtest |
| 404 Not Found | Apache works, but there is no file at that path | ls /var/www/html. Names are case-sensitive: Sensor.php ≠ sensor.php. For /phpmyadmin, see phpMyAdmin. |
| 403 Forbidden | The file exists but Apache (www-data) may not read it | ls -l the file: it needs to be readable by others (-rw-r--r--). Fix: sudo chmod 644 file, folders 755. |
| 500 Internal Server Error | A PHP or config error in that page | sudo tail /var/log/apache2/error.log shows the real error. |
| Blank white page | PHP crashed but error display is off | Same: read error.log. |
| PHP code shown as text, or the browser downloads the .php file | PHP module not active | sudo apt install libapache2-mod-php then sudo systemctl restart apache2 |
| The Apache2 Debian Default Page instead of your page | You opened / and index.html is still the default | Open your file by name, or replace /var/www/html/index.html. |
Watching errors live
sudo tail -f /var/log/apache2/error.log
Leave it running, refresh the page in the browser, and the error appears as it happens. Ctrl+C to stop.
Editing the web folder without sudo
/var/www/html belongs to root, so saving a file there needs sudo nano, and Thonny cannot save there at all. To let user pi edit it:
sudo chown -R pi:www-data /var/www/html
sudo chmod -R 775 /var/www/html
Never use chmod 777: it lets every user and program change your web pages. More in files and permissions.
Port 80 already in use
AH00072: make_sock: could not bind to address [::]:80 means another program has port 80:
sudo ss -tlnp | grep ':80 '
Usually it is an old Apache process or another web server (nginx, lighttpd). Stop it, or restart the Pi.
Start Apache fresh
If the configuration is a mess and you do not have a backup:
sudo cp -a /etc/apache2 ~/apache2-config-backup # keep a copy just in case
sudo apt purge apache2 -y
sudo apt install apache2 libapache2-mod-php -y
This resets /etc/apache2 to the defaults; your files in /var/www/html stay. Removing Apache can also remove the PHP module, which is why the second line installs both. Read the list apt purge shows before saying yes. Afterwards, redo the phpMyAdmin step (phpMyAdmin).