Introduction
LNMP combines four open-source software components: Linux, Nginx, MySQL, and PHP. This stack is often used to power dynamic web applications and websites. Here's a brief overview of each component:
- Linux: An operating system that provides the foundation for the rest of the stack.
- Nginx: A high-performance web server and reverse proxy that can handle significant traffic and efficiently serve static content.
- MySQL: A popular relational database management system that provides a reliable way to store and retrieve data for web applications.
- PHP: A scripting language that is widely used for web development, allowing you to write server-side code that generates dynamic HTML content.
Together, these components form a powerful and flexible platform for building and deploying modern web applications. LNMP is similar to LAMP (Linux, Apache, MySQL, and PHP) but uses Nginx instead of Apache as the web server. Nginx is known for its speed and efficiency, making it a popular choice for high-traffic sites and applications.
Install Nginx
Sure, here are the specific steps to install Nginx on a Linux system:
- Update package lists: Run the following command to update the package lists on the server:
sudo apt-get update
- Use the following command to install Nginx:
sudo apt-get install nginx
- Check if
Nginxis installed successfully
nginx -v
The following image indicates that Nginx was installed successfully, and the currently installed version is 1.18.0.
nginx version: nginx/1.18.0
Install MySQL
Here are the specific steps to install MySQL on a Linux system:
- Use the following command to install MySQL:
sudo apt update
sudo apt-get install mysql-server
- Check if the MySQL server is installed:
mysql --version
The following figure indicates that MySQL is successfully installed.
mysql Ver 8.0.35-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
- Check if MySQL is started, and if not, start it with the following command.
sudo systemctl status mysql
sudo systemctl start mysql
Install PHP
Here are the specific steps to install PHP on a Linux system:
- Install PHP and related modules: Use the following command to install PHP and its related modules.
sudo apt update
sudo apt-get install php8.1-fpm php-mysql
This will install the PHP FastCGI Process Manager (php-fpm) and the MySQL extension for PHP (php-mysql).
- Check the
PHPversion.
sudo php -v
The following figure indicates that PHP is successfully installed.
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Configure Nginx
Once Nginx is installed, it must be configured to work with other components. Follow these steps to do that:
- Edit the Nginx configuration file
/etc/nginx/sites-available/default:
sudo vim /etc/nginx/sites-available/default
- Replace the existing content with the following:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- Check if the Nginx configuration is correct:
sudo nginx -t
- Reload nginx
sudo nginx -s reload
Configure MySQL
- Run the following command to access MySQL.
sudo mysql
- Run the following command to set the root user password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Mysql@1234'
- Run the following command to exit the MySQL database
exit
Configure PHP
- Create and edit a new phpinfo.php file to display PHP information.
sudo vim /var/www/html/phpinfo.php
Write the following content.
<?php echo phpinfo(); ?>
Tips: Where
/var/www/htmlis the site root directory, which can be viewed by running thecat /etc/nginx/sites-enabled/defaultcommand.
- Start PHP-FPM: Once the installation is complete, start the PHP-FPM service using the following command.
sudo systemctl start php8.1-fpm
Tips: Note that the exact name of the PHP-FPM service may differ depending on your Linux distribution and PHP version.
- Enable PHP-FPM to start on boot: To ensure that PHP-FPM starts automatically when the server boots up, run the following command.
sudo systemctl enable php8.1-fpm
Verify
Type http://127.0.0.1/phpinfo.php in the address bar of your browser to access it.
The access result is shown below, and the PHP configuration information page is successfully viewed, which means the LNMP environment is successfully deployed.

Alternatively, the curl command can be used to check.
curl http://127.0.0.1/phpinfo.php -I
Summary
Once you have completed these steps, you should have successfully deployed the LNMP stack. You can place your website code in the /var/www/html directory and visit your website to ensure it runs correctly.



