In this guide, we will show you how to install the LAMP (Linux, Apache, MySQL, PHP) stack on Ubuntu 24.04. This setup allows you to host dynamic websites and web apps. The instructions have been enhanced to ensure the best practices are included for a smooth installation experience.
Step 1: Update System Packages
Begin by updating your package list and upgrading any outdated packages.
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache Web Server
Apache is one of the most popular web servers for hosting web content.
- Install Apache:
sudo apt install apache2 -y
- Verify the Apache service:
sudo systemctl status apache2
- Configure Firewall (UFW): Ensure that Apache is allowed through the firewall so it can serve web pages to users.
sudo ufw allow 'Apache' sudo ufw enable sudo ufw status
Step 3: Install MySQL Database Server
MySQL is a reliable database management system. Install it with:
sudo apt install mysql-server -y
Once installed, secure your MySQL installation by running the built-in security script:
sudo mysql_secure_installation
Follow the prompts to set up the root password and improve the overall security of your database.
Step 4: Install PHP
PHP is the scripting language used to serve dynamic content.
- Install PHP and its Apache integration module:
sudo apt install php libapache2-mod-php php-mysql -y
- Verify your PHP version:
php -v
Step 5: Test PHP with Apache
To ensure everything is working correctly, create a PHP info page.
- Create a new file called
info.phpin the default web root:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
- Access the PHP info page in your web browser using your server’s IP address:
http://your_server_ip/info.php
This page should display information about your PHP setup, confirming it’s working correctly.
Step 6: Adjust Directory Permissions
For security, ensure that the /var/www/html directory is owned by the correct user.
sudo chown -R www-data:www-data /var/www/html
This step ensures that Apache can manage web files properly without unnecessary privileges.
Additional Recommendations
- Enable Apache Mods: To enhance functionality, consider enabling commonly-used Apache modules like
rewritefor URL rewriting:
sudo a2enmod rewrite sudo systemctl restart apache2
- Backup MySQL Databases: It is a good practice to regularly back up your MySQL databases to avoid data loss.
Conclusion
You now have a functional LAMP stack running on Ubuntu 24.04. This setup, complete with firewall configuration, MySQL security improvements, and PHP testing, ensures a secure and robust environment for your web applications. For further customization and optimizations, explore the official documentation for each component.
Guide provided by CW-AI.