In Web Hosting

How to Host Websites Using a LEMP Stack

It’s a simple fact, every website in existence needs to be hosted, and is one of the most commonly performed and maintained IT tasks of the information age. When you think about just how many websites there are on a nearly infinite selection of subjects, you begin to realize just how much web hosting must be going on behind the scenes. There are a multitude of ways to host, but one of the most popular is with a virtual hosting server program known as Nginx. If you want a clean hosting environment that can host more than one website at a time, a LEMP (Linux, Nginx, MySQL, PHP) Stack configuration is an ideal solution.

What Is a LEMP Stack?

A ‘stack’ in this context is a combination of software that works together. Therefore a LEMP stack is formed when you start with a Linux operatingsystem, then install Nginx, MySQL, and PHP. This configuration has it’s own acronym in the tech world because it is an incredibly useful set of programs that complement each other and provide functionality. This is, in fact, the faster but slightly

less common variation of the traditional LAMP stack, which favors Apache as the server element. DigitalOcean has a comprehensive guide on setting up a LEMP stack, but since it was published, PHP7 became stable and is a significant improvement. Simply replace 5 with 7.0 where applicable.

Your Website Root Directory

In order to host a website, Nginx needs to know where to look and must be told which directory holds all the files required to render the web page. Place a copy of your website’s root directory onto the Linux server you’ve just configured and remember the absolute file path. There are a few standard locations, so the choice is up to you. Let’s say you’ve dropped it in “/var/www”, which means that your website root directory is “/var/www/”. Later, when you’re configuring Nginx to host the site, this is where you’ll tell it to look.

Create the Nginx Sites-Available Configuration

Now your website data is ready to be referenced and it’s time to tell Nginx to host it. This is done by creating a sites-available entry configured to reference your website, then linking it to the sites-enabled directory and complete the process. To do this, navigate to /ect/nginx/sites-available and create a new file named after your website. We’ll use as the example. Once the file exists, open it with your favorite text editor, or nano if you don’t have one yet. There are hundreds of ways to configure a Nginx sites-available entry, and your results may vary. Many start by copying and experimenting with the default configuration. You can find plenty of basic configurations, and here is a working example that assumes you’re using PHP7:

[code] server {
listen 80;
access_log /var/log/nginx/access.log main;
return 301$request_uri;

server {
listen 80;
access_log /var/log/nginx/access.log main;
root /var/www/;
index index.php index.html index.htm;

# We don’t want to fill our logs for a commonly accessed file
location = /favicon.ico {
log_not_found off;
access_log off;

# We don’t want to fill our logs with robot activity
location = /robots.txt {
allow all;
log_not_found off;
access_log off;

# handle errors
location /50x.html {
root /var/www/;

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;


Configure for Your Website

With the sites-available configuration frame in place, we can now connect it to your website. At the top of the configuration, you need to change the server_name value to your websites name. You will need to change this for both the non-www and www version. Next locate the line starting with “root”, here is where you need your website’s root directory. Replace the root file path with your website’s path and double check to make sure its exact starting from “/var/www”.

Link to Sites-Enabled

As the terminology might suggest, creating a configuration in sites-available makes Nginx aware of it, and the same configuration in sites-enabled allows it to be hosted. For the process to work, you need to have the same file in both folders. While you could always copy and paste the file from sites-available into sites-enabled, this creates additional work. Anytime you want to edit the configuration for any reason, you will need to remember to re-copy and now overwrite the duplicate. Instead, you want to create a symlink (symbolic link) between the two, which will fool sites-available into thinking it has a file by that name, and look for it in sites-enabled. To create a symlink by running:

[code]sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/[/code]

Remember to change “” to the name of your website when running the command. It is important to delete the default config file in sites-enabled because it’s been known to cause conflicts.

Start Nginx and PHP-FPM

Now that you’re configured, you need to ‘restart’ Nginx and PHP-FPM so they can identify the new website entry and begin to host it. This is easily done by running the service restart commands on both processes, although some system configurations require the systemctl approach instead.

Before restarting the Nginx service it’s important that you test you configuration file. This prevents errors and typos from preventing your server from starting normally. The syntax for checking your configuration file is:

[code] nginx -t

If everything works as intended the output should be something similar to the following:

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

Finally restart the Nginx and PHP service

[code] sudo service nginx restart

sudo php7.0-fpm restart

Direct Your DNS Service

As the final step, if you have a DNS service set up that will direct Internet users to your site when they type in your site name, you’ll want to direct this service to the IP address of your host server. If you don’t yet have a DNS registration for your website, you can edit your hosts file to tell your computer where to look without any power to share with others who do not also have edited host files. With either setup, you should now be able to visit your website by typing your website name into your web browser. If everything has gone perfectly, your website will be waiting for you, otherwise it may be time to troubleshoot.


Contact Us Today!

One of the great things about working with Nginx is how easy it is to virtual host. This allows you to host multiple websites on the same server. This makes it great for use as a development server. All you have to do is repeat this process from sites-available onward for each new website you want to host. Once you have a configuration that works, copying and repurposing it is quick and easy. if you found this post helpful, contact us for more helpful website hosting tips.

Recommended Posts

Leave a Comment

Start typing and press Enter to search

hosting server room title