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 (lemptest1.com)

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 lemptest1.com and lemptest2.com

###COPYING YOUR SERVER BLOCKS FROM TEMPLATES
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/lemptest1.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/lemptest2.com

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

###USE NANO TO EDIT YOUR SERVER BLOCK
sudo nano /etc/nginx/sites-available/mytest1.com

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:

###CHANGE ROOT PATH
root /var/www/lemptest1.com/public_html.

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

###CHANGE SERVER NAMES
server_name lemptest1.com www.lemptest1.com

Save and close nano(Press CTRL+ Xand then press yand Enterto save your changes.)

Change your lemptest1.com 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 (lemptest2.com)

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 lemptest2.comdomain.

###COPY TEMPLATE FOR LEMPTEST2.COM
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/lemptest2.com

Now let’s edit the lemptest2.com file by using nanoand scrolling down to the listenline 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_serverline.

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

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.

###CREATING SYMBOLIC LINKS FOR BOTH DOMAINS IN ONE COMMAND
sudo ln -s /etc/nginx/sites-available/lemptest1.com /etc/nginx/sites-enabled/ && sudo ln -s /etc/nginx/sites-available/lemptest2.com /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.

###REMOVE THE DEFAULT SERVER BLOCK SYMBOLIC LINK
sudo rm /etc/nginx/sites-enabled/default

Now, let’s restart Nginx.

###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/hostsso run sudo nano /etc/hostsand 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 ‘127.0.0.1’

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, lemptest1.com and lemptest2.com

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!