buzzoole code

How to setup Cloudflare and NGINX virtual hosts on your VPS

Ok so we have setted up our LEMP environment and everything it’s up and running. I suppose you’d like to start hosting your website(s). In this guide we will see how to set up Cloudflare too.

Setup DNS records

If you already pointed your DNS records to your machine VPS you can skip this step.
I personally use differents registars since I have various first-level domain names, so I have the domain name registar (with DNS management of course), Cloudflare and finally my VPS.

Setup Cloudflare

cdn-hosting-cloudflare

Once logged in or registerd to Cloudflare just click on the Websites button, and add a new domain name, and then Cloudflare will scan your website records.

When the scan it’s completed – it usually takes about one minute to complete – you can setup your Cloudflare, adding all yours DNS records for that domain name. Make sure you added all your domain-specific needs, such as third-level domain names pointing to your VPS IP address.

Make sure everything it’s correct, and then continue to the next step, where you have to configure the Cloudflare setting for that domain name. I use a free account, since it has all I need. If you need SSL security you can consider the PRO plan.

We can continue to the last step. This one tells us how to change our website current DNS to the new Cloudflare DNS. To make the DNS change happen can require up to 24h, so the traffic will be addressed to the server with the old DNS servers since then.

Setup our Server and NGINX

Server’s files and folders

Let’s get familiar with the server folders, we will work with:

  1. /home/: this is the folder where we will put our websites folders.
  2. /etc/nginx/sites-available: in this folder the virtual hosting files are stored. We are going to create a different host config file for each different website. This makes handling of websites easier.
  3. /etc/nginx/sites-enabled: the symbolic links are stored in this folder.

Setup the website files

Each website will have a web root folder html and another one for the log, called log.
Let’s start creating the folders:

Into the html folder we are going to put the files for our web installation, as per normal in any web installation.

Setup NGINX Virtual host file

Now we’re going to setup the virtual host config files for NGINX. Each website will have a proper config file, inside the folder /etc/nginx/sites-available. To create the file for our yourwebsite.com website create a new file:

and then put this in it:

Make the website work: enable the NGINX config file

We are almost at the end of the process. The last step it’s create a symbolic link of the virtual host config file on the /etc/nginx/sites-enabled.

Just run:

The last step it’s to restart NGINX:

Now we should be able to open our website in the browser.

Questo Blog è Indipendente e Supportato Solamente dai suoi Lettori.
Se vuoi sostenerlo, Condivi il Post sui Social Network:
Aggiungi il nostro sito alla Whitelist di Adblock Plus
e magari Fai Anche un Acquisto su Amazon partendo da Qui
(a te non costa nulla!).