{"id":4584,"date":"2022-03-03T09:10:35","date_gmt":"2022-03-03T03:40:35","guid":{"rendered":"https:\/\/www.hostnamaste.com\/blog\/?p=4584"},"modified":"2022-03-03T09:10:35","modified_gmt":"2022-03-03T03:40:35","slug":"how-to-install-the-lemp-stack","status":"publish","type":"post","link":"https:\/\/www.hostnamaste.com\/blog\/how-to-install-the-lemp-stack\/","title":{"rendered":"How to install the LEMP Stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04"},"content":{"rendered":"<figure id=\"attachment_4587\" aria-describedby=\"caption-attachment-4587\" style=\"width: 1196px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4587\" src=\"https:\/\/www.hostnamaste.com\/blog\/wp-content\/uploads\/2022\/03\/How-to-install-the-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-20.04-\u2013-HostNamaste.png\" alt=\"How to install the LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04 \u2013 HostNamaste\" width=\"1196\" height=\"488\" srcset=\"https:\/\/www.hostnamaste.com\/blog\/wp-content\/uploads\/2022\/03\/How-to-install-the-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-20.04-\u2013-HostNamaste.png 1196w, https:\/\/www.hostnamaste.com\/blog\/wp-content\/uploads\/2022\/03\/How-to-install-the-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-20.04-\u2013-HostNamaste-300x122.png 300w, https:\/\/www.hostnamaste.com\/blog\/wp-content\/uploads\/2022\/03\/How-to-install-the-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-20.04-\u2013-HostNamaste-1024x418.png 1024w, https:\/\/www.hostnamaste.com\/blog\/wp-content\/uploads\/2022\/03\/How-to-install-the-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-20.04-\u2013-HostNamaste-768x313.png 768w\" sizes=\"auto, (max-width: 1196px) 100vw, 1196px\" \/><figcaption id=\"caption-attachment-4587\" class=\"wp-caption-text\"><span style=\"font-size: 10pt; font-family: Verdana, Geneva; color: #000000;\"><strong>How to install the LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04 \u2013 HostNamaste<\/strong><\/span><\/figcaption><\/figure>\n<h2><span id=\"How_to_install_the_LEMP_stack_Linux_Nginx_MySQL_PHP_on_Ubuntu_2004\" style=\"font-family: Verdana, Geneva; font-size: 24pt;\"><strong><b>How to install the LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">The LEMP stack, particularly for\u00a0Linux, Nginx, MySQL, and PHP, is a category of tools that are used in\u00a0supporting complex web sites and web applications written in PHP. The backend data is processed in the MySQL database and the complex analysis is done mostly by PHP programming language.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">With that being said, this blogpost will be presented as a tutorial that would teach you how to install the LEMP stack on Ubuntu version 20.04 system. We will go into how to set up the rest of the elements before we get underway. In order to put this tutorial into action, you will need a connection to a non-root sudo user on the Ubuntu <span style=\"text-decoration: underline;\"><a href=\"https:\/\/www.hostnamaste.com\/kvm-vps.php\"><strong>Virtual Private Server<\/strong><\/a><\/span> with a firewall activated. After following our initial server configuration step-wide tutorial for Ubuntu 20.04, you would be able to launch all your operations.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">With that being said and done, we can now proceed with this blogpost. Without further ado, you should dive straight into the six steps in which you can conveniently install the LEMP stack on Ubuntu 20.04.<\/span><\/p>\n<h2><span id=\"Step_1\" style=\"font-family: Verdana, Geneva;\"><strong><b>Step 1:<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">We are going to be using Nginx, the best\u00a0<span style=\"text-decoration: underline;\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_server\" target=\"_blank\" rel=\"noopener\"><strong>web server<\/strong><\/a>\u00a0<\/span>on the web, for this blog. We can use the apt package manager to download this program. As this is your\u00a0first time employing\u00a0the \u2018apt\u2019\u00a0for this session, commence\u00a0by installing your server\u2019s package index.\u00a0 Following that, you can use the below mentioned commands to install nginx.<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">\u2018sudo apt update\u2019<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">\u2018sudo apt install nginx\u2019<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">After it asks for a confirmation, type Y to indicate approval for installation. It is advisable to activate the most restricting profile which will enable traffic that you will need. Because you haven\u2019t setup SSL for your domain, you only need to support unclassified HTTP transmission on port 80. Type the following commands:<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">Sudo ufw allow \u2018Nginx HTTP\u2019<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">Sudo ufw status<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">When you see a web page saying \u201cWelcome to Nginx\u201d, then it implies that you have succeeding in your task at installing it perfectly.<\/span><\/p>\n<h2><span id=\"Step_2\" style=\"font-family: Verdana, Geneva;\"><strong><b>Step 2:<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">One of the next moves is to create a MySQL\u00a0database server and\u00a0be able to process and handle data for your website. MySQL is a common framework that allows PHP to handle databases.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">Firstly, use \u2018apt\u2019\u00a0to get this software up and running.<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">Sudo apt install myself-server<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">For approval, type Y to indicate Yes. You could consider running a protection script that came pre-installed with MySQL. This script will purge all default settings and protect your database framework. Launch the interactive script by starting this instruction.<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">Sudo mysql_secure_installation<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">After that, confide the VALIDATE PASSWORD PLUGIN. Keep answering Y to indicate Yes and press Enter Key to proceed. You will secure yourself from SQL injection bugs and hackers will no longer exploit the servers with remote root login. When you\u2019re done with the task, verify if you can log in to MySQL console by using:<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">Sudo mysql<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; quit<\/span><\/pre>\n<h2><span id=\"Step_3\" style=\"font-family: Verdana, Geneva;\"><strong><b>Step 3:<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">\u00a0PHP may be used to execute code and can create interactive material for the webserver.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">Nginx doesn\u2019t let Apache install PHP; instead, it allows an external application to manage PHP processing and function as a connection between PHP and the site server. This functionality makes for higher overall output in certain PHP-based websites, but it needs a configuration adjustment. You must install and configure PHP-FPM, a PHP process manager, and configure Nginx to route PHP requests via this program. Furthermore, you\u2019ll want PHP-MySQL which enables PHP to interact with MySql-based databases. Key PHP modules would be built automatically.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">In order to install the PHP-FPM packages, you need to type the following command:<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo apt install php-frm php-mysql<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">Each of the LEMP stack modules has been activated so far. Next, Nginx needs to be configured to lead requests to the PHP process. Nginx is configured by running commands inside server blocks. Hostnets are identical to Apache\u2019s virtual hosts.<\/span><\/p>\n<h2><span id=\"Step_4\" style=\"font-family: Verdana, Geneva;\"><strong><b>Step 4:<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">When utilizing the Nginx web server, we can build servers that are built using distinct server blocks (closely related to virtual hosts in Apache). However, each server block can have an entirely different layout than the other server blocks. In this guide, we\u2019ll be referring to your domain as a sample domain name.\u00a0<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">Using Ubuntu 20.04, Nginx is immediately set up with one server block allowed by default that is set up for serving files out of a directory at \/var\/www\/html. Since this helps one site to handle well, it can be challenging to manage when hosting several sites, particularly on shared hosting providers. Instead of updating \/var\/www\/html, we rather maintain configuration files for the domain website in the \/var\/www\/html and leave the default website as the current directory to be handled for any queries that do not fit any other established pages.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">Proceeding with this, you should type in the following commands:<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo mkdir \/var\/www\/your_domain<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo chown \u2013R $USER:$USER \/var\/www\/your_domain<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">Sudo nano \/etc\/nginx\/sites-available\/your_domain<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">Go ahead and allow your setup by connecting to the configuration file from Nginx\u2019s sites-enabled directory.<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo ln -s \/etc\/nginx\/sites-available\/your_domain \/etc\/nginx\/sites-enabled\/<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo nginx \u2013t<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo systemctl reload nginx<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">nano \/var\/www\/your_domain\/index.html<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">You need to go to a browser and get access to the server\u2019s domain name or the IP address.<\/span><\/p>\n<p><strong><span style=\"font-family: Verdana, Geneva;\"><u>https:\/\/ server_domain_or_IP<\/u><\/span><\/strong><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">After which you will see a web page saying \u201cHello Word!\u201d<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">Since you\u2019re using a temporary file as a landing site for your application, you should keep this file in position as a temporary launch site for your application. Once you have imported the index.html file into the document root, be sure to delete or rename the index.html\u00a0file from file root, since it will take priority over an index.php\u00a0file by default.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">The LEMP stack is tailored to its maximum potential. For the current setup, we can write a PHP script that will take a randomly created filename and verify that Nginx is still able to manage .php files inside the configured website.<\/span><\/p>\n<h2><span id=\"Step_5\" style=\"font-family: Verdana, Geneva;\"><strong><b>Step 5:<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">The entirety of your LEMP stack will now be lined up correctly. You can take the search on that which confirms that Nginx properly serves your\u00a0.php files when you run the test.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">You will evaluate your PHP file in your document root by running the command into your command line. Create a new file named info.php inside the\u00a0document root in the\u00a0text editor, then open it and type:<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">nano \/var\/www\/your_domain\/info.php<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">When you are through with your commands, be sure to save and exit the file by clicking CTRL+X and then ENTER for approval.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">You will now reach the page in the internet browser by accessing the domain or default IP address you\u2019ve configured in your Nginx setup file. After that, it\u2019s \/info.php that you want to use in your internet browser.<\/span><\/p>\n<p><strong><span style=\"font-family: Verdana, Geneva;\"><u>https:\/\/ server_domain_or_IP\/ info.php<\/u><\/span><\/strong><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">After you have identified the connection for the related details regarding your PHP server from that site, it is safest to delete the file you have generated as it contains confidential data about the PHP environment as well as your Ubuntu server.<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo rm \/var\/www\/your_domain\/info.php<\/span><\/pre>\n<h2><span id=\"Step_6\" style=\"font-family: Verdana, Geneva;\"><strong><b>Step 6:<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">In case, you want to evaluate PHP\u00a0and see if it will link to MySQL and perform database queries, you can first build a prototype containing dummy data and then supply the data to a script written in one of the programming languages. First, we would need to build a test data set and a new MySQL user in the framework and then properly configure this new user to access everything.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">Only at beginning of the thread, the local MySQL PHP library does not accept the caching sha2 authentication,\u00a0the standard authentication mechanism for MySQL 8. You\u2019ll need to build a new user using the MySQL native password authentication mechanism required to be allowed to link to our MySQL database from our PHP script. Because databases and users are stuff generally with us, we can build a database called \u201cexample database\u201d, and a user named \u201cexample user\u201d to keep things clear.<\/span><\/p>\n<p><span style=\"font-family: Verdana, Geneva;\">After connecting to MySQL from the root account, use commands:<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">sudo mysql<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; CREATE DATABASE example_database<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; CREATE USER \u2018example_user\u2019@\u2019%\u2019 IDENTIFIED WITH mysql_native_password BY \u2018password\u2019;<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; GRANT ALL ON example_database.* TO \u2018example_user\u2019@\u2019%\u2019;<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; exit<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">Log in to MySQL again by using the user credentials:<\/span><\/p>\n<pre><span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql -u example_user \u2013p<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; SHOW DATABASES;<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; INSERT INTO example_database.todo_list (content) VALUES (\u201cMy first important item\u201d);<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; SELECT * FROM example_database.todo_list;<\/span>\r\n<span style=\"font-family: Verdana, Geneva; font-size: 12pt;\">mysql&gt; exit<\/span><\/pre>\n<p><span style=\"font-family: Verdana, Geneva;\">In your web browser, try opening this web page using the URL:<\/span><\/p>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"font-family: Verdana, Geneva;\">https:\/\/ server_domain_or_IP\/ todo_list.php<\/span><\/strong><\/span><\/p>\n<h2><span id=\"Final_Verdict\" style=\"font-family: Verdana, Geneva;\"><strong><b>Final Verdict<\/b><\/strong><\/span><\/h2>\n<p><span style=\"font-family: Verdana, Geneva;\">Your diligent work and commitment have paid off. We have effectively installed the LEMP stack on the Ubuntu 20.04\u00a0<a href=\"https:\/\/www.hostnamaste.com\/kvm-vps.php\"><strong><span style=\"text-decoration: underline;\">KVM VPS<\/span><\/strong><\/a>. In this tutorial, we\u2019ve created a scalable platform for supporting PHP websites and applications to your visitors, utilizing Nginx as the primary web server and MySQL as the reliable database system. You can commence deploying websites and apps in your latest LEMP network stack. If you have a website, web app, or server-side logic designed on a smartphone application, you can conveniently host it.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to install the LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu 20.04 The LEMP stack, particularly for&nbsp;Linux, Nginx, MySQL, and PHP, is a category&hellip;<\/p>\n","protected":false},"author":2,"featured_media":4587,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3099,3098,3104,3102,3101,3103,3100],"tags":[292,3105,2819,3106,3107,2351,2661,570,3108],"class_list":["post-4584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to-install-the-lemp-stack","category-how-to-install","category-lemp-stack","category-mysql","category-nginx","category-ubuntu","category-web-server","tag-kvm-vps","tag-lemp-stack","tag-linux","tag-mysql","tag-nginx","tag-php","tag-ubuntu","tag-virtual-private-server","tag-web-server"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/posts\/4584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/comments?post=4584"}],"version-history":[{"count":6,"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/posts\/4584\/revisions"}],"predecessor-version":[{"id":4591,"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/posts\/4584\/revisions\/4591"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/media\/4587"}],"wp:attachment":[{"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/media?parent=4584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/categories?post=4584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostnamaste.com\/blog\/wp-json\/wp\/v2\/tags?post=4584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}