How to install Nginx on Ubuntu 20.04 | digital ocean (2023)

Introduction

NginxGenericNameIt is one of the most popular web hosts in the world and is responsible for hosting some of the biggest and busiest websites on the Internet. It's an easy choice that can be used as a web server or reverse proxy.

In this guide, we'll cover how to install Nginx on your Ubuntu 20.04 server, customize the firewall, manage the Nginx process, and configure server blocks to host more than one domain from a single server.

requirements

Before starting this guide, you must have a regular non-root user set up on your server with sudo privileges. You can learn how to set up a regular user account by following ourInitial Server Setup Instructions for Ubuntu 20.04.

Optionally, you'll also want to have a domain name registered before completing the final steps of this tutorial. For more information on setting up a domain name with DigitalOcean, please read ourIntroduction to Digital Ocean DNS.

If you have an account, log in as a non-root user to get started.

Step 1 - Install Nginx

Since Nginx is available in the default Ubuntu repositories, it is possible to install it from these repositories usingappropriatepackaging system.

As this is our first interaction with theappropriatePackage system In this section, we will update our local package index to have access to the latest package lists. After that we can installnginx:

  1. sudo appropriateTo update
  2. sudo appropriate installnginx

After accepting the procedure,appropriateinstall Nginx and all necessary dependencies on your server.

Step 2 - Configuring the firewall

Before testing Nginx, your firewall software must be adjusted to allow access to the service. Nginx registers as a serviceufwafter installation, making it easier to access Nginx.

List the application settings thatufwknow how to work with it by typing:

  1. sudoufw apps list

You should get a list of app profiles:

Production

Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH

As the output shows, there are three profiles available for Nginx:

  • Nginx volumen: this profile opens port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx-HTTP: This profile only opens port 80 (normal, unencrypted web traffic)
  • Nginx-HTTPS: This profile only opens port 443 (TLS/SSL encrypted traffic)

We recommend enabling the most restrictive profile that still allows the traffic you configured. For now, we just need to allow traffic on port 80.

You can enable this by typing:

  1. sudoto allow"Nginx-HTTP"

You can verify the change by typing:

  1. sudoufw-State

The output shows which HTTP traffic is allowed:

Production

Estado: activeTo Aktion From-- ------ ----OpenSSH PERMITIR Anywhere Nginx HTTP PERMITIR Anywhere OpenSSH (v6) PERMITIR Anywhere (v6) Nginx HTTP (v6) PERMITIR Anywhere (v6)

Step 3 - Checking your web server

At the end of the installation process, Ubuntu 20.04 will launch Nginx. The web server should now be up and running.

We can check with thissysteminit to ensure the service is running by typing:

  1. systemctl-status nginx

Production

● nginx.service – A high-performance web server and reverse proxy server Loaded: Loaded (/lib/systemd/system/nginx.service; enabled; factory default: enabled) Active:active (in progress)as of Friday, 20/04/2020 16:08:19 UTC; 3 days ago Docs: man:nginx(8) Main PID: 2369 (nginx) Tasks: 2 (limit: 1153) Memory: 3.5 MB CGroup: /system.slice/nginx.service ├─2369 nginx: master process / usr/sbin/nginx -g daemon enabled; master_process activated; └─2380 nginx: worker process

As this confirms, the service has started successfully. However, the best way to test this is to request an Nginx page.

You can access the default Nginx home page to confirm the software is running correctly by browsing to your server's IP address. If you don't know your server's IP address, you can use it to find outicanhazip.comTool that provides your public IP address obtained from elsewhere on the Internet:

  1. ruffled ruffle-4 icanhazip.com

Once you get your server's IP address, enter it into your browser's address bar:

http://your_server_ip

You should get the default Nginx home page:

How to install Nginx on Ubuntu 20.04 | digital ocean (1)

If you are on this page, your server is working correctly and can be managed.

Step 4 – Manage the Nginx process

Now that you have your web server up and running, let's look at some basic administration commands.

To stop your web server, type:

  1. sudosystemctl detener nginx

To start the web server when it is stopped, type:

  1. sudosystemctl startet nginx

To stop and restart the service, type:

  1. sudosystemctl startet nginx o

If you only make configuration changes, Nginx can usually reload without interrupting connections. To do this, type the following:

  1. sudosystemctl nginx neu loaded

By default, Nginx is configured to start automatically when the server starts. If you don't want this, you can disable this behavior by typing:

  1. sudosystemctl deshabilita nginx

To allow the service to start again on startup, you can type:

  1. sudosystemctlactivatenginx

You've now learned the basic administration commands and should be ready to configure your site to host more than one domain.

Step 5 - Configure Server Blocks (Recommended)

If you are using the Nginx web server,server blocked(similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. We created a domain calledyour domain, but you shouldReplace this with your own domain name.

By default, Nginx on Ubuntu 20.04 has a server block enabled which is configured to serve documents from a directory at/var/www/html. While this works fine for a single site, it can become cumbersome when hosting multiple sites. instead of modifying/var/www/html, we create a directory structure in it/var/wwwfor usyour domainlocation, leave/var/www/htmlconfigured as the default directory to be served when a client request does not match any other sites.

Create the directory foryour domainas follows using-page-flag to create the required home directories:

  1. sudo mkdir-p /var/www/your domain/html

Then assign ownership of the directory with the$USEREnvironmental variable:

  1. sudo chon-R$USER:$USER/var/www/your domain/html

Your web roots permissions should be correct if you haven't changed yours.mask-Value that defines the default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files, while only granting read and execute permissions to groups and others, you can enter the following command:

  1. sudo chmod-R755/var/www/your domain

Then create an example.index.htmlpage of usenanoyour favorite editor:

  1. sudo nano/var/www/your domain/html/index.html

Inside, add the following sample HTML code:

/var/www/ihre_domain/html/index.html

<html> <Kopf> <title>Welcome toyour domain!</title> </Kopf> <body> <h1>Success! Thisyour domainServer lock works!</h1> </body></html>

Save and close the file by pressingStrg+Xto exit, then when asked to save,Yand soonProhibited.

In order for Nginx to serve this content, a server block must be created with the correct instructions. Instead of changing the default configuration file directly, we create a new one at/etc/nginx/available-sites/your domain:

  1. sudo nano/etc/nginx/available-sites/your domain

Paste in the following configuration block, which is similar to the default but updated for our new directory and domain name:

/etc/nginx/available-sites/your_domain

server { listen 80; listen [::]:80; root /var/www/your domain/html; index index.html index.htm index.nginx-debian.html; Nome do servidoryour domainwww.your domain; location / { test files $uri $uri/ =404; }}

Please note that we have updated thesourceconfiguration in our new directory and theserver nameto our domain name.

We then activate the file by linking it to theenabled sitesDirectory Nginx reads from on startup:

  1. sudo no-s /etc/nginx/sites-available/your domain/etc/nginx/enabled sites/

Use:Nginx uses a common practice called symbolic links, or symlinks, to keep track of which blocks on your server are enabled. Creating a symbolic link is like creating a shortcut on disk so that you can later remove the shortcut fromenabled sitesDirectory while server block stays inavailable sitesif you want to activate it.

Two farms are now enabled and configured to respond to requests based on your own.listeningyserver nameDirectives (You can read more about how Nginx handles these directiveson here):

  • your domain: Responds to requests foryour domainywww.your_domain.
  • Originally: Responds to all requests on port 80 that do not match the other two blocks.

To avoid a potential hash bucket storage issue that can result from adding additional server names, a single value should be defined in the/etc/nginx/nginx.confFile, File. Open the file:

  1. sudo nano/etc/nginx/nginx.conf

find itserver_names_hash_bucket_sizedirective and remove the#Line comment icon. Using nano, you can quickly search for words in the file by pressingTO CONTROLyW.

Use:Comment out lines of code, usually by putting#at the beginning of a line is another way to disable them without actually deleting them. Many configuration files come with various options commented out so that they can be turned on or off by switching between the live code and the documentation.

/etc/nginx/nginx.conf

... http { ... server_names_hash_bucket_size 64; ...}...

Save and close the file when finished.

Then test to make sure none of your Nginx files have syntax errors:

  1. sudonginx-t

If there are no issues, restart Nginx to activate your changes:

  1. sudosystemctl startet nginx o

Nginx should now serve your domain name. You can test this by navigating tohttp://your domain, where you should see something like this:

How to install Nginx on Ubuntu 20.04 | digital ocean (2)

Step 6 – Familiarize yourself with important Nginx files and directories

Now that you know how to manage the Nginx service yourself, it's worth taking a few minutes to familiarize yourself with some important directories and files.

satisfied

  • /var/www/html: The actual web content, which by default is just the default Nginx page you saw earlier, is served from the/var/www/htmlDirectory. This can be changed by changing the Nginx configuration files.

server settings

  • /etc/nginx: The Nginx configuration directory. All Nginx configuration files are located here.
  • /etc/nginx/nginx.conf: The main Nginx configuration file. This can be changed to make changes to the Nginx global configuration.
  • /etc/nginx/available-sites/: The directory where location-based server blocks can be stored. Nginx will only use the configuration files found in this directory if they are linked to theenabled sitesDirectory. Normally all server block configuration is done in this directory and then activated by a link to the other directory.
  • /etc/nginx/enabled sites/: The directory where location-aware enabled server blocks are stored. They are usually created by linking configuration files located on theavailable sitesDirectory.
  • /etc/nginx/fragments: This directory contains configuration snippets that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for block refactoring.

logs do servidor

  • /var/log/nginx/acceso.log: Every request to your web server is recorded in this log file unless Nginx is configured otherwise.
  • /var/log/nginx/error.registro: All Nginx errors are logged in this log.

Conclusion

After installing your web server, you have many options for the type of content to display and the technologies you want to use to create a richer experience.

If you want to build a more complete application stack, read the articleHow to install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 20.04.

To configure HTTPS for your domain name with a free SSL certificate, uselet's encryptyou should go aheadHow to Secure Nginx with Let's Encrypt on Ubuntu 20.04.

FAQs

How to install Nginx on Ubuntu 22.04 Digitalocean? ›

When you have an account available, log in as your non-root user to begin.
  1. Step 1 – Installing Nginx. ...
  2. Step 2 – Adjusting the Firewall. ...
  3. Step 3 – Checking your Web Server. ...
  4. Step 4 – Managing the Nginx Process. ...
  5. Step 5 – Setting Up Server Blocks (Recommended) ...
  6. Step 6 – Getting Familiar with Important Nginx Files and Directories.
Apr 25, 2022

How to install Nginx server in Ubuntu? ›

Installing a Prebuilt Ubuntu Package from an Ubuntu Repository
  1. Update the Ubuntu repository information: sudo apt-get update.
  2. Install the package: sudo apt-get install nginx.
  3. Verify the installation: sudo nginx -v nginx version: nginx/1.4.6 (Ubuntu)

How to install Nginx on digital ocean droplet? ›

When you have an account available, log in as your non-root user to begin.
  1. Step 1 – Installing Nginx. ...
  2. Step 2 – Adjusting the Firewall. ...
  3. Step 3 – Checking your Web Server. ...
  4. Step 4 – Managing the Nginx Process. ...
  5. Step 5 – Setting Up Server Blocks (Recommended) ...
  6. Step 6 – Getting Familiar with Important Nginx Files and Directories.
Apr 24, 2020

How do I know if Nginx is installed on Ubuntu? ›

We can verify that the Nginx is installed and check its version by using the following command: $ nginx -v.

How install Nginx install? ›

To install NGINX Open Source, follow these steps:
  1. Access your terminal.
  2. Add the key: $ sudo apt-key add nginx_signing.key.
  3. Change directory to /etc/apt. ...
  4. Update the NGINX software: $ sudo apt-get update.
  5. Install NGINX: $ sudo apt-get install nginx.
  6. Type Y when prompted.
  7. Start NGINX: $ sudo systemctl start nginx.service.
Nov 12, 2021

How to install Nginx reverse proxy on Ubuntu? ›

How to Set Up an Nginx Reverse Proxy?
  1. Install Nginx. We'll be using the apt command on Ubuntu 18.04: sudo apt-get update sudo apt-get install nginx.
  2. Disable the Default Virtual Host. ...
  3. Create the Nginx Reverse Proxy. ...
  4. Test Nginx and the Nginx Reverse Proxy.
Nov 30, 2022

How do I get latest Nginx for Ubuntu? ›

How to Install Nginx Latest Version on Ubuntu 20.04, Ubuntu 18.04
  1. Installing Nginx Latest Version on Ubuntu 18.04, 20.04 from Official Nginx Repository. ...
  2. Setting the Nginx Process User. ...
  3. Including Server Block Files. ...
  4. Install Certbot Nginx Plugin. ...
  5. How to Automatically Restart Nginx. ...
  6. Nginx Listens on Multiple IP Addresses.
Jul 22, 2022

How do I install Nginx on Ubuntu 20.04 and host website? ›

Install NGINX
  1. Install NGINX from the package manager. sudo apt install nginx.
  2. The NGINX service starts running immediately. You can verify its status with the following command: sudo systemctl status nginx. ...
  3. Open port 80 on your system's firewall. UFW is the frontend typically used to manage firewall rules on Ubuntu.
Jul 9, 2021

Where is installed Nginx Ubuntu? ›

By default, NGINX will be installed in /usr/local/nginx . You may change this and other options with the Installation and Compile-Time Options.

Where is Nginx located in Ubuntu? ›

On Ubuntu, the Nginx web server stores its documents in /var/www/html , which is typically located on the root filesystem with the rest of the operating system.

How to install nginx on Ubuntu using Docker? ›

With Docker installed on your server, you can proceed with the first step.
  1. Step 1 — Downloading Nginx From Docker Hub. ...
  2. Step 2 — Running in Detached Mode. ...
  3. Step 3 — Building a Web Page to Serve on Nginx. ...
  4. Step 4 — Linking the Container to the Local Filesystem. ...
  5. Step 5 — Using Your Own Nginx Configuration File (Optional)
Oct 28, 2022

How to install nginx on Ubuntu 22? ›

This article will demonstrate the method to set up an Nginx web server on Ubuntu 22.04.
  1. Prerequisites. The operating system running Ubuntu 22.04 Linux. ...
  2. Update Software Repositories. ...
  3. Installing Nginx on Ubuntu 22.04 LTS. ...
  4. Verify Installation. ...
  5. Managing the Nginx Process. ...
  6. Nginx Adjusting Firewall. ...
  7. Test.

How to install nginx in docker container? ›

To create, configure and run a Docker Nginx reverse proxy image, follow these steps: Download the official Nginx Docker image from Docker Hub. Run the Nginx Docker image as a container. Copy the Docker container's Nginx config file to your local file system.

How do I start Nginx from terminal? ›

Nginx can be started from the following command line: $ sudo systemctl start nginx.

How do you check if nginx is installed or not? ›

$ ps -auxww | grep nginx

Sending it to grep allows us to find specific words in the output, in this case: nginx. If Nginx is actually running, the output will include a master and one or more worker processes. If these are present in your output, then you can conclude that NginX is installed and running properly.

How do I enable Nginx? ›

To start the Nginx service on a Linux machine, use the command:
  1. $ sudo systemctl start nginx.service.
  2. $ sudo service nginx start.
  3. $ sudo systemctl stop nginx.service.
  4. $ sudo service nginx stop.
  5. $ sudo systemctl reload nginx.service.
  6. $ sudo service nginx reload.
  7. $ sudo systemctl restart nginx.service.

How to install Nginx and Apache on Ubuntu? ›

  1. Prerequisites.
  2. Step 1 — Installing Apache and PHP-FPM.
  3. Step 2 — Configuring Apache and PHP-FPM.
  4. Step 3 — Configuring Apache to Use mod_fastcgi.
  5. Step 4 — Verifying PHP Functionality.
  6. Step 5 — Creating Virtual Hosts for Apache.
  7. Step 6 — Installing and Configuring Nginx.
  8. Step 7 — Configuring Nginx for Apache's Virtual Hosts.
Aug 27, 2020

How to install Nginx plus on Ubuntu? ›

To install the Ngnix Plus on Debian or Ubuntu, use the following steps: Step 1: Create the /etc/ssl/nginx directory to store the repository certificates. Step 2: We will need to download both public and private certificates from the Nginx customer portal https://cs.nginx.com and download nginx-repo. crt and nginx-repo.

Should I use Nginx as reverse proxy? ›

The benefits of using Nginx as a reverse proxy include: Clients access all backend resources through a single web address. The reverse proxy can serve static content, which reduces the load on application servers such as Express, Tomcat or WebSphere.

Is proxy the same as reverse proxy? ›

A traditional forward proxy server allows multiple clients to route traffic to an external network. For instance, a business may have a proxy that routes and filters employee traffic to the public Internet. A reverse proxy, on the other hand, routes traffic on behalf of multiple servers.

What is the default Nginx config file in Ubuntu? ›

Usually, in Ubuntu, application configurations will be placed in /etc/ the folder. For Nginx, this is /etc/nginx/ by default. The main configuration file will be /etc/nginx/nginx.

What version of Nginx should I use? ›

Which Version Should I Use? We recommend that in general you deploy the NGINX mainline branch at all times. The main reason to use the stable branch is that you are concerned about possible impacts of new features, such as incompatibility with third‑party modules or the inadvertent introduction of bugs in new features.

How do I upgrade from Ubuntu 20.04 LTS to 22.04 LTS today? ›

  1. 1 Make a backup.
  2. 2 What's new in Ubuntu 22.04?
  3. 3 Update your system.
  4. 4 Finding the current kernel version.
  5. 5 Open TCP port 1022.
  6. 6 Upgrade to 22.04 LTS using the CLI.
  7. 7 Verification.
  8. 8 Enable 3rd party repos/mirros.
Dec 19, 2022

How manually install nginx in Ubuntu? ›

  1. Installing Nginx. To install Nginx, use following command: sudo apt update sudo apt install nginx. ...
  2. Creating our own website. Default page is placed in /var/www/html/ location. ...
  3. Setting up virtual host. ...
  4. Activating virtual host and testing results.

How to secure nginx with Let's Encrypt on Ubuntu 20.04 Digitalocean? ›

This tutorial will use /etc/nginx/sites-available/ example.com as an example.
  1. Step 1 — Installing Certbot. ...
  2. Step 2 — Confirming Nginx's Configuration. ...
  3. Step 3 — Allowing HTTPS Through the Firewall. ...
  4. Step 4 — Obtaining an SSL Certificate. ...
  5. Step 5 — Verifying Certbot Auto-Renewal.
Apr 25, 2022

How install and use Docker compose on Ubuntu 20.04 Digitalocean? ›

Here are the basic steps you need to follow to install Docker and docker compose on Ubuntu:
  1. Install the prerequisite software.
  2. Download Docker GPG file.
  3. Configure the Docker software repository.
  4. Install Docker along with containerd and the docker compose plugin.
  5. Validate the Docker and docker compose setup.
Jun 4, 2022

How do I install SSL on Ubuntu 20.04 Digitalocean? ›

We can create the SSL key and certificate files with the openssl command: sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned. key -out /etc/ssl/certs/apache-selfsigned. crt.

How do I install SSL certificate on Ubuntu 20.04 for free? ›

  1. Step 1 Configure your firewall. Ubuntu 20.04 comes with ufw installed, which makes it easy to manage the firewall on your Linux machine. ...
  2. Step 2 Connect Apache web server. ...
  3. Step 3: Install certbot.
  4. Step 4 Install python3 certbot apache. ...
  5. Step 5 Test Let's Encrypt SSL/TLS Certificate.
Jul 4, 2022

How can I make Ubuntu 20.04 more secure? ›

The Most Important Things you Can do to Quickly Secure Ubuntu...
  1. Encrypt Your Hard Drive. ...
  2. Update your Operating System Regularly. ...
  3. Disable Root Access. ...
  4. Disable unnecessary services. ...
  5. Safe Internet Browsing. ...
  6. Conclusion.
Oct 10, 2022

Does Ubuntu 20.04 have Docker? ›

You can install the latest Docker release on Ubuntu versions 18.04, 20.04, 21.10, and 22.04.

Does Ubuntu 20.04 Docker image have Python? ›

Ubuntu 20.04 will end up installing Python 3.8 as a side-effect of installing Python 3.9, which is annoying and leads to larger images.

How do I know if Docker Compose is installed on Ubuntu? ›

Step 3: Check the Version
  1. To verify the installation, check the Docker Compose version by running: docker compose version.
  2. If the output shows the release downloaded in Step 3, you have successfully installed the package.
  3. Also see: This blog covers the important part of Kubernetes which is Kubernetes Network Policy.
May 16, 2022

References

Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated: 11/09/2023

Views: 5873

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.