Linux Deploy LNMP

LinuxLinuxBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/PackagesandSoftwaresGroup -.-> linux/curl("`URL Data Transferring`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") subgraph Lab Skills linux/exit -.-> lab-7787{{"`Linux Deploy LNMP`"}} linux/curl -.-> lab-7787{{"`Linux Deploy LNMP`"}} linux/apt -.-> lab-7787{{"`Linux Deploy LNMP`"}} linux/sudo -.-> lab-7787{{"`Linux Deploy LNMP`"}} linux/vim -.-> lab-7787{{"`Linux Deploy LNMP`"}} shell/quoting -.-> lab-7787{{"`Linux Deploy LNMP`"}} shell/variables_decl -.-> lab-7787{{"`Linux Deploy LNMP`"}} shell/exit_status -.-> lab-7787{{"`Linux Deploy LNMP`"}} shell/arith_ops -.-> lab-7787{{"`Linux Deploy LNMP`"}} end

Install Nginx

Sure, here are the specific steps to install Nginx on a Linux system:

  1. Update package lists: Run the following command to update the package lists on the server:
sudo apt-get update
  1. Use the following command to install Nginx:
sudo apt-get install nginx
  1. Check if Nginx is 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:

  1. Use the following command to install MySQL:
sudo apt-get install mysql-server
  1. 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))
  1. Check if MySQL is started, and if not, start it with the following command.
sudo systemctl status mysql
sudo systctl start mysql

Install PHP

Here are the specific steps to install PHP on a Linux system:

  1. Install PHP and related modules: Use the following command to install PHP and its related modules.
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).

  1. Check the PHP version.
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:

  1. Edit the Nginx configuration file /etc/nginx/sites-available/default:
sudo vim /etc/nginx/sites-available/default
  1. 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;
        }
}
  1. Check if the Nginx configuration is correct:
sudo nginx -t
  1. Reload nginx
sudo nginx -s reload

Configure MySQL

  1. Run the following command to access MySQL.
sudo mysql
  1. Run the following command to set the root user password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Mysql@1234'
  1. Run the following command to exit the MySQL database
exit

Configure PHP

  1. 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/html is the site root directory, which can be viewed by running the cat /etc/nginx/sites-enabled/default command.

  1. 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.

  1. 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.

lab-linux-deploy-lnmp-7-1

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.

Other Linux Tutorials you may like