Growing up it was always common to spin up a "LAMP" box to host a website. The typical setup was:
Linux
Apache
MySQL
PHP
Over the past few years, this model has slightly changed due to new open source technologies bringing new ideas to solve performance and licensing issues at massive scale. In this tutorial, we are going to look at setting up a LEMP box on Debian Stretch (9.1).
Linux
nginx [engine x]
MariaDB
PHP
Please note, MariaDB could easily be swapped out with MySQL in this tutorial, however many have opted to jump over to MariaDB as an open source alternative (actually designed by the original developers of MySQL) over fear Oracle may close source MySQL.
Installing Linux
This tutorial assumes you already have either a copy of Ubuntu 14+ or Debian 7+. This probably works on earlier versions as well, but I haven't tested them. On a side note, I typically don't install Linux builds with an interactive desktop environment, so grab yourself a copy of Putty and ssh in or open up Terminal if you have interactive access to the Desktop Environment. Before continuing, go ahead and update apt-get repos and upgrade any packages currently installed:
apt-get update && apt-get upgrade
Installing nginx
Grab a copy of nginx
apt-get install nginx
Installing MariaDB
Grab a copy of MariaDB
apt-get install mariadb-server
Installing PHP
In this case, I want to roll with PHP7. You can specify php5 or php7 depending on your application, but PHP7 has some great performance enhancements, so for new apps, I'd leverage it. The biggest thing here is to make sure you use the FastCGI Process Manager package. If you specify just php or php7, package manager will pull down apache2 as a dependency. That is not what we want in our LEMP stack.
apt-get install php7.3-fpm
Once installed, fire up your favorite text editor (it's ok if it's vi :)) and edit the default site for nginx
vi /etc/nginx/sites-enabled/default
Search for the comment # Add index.php to the list if you are using PHP and add index.php to the line below it. For example:
index index.html index.htm index.php index.nginx-debian.html;
Next, find the comment # pass PHP scripts to FastCGI server and change the block of code to the following to tell nginx to process .PHP files with FastCGI-PHP:
# pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; }
Save the file. If using vi, you can do that by executing :wq
Next, reload the nginx service to pickup the new changes to our configuration:
service nginx reload
Test
At this point, we can create a php file to validate things are working well. Go ahead and create a new file /var/www/html/info.php and add the following line:
<?php phpinfo();
If you see a page listing the PHP version and the corresponding environment configuration, congratulations, you have finished setting up your new LEMP stack! 🙂