Adding a New Domain or Subdomain

From 24PinTech Wiki
Revision as of 17:26, 12 May 2022 by Lchristopherson (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

Configuring the Domain

Namecheap 2.png

Before beginning any work on the webserver we need to make sure that the domain is ready to be used, you need to add the A record for it. This will most likely be done by Bradley Chamberlain, but there is a chance that you will sometime have to do this or he will forget how to do it, that is why I am documenting the procedure anyways.

To start, you will log into Namecheap with the account that owns the domain. Once logged in you will see all of the domains that you own listed. Select "Manage" on the domain that you will be using. It will take you to this screen, select "Advanced DNS".

Namecheap .png

After making your way to the Advanced DNS settings, you will see a screen of A records that are what points the website "names" to actual IP addresses. To configure the domain to work with our webserver, select "ADD NEW RECORD", and select the "A record". You will make the host the "@" symbol, and the value will be the public IP of our webserver. Make sure you save the record, and the page. Once all of this is done, you are ready to begin the creation of the server block.

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;
     }
}

Enabling the Server Block

This is the completed server config. All configs vary based on what type of website you are creating, but this should work for a basic WordPress website. Save and exit your file. Next you need to enable the site. To do this, you will run the command sudo ln -s ./example.org ../sites-enabled . This command is saying to make a symbolic link for the server config available site, to the enabled directory. This is essentially how you enable a server config in Nginx.

Nginx successful config.png

Before applying the config changes, you need to run sudo nginx -t to check the Nginx configuration, if this doesn't return something similar to the example picture, that means the configuration file you have just edited is faulty. This could mean many things, from forgetting a semicolon at the end of a line, to using a setting that is not allowed. This is where you must debug the file, and use those skills you have been learning. Once the test returns successful, you will run sudo systemctl restart nginx. To restart the Nginx service. This will enable the website, and you can move on to the next step.

Testing Working Configuration

To check that your sever block is fully functional, you need to create an actual page for the website to run. First, navigate to the websites root directory by running cd /var/www/example.org. This is where all of the files that the website runs on are located. Run the command sudo nano ./info.phpto create and edit the information file. Once in the file, paste this code:

<?php
     phpinfo();
?>

Save and Exit the file by using "Ctrl + O", and "Ctrl + X". Now navigate to the website in the web browser by using the link: example.org/info.php. This will display all of the sensitive information regarding our webserver, make sure that once you see the page pop up, immediately delete the file from the webserver by running, sudo rm ./info.php.

Now the Server block is successfully set up. Congratulations.

New Sub-Domain in Nginx

About

Adding subdomains is a really easy process and doesn't change a lot from adding a normal domain. You'll just need to remember the general process from the Adding a Domain section.

Subdomain Setup

The same process for adding a subdomain in Namecheap will be used as adding a normal domain. For the host variable, instad of using the "@" symbol, you will instead type the name of the subdomain. ex. for wiki.example.org, the host variable will be "wiki". Remember to save the Record and the page. Then you are ready to begin the server setup.

Procedure

To start, navigate to the sites-available directory by running cd /etc/nginx/sites-available. Then, you can either copy a pre-existing configuration by running sudo cp ./example.com ./subdomain.example.org or creating a new file by running sudo nano subdomain.example.org. If you copied it, edit the file by running sudo nano subdomain.example.org.

What needs to be changed

The only important things that need to be changed are the root and server_name variables. Find where the variables are located, and change the root variable to an updated path where the contents for the subdomain can be found. Generally, good practice for this is to create a new folder in the primary domains path with the name of the subdomain. So, if we were trying to create subdomain.example.org, we'd create a new folder by running the command sudo mkdir /var/www/example.org/subdomain. Then, we'd modify our root variable to look something like this:

# before
root /var/www/example.org;

# after
root /var/www/example.org/subdomain;

The server_name variable is just as simple. Just add the subdomain prefix to the beginning of your domain:

# before
server_name example.org;

# after
server_name subdomain.example.org;

Once you've made the proper changes, save and quit the file. Then, run cd ../sites-enabled && sudo ln -s ../sites-available/subdomain.example.org./ . Check the Nginx configuration by running sudo nginx -t for a successful return. Once that is done, restart Nginx by running sudo systemctl restart nginx.