Websites served by NGINX are defined in server configuration files also known as virtual hosts or vhost files. In this video, learn how to create and test a basic server configuration.
- [Instructor] Let's create our first server configuration. My development VM is already up and running, and I'm connected to it as the default user. Since we'll need elevated privileges for many of the commands we'll be running, I'll go ahead and use "sudo su -" to substitute my user for the root user. First and foremost, let's get rid of that default site. We know nginx is working fine, so we don't need this configuration anymore. We'll find the default configuration in "/etc/nginx/sites-enabled/".
Because the default configuration is linked to a file in "sites-available", we can remove that link with the "unlink" command. If you wanna make sure the default file that was linked to is still there, you can check the contents of the "sites-available" directory. Now, we need to create the configuration file that will hold the configuration for our site. We'll be storing our configurations in "etc/nginx/conf.d/" We'll be setting up a demo site for a veterinary clinic called Wisdom Pet Medicine.
And our demo site will be called "wisdompetmed.local". I'll use Vim to edit a file named "wisdompetmed.local.conf" in the "conf.d" directory. It's good practice to name the vhost file after the site to keep things organized. Since a single nginx server can run multiple sites, naming the configuration file the same as the site makes things easier to manage. It's also important that we use the ".conf" extension, so that the vhost gets picked up by nginx when we reload the configuration.
If there's ever a vhost that we don't want to use, we can either remove the file, or just rename it, so ".conf" isn't the file extension. Inside the file, I'll start the vhost with the "server" directive, and a set of opening and closing curly braces to define the server block. Now, we can add a "listen" directive to tell nginx which port to listen on for requests to serve the site.
Okay, now we can save this file and check out the configuration. There's our vhost. Now, let's use the "nginx" command with the "-t" switch to test the configuration. If all went well, we should see output from nginx stating that the syntax is okay and the test was successful. Now, we need to reload the configuration into the running server. To do that, we'll use the "systemctl" command, and tell it to reload nginx.
If all goes well with this step, we won't see any output, here. But we can confirm with the "systemctl status" command. At the very end of this output, we that nginx was successfully reloaded. Now, let's create the directory that we used in the server config. Just to make sure that our configuration is correct, let's put a test file in that directory.
We can put a simple one in there, just to confirm that all is well. Now, let's take a look at the site to see the response. I'll open a browser and go to 192.168.0.3, the IP address for our development server. Nice! Well, our site isn't much to look at, but seeing the test message gives some confidence that once we put content into the root directory, it'll be served, as expected.
- Installing NGINX on Linux
- Configuring a virtual host
- Installing PHP and MariaDB for the LEMP stack
- Securing sites with NGINX
- Creating SSL certificates
- Reverse proxies
- Load balancing