
LEMP is a web application stack consisting of Linux, Nginx, MySQL, and PHP. Nginx handles HTTP requests and serves static content, MySQL stores and manages relational data, and PHP processes server-side logic to generate dynamic web pages. Together, these components form a complete platform for hosting web applications such as content management systems, e-commerce platforms, and custom web services.
This article explains how to install the LEMP stack on an Ubuntu 26.04 server. It covers setting up each component, configuring Nginx to process PHP requests through PHP-FPM, securing the server with firewall rules and a Let's Encrypt SSL certificate, and testing the stack with a sample PHP application that queries a MySQL database.
Before you begin, you need to:
app.example.com.Nginx is available in the default Ubuntu 26.04 APT repositories. The following steps install the web server and verify that the service is running.
Update the APT package index.
Install Nginx.
Enable Nginx to start automatically at boot time.
Start the Nginx service.
Verify that Nginx is active and running.
The output should display active (running), confirming that Nginx 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 to apply the password change.
Exit the MySQL console.
PHP-FPM (FastCGI Process Manager) enables Nginx to process PHP scripts by forwarding requests through a Unix socket. The following steps install PHP, PHP-FPM, and the MySQL extension required for database connectivity.
Install PHP, PHP-FPM, and essential modules.
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-cli: Provides the PHP command-line interpreter for running scripts in the terminal.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.
Uncomplicated Firewall (UFW) is active by default on Ubuntu 26.04. The following steps open ports for HTTP and HTTPS traffic so that Nginx can serve web applications.
Allow HTTP traffic on port 80.
Allow HTTPS traffic on port 443.
Verify the active firewall rules.
Your output should be similar to the one below:
Nginx does not process PHP scripts natively. It forwards PHP requests to PHP-FPM through a Unix socket for execution. The following steps create a virtual host configuration that connects Nginx to PHP-FPM and serves a PHP application.
Unlink the default Nginx virtual host configuration.
Create a new virtual host configuration file. Replace app.example.com with your actual domain name.
Add the following configuration to the file.
Save and close the file.
Within the configuration:
server_name: Defines the domain that triggers this virtual host.root: Sets the directory from which Nginx serves files.location ~ \.php$: Matches requests ending in .php and forwards them to PHP-FPM.include snippets/fastcgi-php.conf: Loads the predefined FastCGI directives for PHP processing.fastcgi_pass: Specifies the PHP-FPM Unix socket path for communication.Enable the virtual host by creating a symbolic link.
Test the Nginx configuration for syntax errors.
The output should confirm that the configuration syntax is ok and the test is successful.
Restart Nginx to activate the virtual host.
SSL certificates encrypt communication between the client browser and the Nginx 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 Nginx plugin.
Generate an SSL certificate for your domain. Replace app.example.com with your actual domain name.
Follow the on-screen prompts to complete the certificate generation. Certbot automatically updates the Nginx virtual host configuration to enable HTTPS.
Verify that the automatic renewal process is configured correctly.
A working LEMP stack allows PHP to query data from MySQL and return dynamic content through Nginx. 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 name.
The browser displays the message Hello World from LEMP Stack, confirming that Nginx, MySQL, and PHP are working together.
You have deployed and secured the LEMP stack on an Ubuntu 26.04 server. Nginx 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.