
Linux, Nginx, MySQL, and PHP (LEMP stack) is a collection of open-source applications that enable the development and hosting of dynamic web applications on a server. The stack includes Linux as the operating system, Nginx as the web server application, MySQL (or MariaDB) as the database backend, and PHP (or Python/Perl) as the server-side scripting language. The applications connect to each other using server modules to offer a powerful, efficient, and scalable environment for web development that allows developers to create, manage, and deliver high-performance web applications.
This article explains how to install the LEMP stack on Rocky Linux 9 and to deliver dynamic web applications on a server.
Before you begin:
app.example.com.Nginx is available in the default repositories on Rocky Linux 9. Follow the steps below to install the Nginx web server package using the default DNF package manager.
Install Nginx.
View the installed Nginx version on your server.
Output:
Enable the Nginx service to start at boot time.
Start the Nginx service on your server.
View the Nginx service status and verify that it's running on your server.
Output:
MySQL is available in the default Rocky Linux 9 repositories with the latest package information. You can switch between the MySQL package and MariaDB to use a database server depending on your development needs. In the following steps, install the MySQL database server package and configure the application to enable secure authentication for all database users.
Install the latest MySQL database server package.
View the installed MySQL version on your server.
Output:
Enable the MySQL service to automatically start at boot.
Output:
Start the MySQL service.
View the MySQL service status and verify that it's running on the server.
Output:
Run the mysql_secure_installation script to enable authentication and disable insecure defaults on your MySQL database server.
Output:
Log in to the MySQL database server as the root user. Enter the root database user password you set earlier when prompted to access the MySQL console.
View all available databases on the server.
Output:
Exit the MySQL console..
PHP processes the dynamic web content in your applications while PHP-FPM (FastCGI Process Manager) enables access to PHP with specific performance configurations on your server. Follow the steps below to install PHP and configure PHP-FPM to connect with the Nginx web server.
Install PHP and PHP-FPM on your server.
Install common PHP extensions required by most web applications.
The above command installs the following extensions:
php-mysqlnd: Enables PHP to connect to the MySQL database server and perform SQL operations.php-mbstring: Enables multi-byte string functions in PHP applications.php-xml: Enables XML functionalities.php-gd: Enables image manipulation functions in PHP applications.View the installed PHP version on your server.
Output:
Enable the PHP-FPM service to start at boot time.
Output:
Start the PHP-FPM service.
Open the default PHP-FPM pool www.conf to enable connections to the Nginx web server.
Find the following user and group directives. Then, change the default values from apache to nginx.
Save and close the file.
Restart the PHP-FPM service to apply your configuration changes.
Nginx connects to the PHP-FPM service using the socket path or the default TCP port 9000 on your server. In addition, Nginx must use the same user and group with PHP-FPM to allow PHP connections on the server. Follow the steps below to configure Nginx with PHP-FPM and enable the processing of PHP scripts on your server.
Create a new info.php file in your default web root directory /var/www/html.
Add the following contents to the file.
Save and close the file.
The above PHP application code outputs information about the active PHP version information and modules available on your server.
Create a new Nginx virtual host configuration file such as app.example.com.conf in the /etc/nginx/conf.d directory.
Add the following configurations to the file. Replace app.example.com with your actual domain name.
Save and close the file.
The above Nginx server block configuration creates a new virtual host that listens for incoming connections on the HTTP port 80 using your domain app.example.com. Then, the web server delivers files using the index info.php from the default web root directory /var/www/html. The location ~ \.php block includes proxy directives that connect to the PHP-FPM Unix socket /run/php-fpm/www.sock to process all PHP file requests on your server.
Test the Nginx configuration for errors.
Output.
Restart Nginx to apply your new configuration.
Grant the Nginx user and group nginx ownership privileges to the web root directory.
Set the info.php file permissions to 755 to grant the nginx user full privileges. Then, assign read, and execute permissions to other users.
Set the web root directory SELinux (Security-Enhanced Linux) context to httpd_sys_content_t to allow the Nginx web server to deliver files from the directory.
Restore the SELinux security context based on the httpd_sys_content_t policy rules to apply changes.
View the web root directory context and verify that the web server policy httpd_sys_content_t is available.
Output:
Allow HTTP connections through the default firewall.
Reload the firewall to apply the configuration changes.
Access your domain using a web browser such as Chrome and verify that your PHP application information displays.
Nginx handles HTTP requests and forwards them to PHP-FPM for processing. In addition, the php-mysqlnd module interacts with the MySQL database to retrieve or store data on the server. Then, PHP-FPM processes the dynamic content and to response to the Nginx user request. Follow the steps below to create a new sample PHP application that displays a Greetings from Vultr message from a MySQL database on your server.
Log in to the MySQL database server as the root user.
Create a new sample database test_db.
Switch to the test_db database.
Create a new database user. For example, test_user with a strong password. Replace Strong)P@ssword123 with your desired password.
Grant the new test_user full privileges to the test_db database.
Reload the MySQL privileges table to apply changes.
Switch to the test_db database.
Create a new sample table greetings with two columns: id to store numeric data and message for mixed-characters.
Insert a new Greetings from Vultr string to the message table column.
Query all available table data to verify that the new Greetings from Vultr string is available.
Output:
Exit the MySQL console.
Create a new PHP application file webapp.php in your /var/www/html/ web root directory.
Add the following contents to the file.
Save and close the file.
The above PHP application code connects to the MySQL database server and retrieves the message Greetings from Vultr from the greetings table. Then, it displays the result in your web browser. The application displays a No message found prompt in your browser when the database table is empty.
Enable the httpd_sys_content_t SELinux context on the new PHP application file.
Access your domain and load the /webapp.php URL path in your web browser to test the PHP application.
You have installed Nginx, MySQL, and PHP (LEMP stack) on your Rocky Linux 9 server. You can use the LEMP stack to develop, host, and securely deliver dynamic web applications on your server. For more information and configuration options, visit the following official documentation resources:
0 Comments
Be the first to comment and share your perspective with the community.