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

In the first part of this LEMP installation guide, we went over updating and installing your Linux packages, installing and testing Nginx, configuring network ports, and creating test pages.

In part 2, we are going to configure our server blocks, move our test pages, make sure they can be served, and edit our host file. Now let’s continue…

Configure the First Server Block (

Nginx already has a handy template for us to use when creating our own server blocks. Copy the file below to the domain you are going to use. For our example, we’ve been using and

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/

Now let’s edit the first Server Block we copied.

sudo nano /etc/nginx/sites-available/

While in the configuration file, scroll down to the line that shows the root to the path of the directory root /var/www/html;. Change this path to the directory we created earlier:

root /var/www/

Now look for the server_name _; line. (TIP: You can use CTRL + W to search while in nano.) Change this line to include the domain name, which in our case is We also will add a www. prefix to our domain list here.


Save and close nano (Press CTRL + X and then press y and Enter to save your changes.)

Change your config file

TIP: to ensure your Nginx config file doesn’t have any syntax issues, run sudo nginx -t. You should get an output of:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Configure the Second Server Block (

The only big difference between configuring your first and second server blocks is the difference in domain names and also making sure there is only one default server running in your instance. You can only have one default server, or else your web server will fail.

Let’s copy over that default Nginx template again, this time to our domain.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/

Now let’s edit the file by using nano and scrolling down to the listen line and removing default_server.

For the sake of keeping this part of the guide short, I’ve included screenshots of the config file and how it should look. By repeating the same steps as the first Server Block, you should have no problem, just make sure to remove the default_server line.

Remove the default_server text
Change the root path and server_name

Don’t forget to check your Nginx syntax by using the sudo nginx -t command.

Creating Symbolic Links

Nginx reads symbolic links upon startup so we will need to configure them to make sure Nginx serves them and they are accessible.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/ && sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Be sure to remove the symbolic link for the default server block, or else it will cause problems with our two main ones we just added.

sudo rm /etc/nginx/sites-enabled/default

Now, let’s restart Nginx.

sudo service nginx restart

Editing Your Host File, and Testing Nginx

If you don’t have domains registered on the public web, you can just edit your host file to make sure your config files are properly defined. Your config file lives in /etc/hosts so run sudo nano /etc/hosts and add your domains along with your server IP. Remember, you can find your server IP by running the following command: sudo ifconfig | grep -Eo ‘inet (addr:)?([0–9]*\.){3}[0–9]*’ | grep -Eo ‘([0–9]*\.){3}[0–9]*’ | grep -v ‘’

Editing your host file

If done correctly, you should be able to open a browser and see your test files by going to your test domains, in this case, and

If you see this, then you win!

In the final installment of this three-part guide, we will be installing MySQL, configuring the security settings, installing PHP and ensuring Nginx is properly configured to serve PHP files. Stay tuned!




Systems thinker, cloud engineer, market speculator

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Installing MariaDB Tuning Performance Tools.

How to add code highlight in Medium without embedding

How To Use GoCenter with Golang 1.13

10-Minute Guide to Set up Test Automation using Selenium WebDriver with Ruby

LeanFT — Setup, Develop, Execute

Quick terminal tip

The “Hello world” of Data Structure-Linked List.

Current Trends in Java Technology

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jovan S Hernandez

Jovan S Hernandez

Systems thinker, cloud engineer, market speculator

More from Medium

Set-Up docker for pen-testing

Ubuntu Firewall | Ubuntu Open Port | UFW Allow Port

Tools Overview

Cron Jobs to Delete Files after ‘X’ days