Nginx is one of the popular websites. It is used as a web server in the popular LEMP stack being the E in the acronym. It is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.
In this guide, we will explore how to use nginx in Ubuntu 20.04, i.e. Installing nginx and configuring nginx to serve a virual site.
Installing Nginx
update our local package index so that we have access to the most recent package listings
|
|
Install nginx
|
|
Update firewall
List available ufw apps:
|
|
Sample output:
|
|
The listed Nginx profiles:
Nginx Full
- Both http - 80 and https - 443Nginx HTTP
- only http traffic 80Nginx HTTPS
- Only https traffic 443 - TLS/SSL encrypted traffic
Enable the profile you want - both http and https in our case:
|
|
Verify the change
|
|
The above command will output which traffic is allowed.
Configuring nginx
Check installed version
|
|
Check systemd service
|
|
You should see this output showing that nginx is running
|
|
To ensure that the nginx service is always running even after reboots, use this systemnd command to enable:
|
|
You can use these other systemd commands to manage nginx:
|
|
To confirm that nginx is actually running and is reachable, use this command:
|
|
where server_ip
is the ip of your server. If by any reason you do not know the public ip of your server use this to check:
|
|
Then:
|
|
You should get default nginx page.
Serving simple static page nginx
Lets take a look at an example to serve static page with nginx. Static page is html and css that doesn’t need any server side rendering. We are going to use the example domain your.domain.com
in our case.
Steps:
- Create the directory to store our static content (we will use
/var/www/your.domain.com
)
|
|
- Ensure the created directory is accessible by the current user
|
|
- Add a sample file to the directory (
/var/www/your.domain.com/index.html
)
|
|
- Create nginx configuration in the directory
/etc/nginx/conf.d
where nginx searches for configs to include when serving. We will create the file/etc/nginx/conf.d/your.domain.com.conf
with the required content. Use this command:
|
|
- Test to make sure that there are no syntax errors in any of the nginx files
|
|
- If there are no errors, restart nginx to reload config
|
|
- To ensure that your domain can reach the server, edit your DNS config to ensure that A records of the site
your.domain.com
is pointing to the server IP.
Your site should now be up and running. Confirm by visiting http://your.domain.com
Important Nginx Files and Directories in Nginx
/var/www/html
: This is the web content directory. This contains the default page content in the fileindex.nginx-debian.html
. That page is rendered when no specifix server host is defined./etc/nginx
: This contains all nginx configuration files./etc/nginx/nginx.conf
: This is the main global nginx configuration file/etc/nginx/sites-available/
: The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to thesites-enabled
directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory./etc/nginx/sites-enabled/
: The directory where enabled per-site server blocks are stored. Typically, these are created by linking to configuration files found in the sites-available directory./etc/nginx/snippets
: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets./var/log/nginx/access.log
: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise./var/log/nginx/error.log
: Any Nginx errors will be recorded in this log.