• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install LEMP Stack on Ubuntu 18.04

Written by Admin, Updated On May 7, 2019
lemp, mysql, nginx, php, ubuntu

LEMP stack on Ubuntu 18.04 is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. LEMP stands for Linux OS, with the Nginx (pronounced like “Engine-X”) web server, Data store in a MySQL database, and dynamic content is processed by PHP.

How To Install LEMP Stack on Ubuntu 18.04

Install LEMP Stack on Ubuntu 18.04#

This tutorial will shows one by one step how to install LEMP stack on Ubuntu 18.04 server.

Prerequisites#

First of all you need Ubuntu 18.04 server with a non-root sudo enabled user account.

Step 1 – Install Nginx Web Server#

Nginx is a modern and efficient web server now a days. This will be used to show web pages to our site users.

First you need to update your software packages and then install Nginx, an open source, fast and high-performance web server so execute following command to update packages and install Nginx web server :

sudo apt update
sudo apt install nginx

Once it installed, Nginx service should start automatically and will be enabled to start at boot time. You can check status by executing below command :

sudo systemctl status nginx

If you have enabled firewall then we need to allow connections to Nginx. By default Nginx registers itself with ufw upon installation. You can enabled by below command :

sudo ufw allow 'Nginx HTTP'

You also can verify by execute command :

sudo ufw status

It will show you following output :

Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)

Alternate option is, you can directly allow default ports by following commands:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

Now, we will test is nginx is installed properly and working or not. So open your web browser and type below url and it will open nginx default page as given below.

http://YOUR_SERVER_IP/
Ubuntu 18.04 Nginx default

Step 2 – Install MySQL#

As you know MySQL is a database management system. So, It will organize and provide access to databases where your site can store information. Execute following command to install mysql server.

sudo apt install mysql-server

This command will also show you details of packages which will be install and how much disk space will take. Press Y to continue. Also it will prompt you to set MySQL root user password and it will be your mysql root password.

After that, now run MySQL pre-installed simple security script. Which will remove defaults and lock down access to your database system. Execute given command :

sudo mysql_secure_installation

It will prompt you to configure password validation policy and more settings for MySQL. It will also ask to remove unnecessary users, disallow remote root login and test databases so go throughout it.

Finally, your database set up is completed and we will go ahead to install last component of the LEMP stack.

Step 3 — Install PHP and Configuring Nginx#

Now, we have installed Nginx web server and MySQL database but these are only will not display dynamic content so we need to install PHP to play this role.

Nginx does not contain native PHP processing like some other web servers, you will need to install php-fpm, which stands for “fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing.

sudo add-apt-repository universe

Now we will install php-fpm module along with an additional helper package, php-mysql. php-mysql will allow PHP to communicate with your database backend. Execute below command :

sudo apt install php-fpm php-mysql

After complete this installation you need to do few configuration changes in order to tell Nginx to use the PHP processor for dynamic content.

In Nginx have server blocks like VirtualHost in Apache, so we will do changes at server block level. Create a new file server block configuration file at /etc/nginx/sites-available/ directory. Give file as your domain name like yourdomain.com

sudo nano /etc/nginx/sites-available/yourdomain.com

Add the following content to this file :

server {
	listen 80;
	root /var/www/html;
	index index.php index.html index.htm index.nginx-debian.html;
	server_name yourdomain.com;

	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;
	}
}

Save and close the file. Now, you have to create a symbolic link from your new server block configuration file to enable your new server block. It will be stored at /etc/nginx/sites-available/ directory. Execute below command :

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

Next, run below command to test new configuration file for syntax errors :

sudo nginx -t

If everything is okay then it will show output as following :

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

You must need to reload Nginx in order to take changes in effect by below command :

sudo systemctl reload nginx

Step 4 – Test Configuration#

Now your LEMP stack completely set up. So we will check it by creating a .php file. Create a test PHP file called info.php in your document root:

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

Add following code lines to this file :

<?php
phpinfo();
?>

Save and close this file. Now visit this page using your server’s public ip address like below :

http://YOUR_SERVER_IP_OR_DOMAIN/info.php

You should see a web page that has been generated by PHP with information about your server:

How to Install LEMP Stack on Ubuntu 18.04

That’s all.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install LAMP Stack on Ubuntu 18.04
Next Article   How to Install LAMP Stack on Debian 9

Related Posts

  • How to Install and Use PHP Composer on Ubuntu 22.04

    How to Install Composer on Ubuntu 22.04

    January 31, 2023
  • How to Install Nginx on Ubuntu 22.04

    How to Install Nginx on Ubuntu 22.04

    January 28, 2023
  • How to Install Puppet Agent on Ubuntu 22.04

    How to Install Puppet Agent on Ubuntu 22.04

    January 22, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook