# Install and setup a Lemp Stack in Debian/Ubuntu server

LEMP software stack is a group of software that can be use to sever dynamic web pages and web applications written in php.

All this is for ubuntu server 20.04 it should work in debian 10 but phpmyadmin needs to be install from source

## Install web server NGINX

Install nginx

sudo apt install nginx

When the istallation is done, check in your web browser


It will show something like “Welcome to nginx!”

Check here to setup nginx. Nginx is setup only to work with html in it base configuration you need to create a new server block to work with php.

## Install MySQL (database)

Install mysql

sudo apt install mysql-server

When the installation is done, run a security script that comes pre-installed with MySQL

sudo mysql_secure_installation

Follow the instructions to setup the root password and other security checks for MySQL. Then run

sudo mysql

To check that everything works fine. If you have problems with your passwork check here to reset the root password for MySQL.

## Install PHP

Install PHP to process code and generate dynamic content for the web server

sudo apt install php-fpm php-mysql

Now that everything is install, let’s jump to the fun part

## Setting Nginx to Use the PHP Processor

Create a new server block to work with php, this is a new server that will point to a new website out of the html/ directory

First create a new directory (replace mydomain.xyz for your domain)

sudo mkdir /var/www/mydomain

Create a new nginx block in sites-avaible

sudo vim /etc/nginx/sites-avaiable/mydomain

Copy and paste this block, and make all the changes you need

server {
    listen 80;
    server_name mydomain.xyz www.mydomain.xyz;
    root /var/www/mydomain;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

    location ~ /\.ht {
        deny all;


When you done, save it and close it. Then enable your new domain

sudo ln -s /etc/nginx/sites-avaiable/mydomain /etc/nginx/sites-enable/

Now reload nginx

sudo systemctl reload nginx

And if you like you can re-start nginx as well

sudo systemctl restart nginx

Before your test your new server, create a php file in your root directory

echo "<?php phpinfo(); ?>" >> /var/www/mydomain/index.php

Now test your new php web site, it will show all the informations for php.

## Install PhpMyAdmin (Optional)

I like to use phpmyadmin to manage my databases, it’s simple. To install if run

sudo apt install phpmyadmin

In the installation process phpmyadmin will ask some question to setup the config of ti. Answer to the questions

## Setting Nginx and PhpMyAdmin

In here we need to create a new nginx server block with another domain or alias, in this case i will use and alias phpadmin.mydomain.xyz

Create a new file in

sudo vim /etc/nginx/conf.d/phpmyadmin.conf

Copy and past this block, and make all the changes you need

server {
	listen 80;
	listen [::]:80;

	server_name phpadmin.mydomain.xyz;
	root /usr/share/phpmyadmin/;
	index index.php index.html index.htm index.nginx-debian.html;

	access_log /var/log/nginx/phpmyadmin_access.log;
	error_log /var/log/nginx/phpmyadmin_error.log;

	location / {
		try_files $uri $uri/ /index.php;

	location ~ ^/(doc|sql|setup)/ {
		deny all;

	location ~ \.php$ {
		fastcgi_pass unix:/run/php/php7.4-fpm.sock;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
		include snippets/fastcgi-php.conf;

	location ~ /\.ht {
		deny all;

Save it and close the new file. phpMyAdmin files are storage in /usr/shara/phpmyadmin direcotry

Test nginx configurations

sudo nginx -t

If any errors aren’t show realod nginx

sudo systemctl reload nginx

Now go and test the domain you setup, in this case has phpadmin.mydmoain.xyz if everything whent well you will see phpmyadmin login.

Try out our script to automate all the process

git clone https://github.com/codedarkness/lemp_stack



© DarknessCode - LinuxSucks