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 #
- Update the package index:
sudo apt update
- Install Apache:
sudo apt install apache2 -y
- Start and enable the Apache service:
sudo systemctl start apache2
sudo systemctl enable apache2
- Check that Apache is running:
sudo systemctl status apache2
- Allow HTTP traffic through the firewall:
sudo ufw allow 80/tcp
- Confirm installation by visiting
http://SERVER-IP
in a browser.
Step 2: Install MySQL Database Server #
- Install MySQL:
sudo apt install -y mysql-server
- Enable and start MySQL:
sudo systemctl enable mysql
sudo systemctl start mysql
- Secure the installation:
sudo mysql_secure_installation
Respond to the prompts to configure password policies and remove insecure defaults.
- Log into MySQL and update the root password:
sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Strong@@password123';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
- Re-login using the new password:
sudo mysql -u root -p
- 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 #
- Install PHP and modules:
sudo apt install -y php php-fpm php-mysql php-opcache php-cli libapache2-mod-php
- Check PHP version:
php -v
- Start and enable PHP-FPM:
sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
- Check PHP-FPM status:
sudo systemctl status php8.3-fpm
Step 4: Configure PHP-FPM with Apache #
- Enable Apache modules:
sudo a2enmod proxy_fcgi setenvif
- Enable PHP-FPM configuration:
sudo a2enconf php8.3-fpm
sudo systemctl restart apache2
- Optional: Tune
/etc/php/8.3/fpm/pool.d/www.conf
for performance.
Step 5: Set Up Apache Virtual Host #
- Remove the default config:
sudo rm /etc/apache2/sites-enabled/000-default.conf
sudo rm /etc/apache2/sites-available/000-default.conf
- Create a new config file:
sudo nano /etc/apache2/sites-available/app.example.com.conf
- 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>
- Enable and test the configuration:
sudo a2ensite app.example.com.conf
sudo apache2ctl configtest
sudo systemctl restart apache2
- Create web root:
sudo mkdir -p /var/www/app.example.com
- Create a sample PHP file:
sudo nano /var/www/app.example.com/info.php
Add:
<?php phpinfo(); ?>
- Visit
http://app.example.com/info.php
to verify.
Step 6: Configure UFW Firewall #
- Allow web traffic:
sudo ufw allow "Apache Full"
sudo ufw reload
Step 7: Enable HTTPS with Let’s Encrypt #
- Install Certbot:
sudo snap install certbot --classic
- Request a certificate:
sudo certbot --apache -d app.example.com -m admin@example.com --agree-tos
- Test auto-renewal:
sudo certbot renew --dry-run
Step 8: Test the LAMP Stack #
- 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');
- 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();
?>
- Fix permissions:
sudo chown -R www-data:www-data /var/www/app.example.com/
- 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.