How to Install LEMP Stack (Nginx, MySQL, PHP7.2) on Ubuntu 16|18.04|20

LEMP Stack – this is a combination of software packages – Linux, Nginx, MySQL and PHP.

Prerequisite:

  • Ubuntu Server 16, 18 or 20th version.
  • Basic Knowledge of linux commands.

What we will do?

  1. Install Nginx
  2. Install MySQL
  3. Install PHP-FPM
  4. Configure Nginx and PHP-FPM
  5. Testing

Step 1: Update Software Packages

Before we install the LEMP stack, it’s a good practice to update repository and software packages by running the following commands on your Ubuntu OS.

sudo apt update -y && sudo apt upgrade -y

Step 2: Install Nginx Web Server

Nginx is a high performance web server and very popular these days. It also can be used as a reverse proxy and caching server. Enter this command to install Nginx Web server.
sudo apt install nginx -y 
After installing Nginx, the commands below can be used to start and enable Nginx service to always start up with the server boots.
sudo systemctl start nginx.service && sudo systemctl enable nginx.service
Now check out its status.
systemctl status nginx
Click on image to view/zoom

Step 3: Install MySQL Database Server

sudo apt-get install mysql-server

After it’s installed, MySQL server should be automatically stared. Use systemctl to check its status.

systemctl status mysql
Click on image to view/zoom

If it’s not running, start and enable service with the below command:

sudo systemctl start mysql && sudo systemctl enable mysql

Step 4: Install php7.2

To install another php version check our blog here.

Add apt repository and Update the system.

sudo apt install software-properties-common -y && sudo add-apt-repository ppa:ondrej/php
sudo apt update -y
sudo apt install php7.2 php7.2-mysql php-common php7.2-fpm php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline php7.2-mbstring php7.2-gd php7.2-zip php7.2-xmlrpc -y

After installing PHP, run the commands below to find the version installed on the server…

php --version
Click on image to view/zoom

Veirfy php fpm service is running or not.

sudo systemctl status php7.2-fpm 

if not then use below command to start and enable.

sudo systemctl start php7.2-fpm && sudo systemctl enable php7.2-fpm

Step 5: Add new VirtualHost Configuration file in Nginx.

vim /etc/nginx/conf.d/newsite.conf
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name domainname.com ;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
#try_files $uri /index.php?/$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 240;
}
location ~ /\.ht {
deny all; }
}

Save and exit and first check the syntax status of nginx configuration file then, restart the nginx service

sudo nginx -t

if syntax is OK it’s Good else we have to check the configuratioin file error.

sudo systemctl restart nginx

Step 6: Testing

To test PHP settings with Nginx, create a phpinfo.php file in Nginx root directory by running the commands below.

sudo vim /usr/share/nginx/html/info.php
<?php phpinfo( ); ?>

Save the file.. then browse to your server hostname followed by info.php
http://domainname.com/info.php
You should see PHP default test page…

Using the above tutorial you have learned How To Install LEMP Stack on Ubuntu Servers.

Please Drop Comment if you faced any issue at any point, I will try to help asap.
50% LikesVS
50% Dislikes

5 Comments

  • You made some nice points there. I did a search on the subject matter and found most people will agree with your site. Yolanda Brantley Tjader

  • Good blog post. I absolutely appreciate this website. Thanks! Jobey Virge Monetta

  • I was able to find good advice from your blog posts. Malinda Allen Brenton

  • This will add to your list of readers and will make most of them come back and read your blogs later on. Brier Richmound Hax Maxi Delaney Uund

  • I must show my love for your kindness giving support to individuals who should have assistance with this one issue. Your real dedication to getting the message all over was astonishingly good and have always made somebody just like me to arrive at their targets. Your new warm and friendly report implies a whole lot a person like me and a whole lot more to my peers. Thanks a ton; from everyone of us. Emmalee Ludwig Swift

Comments are closed.