
LEMP (Linux, Nginx, MySQL, PHP) is an open-source web application stack that enables the development and deployment of dynamic web applications on a server. Within the stack, Nginx serves the application data stored in MySQL databases while PHP processes the dynamic web application contents on your server.
This article explains how to install the Nginx, MySQL, and PHP (LEMP) stack on a Ubuntu 24.04 server. You will set up a basic web application to test the stack functionalities and secure connections to the server using trusted SSL certificates.
Before you begin:
Deploy an Ubuntu 24.04 instance on Vultr.
Create a new domain A record pointing to the server IP address. For example, app.example.com.
Access the server using SSH and log in as a non-root user with sudo privileges.
Install Nginx.
Start Nginx.
Enable Nginx to start at boot time.
View the Nginx service status and verify that it's active on your server.
Output:
Install the latest MySQL database server package on your server.
Start the MySQL service.
Enable the MySQL service to start at boot time.
View the MySQL service status and verify that it's active on the server.
Output:
Start the MySQL secure installation script to disable insecure default configurations.
Access the MySQL console to set up a new root user password.
Modify the root user with a strong password and enable mysql_native_password as the default authentication method.
Reload the MySQL Privilege tables to apply changes.
Exit the MySQL console.
Restart the MySQL database server.
PHP is available in the default APT repositories on Ubuntu 24.04. The PHP-FPM (FastCGI Process Manager) package enables PHP to run as a service on your server to interact with other applications. Follow the steps below to install PHP, PHP-FPM, and essential modules such as php-mysql on your server.
Install PHP and PHP-FPM on your server.
Install essential PHP modules required by most dynamic applications.
The above command installs the following PHP modules:
php-mysql: Enables PHP to connect to the MySQL database server and perform SQL operations.php-cli: The PHP Command Line Interface (CLI) allows you to run PHP scripts directly in your server 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 version such as PHP 8.3.
Enable PHP-FPM to start at boot time.
View the PHP-FPM service status and verify that it's running.
Output:
PHP-FPM (FastCGI Process Manager) runs as a system process on your server using pools to handle all PHP connections on your server. Each PHP-FPM pool includes multiple configuration settings that influence the performance and access to PHP on your server. Follow the steps below to modify your default PHP-FPM pool configurations to enable better resource management and optimization on your server.
View the PHP-FPM unix socket path using the ss utility.
Your output should be similar to the one below.
/run/php/php8.3-fpm.sock is the PHP-FPM sock path based on the above output. PHP-FPM communicates with other applications such as Nginx using the Unix socket file or TCP connections on the localhost port 9000.
Navigate to the PHP-FPM pool configurations directory.
Open the default PHP-FPM pool configuration www.conf using a text editor such as Nano.
www-data.Save and close the file.
Within the pool configuration:
pm.max_children: Sets the maximum number of child PHP processes that simultaneously run on the server to ensure balanced memory and CPU resource usage.pm.start_servers: Specifies the number of child processes created when PHP starts on the server to provide an initial pool of ready-to-use PHP processes to handle requests. The default value 2 is based on the average min_spare_servers and max_spare_servers values using the equation ((min_spare_servers + max_spare_servers) / 2).pm.min_spare_servers: Sets the minimum number of idle child processes. The default value 1 creates a child process if the number of idle processes is below 1.pm.max_spare_server: Sets the maximum number of idle child processes. The default value 3 stops PHP processes if the number of idle processes exceeds 3.pm.max_requests: Enables the maximum number of requests each child PHP process can execute.You can change the PHP-FPM pool values depending on your server specifications and available resources.
Nginx communicates with PHP using the PHP-FPM unix socket or the TCP port 9000 on your server. By default, Nginx cannot serve dynamic web pages without connecting to PHP-FPM. Follow the steps below to configure Nginx with PHP-FPM to serve web application files on the server.
Create a new index.php file in your web root directory /var/www/html.
Run the following command to add new PHP information contents to the file.
Remove the default Nginx configuration.
Create a new Nginx virtual host configuration file such as app.example.com.conf depending on your domain or web application name.
Add the following configurations to the file. Replace app.example.com with your actual domain name.
Save and close the file.
The above Nginx configuration creates a new virtual host on the server that listens for incoming connections on the HTTP port 80 and serves content using the domain app.example.com from the /var/www/html web root directory. Within the configuration:
index index.html index.php index.nginx-debian.html;: Specifies the default file to serve using the main request path.location ~ \.php$ {: Sets the configurations to apply when a specific request matches a .php file extension.include snippets/fastcgi-php.conf;: Enables the predefined FastCGI configuration snippet for Nginx that contains various directives for handling PHP requests.fastcgi_pass unix:/run/php/php8.3-fpm.sock;: Sets the location of the PHP-FPM socket Nginx should use to communicate with the PHP-FPM service.Enable the new Nginx virtual host configuration. Replace app.example.com with your domain name.
Test the Nginx configuration for errors.
Output:
Restart Nginx to apply your configuration changes.
Allow the HTTP port 80 through the firewall.
Access your domain using a web browser such as Chrome and verify that your PHP Information displays.
Server security is important when deploying dynamic web applications using the LEMP stack on your server. All installed components must accept only internal connection requests on the loopback IP 127.0.0.1 while the Nginx web server listens for connections on the HTTP port 80 and HTTPS port 443. Follow the sections below to configure the default UFW firewall to allow connection requests and generate trusted SSL certificates to secure your server traffic.
Uncomplicated Firewall (UFW) is active and enabled on Ubuntu 24.04 Vultr servers by default. Follow the steps below to configure UFW and allow connections to the Nginx web server.
List all available UFW application profiles.
Verify that the default Nginx profile is available similar to the following output.
Allow the Nginx Full profile to enable the HTTP Port 80 and HTTPS Port 443 through the firewall.
Reload UFW to apply the firewall changes.
View the firewall status to verify that the new rules are available.
You should see an output like this:
Install the Certbot Let's Encrypt client application plugin for Nginx.
Request a new Let's Encrypt SSL certificate on your server. Replace app.example.com with your domain and hello@example.com with your actual email address.
Verify that Certbot auto-renews the SSL certificate upon expiry.
Restart Nginx to apply the configuration changes.
Nginx, MySQL, and PHP (LEMP) continuously intercommunicate with your web applications to deliver dynamic contents on your server. Follow the steps below to set up a sample application that displays a Greetings from Vultr message from a MySQL database on your server.
Log in to MySQL as the root user.
Enter the root user password you set earlier to access the MySQL console.
Create a new sample database vultr_db.
Switch to the database.
Create a new database user db_user with a strong password.
Grant the user full privileges to the vultr_db database.
Reload the MySQL privileges table to apply the new user changes.
Create a new table VultrDocs with two columns. The id column works as the primary key, and message contains VARCHAR strings.
Insert new data to the message column in the VultrDocs table with the string Greetings from Vultr.
View the table data to verify that the new string is available.
Output:
Exit the MySQL console.
Create a new test.php file in your /var/www/html/ web root directory using a text editor such as Nano.
Add the following contents to the file.
Save and close the file.
The above PHP application code connects to your MySQL database and displays the Greetings from Vultr string from your sample vultr_db database when the connection is successful.
Access your domain in a new web browser window using the /test.php path.
Verify that your Greetings from Vultr string displays in your browser window.
You have deployed and secured the Nginx, MySQL and PHP (LEMP) stack on your Ubuntu 24.04 server. You can create and securely deliver multiple web applications depending on your server configurations. For more configuration options, visit the following official documentation resources:
0 Comments
Be the first to comment and share your perspective with the community.