
LAMP is a web application stack consisting of Linux, Apache, MySQL, and PHP. Apache serves as the HTTP web server, MySQL provides the relational database backend, and PHP processes server-side scripts to generate dynamic content. Together, these components form a widely used platform for hosting content management systems, e-commerce applications, and custom web services.
This article explains how to install the LAMP stack on an Ubuntu 26.04 server, configure Apache to process PHP requests through PHP-FPM, secure the server with firewall rules and a Let's Encrypt SSL certificate, and test the stack with a sample PHP application that queries a MySQL database.
Before you begin, you need to:
app.example.com.The default Ubuntu 26.04 APT repositories include the Apache web server package. The following steps install Apache and verify that the service is running.
Update the APT package index.
Install Apache.
Enable Apache to start automatically at boot time.
Start the Apache service.
Verify that Apache is active and running.
The output should display active (running), confirming that the Apache web server is operational.
MySQL is included in the default Ubuntu 26.04 repositories. The following steps install the database server, enable the service, and run the secure installation script to harden the default configuration.
Install the MySQL server package.
Enable MySQL to start automatically at boot time.
Start the MySQL service.
Verify that MySQL is active and running.
The output should display active (running), confirming that the MySQL server is operational.
Run the MySQL secure installation script to remove insecure defaults.
Respond to each prompt as described below:
Open the MySQL server configuration file.
Add the following directive under the [mysqld] section to activate the mysql_native_password authentication plugin.
Save and close the file.
Restart MySQL to load the updated configuration.
Access the MySQL console as the root system user.
Set a strong password for the root account. Replace your_strong_password with a secure password that meets your validation policy.
Reload the privilege tables.
Exit the MySQL console.
PHP-FPM (FastCGI Process Manager) enables Apache to process PHP scripts by forwarding requests through a Unix socket. The following steps install PHP, PHP-FPM, and essential modules for the LAMP stack.
Install PHP, PHP-FPM, and common extensions.
The command installs the following packages:
php-fpm: Manages PHP worker processes for handling web server requests.php-mysql: Enables PHP to connect to MySQL databases.php-opcache: Caches precompiled PHP scripts in memory for faster execution.php-cli: Provides the PHP command-line interpreter.libapache2-mod-php: Enables Apache to process PHP scripts directly.Confirm the installed PHP version.
Your output should be similar to the one below:
Enable PHP-FPM to start automatically at boot time.
Start the PHP-FPM service.
Verify that PHP-FPM is active and running.
The output should display active (running), confirming that PHP-FPM is operational.
Apache requires additional modules to forward PHP requests to PHP-FPM. The following steps enable the required modules and activate the PHP-FPM configuration.
Enable the Apache proxy and environment modules.
Enable the PHP-FPM configuration for Apache.
Restart Apache to apply the module changes.
Uncomplicated Firewall (UFW) is active by default on Ubuntu 26.04. The following steps open ports for HTTP and HTTPS traffic.
Allow HTTP traffic on port 80.
Allow HTTPS traffic on port 443.
Apache uses virtual host configurations to serve content based on the requested domain name. The following steps remove the default configuration, create a virtual host for your domain, and connect it to PHP-FPM.
Unlink the default Apache virtual host configuration.
Create a new virtual host configuration file. Replace app.example.com with your actual domain.
Add the following configuration to the file. Replace app.example.com with your actual domain.
Save and close the file.
Within the configuration:
ServerName: Defines the domain that triggers this virtual host.DocumentRoot: Sets the directory from which Apache serves files.<FilesMatch \.php$>: Forwards all PHP file requests to the PHP-FPM socket for processing.ErrorLog and CustomLog: Store virtual host-specific error and access logs.Create the web root directory for the virtual host.
Enable the new virtual host configuration.
Test the Apache configuration for syntax errors.
The output should display Syntax OK.
Restart Apache to activate the virtual host.
SSL certificates encrypt communication between the client browser and the Apache web server over HTTPS. The following steps install Certbot and generate a trusted Let's Encrypt SSL certificate for your domain.
Install the Certbot Let's Encrypt client and the Apache plugin.
Generate an SSL certificate for your domain. Replace app.example.com with your actual domain.
Follow the on-screen prompts to complete the certificate generation. Certbot automatically updates the Apache virtual host configuration to enable HTTPS.
Verify that the automatic renewal process is configured correctly.
A working LAMP stack allows PHP to query data from MySQL and return dynamic content through Apache. The following steps create a sample database, insert a test record, and build a PHP application that retrieves and displays the data in a web browser.
Log in to the MySQL console as the root user.
Enter the root password you configured earlier.
Create a new database named example_db.
Create a database user named example_user with a strong password. Replace strong_password with your desired password.
Grant the user full privileges on the example_db database.
Reload the privilege tables.
Switch to the example_db database.
Create a table named messages with an auto-incrementing ID and a text column.
Insert a sample record into the table.
Verify that the record exists.
Your output should be similar to the one below:
Exit the MySQL console.
Create a PHP test file in the web root directory.
Add the following PHP code to the file. Replace strong_password with the example_user password you set earlier.
Save and close the file.
This script connects to the example_db database, queries the messages table, and displays the result as an HTML heading.
Access the test page in a web browser. Replace app.example.com with your actual domain.
The browser displays the message Hello World from LAMP Stack, confirming that Apache, MySQL, and PHP are working together.
You have deployed and secured the LAMP stack on an Ubuntu 26.04 server. Apache serves web requests, PHP-FPM processes dynamic content, and MySQL manages the application data. This stack supports multiple virtual hosts and integrates with frameworks such as Laravel and WordPress. For more configuration options, refer to the following resources:
0 Comments
Be the first to comment and share your perspective with the community.