Skip to main content

How to install a LAMP stack on GNU+Linux

A LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) is a popular software bundle used for hosting dynamic websites and web applications. This guide provides step-by-step instructions for installing the LAMP stack on various GNU/Linux distributions, including Debian/Ubuntu, Arch, Fedora, and Rocky Linux. Where commands differ between distributions, each step will specify the appropriate instructions.


Prerequisites

Before starting, ensure you have:

  • A fresh GNU/Linux installation with root or sudo access.
  • A stable internet connection.

Step 1: Update the System

Update your system’s package list and installed packages.

Debian/Ubuntu:

sudo apt update && sudo apt upgrade -y

Arch Linux:

sudo pacman -Syu

Fedora:

sudo dnf upgrade --refresh -y

Rocky Linux:

sudo dnf update -y

Step 2: Install Apache

Apache is the web server component of the LAMP stack.

Debian/Ubuntu:

sudo apt install apache2 -y

Arch Linux:

sudo pacman -S apache

Fedora/Rocky Linux:

sudo dnf install httpd -y

Start and Enable Apache:

For all distributions, start and enable Apache to run on boot:

sudo systemctl start apache2  # Replace 'apache2' with 'httpd' for Fedora/Rocky
sudo systemctl enable apache2  # Replace 'apache2' with 'httpd' for Fedora/Rocky

Verify installation by accessing http://your_server_ip in a browser. You should see the default Apache welcome page.


Step 3: Install MySQL or MariaDB

The database server stores and manages data for your applications. Choose between MySQL and MariaDB based on your requirements.

Debian/Ubuntu:

sudo apt install mariadb-server mariadb-client -y

Arch Linux:

sudo pacman -S mariadb

Fedora/Rocky Linux:

sudo dnf install mariadb-server mariadb -y

Initialize the Database:

Run the secure installation script to set up the database server:

sudo mysql_secure_installation

Follow the prompts to set a root password and secure the installation.

Start and Enable MariaDB:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Step 4: Install PHP

PHP processes dynamic content and integrates with the web and database servers.

Debian/Ubuntu:

sudo apt install php libapache2-mod-php php-mysql -y

Arch Linux:

sudo pacman -S php php-apache

Fedora/Rocky Linux:

sudo dnf install php php-mysqlnd -y

Configure PHP:

For Debian/Ubuntu, prioritize PHP files by editing the main configuration file:

sudo nano /etc/apache2/mods-enabled/dir.conf

Ensure index.php is listed first:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Save and close the file, then restart Apache:

sudo systemctl restart apache2  # Replace 'apache2' with 'httpd' for Fedora/Rocky

Step 5: Verify the LAMP Stack

Create a test PHP file to verify the stack is working:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Access it via http://your_server_ip/info.php in a browser. You should see the PHP information page.


Additional configuration

Firewall Settings

Ensure your firewall allows HTTP and HTTPS traffic:

Debian/Ubuntu:

sudo ufw allow 'Apache Full'

Fedora/Rocky Linux:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Enable HTTPS

Install and configure SSL/TLS using Let’s Encrypt:

Install Certbot:

  • Debian/Ubuntu:
    sudo apt install certbot python3-certbot-apache -y
  • Arch Linux:
    sudo pacman -S certbot python-certbot-apache
  • Fedora/Rocky Linux:
    sudo dnf install certbot python3-certbot-apache -y

Obtain and Apply SSL Certificate:

sudo certbot --apache

Follow the prompts to secure your site.

Schedule Certificate Renewal:

Certbot automatically installs a renewal script. Verify with:

sudo systemctl list-timers | grep certbot

Troubleshooting

  • Check logs for errors:
    • Apache: /var/log/apache2/error.log (or /var/log/httpd/error_log for Fedora/Rocky)
    • PHP: /var/log/php7.4-fpm.log (adjust version as necessary)
    • MariaDB: /var/log/mysql/error.log
  • Test configuration:
    sudo apachectl configtest
    sudo systemctl status apache2 mariadb php

Conclusion

You now have a fully functional LAMP stack installed on your GNU/Linux system. This setup can host dynamic websites and applications efficiently. Customize your stack as needed, and ensure to keep the software updated for security and performance improvements.