Install and configure Subdomain on Nginx CentOS 7

Tram Ho

Nginx is open source software as a web server. In addition to the features of an HTTP server, Nginx can also act as a proxy server for email (IMAP, POP3 and SMTP) and a load balancer and reverse proxy for hosts. Host HTTP, TCP and UDP.

Nginx was created by Igor Sysoev and first released in 2004. According to W3techs, Nginx is used by many users as a Web server, accounting for 42.1% of the total number of Web servers in the world.

1. Install

Step 1: Install Epel Repo

Step 2: Install Nginx

Step 3: Start Nginx

If you are turning on the Firewall, run the following commands to enable the launch of HTTP and HTTPS protocols

Allow to start Nginx service

Now when you access the domain name or IP address you will see the default index page of Nginx. So our installation steps are complete

2. The basic command line for Nginx operation

Start Nginx

Turn off Nginx

Restart Nginx

Check Nginx status

Check Nginx config file

3. Basic configuration form for Nginx

Nginx's default configuration template

First we move to the configuration directory of Nginx on CentOS

Let's see the default config file of Nginx

include /etc/nginx/conf.d/*.conf; : call the .conf files that contain our own configuration commands

listen : the network port we need to use

server_name : our domain name

If you are configuring locally, add domain local at the hosts file in the directory etc

Move to the directory etc

Edit hosts file

Add our local domain

root : address of the project directory that we want to call, by default, we will call the index.html file in that directory, if we want to show the file we specify then we need to declare the file name under the sentence root command

4. Configure Subdomain on Nginx

In this section, I will configure the domain and subdomain as:

https://duongtiendat.com

https://cv.duongtiendat.com

Step 1: Prepare the project

First, I will prepare 2 project folders to use for the domain, subdomain at /var/www/html/duongtiendat/ and /var/www/html/my_resume/

If you do not already have a project, you can create 2 basic index.html files for testing

Step 2: Configure the domain

We move to the /etc/nginx/conf.d/ directory, which will contain our config files

I will create a domain config file called duongtiendat.com.conf

⚠️ Note: The config files must always be in .conf format

The file config will have the following content (This file I configured for the WordPress project should have a few more options):

Step 3: Configure Subdomain

We create subdomain config file named cv.duongtiendat.com.conf

Step 4: Complete config

To check if the last config file has errors, we use the command

If the command returns as below, then we have succeeded

And now we restart nginx to update the latest customizations

So we have finished config, now let's access the domain and subdomain to see the results ??

https://duongtiendat.com

https://cv.duongtiendat.com

Share the news now

Source : Viblo