Table of Contents
A LAMP stack uses Linux, Apache, MySQL, and PHP to run a web application. This walkthrough uses Ubuntu 24.04 LTS and a fresh server where you have sudo access. Ubuntu 18.04 LTS reached the end of standard support in May 2023. If you're maintaining an existing 18.04 host, check its extended maintenance coverage or plan an upgrade before using it for a public site.

Install Apache, MySQL, and PHP
sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql curl
systemctl is-active apache2 mysql
curl -I http://127.0.0.1/Both services should report active. On a fresh installation, the HTTP request should reach Apache on port 80 and normally return 200 OK. Keep the test local while setting things up. If a remote browser needs to connect, check your firewall and any cloud network rules, and permit only the intended web traffic. Configure HTTPS before handling real credentials.
Verify MySQL without changing root authentication
sudo mysql -e 'SELECT VERSION();'This uses the local administrative access normally configured by the Ubuntu MySQL package. Give your PHP application a separate MySQL user with permission only on its own database. Keep its credentials outside the document root and use the application's normal secret configuration. A local LAMP install does not require exposing MySQL's port to the internet or switching the root account to a password-based legacy plugin.
Make sure Apache executes PHP
On a fresh server using Apache's default document root, add a short test file, request it locally, and remove it after verification.
printf '%sn' '<?php echo "PHP works";' | sudo tee /var/www/html/lamp-test.php > /dev/null
curl --fail http://127.0.0.1/lamp-test.php
sudo rm /var/www/html/lamp-test.phpExpect PHP works. If the response displays PHP source code or offers it as a download, remove the test file and check the PHP handler before deploying. If you use a custom virtual host, check its actual document root. See the Ubuntu PHP setup guide for additional extensions.
Troubleshoot service and connection errors
- For Apache configuration errors, run
sudo apache2ctl configtestand reviewjournalctl -u apache2. See the Linux service logs guide for a troubleshooting sequence. - If Apache works locally but can't be reached from another device, check network access and firewall rules before editing Apache.
- If PHP loads but the app cannot connect to MySQL, confirm the local connection mode, credentials, database name, and permissions. Keep runtime checks and backup tests in your server administration workflow.
Reader Comments 0
Sign in with email or Google to join the discussion.