Getting Started with LEMP: Installing Nginx, MySQL, PHP on Ubuntu 18.04 (Part 1 of 3)

You can find many LAMP/LEMP tutorials online, but I am curating this “How-To” guide for my own benefit so I can come back to this when needed, and also perhaps if anyone needs help, this can serve as a starting point for them.

I’m going to be using VirtualBox for this and I highly recommend if you are new to Linux, you also start off on VirtualBox. It allows you to create snapshots, destroy, and rebuild virtual systems really quickly, easily, and all for free (thanks Oracle!). You can download VirtualBox here.

VirtualBox is great for learning Linux

Once you have VirtualBox installed and your setting for Linux configured, let’s start by updating and upgrading your Linux distro. (NOTE: If you need a tutorial for how to configure VirtualBox, you can find many online. I may add one in the near future, but I just haven’t gotten around to it yet.)

Updating packages and installing Nginx

Let’s start by updating and upgrading any Linux packages that may need some TLC.

sudo apt update && sudo apt upgrade
Update your Ubuntu packages

Once you are good to go, we can begin with installing Nginx

sudo apt update && sudo apt install nginx

Once installed, you can run a service status check to ensure Nginx is running on your instance.

sudo service nginx status
Check the status of Nginx to make sure it’s running

Configuring Network Ports

We’re going to need to enable the Uncomplicated Firewall, also known as ufwin Ubuntu. We’ll add rules for both Nginx and also SSH so we don’t get locked out of our server if we need to access it remotely.

sudo apt-get install openssh-server -y && sudo ufw allow OpenSSH
sudo ufw allow ‘Nginx HTTP’
sudo ufw enable

Once everything is configured, you can check the status of ufw and see the rules you’ve configured.

You’ve now got Nginx installed and running! To test, you can open a browser and visit localhostand you should see the Nginx landing page. You can also find your servers IP by typing the following command in Terminal.

sudo ifconfig | grep -Eo ‘inet (addr:)?([0–9]*\.){3}[0–9]*’ | grep -Eo ‘([0–9]*\.){3}[0–9]*’ | grep -v ‘

Configuring Server Blocks for hosting multiple domains (multi-part section)

Chances are if you are running through this tutorial, you are just learning on how to configure LEMP as a side project and not in a production environment, but when it comes to really configuring a LEMP server, you are going to need to set up Server Blocks. Server Blocks configure your directory to allow you to host multiple domains on one single server. Let’s get started!

Create Directories and Set Permissions

Your web pages will live in the /var/www/directory. Let’s create two new directories in the /var/www/directory for our two domains.

sudo mkdir -p /var/www/
sudo mkdir -p /var/www/

Again, chances are you are just running through this tutorial on your own but if you are actually a Server Administrator, you are going to want to change permissions on files and directories so non-root users can make edits.

sudo chown -R $(whoami):$(whoami) /var/www/
sudo chown -R $(whoami):$(whoami) /var/www/

The $(whoami)variable will take the value of the user you are currently logged in as.

We also need to change the permissions for the /var/www/directory and the data within it to 755in order for the pages to be reached correctly.

sudo chmod -R 755 /var/www

Creating test pages for your Server Blocks

Now, with the directories created with proper permissions, we can create some test pages for each domain to make sure everything is being served properly. We can do this in by using the echocommand and redirect the output to create text index.htmlpages.

sudo echo “Welcome to!” > /var/www/
sudo echo “Welcome to!” > /var/www/

At this point, we are ready to configure our Server Blocks. Lucky for us Nginx provides default templates that allow us to copy these templates and configure the Server Blocks for our own needs. We will be configuring these Server blocks in Part 2 of Installing Nginx, MySQL, PHP (LEMP) Stack on Ubuntu 18.04.