{"id":1277,"date":"2018-11-05T22:39:01","date_gmt":"2018-11-05T19:39:01","guid":{"rendered":"http:\/\/kifarunix.com\/?p=1277"},"modified":"2024-03-11T21:37:56","modified_gmt":"2024-03-11T18:37:56","slug":"how-to-setup-lemp-stack-nginx-mariadb-php-7-2-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-setup-lemp-stack-nginx-mariadb-php-7-2-on-ubuntu-18-04\/","title":{"rendered":"How To Setup LEMP Stack (Nginx, MariaDB, PHP 7.2) on Ubuntu 18.04"},"content":{"rendered":"\n<p>Follow through this guide to learn how to Setup LEMP Stack (Nginx, MariaDB, PHP 7.2) on Ubuntu 18.04. In our previous article, we covered <a href=\"https:\/\/kifarunix.com\/howtos\/how-to-install-lamp-stack-apachemariadb-php-7-2-on-ubuntu-18-04-lts\/\" target=\"_blank\" rel=\"noopener\">how to setup LAMP Stack on Ubuntu 18.04<\/a>. Well, we are once again here to guide you on how to setup LEMP Stack on Ubuntu 18.04.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setup LEMP Stack on Ubuntu 18.04<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install Nginx Web Server<\/h3>\n\n\n\n<p>Nginx, commonly pronounced as <strong>E<\/strong>ngine-X, is available on Ubuntu repositories by default. Assuming your package index is up-to-date, run the command below to install Nginx.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install nginx -y<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Start and Enable Nginx to run on system reboot<\/h4>\n\n\n\n<p>Note that Nginx is set to run after installation by default on Ubuntu 18.04. You can verify the same by running the command;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nsystemctl status nginx\n<span style=\"color: #00ff00;\">\u25cf<\/span> 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: <span style=\"color: #00ff00;\"><strong>active (running)<\/strong><\/span> since Mon 2018-11-05 21:35:24 EAT; 1min 45s ago\n     Docs: man:nginx(8)\n  Process: 1974 ExecStart=\/usr\/sbin\/nginx -g daemon on; master_process on; (code=exited, status=0\/SUCCESS)\n  Process: 1961 ExecStartPre=\/usr\/sbin\/nginx -t -q -g daemon on; master_process on; (code=exited, status=0\n<\/code><\/pre>\n\n\n\n<p>Now that Nginx is running, run the command below to enable it start it on system reboot;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable nginx<\/code><\/pre>\n\n\n\n<p>If UFW is running, enable Nginx through it. You can run the command below to list applications profiles so you can enable specific profile for Nginx.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw app list | grep -i nginx\n  Nginx Full\n  Nginx HTTP\n  Nginx HTTPS<\/code><\/pre>\n\n\n\n<p>In this case, you can allow any of the Nginx profiles above depending on your current traffic needs, For example, to allow traffic on port 80 (HTTP), just run the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw allow \"Nginx HTTP\"\nufw reload<\/code><\/pre>\n\n\n\n<p>You can now test your web server by navigating to your web browser and entering your server&#8217;s IP address. If you see this page below, then Nginx is running well.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/nginx-test-page.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1027\" height=\"364\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/nginx-test-page.png\" alt=\"LEMP Stack Ubuntu 18.04 nginx test page\" class=\"wp-image-1280\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/nginx-test-page.png 1027w, https:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/nginx-test-page-768x272.png 768w\" sizes=\"(max-width: 1027px) 100vw, 1027px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Install MariaDB RDBMS<\/h3>\n\n\n\n<p>MariaDB server is an open-source fork of MySQL&nbsp;<span class=\"st\">relational database management system<\/span>. Run the command below to install MariaDB server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install mariadb-server -y<\/code><\/pre>\n\n\n\n<p>Just like Nginx, MariaDB is set to run by default after installation. Again, you can run the command, <strong class=\"userinput\"><code>systemctl status mariadb<\/code><\/strong>. You can also set it up to run on system boot by running, <strong class=\"userinput\"><code>systemctl enable mariadb<\/code><\/strong>.<\/p>\n\n\n\n<p>You may want to use MySQL servrer instead of MariaDB. If so, run the command below to install MySQL server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"line\">apt install mysql-server<\/span><\/code><\/pre>\n\n\n\n<p>Once the installation is done, you can run the security script below to perform initial database server security configurations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mysql_secure_installation<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n<strong>...output snipped...<\/strong>\nEnter current password for root (enter for none): <strong>ENTER<\/strong>\nOK, successfully used password, moving on...\n\nSetting the root password ensures that nobody can log into the MariaDB\nroot user without the proper authorisation.\n\nSet root password? [Y\/n] <strong>y<\/strong>\nNew password: <strong>ENTER YOUR PASSWORD<\/strong>\nRe-enter new password: <strong>CONFIRM YOUR PASSWORD<\/strong>\nPassword updated successfully!\nReloading privilege tables..\n ... Success!\n\n\nBy default, a MariaDB installation has an anonymous user, allowing anyone\nto log into MariaDB without having to have a user account created for\nthem.  This is intended only for testing, and to make the installation\ngo a bit smoother.  You should remove them before moving into a\nproduction environment.\n\nRemove anonymous users? [Y\/n] <strong>y<\/strong>\n ... Success!\n\nNormally, root should only be allowed to connect from 'localhost'.  This\nensures that someone cannot guess at the root password from the network.\n\nDisallow root login remotely? [Y\/n] <strong>y<\/strong>\n ... Success!\n\nBy default, MariaDB comes with a database named 'test' that anyone can\naccess.  This is also intended only for testing, and should be removed\nbefore moving into a production environment.\n\nRemove test database and access to it? [Y\/n] <strong>y<\/strong>\n - Dropping test database...\n ... Success!\n - Removing privileges on test database...\n ... Success!\n\nReloading the privilege tables will ensure that all changes made so far\nwill take effect immediately.\n\nReload privilege tables now? [Y\/n] <strong>y<\/strong>\n ... Success!\n\nCleaning up...\n\nAll done!  If you've completed all of the above steps, your MariaDB\ninstallation should now be secure.\n\nThanks for using MariaDB!\n<\/code><\/pre>\n\n\n\n<p>Note, you can press <strong>Enter<\/strong> to accept the default <strong>Y<\/strong>es.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install PHP<\/h2>\n\n\n\n<p>PHP, Hypertext Preprocessor, is a server-side scripting language designed for Web development, but also used as a general-purpose programming language. Run the following command to install PHP and PHP modules for Apache and MySQL.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install php php-fpm php-mysql -y<\/code><\/pre>\n\n\n\n<p>In case you want to install more PHP modules, you can just search the module you want from Ubuntu repos and install it as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt-cache search php- | more<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nlibnet-libidn-perl - Perl bindings for GNU Libidn\nphp-all-dev - package depending on all supported PHP development packages\nphp-cgi - server-side, HTML-embedded scripting language (CGI binary) (default)\nphp-cli - command-line interpreter for the PHP scripting language (default)\nphp-common - Common files for PHP packages\nphp-curl - CURL module for PHP [default]\nphp-dev - Files for PHP module development (default)\nphp-gd - GD module for PHP [default]\nphp-gmp - GMP module for PHP [default]\nphp-ldap - LDAP module for PHP [default]\n--More--\n<\/code><\/pre>\n\n\n\n<p>So if you want to install GD module for example, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install php-gd -y<\/code><\/pre>\n\n\n\n<p>Note that Ubuntu 18.04 ships with the latest version of PHP, <strong>PHP 7.2<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-3-\u2013-installing-php-and-configuring-nginx-to-use-the-php-processor\">Configuring Nginx to Process PHP Pages<\/h3>\n\n\n\n<p>Once the installation is done, you need to configure Nginx to be able to process PHP pages.<\/p>\n\n\n\n<p>To achieve this, edit the Nginx default server block configuration file, <strong class=\"userinput\"><code>\/etc\/nginx\/sites-available\/default<\/code><\/strong>;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>locate the web server root directory and add index.php to the list of files to be processed<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>...<\/strong>\n<strong>root \/var\/www\/html;<\/strong>\n\n# Add index.php to the list if you are using PHP\nindex index.html index.htm index.nginx-debian.html <strong>index.php<\/strong>;\n<strong>..<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>locate the <strong>location <\/strong>block defining PHP script to pass to FastCGI server and make the changes such that the configuration look like;<\/li>\n<\/ul>\n\n\n\n<pre class=\"scroll-box\"><code>\n# pass PHP scripts to FastCGI server\n#\n<strong>location ~ \\.php$ {<\/strong>\n<strong>      include snippets\/fastcgi-php.conf;<\/strong>\n#\n#     # With php-fpm (or other unix sockets):\n      <strong>fastcgi_pass unix:\/var\/run\/php\/php7.2-fpm.sock;<\/strong>\n#     # With php-cgi (or other tcp sockets):\n#     fastcgi_pass 127.0.0.1:9000;\n<strong>}<\/strong>\n<\/code><\/pre>\n\n\n\n<p>Once you are done editing, save the configuration and quit.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Restart Nginx<\/h4>\n\n\n\n<p>In order for the changes to take effect restart Nginx. But before you can do that, verify that the configuration has no syntax errors.<\/p>\n\n\n\n<pre id=\"block-e73268a2-8a1d-4d48-8cde-2176ac8c3fa8\" class=\"wp-block-code\"><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>If everything is okay, go ahead and do restart Nginx<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart nginx<\/code><\/pre>\n\n\n\n<p>Test that PHP is actually working by creating a test php file for example, <strong>index.php<\/strong> under Nginx web root document folder, <strong>\/var\/www\/html<\/strong> and put the following content.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/var\/www\/html\/index.php<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;?php\nphpinfo();\n?&gt;<\/code><\/pre>\n\n\n\n<p>After that, save the file and quit. Restart Apache service to effect the changes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart nginx<\/code><\/pre>\n\n\n\n<p>Now, to test PHP processing, navigate to your browser and enter the following URL in your address bar.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>http:\/\/your-server-IP\/index.php<\/strong><\/code><\/pre>\n\n\n\n<p>You should be able to see a page similar to the one shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_1071\"><a href=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/php-test-page.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1053\" height=\"729\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/php-test-page.png\" alt=\"LEMP Stack on Ubuntu 18.04 php-test-page\" class=\"wp-image-1295\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/php-test-page.png 1053w, https:\/\/kifarunix.com\/wp-content\/uploads\/2018\/11\/php-test-page-768x532.png 768w\" sizes=\"(max-width: 1053px) 100vw, 1053px\" \/><\/a><\/figure>\n\n\n\n<p>You can check the version of the PHP installed with the following command;<\/p>\n\n\n\n<pre id=\"block-3f09f9a7-c843-4be1-ae57-bfdaa128e3c6\" class=\"wp-block-code\"><code>php -v<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>PHP 7.2.10<\/strong>-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )\nCopyright (c) 1997-2018 The PHP Group\nZend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies\n    with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies<\/code><\/pre>\n\n\n\n<p>Now that everything seems fine, remove the PHP test configuration file to avoid creating an attack surface.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>rm -rf \/var\/www\/html\/index.php<\/code><\/pre>\n\n\n\n<p>Congratulations! You have successfully installed and configured a LEMP stack on Ubuntu 18.04 (LTS).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow through this guide to learn how to Setup LEMP Stack (Nginx, MariaDB, PHP 7.2) on Ubuntu 18.04. In our previous article, we covered how<\/p>\n","protected":false},"author":1,"featured_media":12497,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,201],"tags":[136,135,229,203,5005],"class_list":["post-1277","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-lamp-stack","tag-mariadb","tag-mysql","tag-nginx","tag-php","tag-ubuntu-18-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\/1277"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=1277"}],"version-history":[{"count":9,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1277\/revisions"}],"predecessor-version":[{"id":21067,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1277\/revisions\/21067"}],"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=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}