{"id":21,"date":"2017-01-23T14:31:00","date_gmt":"2017-01-23T14:31:00","guid":{"rendered":""},"modified":"2021-02-22T01:29:18","modified_gmt":"2021-02-22T01:29:18","slug":"installing-lemp-ubuntu-1604-nginx","status":"publish","type":"post","link":"https:\/\/www.tech-and-dev.com\/blog\/2017\/01\/installing-lemp-ubuntu-1604-nginx.html","title":{"rendered":"Installing LEMP &#8211; Ubuntu 16.04, Nginx, MariaDB &#038; PHP 7"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<div dir=\"ltr\" style=\"text-align: left;\">\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/lemp.png\"><img decoding=\"async\" title=\"LEMP Ubuntu Nginx MariaDB PHP\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/lemp.png\" alt=\"LEMP Ubuntu Nginx MariaDB PHP\" border=\"0\" \/><\/a><\/div>\n<p>Make sure you have Ubuntu 16.04 64 bit. In this tutorial, I&#8217;m assuming you are logged in as root and issuing the commands as root (Not recommended in live environment). If you&#8217;re not a root user, use sudo before every command.<\/p>\n<p><a name=\"more\"><\/a><\/p>\n<h2 style=\"text-align: left;\">Let&#8217;s start with updating everything<\/h2>\n<pre>apt-get update &amp;&amp; apt-get upgrade<\/pre>\n<p>If you&#8217;re on a fresh Ubuntu setup and get the following error:<\/p>\n<blockquote><p><span style=\"font-size: small;\"><i>perl: warning: Setting locale failed.<br \/>\nperl: warning: Please check that your locale settings:<br \/>\nLANGUAGE = &#8220;en_US:en&#8221;,<br \/>\nLC_ALL = (unset),<br \/>\nLC_MESSAGES = &#8220;en_US.UTF-8&#8221;,<br \/>\nLANG = &#8220;en_US.UTF-8&#8221;<br \/>\nare supported and installed on your system.\u00a0<\/i><\/span><span style=\"font-size: small;\"><span style=\"font-size: xx-small;\"><i><br \/>\n<\/i><\/span><\/span><span style=\"font-size: small;\"><i><a href=\"https:\/\/www.blogger.com\/null\" name=\"more\"><\/a><br \/>\nperl: warning: Falling back to the standard locale (&#8220;C&#8221;).<br \/>\nlocale: Cannot set LC_CTYPE to default locale: No such file or directory<br \/>\nlocale: Cannot set LC_MESSAGES to default locale: No such file or directory<br \/>\nlocale: Cannot set LC_ALL to default locale: No such file or directory<\/i><\/span><\/p><\/blockquote>\n<p>you can check how to fix it in <a href=\"https:\/\/www.tech-and-dev.com\/blog\/2017\/01\/23\/fixing-perl-warning-setting-locale-failed-on-ubuntu\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<h2 style=\"text-align: left;\">Make sure Apache is not running, and remove it if it&#8217;s installed<\/h2>\n<pre>systemctl stop apache2\r\napt-get remove apache2<\/pre>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: orange;\">(Optional)<\/span> If Apache was installed, remove its default index.php<\/h2>\n<pre>rm \/var\/www\/html\/index.html<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: left;\">Install Nginx<\/h2>\n<pre>apt-get install nginx<\/pre>\n<p>Nginx should be installed and running, you can check it by going to your domain name (If you already pointed it to the IP) or server&#8217;s IP (If the IP is dedicated)<\/p>\n<div style=\"clear: both; text-align: center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/nginx2Bsuccessful2Binstall.png\" width=\"400\" height=\"157\" border=\"0\" \/><\/div>\n<p>If you can&#8217;t access the page, you probably have the Ubuntu firewall installed, simply add the following rule:<\/p>\n<pre>ufw allow 'Nginx HTTP'<\/pre>\n<\/div>\n<p>And add the rules for https too:<\/p>\n<pre>ufw allow 'Nginx HTTPS'<\/pre>\n<p>Or you can add both at the same time<\/p>\n<pre>ufw allow 'Nginx Full'<\/pre>\n<p>You can check the current allowed rules by issuing the below command:<\/p>\n<pre>ufw status verbose<\/pre>\n<p>And you will see the output similar to this:<\/p>\n<div style=\"clear: both; text-align: center;\"><img decoding=\"async\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/nginx2Badded2Bto2Bufw2Bfirewall.png\" border=\"0\" \/><\/div>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: left;\">Install Maria DB<\/h2>\n<pre>apt-get install mysql-server<\/pre>\n<p>You will be asked to enter a root password. I suggest you enter a hard to guess one. Don&#8217;t lose it, you will need it for the next step.<\/p>\n<p>Secure your MariaDB installation by running the following command:<\/p>\n<pre>mysql_secure_installation<\/pre>\n<p>You will be asked several questions, most importantly remove access of the anonymous user and set hard passwords.<\/p>\n<h2 style=\"text-align: left;\">Install PHP-FPM<\/h2>\n<p>Php-fpm (php fastcgi process manager) is responsible for processing the php code and arguably the fastest process manager nowadays. By default PHP 7 will be installed<\/p>\n<pre>apt-get install php-fpm<\/pre>\n<p>Installing PHP for Mysql, to allow php to connect to the mysql (MariaDB) server<\/p>\n<pre>apt-get install php-mysql<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: left;\">Configuring Nginx<\/h2>\n<p><b><span style=\"color: purple;\">Processing PHP<\/span><\/b><br \/>\nWe need to modify the Virtual Host configuration to allow Nginx to process PHP, by simply adding index.php to the line<\/p>\n<pre>index index.html index.htm index.nginx-debian.html;<\/pre>\n<p>so it would become:<\/p>\n<pre>index index.php index.html index.htm index.nginx-debian.html;<\/pre>\n<p><span style=\"color: purple;\"><b>Uncomment the following to process PHP with FPM:<\/b><\/span><\/p>\n<pre>location ~ .php$ {<\/pre>\n<p>include snippets\/fastcgi-php.conf;<\/p>\n<p>fastcgi_pass unix:\/run\/php\/php7.0-fpm.sock;<\/p>\n<p>}<\/p>\n<p><span style=\"color: purple;\"><b>Uncomment the following to disallow users from accessing .htaccess files<\/b><\/span><\/p>\n<pre>location ~ \/.ht {<\/pre>\n<p>deny all;<\/p>\n<p>}<\/p>\n<p><span style=\"color: purple;\"><b>Set your server IP or domain name by replacing:<\/b><\/span><\/p>\n<pre>server_name _;<\/pre>\n<p>to<\/p>\n<pre>server_name <span style=\"color: #38761d;\">xxx.xxx.xxx.xxx<\/span>;<\/pre>\n<p>Where <span style=\"color: #38761d;\">xxx.xxx.xxx.xxx<\/span> is your server&#8217;s IP address or you can enter the domain name instead.<\/p>\n<p><span style=\"color: purple;\"><b>:wq<\/b><\/span> to save and quit<\/p>\n<p><b><\/b><br \/>\n<span style=\"color: purple;\"><b>Check for Nginx Syntax errors:<\/b><\/span><\/p>\n<pre>nginx -t<\/pre>\n<p>You should get a message:<br \/>\n<span style=\"color: #38761d;\"><i>nginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok<br \/>\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/i><\/span><\/p>\n<p><span style=\"color: purple;\"><b>Reload Nginx Configurations (Or Restart):<\/b><\/span><\/p>\n<pre>systemctl reload nginx<\/pre>\n<p>(restart = stop + start, reload = keep running + reload configuration files)<\/p>\n<h2 style=\"text-align: left;\">Configuring PHP<\/h2>\n<p><b><span style=\"color: purple;\">Disable cgi.fix-pathinfo<\/span><\/b> (http:\/\/php.net\/cgi.fix-pathinfo)<\/p>\n<pre>vim \/etc\/php\/7.0\/fpm\/php.ini<\/pre>\n<p><i>&#8220;Setting this to 1 will cause PHP <acronym title=\"Common Gateway Interface\">CGI<\/acronym> to fix its paths to conform to the spec<\/i>&#8221; &#8211; php.net<br \/>\nWhich basically means PHP will attempt to run a similar file if the requested one is not available, as a result, an attacker can maliciously use this to their advantage.<\/p>\n<p><span style=\"color: purple;\"><b>Search for cgi.fix-pathinfo inside vim<\/b><\/span><\/p>\n<pre>\/cgi.fix-pathinfo<\/pre>\n<p><span style=\"color: purple;\"><b>Enter editing mode by pressing &#8216;i&#8217;,<\/b><\/span><br \/>\nUncomment it by removing the semicolon and set it to 0<\/p>\n<pre>cgi.fix_pathinfo=0<\/pre>\n<p><span style=\"color: purple;\"><b>:wq <\/b><\/span>to save and quit<\/p>\n<p><span style=\"color: purple;\"><b>Restart PHP-fpm<\/b><\/span><\/p>\n<pre>systemctl restart php7.0-fpm<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: left;\">Create &amp; Test a PHP script:<\/h2>\n<p>Your server should be ready to run PHP, <span style=\"color: purple;\"><b>Create a script in \/var\/www\/html called index.php or anything you want,<\/b><\/span><\/p>\n<pre>vim var\/www\/html\/<span style=\"color: orange;\">index.php<\/span><\/pre>\n<p><span style=\"color: purple;\"><b>And insdie it write:<\/b><\/span><\/p>\n<pre>&lt;?php phpinfo(); ?&gt;<\/pre>\n<p><span style=\"color: purple;\"><b>:wq <\/b><\/span>to save and quit<\/p>\n<p><span style=\"color: purple;\"><b>Test your Nginx configuration<\/b><\/span> by going to your IP or domain name \/index.php, example http:\/\/<span style=\"color: #38761d;\">xxx.xxx.xxx.xxx<\/span>\/<span style=\"color: orange;\">index.php<\/span><br \/>\nWhere <span style=\"color: #38761d;\">xxx.xxx.xxx.xxx<\/span> is your server&#8217;s IP address or you can enter the domain name instead. And <span style=\"color: orange;\">index.php <span style=\"color: black;\">is the filename you chose earlier.<\/span><\/span><\/p>\n<div style=\"clear: both; text-align: center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/lemp2Bphp2Binfo.png\" width=\"400\" height=\"235\" border=\"0\" \/><\/div>\n<p><b>Any comments or questions? Leave them below!<\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Make sure you have Ubuntu 16.04 64 bit. In this tutorial, I&#8217;m assuming you are logged in as root and issuing the commands as root (Not recommended in live environment). If you&#8217;re not a root user, use sudo before every command. Let&#8217;s start with updating everything apt-get update &amp;&amp; apt-get upgrade If you&#8217;re on a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,6,30,27,22,4],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-lemp","category-linux","category-mariadb","category-nginx","category-php","category-ubuntu"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":3,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":407,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions\/407"}],"wp:attachment":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}