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.

###UPDATING AND UPGRADING LINUX PACKAGES
sudo apt update && sudo apt upgrade
Update your Ubuntu packages

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

###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.

###CHECKING NGINX STATUS
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.

###INSTALL OPENSSH SERVER PACKAGE & ENABLE OPENSSH ON UFW
sudo apt-get install openssh-server -y && sudo ufw allow OpenSSH
###ADD RULE FOR NGINX HTTP
sudo ufw allow ‘Nginx HTTP’
###ENABLE UFW
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.

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

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.

###CREATE DIRECTORIES FOR SEPERATE PAGES
sudo mkdir -p /var/www/lemptest1.com/public_html
sudo mkdir -p /var/www/lemptest2.com/public_html

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.

###CHANGING PERMISSION AND OWNERSHIP
sudo chown -R $(whoami):$(whoami) /var/www/lemptest1.com/public_html
sudo chown -R $(whoami):$(whoami) /var/www/lemptest1.com/public_html

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.

###CHANGE PERMISSION OF /VAR/WWW
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.

###TESTPAGE1
sudo echo “Welcome to LEMPTest1.com!” > /var/www/lemptest1.com/public_html/index.html
###TESTPAGE2
sudo echo “Welcome to LEMPTest2.com!” > /var/www/lemptest2.com/public_html/index.html

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.

Leave a Reply

Your email address will not be published. Required fields are marked *