Skip to main content

How to Install Apache MySQL PHP (LAMP Stack) on Ubuntu 24.04

·613 words·3 mins
Ubuntu 24.04 Apache Mysql PHP
Table of Contents

Set up a complete LAMP stack on Ubuntu 24.04 to host dynamic web applications with Apache, MySQL, and PHP.

Introduction
#

The LAMP stack is a foundational set of open-source tools used to host and serve web applications. It includes:

  • Linux: The operating system (Ubuntu 24.04)
  • Apache: The web server
  • MySQL: The database engine
  • PHP: The scripting language

This guide walks you through installing and configuring each component on Ubuntu 24.04.

Step 1: Install Apache Web Server
#

  1. Update the package index:
sudo apt update
  1. Install Apache:
sudo apt install apache2 -y
  1. Start and enable the Apache service:
sudo systemctl start apache2
sudo systemctl enable apache2
  1. Check that Apache is running:
sudo systemctl status apache2
  1. Allow HTTP traffic through the firewall:
sudo ufw allow 80/tcp
  1. Confirm installation by visiting http://SERVER-IP in a browser.

Step 2: Install MySQL Database Server
#

  1. Install MySQL:
sudo apt install -y mysql-server
  1. Enable and start MySQL:
sudo systemctl enable mysql
sudo systemctl start mysql
  1. Secure the installation:
sudo mysql_secure_installation

Respond to the prompts to configure password policies and remove insecure defaults.

  1. Log into MySQL and update the root password:
sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Strong@@password123';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
  1. Re-login using the new password:
sudo mysql -u root -p
  1. Create a new database and user:
CREATE DATABASE content_database;
CREATE USER 'dbadmin'@'localhost' IDENTIFIED BY 'Strong@@password123';
GRANT ALL PRIVILEGES ON content_database.* TO 'dbadmin'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 3: Install PHP and PHP-FPM
#

  1. Install PHP and modules:
sudo apt install -y php php-fpm php-mysql php-opcache php-cli libapache2-mod-php
  1. Check PHP version:
php -v
  1. Start and enable PHP-FPM:
sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
  1. Check PHP-FPM status:
sudo systemctl status php8.3-fpm

Step 4: Configure PHP-FPM with Apache
#

  1. Enable Apache modules:
sudo a2enmod proxy_fcgi setenvif
  1. Enable PHP-FPM configuration:
sudo a2enconf php8.3-fpm
sudo systemctl restart apache2
  1. Optional: Tune /etc/php/8.3/fpm/pool.d/www.conf for performance.

Step 5: Set Up Apache Virtual Host
#

  1. Remove the default config:
sudo rm /etc/apache2/sites-enabled/000-default.conf
sudo rm /etc/apache2/sites-available/000-default.conf
  1. Create a new config file:
sudo nano /etc/apache2/sites-available/app.example.com.conf
  1. Add configuration (replace with your domain):
<VirtualHost *:80>
    ServerName app.example.com
    DocumentRoot /var/www/app.example.com

    <Directory /var/www/app.example.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://localhost/"
    </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/app.example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/app.example.com_access.log combined
</VirtualHost>
  1. Enable and test the configuration:
sudo a2ensite app.example.com.conf
sudo apache2ctl configtest
sudo systemctl restart apache2
  1. Create web root:
sudo mkdir -p /var/www/app.example.com
  1. Create a sample PHP file:
sudo nano /var/www/app.example.com/info.php

Add:

<?php phpinfo(); ?>
  1. Visit http://app.example.com/info.php to verify.

Step 6: Configure UFW Firewall
#

  1. Allow web traffic:
sudo ufw allow "Apache Full"
sudo ufw reload

Step 7: Enable HTTPS with Let’s Encrypt
#

  1. Install Certbot:
sudo snap install certbot --classic
  1. Request a certificate:
sudo certbot --apache -d app.example.com -m admin@example.com --agree-tos
  1. Test auto-renewal:
sudo certbot renew --dry-run

Step 8: Test the LAMP Stack
#

  1. Create table and data:
CREATE TABLE messages (
    content_id INT AUTO_INCREMENT PRIMARY KEY,
    content VARCHAR(255) NOT NULL
);
INSERT INTO messages (content) VALUES ('Hello World! Greetings from Vultr');
  1. Create test PHP app:
sudo nano /var/www/app.example.com/setup.php

Add:

<?php
$hostname = "localhost";
$username = "dbadmin";
$password = "Strong@@password123";
$dbname = "content_database";

$conn = new mysqli($hostname, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
}

$sql = "SELECT content FROM messages";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    echo "<h2 style='color: blue; text-align: center;'>" . htmlspecialchars($row["content"]) . "</h2>";
} else {
    echo "<h1>No records found.</h1>";
}

$conn->close();
?>
  1. Fix permissions:
sudo chown -R www-data:www-data /var/www/app.example.com/
  1. Access: https://app.example.com/setup.php

Conclusion
#

You’ve successfully installed and configured a secure LAMP stack on Ubuntu 24.04, complete with Apache, MySQL, and PHP. Your server is now ready to host dynamic PHP-based web applications. For further customization, consult the official documentation for each component.

Related

How to Set Up a Secure Apache Server on Ubuntu 24.04
·740 words·4 mins
Apache Ubuntu 24.04
使用命令行备份MySQL数据库
·63 words·1 min
Mysql Database
Comprehensive Comparison of Ubuntu and Fedora Desktop Systems
·580 words·3 mins
Ubuntu Fedora