Top.Mail.Ru

How to install LEMP stack (Linux + Nginx + MySQL + PHP)

How to install LEMP stack (Linux + Nginx + MySQL + PHP)

To run modern websites and applications, the LEMP stack is often used: Linux, Nginx, MySQL/MariaDB and PHP. This combination combines stability and high performance, allowing you to quickly process requests and work with dynamic content.

LEMP is suitable for web services of any scale: personal blog, corporate websites, online stores and applications of any complexity. Flexible configuration and low resource consumption make it a popular solution among developers and system administrators.

In this article we will look at how to install a LEMP stack step by step.

Step 1: Prepare the server

  1. Connect to the server via SSH 
  2. Create a new user (so as not to work as root):
    adduser userName
  3. Give him administrator rights:
    usermod -aG sudo userName
  4. Switch to new user:
    su - userName
  5. Update the system:
    sudo apt-get update && sudo apt-get upgrade -y

Step 2. Install and configure Nginx

  1. Install the web server:
    sudo apt-get install nginx
  2. Open ports in the firewall:For HTTP (80):
    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    sudo iptables-save

    For HTTPS (443):
    sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    sudo iptables-save
  3. Go to the server IP address — the standard Nginx page should open.

Step 3: Install and Configure MySQL

  1. Install the DBMS. In our case it will be MySQL:
    sudo apt-get install mysql-server
  2. Connect to MySQL as root:
    mysql -u root -p
  3. Create a user:CREATE USER 'user-name'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *.* TO 'user-name'@'localhost';
    FLUSH PRIVILEGES;
  4. Create a database:
    CREATE DATABASE BaseName;

Step 4: Install and Configure PHP

  1. Install PHP and modules:
    sudo apt-get install php-fpm php-mysql
  2. Determine PHP Version
    php -v
  3. Open the php.ini settings file:
    sudo nano /etc/php/X.X/fpm/php.ini (instead of X.X, substitute your version, for example 7.4).
  4. Find the line
    ;cgi.fix_pathinfo=1
  5.  Remove ; and replace with:
    cgi.fix_pathinfo=0
  6. Restart PHP:
    sudo systemctl restart phpX.X-fpm

Step 5. Configure Nginx to work with PHP

  1. Open the configuration:
    sudo nano /etc/nginx/sites-available/default
  2. Make changes:

    add to the index line index.php;
    in server_name specify the domain or IP;
    uncomment the block location ~ .php$ and configure fastcgi_pass unix:/run/php/phpX.X-fpm.sock;
    uncomment the block location ~ /.ht.
  3. Check the file for errors:
    sudo nginx -t
  4. Restart Nginx:
    sudo systemctl reload nginx

Step 6. Check the server operation

  1. Create a test file:
    sudo bash -c 'echo "<?php phpinfo(); ?>" > /var/www/html/info.php
  2. Go to your browser to:
    http://IP_server/info.php
  3. Make sure the PHP options page opens.
  4. Remove the test file:

sudo rm /var/www/html/info.php

Everything is ready: the LEMP stack is now installed and configured on the server. You can host websites and connect databases.

CONTENT:

Similar

All news

Похожее

Все новости

Adaxa Suite: подробный обзор ERP-системы корпоративного класса

Adaxa Suite — комплексная ERP-платформа для компаний, которым уже тесно в рамках простых учётных систем, но которые при этом не готовы идти в сторону дорогих корпоративных решений уровня SAP или Oracle. Изначально продукт создавался для среднего бизнеса, которому нужна большой набор функций, сквозная автоматизация процессов и надёжная архитектура без чрезмерной стоимости владения. Архитектура и техническая […]

Как заказать дополнительные IP-адреса на UFO.Hosting: пошаговая инструкция

По мере роста проекта одного IP-адреса может стать недостаточно. Это типичная ситуация для компаний, которые масштабируют инфраструктуру, запускают новые сервисы или разделяют внутренние процессы. В UFO.Hosting подключение дополнительных IP-адресов выполняется через биллинговую панель и занимает всего несколько минут. Важно: возможность для заказа дополнительных IP-адресов доступна для тарифов VPS начиная с Haedus. Зачем нужны дополнительные IP-адреса […]