Setup Nginx Website Easy Simple Guide

November 26, 2020

Table of Contents

What is needed for a quick Nginx server and website setup?

  1. A Dedicated or VPS Server
  2. A compatible operating system 247Rack recommends: Centos OS
  3. A domain name pointed to the server IP address
  4. A test HTML only website (download a free HTML website template)

Quick install of Nginx and setup of a website

  1. On a fresh server run these commands:

yum update -y

reboot

yum install epel-release

yum install nginx

Use command:

nano /etc/nginx/sites-enabled/

Copy the code below starting with the letter s ending }

server {
  listen 80 default_server;
  listen [::]:80 default_server;  root /var/www/example.com;  index index.html;  server_name example.com www.example.com;  location / {
    try_files $uri $uri/ =404;
  }
}

Now click ctrl and Capital O for orange ctrl+O to save the file.

To test Nginx, you need to visit your server’s public IP address in your web browser:

http://your_server_IP_address/ or http://yourdomain.com/
do a quick Nginx test using command:
sudo nginx –t

Restart Nginx using the following command:
sudo systemctl restart nginx

Please pay attention to any errors as nginx will tell you what the problem is and if you do not know how to fix the issue ask Google.

Auto FREE Let’s Encrypt SSL Nginx Guide:

First, download the Let’s Encrypt client, certbot: Create the certbot repository:

add-apt-repository ppa:certbot/certbot

Install certbot:

apt-get update $ apt-get install python-certbot-nginx

The Let’s Encrypt client is now ready to use.

certbot can automatically configure NGINX for SSL/TLS. It looks for and modifies the server block in your NGINX configuration that contains a server_name directive with the domain name you’re requesting a certificate for. In our example, the domain is www.example.com.Assuming you’re starting with a fresh NGINX install, nano text editor to create a file in the /etc/nginx/conf.d directory named domain‑name.conf (so in our example, www.example.com.conf).

Specify your domain name (and variants, if any) with the server_name directive:server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; server_name example.com www.example.com; }

Save the file, then run this command to verify the syntax of your configuration and restart NGINX:

nginx -t && nginx -s reload

Get The SSL/TLS Certificate

The NGINX plug‑in for certbot takes care of reconfiguring NGINX and reloading its configuration whenever necessary.Run the following command to generate certificates with the NGINX plug‑in:

sudo certbot --nginx -d example.com -d www.example.com

Respond to prompts from certbot to configure your HTTPS settings, which involves entering your email address and agreeing to the Let’s Encrypt terms of service.

If you look at domain‑name.conf, you can see that the open source certbot script has modified it:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    server_name  example.com www.example.com;

    listen 443 ssl; # managed by Certbot

    # RSA certificate
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    # Redirect non-https traffic to https
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}

Automatically Renew Let’s Encrypt Certificates

Let’s Encrypt certificates expire after 90 days. We encourage you to renew your certificates automatically. Here we add a cron job to an existing crontab file to do this.

Open the crontab file with command:
crontab -e

Save and close the file. All installed certificates should now be automatically renewed and reloaded.

Further Information using Nginx to host a website

Extra Website Nginx Guide Installation instructions

Before you install Nginx for the first time on a new machine, you need to set up the Nginx packages repository. Afterwards, you can install and update Nginx from the repository. Remember if you are logged in to your serer as root user, remove the command sudo from all commands below.

If you see this page, then NGINX is correctly installed.

On the “Welcome page,” you will find that the default server root directory is /usr/share/nginx/html. Files that get uploaded to this directory will be available on your web server.

The main Nginx config file is located at /etc/nginx/nginx.conf.

For example you can edit any file using commands like:

nano /etc/nginx/nginx.conf

Configure NGINX to Start on Boot.

The last thing you will want to do is enable NGINX to start on boot. Use the following command if you have not already:

systemctl enable nginx

Building from Sources

If some special functionality is required, not available with packages and ports, Nginx can also be compiled from source files. While more flexible, this approach may be complex for a beginner. For more information, see Building nginx from Sources.

For further information on Nginx please go to the official website http://nginx.org/

Share on
Facebook
Twitter
LinkedIn
Pinterest
More posts

Dedicated Servers Quick Guide

What is a Dedicated Servers? Why bother using a dedicated server over a VPS or Shared Hosting?A dedicated server is a server 100% dedicated to your website/project or business needs.

Get 90% Discount

First 3 People gets the Bonus!
Don't Miss Out Our Big Sale

Get 0-90% On All
247Rack Services

247Rack

The Sale Is Until The End Of March