
Linux, Nginx, MySQL, and PHP (LEMP) Stack is a collection of open source applications that enable the development and deployment of web applications on a server. Linux works as the operating system, Nginx handles webserver functionalities, MySQL manages the database, and PHP processes dynamic web contents.
This article explains how to install the LEMP stack on Debian 12 to deliver dynamic web applications on the server.
Before you begin:
Nginx is available in the default package repositories on Debian 12. Follow the steps below to install Nginx using the APT package manager on your server.
Install Nginx.
Verify the installed Nginx version.
Output:
Enable Nginx to automatically start at boot.
Start the Nginx service.
View the Nginx service status and verify that it's running.
Output:
MySQL is not available in the default package repositories on Debian 12. Follow the steps below to add the MySQL repository information and install the database.
Download the latest MySQL package repository setup file.
Visit the MySQL APT repository page to verify the latest Debian package file to download on your server.
Run the file to install the latest MySQL repository information on your server.
Select the MySQL Server & Cluster option and press Enter when prompted.
Use Up and Down arrow keys to select your preferred MySQL server version and press Enter. For example, select mysql-8.4-lts version.
Press the Down arrow key to select Ok and press Enter to save changes.
Update the server's package index to apply the new MySQL package information.
Install the MySQL database server package.
Enter a new root database user password when prompted and press Enter.
Enter the password again to validate it and press Enter to update the root database user.
View the installed MySQL version on your server.
Your output should be similar to the one below.
Enable the MySQL service to automatically start at system boot.
Start the MySQL system service.
View the MySQL service status and verify that it's running on your server.
Your output should be similar to the one below.
Run the MySQL secure installation script to secure the database server.
Enter the root database user password you set earlier and press Enter.
Enter Y when prompted to setup the VALIDATE PASSWORD component.
Set the preferred password strength policy for your MySQL database server. For example, enter 2 to enable the use of strong passwords on your server.
Enter N when prompted to change the root database user password, or enter Y to change the password.
Enter Y when prompted to remove anonymous users on your MySQL database server.
Enter Y when prompted to disallow remote login for the root user.
Enter Y when prompted to remove the test database from your MySQL database server.
Enter Y to reload the MySQL privileges table and apply all configuration changes.
Output:
PHP is available in the default package repositories on Debian 12. Follow the steps below to install PHP, PHP-FPM, and recommended web application modules on your server.
Install PHP and PHP-FPM.
Install PHP modules.
The above command installs the following PHP modules:
php-mysql: Allows PHP to connect to the MySQL database server and perform SQL operations.php-cli: Enables PHP scripts to run in your terminal session.View the installed PHP version on your server.
Your output should be similar to the one below.
Start the PHP-FPM service depending on your installed PHP version. For example, PHP 8.2.
Enable PHP-FPM to start automatically at system boot.
View the PHP-FPM service status and verify that it's running.
Output:
PHP-FPM (FastCGI Process Manager) is a process manager for PHP that handles webserver requests for dynamic PHP applications. PHP-FPM offers several enhancements, including process management, server-specific configuration, and improved performance. Follow the steps below to configure PHP-FPM on your server.
Open the default PHP-FPM pool www.conf configuration using a text editor such as nano.
View the PHP-FPM pool name and verify that it's set to www.
Find the following directives and verify that the values are set to www-data.
Find the listen directive and verify that it is set to the PHP-FPM unix socket path /run/php/php8.2-fpm.sock.
Find the following directives and modify the values depending on your server specifications and available resources.
pm: Specifies the process manager type. The dynamic value allows PHP child processes to adjust automatically based on server needs.pm.max_children: Defines the maximum number of PHP child processes to run simultaneously.pm.start_servers: Sets the number of child processes created on startup, providing an initial pool to handle requests.pm.min_spare_servers: Sets the minimum number of idle child processes. If the number of idle processes is less than the specified value, new child processes are created.pm.max_spare_servers: Sets the maximum number of idle child processes allowed. If the number of idle child processes exceeds the specified value, the excess idle child processes are stopped.Save and close the file.
Restart the PHP-FPM service to apply your configuration changes.
Follow the steps below to configure Nginx to process PHP scripts by creating a new virtual host configuration that connects to the PHP-FPM service to process dynamic application requests.
Create a new virtual host configuration file in the /etc/nginx/sites-available/ directory.
Add the following contents to the file. Replace app.example.com with your actual domain.
Save and close the file.
The above Nginx configuration creates a new virtual host that listens for connections using the app.example.com domain and delivers web application files from the /var/www/app.example.com directory. All PHP file requests are forwarded to the /var/run/php/php-fpm.sock PHP-FPM Unix socket for processing.
Create a new symbolic link to the /etc/nginx/sites-enabled/ directory to activate the virtual host configuration.
Test the Nginx configuration for errors.
Output:
Reload Nginx to apply the configuration changes.
Create a new web root directory to store your web application files.
Create a new phpinfo.php web application file in the directory.
Add the following contents to the file.
Save and close the file.
The above application displays the PHP information on your server when accessed using your virtual host's domain.
Allow HTTP connections through the firewall.
Reload UFW to apply changes.
Access the /phpinfo domain path using a web browser such as Chrome.
Uncomplicated Firewall (UFW) is active and available on Vultr Debian servers by default. Follow the steps below to allow connections to the HTTP and HTTPS ports to enable Nginx deliver web application on the server.
View the UFW status and verify that it's active.
Output:
Allow HTTP and HTTPS connections on the server using the Nginx Full service profile.
View the UFW status and verify that the new rules are available.
Output:
Nginx processes HTTP requests and forwards them to PHP-FPM for handling. The php-mysql extension connects PHP to the MySQL database, enabling data retrieval and storage. Follow these steps to create a PHP application that displays a 'Greetings from Vultr' message retrieved from your MySQL database.
Log in to the MySQL database server as the root database user.
Create a new test_db database.
Switch to the test_db database.
Create a new sample database user test_user with a strong password. Replace Strong)P@ssword123 with your desired password.
Grant the user full privileges to the test_db database.
Reload the MySQL privileges table to apply changes.
Create a new greetings table with two columns: id (for numeric data) and message (for text):
Insert a Greetings from Vultr record into the greetings table.
Query the greetings table to verify that the records are available.
Output:
Exit the MySQL console.
Create a new PHP application file greetings.php in your /var/www/app.example.com/ web root directory.
Add the following contents to the file.
Save and close the file.
The above application code displays a Greetings from Vultr message from the MySQL database when accessed using your virtual host's domain.
Access the /greetings.php domain path to test access to the PHP application.
You have installed the LEMP stack on your Debian 12 server. You set up Nginx as the webserver, MySQL as the database backend, and PHP with PHP-FPM to process dynamic web application contents. For more information and configuration options for each application within the stack, visit the following resources.
0 Comments
Be the first to comment and share your perspective with the community.