How to set up a LAMP stack on Ubuntu
· Category: Linux
Short answer
Install Apache, MySQL, and PHP on Ubuntu with apt install apache2 mysql-server php libapache2-mod-php, secure MySQL with mysql_secure_installation, and verify by creating a test PHP file. For firewall setup, see how to configure networking with iptables and UFW.
Installation steps
sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
sudo mysql_secure_installation
Test the setup
Create /var/www/html/info.php:
<?php phpinfo(); ?>
Visit http://your-server-ip/info.php to verify PHP is working.
Secure your server
- Allow only necessary ports:
sudo ufw allow 80/tcp && sudo ufw allow 443/tcp - Remove the info.php test file after verification
- Use SSL with Let's Encrypt for HTTPS
Tips
- Use
systemctlto manage services — see how to use systemd - For database management, install phpMyAdmin or use the MySQL CLI