Installation of LEMP on ubuntu ?


New Member
Hello my name is naresh & i will guide you how to install LEMP stack on your fresh ubuntu server , forgive me for my bad grammar (not native speaker).

What is LEMP ?
L-> Linux OS
E-> Nginx (Engine-X)
M-> Mysql DB

Installing Nginx via terminal / SSH
  1. sudo apt update
  2. sudo apt install nginx
  3. sudo ufw allow 'Nginx HTTP'
  4. sudo ufw allow 'OpenSSH'
  5. sudo ufw status # If it shows inactive than run this command -> sudo ufw enable
  6. sudo systemctl start nginx # it will start your nginx server
  7. sudo systemctl status nginx #it will show if your nginx is active and running properly (Ubuntu 18.04 & below have bug) "nginx.service: Failed to read PID" which can be resolved by referring ->
Installing Mysql
  1. sudo apt install mysql-server
  2. sudo mysql_secure_installation (Answer y to continue & choose the password strength) than set up the password.
  3. if you are okay with auth_socket plugin than you can continue with part - III or else move to step 4.
  4. sudo mysql
  5. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpasswordhere'; #this will update the root mysql password with your password.
  7. SELECT user,authentication_string,plugin,host FROM mysql.user; # Now your root authenticate using mysql_native_password plugin instead of auth_socket.
  8. exit
Installing PHP
  1. sudo apt install php-fpm php-mysql
  2. sudo nano /etc/nginx/sites-available/ #Enter your doamin name in place of yourdomain-name and don't include www.
  3. Add the following code mentioned below to it :-
  4. server { listen 80;
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

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

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

    location ~ /\.ht {
    deny all;
  5. Replace with your domain at server_name .
  6. Ctrl + X than click Y to save it
  7. sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
  8. sudo unlink /etc/nginx/sites-enabled/default #it will delink the default server block.
  9. sudo systemctl reload nginx

Extra commands for information
--> sudo nginx -t # it will show if any error or syntax mistake is present in your nginx configuration.