A LAMP stack (Linux, Apache, MySQL/MariaDB, and PHP) is essential for hosting websites and web applications. This guide explains how to install and configure a LAMP stack on Ubuntu.
1. Install Apache (The Web Server)
- Update Package Lists:
sudo apt update
- Install Apache:
sudo apt install apache2
- Verify Installation:
- Open your browser and visit
http://localhost
. - You should see the Apache default page.
- Helpful Link:
Learn more about configuring Apache at Apache’s official documentation.
2. Install MySQL or MariaDB (The Database Server)
- Install MySQL:
sudo apt install mysql-server
- Secure MySQL Installation:
Run the security script to set up a root password and remove unnecessary features:
sudo mysql_secure_installation
- Check MySQL Service:
Ensure it’s running:
sudo systemctl status mysql
- Using MariaDB Instead:
MariaDB is a popular MySQL fork. Install it using:
sudo apt install mariadb-server
- Resource:
For in-depth database security practices, check MySQL’s security guide.
3. Install PHP (The Server-Side Language)
- Install PHP and Required Modules:
sudo apt install php libapache2-mod-php php-mysql
- Check PHP Version:
Verify installation:
php -v
- Test PHP:
- Create a PHP info file:
bash sudo nano /var/www/html/info.php
Add this line:php <?php phpinfo(); ?>
- Visit
http://localhost/info.php
in your browser.
- Helpful Link:
Refer to the PHP documentation for advanced configurations.
4. Adjust Apache to Serve PHP Files
- Prioritize PHP Files:
Edit Apache’s configuration file:
sudo nano /etc/apache2/mods-enabled/dir.conf
Move index.php
to the front of the list.
- Restart Apache:
sudo systemctl restart apache2
- More Info:
Apache-specific PHP integration tips can be found in this DigitalOcean guide.
5. Configure Firewall (Optional)
- Allow Apache Through UFW:
If UFW (Uncomplicated Firewall) is enabled, allow Apache traffic:
sudo ufw allow in "Apache"
sudo ufw enable
- Verify Firewall Rules:
sudo ufw status
6. Test the LAMP Stack
- Host a Test Page:
Create an HTML or PHP file in/var/www/html
and access it via your browser. - Access MySQL:
Open the MySQL shell:
sudo mysql
- Benchmark Your Stack:
Tools like Apache JMeter can simulate traffic to test performance.
7. Troubleshooting
- Apache Not Starting:
Check logs for errors:
sudo journalctl -xe
- MySQL Connection Issues:
Verify credentials and permissions. For help, visit the MySQL troubleshooting guide. - PHP Errors:
Enable error reporting in your PHP script or modifyphp.ini
:
sudo nano /etc/php/7.x/apache2/php.ini
- More Help:
Search for solutions on Ask Ubuntu if issues persist.