Adding a New Domain or Subdomain

From 24PinTech Wiki
Jump to navigation Jump to search


New Domains in Nginx

About

Generally speaking, adding a new (website/domain/server block) is very easy in Nginx. A common misconception throughout 24PinTech is that creating a server block is difficult, to try to curb the fear, I will go over adding a new website to our specific server setup.

What you need to start

Procedure

Esxi login.png

Log in to the Nginx Webserver

To begin your process of adding a server block to Nginx navigate to your favorite web browser and type in the address bar, "10.21.25.2". This will take you to our Valhalla ESXi box, this is our ESXi box that hosts all of our production VMs, which is also where devloganweb lives (our Webserver). If you are an administrator on our domain you may log in with your account for the in-class computers, otherwise see a server manager or Bradley Chamberlain for root account access. Navigate to the Webserver VM and log into it using the information provided in the secret sauce.

Creating the Nginx Server Block

Once you have logged into the webserver you will need to create an Nginx server block. Use the Linux Basics for Webserver page if any of the commands used are confusing to you. Make sure you have a full understanding of this process otherwise it will be a pain to debug.

Firstly, navigate to the sites-available directory by running the command cd /etc/nginx/sites-available. This directory holds all of the Nginx website server blocks. These server blocks are what actually lets the website run on the internet. Once in the sites-available directory you will have access to view/edit all of the other website server blocks. To create a new server block for your domain, run the command sudo touch ./example.org to create the file, then sudo nano ./example.org to edit the file. Alternatively, you may just run the ladder command to create and edit the file. The server blocks have very specific formatting for everything to run correctly. The following is a basic server block that has been commented to increase understanding.

server {
     listen 80; # This is the port that Nginx will be listening on
     listen [::]:80; # ex. 80 = HTTP, while 443 = HTTPS. We will never use 443.

     # If you're not going to be using a domain,
     # then you can replace it with an IP
     server_name domain.com;

     # The root is the directory that the website files will be held in
     root /var/www/domain.com;
     index index.php index.html index.htm; # The default web page

     location = /favicon.io {
          log_not_found off;
          access_log off;
     }

     location = /robots.txt {
          allow all;
          log_not_found off;
          access_log off;
     }

     location / {
          try_files $uri $uri/ /index.php$args;
     }

     location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_intercept_errors on;
          fastcgi_pass unix:/run/php/php7.4-fpm.sock; # Change this to current php version
     }

     location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
          expires max;
          log_not_found off;
     }
}

Save and exit your file.

New Sub-Domain in Nginx