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:

mkdir /home//yourwebsite.com
mkdir /home//yourwebsite.com/html
mkdir /home//yourwebsite.com/log

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:

sudo nano /etc/nginx/sites-available/yourwebsite.com.conf

and then put this in it:

server
{
server_name .yourwebsite.com;

access_log /home/mimo/yourwebsite.com/log/yourwebsite.com.access.log;

error_log /home/mimo/yourwebsite.com/log/yourwebsite.com.error.log;

root /home/mimo/yourwebsite.com/html;

index index.php index.html index.htm;

# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

#Add Expire Headers for static content to speed up
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}

# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}

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:

ln -s /etc/nginx/sites-available/yourwebsite.com.conf /etc/nginx/sites-enabled/yourwebsite.com.conf

The last step it’s to restart NGINX:

sudo /etc/init.d/nginx restart

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


Lascia un Commento!