{"id":6402,"date":"2020-07-15T09:36:46","date_gmt":"2020-07-15T06:36:46","guid":{"rendered":"https:\/\/kifarunix.com\/?p=6402"},"modified":"2024-03-14T22:10:19","modified_gmt":"2024-03-14T19:10:19","slug":"install-and-setup-lemp-stack-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-setup-lemp-stack-on-ubuntu-20-04\/","title":{"rendered":"Install and Setup LEMP Stack on Ubuntu 20.04"},"content":{"rendered":"\n<p>Welcome to our tutorial on how to install and setup LEMP stack on Ubuntu 20.04. &nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/lemp.io\/\" target=\"_blank\">LEMP Stack<\/a>&nbsp;is a group of open source tools commonly used for developing and deploying&nbsp;web applications. It consists of the Linux Operating System (Ubuntu 20.04 in this case), lightweight and powerful Nginx (Engine-X) web server, MySQL\/MariaDB RDBMS and the server-side scripting language, PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing LEMP Stack on Ubuntu 20.04<\/h2>\n\n\n\n<p>Run system update<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt upgrade<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install Nginx Web Server on Ubuntu 20.04<\/h3>\n\n\n\n<p>Nginx is available on the default Ubuntu 20.04 repos. It can simply be installed by executing the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install nginx<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Running Apache<\/h4>\n\n\n\n<p>Once the installation is done, Nginx is started and enabled to run on system boot.<\/p>\n\n\n\n<p>To check the status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status nginx<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n\u25cf nginx.service - A high performance web server and a reverse proxy server\n     Loaded: loaded (\/lib\/systemd\/system\/nginx.service; enabled; vendor preset: enabled)\n     Active: active (running) since Tue 2020-07-14 05:35:51 UTC; 17s ago\n       Docs: man:nginx(8)\n   Main PID: 64530 (nginx)\n      Tasks: 3 (limit: 2282)\n     Memory: 6.4M\n     CGroup: \/system.slice\/nginx.service\n             \u251c\u250064530 nginx: master process \/usr\/sbin\/nginx -g daemon on; master_process on;\n             \u251c\u250064531 nginx: worker process\n             \u2514\u250064532 nginx: worker process\n\nJul 14 05:35:51 ubuntu20 systemd[1]: Starting A high performance web server and a reverse proxy server...\nJul 14 05:35:51 ubuntu20 systemd[1]: Started A high performance web server and a reverse proxy server.\n<\/code><\/pre>\n\n\n\n<p>Check if it is enabled to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl is-enabled nginx<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>enabled<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Allow Web Server External Access on Firewall<\/h4>\n\n\n\n<p>You can allow external access to Nginx using the ports or the Nginx application name. For example, to allow access to both Nginx HTTP and HTTPs traffic;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw allow 80,443\/tcp<\/code><\/pre>\n\n\n\n<p>Similarly, you can use Nginx app, <strong>Nginx Full<\/strong>, to allow both HTTP and HTTPs;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw allow \"Nginx Full\"<\/code><\/pre>\n\n\n\n<p>For HTTP or HTPPS, simple use <strong>Nginx HTTP<\/strong> or <strong>Nginx HTTPS<\/strong> respectively as the app names. <\/p>\n\n\n\n<p>Verify access to Nginx from browser by using the address, <strong><code>http:\/\/Server.IP_or_hostname<\/code><\/strong>. You should land on Nginx HTTP server test page which basically shows that nginx web server is successfully installed and working.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1237\" height=\"377\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/07\/nginx-test-page.png\" alt=\"Install and Setup LEMP Stack on Ubuntu 20.04\" class=\"wp-image-6424\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/07\/nginx-test-page.png?v=1594794813 1237w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/07\/nginx-test-page-768x234.png?v=1594794813 768w\" sizes=\"(max-width: 1237px) 100vw, 1237px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Install MySQL\/MariaDB Database Server on Ubuntu 20.04<\/h3>\n\n\n\n<p>You can choose to use MySQL or MariaDB for your RDMS. In this tutorial, we are using MariaDB 10.5 as our database system.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install MariaDB 10.5 on Ubuntu 20.04<\/h4>\n\n\n\n<p>Import MariaDB GPG signing key<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt-key adv --fetch-keys 'https:\/\/mariadb.org\/mariadb_release_signing_key.asc'<\/code><\/pre>\n\n\n\n<p>Install MariaDB APT repository<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>add-apt-repository 'deb [arch=amd64] http:\/\/mariadb.mirror.globo.tech\/repo\/10.5\/ubuntu focal main'<\/code><\/pre>\n\n\n\n<p>Install MariaDB 10.5 on Ubuntu 20.04<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update\napt install mariadb-server mariadb-client<\/code><\/pre>\n\n\n\n<p>When installed, MariaDB is started and enabled to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status mariadb<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n\u25cf mariadb.service - MariaDB 10.5.4 database server\n     Loaded: loaded (\/lib\/systemd\/system\/mariadb.service; enabled; vendor preset: enabled)\n    Drop-In: \/etc\/systemd\/system\/mariadb.service.d\n             \u2514\u2500migrated-from-my.cnf-settings.conf\n     Active: active (running) since Wed 2020-07-15 05:20:26 UTC; 6min ago\n       Docs: man:mariadbd(8)\n             https:\/\/mariadb.com\/kb\/en\/library\/systemd\/\n   Main PID: 3207 (mariadbd)\n     Status: \"Taking your SQL requests now...\"\n      Tasks: 9 (limit: 2282)\n     Memory: 68.9M\n     CGroup: \/system.slice\/mariadb.service\n             \u2514\u25003207 \/usr\/sbin\/mariadbd\n\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: Processing databases\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: information_schema\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: mysql\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: performance_schema\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: Phase 6\/7: Checking and upgrading tables\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: Processing databases\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: information_schema\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: performance_schema\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: Phase 7\/7: Running 'FLUSH PRIVILEGES'\nJul 15 05:23:06 ubuntu20 \/etc\/mysql\/debian-start[3229]: OK\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Securing MariaDB<\/h4>\n\n\n\n<p>MariaDB ships with some initial security script called <code>mysql_secure_installation<\/code>. When run, this script removes all test databases, disables remote root account login and remove test user accounts as part of the initial security checks.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mysql_secure_installation<\/code><\/pre>\n\n\n\n<p>You can now login and create a database and database user as required by whatever application you want to run.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Install PHP 7.4 and Other Required PHP Modules on Ubuntu 20.04<\/h3>\n\n\n\n<p>Next, install PHP 7.4 on Ubuntu 20.04. By default, Ubuntu 20.04 ships with PHP 7.4 in its default repos. Hence, installation of PHP 7.4 is as simple as running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install php php-fpm php-mysql<\/code><\/pre>\n\n\n\n<p>This will install alongside itself other default PHP modules.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install Other Required PHP Modules<\/h4>\n\n\n\n<p>If any other required modules needs to be installed, you can simply install it by running the command, <code><strong>apt install php-extension<\/strong><\/code>, where <strong>extension<\/strong> is the name of the PHP module. For example;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install php-{bcmath,bz2,imap,intl,mbstring,soap,sybase,xml,zip}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configure Nginx for PHP Processing<\/h3>\n\n\n\n<p>For Nginx to process PHP pages, you need to include&nbsp;<strong>index.php<\/strong>&nbsp;as a value for the&nbsp;<strong>index<\/strong>&nbsp;parameter. Since we are using the default configuration for testing, edit the it as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/nginx\/sites-enabled\/default<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>...\n        root \/var\/www\/html;\n\n        # Add index.php to the list if you are using PHP\n        index <strong>index.php<\/strong> index.html index.htm index.nginx-debian.html;\n...<\/code><\/pre>\n\n\n\n<p>Also, you need to configure Nginx to pass PHP scripts to FastCGI server. Uncomment the line&nbsp;<code>location ~ \\.php$<\/code>&nbsp;and configure it as shown below<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>        location ~ \\.php$ {\n                include snippets\/fastcgi-php.conf;\n                fastcgi_pass unix:\/run\/php\/php7.4-fpm.sock;\n        }<\/code><\/pre>\n\n\n\n<p>Save and exit the file,<\/p>\n\n\n\n<p>Test Nginx configuration syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nginx -t<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/code><\/pre>\n\n\n\n<p>Restart Nginx and FastCGI process manager, if there is no configuration syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart nginx php7.4-fpm<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Testing Nginx PHP Processing&nbsp;on Ubuntu 20.04<\/h4>\n\n\n\n<p>You can test Nginx PHP processing to confirm that is working as required as well check the version and installed modules using the simple PHP info script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"&lt;?php phpinfo(); ?&gt;\" &gt; \/var\/www\/html\/index.php<\/code><\/pre>\n\n\n\n<p>Next, navigate to the browser and enter the address,&nbsp;<strong>http:\/\/&lt;server-IP-or-hostname&gt;\/index.php<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1197\" height=\"994\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/07\/nginx-php-processing.png\" alt=\"PHP test page\" class=\"wp-image-6423\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/07\/nginx-php-processing.png?v=1594794789 1197w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/07\/nginx-php-processing-768x638.png?v=1594794789 768w\" sizes=\"(max-width: 1197px) 100vw, 1197px\" \/><\/figure>\n\n\n\n<p>And there you go. Your LEMP stack is installed and setup on Ubuntu 20.04. Ensure that all the required php modules for the specific application you want to deploy are met.<\/p>\n\n\n\n<p>Once you have verified Nginx PHP processing, remove the PHP info file;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>rm -rf \/var\/www\/html\/index.php<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other Related Tutorials<\/h3>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-lemp-stack-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install LEMP Stack on CentOS 8<\/a><\/p>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-lemp-stack-on-debian-10-buster\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install LEMP Stack on Debian 10 Buster<\/a><\/p>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-lemp-stack-with-mysql-8-on-fedora-30-fedora-29\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install LEMP Stack with MySQL 8 on Fedora 30\/Fedora 29<\/a><\/p>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-setup-lemp-stack-nginx-mariadb-php-7-2-on-ubuntu-18-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Setup LEMP Stack (Nginx, MariaDB, PHP 7.2) on Ubuntu 18.04<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-latest-wordpress-with-lamp-stack-on-ubuntu-20-04\/\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">Install latest WordPress with LAMP Stack on Ubuntu 20.04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to our tutorial on how to install and setup LEMP stack on Ubuntu 20.04. &nbsp;LEMP Stack&nbsp;is a group of open source tools commonly used<\/p>\n","protected":false},"author":3,"featured_media":12497,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,299,235,928,304,342,253],"tags":[1811,1010,1816,1817,1814,1200,1815],"class_list":["post-6402","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-database","category-lemp-stack","category-mariadb","category-nginx","category-php","category-web-servers","tag-install-lemp-stack-ubuntu-20-04","tag-lemp-stack","tag-mariadb-10-5-ubuntu-20-04","tag-php-7-4-on-ubuntu-20-04","tag-setup-lemp-stack-on-ubuntu-20-04","tag-ubuntu-20-04","tag-ubuntu-20-04-lemp-stack","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6402"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=6402"}],"version-history":[{"count":9,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6402\/revisions"}],"predecessor-version":[{"id":21448,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6402\/revisions\/21448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/12497"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=6402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=6402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=6402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}