Introduction to LEMP Stack

🧰Introduction to LEMP Stack

LEMP stands for:

Linux: The base operating system.
Nginx: A high-performance web server.
MariaDB: A database management system (replacement for MySQL).
PHP: A server-side scripting language.

Steps to install LEMP Stack
Step 1: Log in to the server via SSH

Use SSH to access your server or VPS as root:

bash
ssh root@your_server_ip

Step 2: Update the system

Before installation, update the system to ensure all packages are up to date:

bash
apt update && apt upgrade –y

Step 3: Install Nginx

Install the Nginx web server:

bash
apt install nginx –y

Start and check the status of Nginx:

bash
systemctl start nginx
systemctl enable nginx
systemctl status nginx

To check if Nginx is working, open a browser and visit http://your_server_ip. You will see the default Nginx page.

Step 4: Install MariaDB

Install the MariaDB database management system:

bash
apt install mariadb-server mariadb-client –y

Start and check the status of MariaDB:

bash
systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb

Run the MariaDB security configuration:

bash
mysql_secure_installation

Follow the prompts to set the root password and apply other security options.

Step 5: Install PHP and required modules

Install PHP and necessary modules to run with Nginx:

bash
apt install php8.1 php8.1-fpm php8.1-mysql php8.1-cli php8.1-curl php8.1-mbstring php8.1-xml php8.1-zip –y

Start and check the status of PHP-FPM:

bash
systemctl start php8.1-fpm
systemctl enable php8.1-fpm
systemctl status php8.1-fpm

Step 6: Configure Nginx to handle PHP

Create or edit the Nginx config file to handle PHP files. Example:

bash
nano /etc/nginx/sites-available/default

Add or modify the following configuration:

nginx
server {
listen 80;
server_name your_domain.com;
root /var/www/wordpress;

index index.php index.html;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}

location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}

location ~ /.ht {
deny all;
}
}

Save and exit the file, then test and restart Nginx:

bash
nginx –t
systemctl restart nginx

Step 7: Install phpMyAdmin (Optional)

If you prefer a graphical interface to manage the database, install phpMyAdmin:

bash
apt install phpmyadmin –y

During installation, choose Nginx as the web server and follow the setup.

After installation, create a symbolic link for phpMyAdmin to work with Nginx:

bash
ln –s /usr/share/phpmyadmin /var/www/html/phpmyadmin

Now, access phpMyAdmin at http://your_server_ip/phpmyadmin.

Verify Operation

To verify that PHP is working, create an info.php file:

bash
echo “<?php phpinfo(); ?>” > /var/www/html/info.php

Visit http://your_server_ip/info.php to see PHP configuration info.


Check operation

To check if PHP is working correctly, create a info.php file:

bash
 echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Access http://your_server_ip/info.phphttp://your_server_ip/info.php to view PHP configuration information.