Nginx Ubuntu: installation and configuration

Table of contents:

Nginx Ubuntu: installation and configuration
Nginx Ubuntu: installation and configuration
Anonim

Nginx Ubuntu is an open source web server with powerful load balancing, reverse proxy and caching features. It was originally developed to address scaling and concurrency failures on existing servers. The asynchronous event-based architecture has made it the most popular and efficient.

Two installation versions

Two open source versions of Nginx Ubuntu are usually chosen - mainstream and stable. The major version represents a small part of the complete package. The status "Development and Trial" does not mean that the main one is vulnerable, on the contrary, it is quite stable. Its advantages: you can always try out the best features and the latest features beforehand. Disadvantage - more chance of getting errors compared to stable.

The latest (full version of Nginx Ubuntu) has been analyzed for features and security through a series of tests and has a very high score. As a result, the user receives a stable and reliable web server that is suitable for use as a servere-commerce.

Nginx installation type

Beginners need to correctly apply the installation parameters to deploy the server.

There are several options for installing Nginx Ubuntu:

  1. From the finished Debian/Ubuntu package.
  2. From the finished Nginx repository package.
  3. From source code.

Installation from prepackaged (Debian/Ubuntu) both with kernel and default module is very easy along with automatic upgrade process. The process is carried out by a program consisting of a stable version and several module options.

Installing Nginx Ubuntu from the repository package is as easy as installing from the default operating system. You can choose to use the stable version or the main one. In addition, you can more flexibly choose the dynamic module that you plan to use. The latter is different from a static module, but some of them can be made dynamic. For Nginx, the process of updating the kernel is quite complicated, so it must be done manually.

Pre-assembled package

If you need more flexibility, installing Nginx from source is the right choice. All modules are used as needed, you can change the main code. Running Nginx install Ubuntu from a prepackaged package:

pre-assembled package
pre-assembled package

To set this scheme as default, check Ubuntu or Debian Web to see what modules are built into Full. Next, install the software from the pre-built Repository package.

Software from a pre-built Repository package
Software from a pre-built Repository package

After that, the following signing key is added to ensure that the repository and packages are listed in the manager keychain (apt).

Installation algorithm:

  1. Next, add the default Nginx repository from the Ubuntu or Debian package.
  2. To do this, define the repository in the package file sources.list.
  3. First open the sources.list file in the nano console as a file editor.
  4. After opening the file, add the repository to the bottom line with a common template.
  5. Save changes by pressing Ctrl + x → Y → Enter.
  6. After that, you can install Nginx in the usual way.

The stable version of Nginx Ubuntu server will be installed on the system.

Where:

  • {Linux OS} - Ubuntu or Debian;
  • {codename} - OS name.

The code name can be seen in the following table.

codename
codename

Installation code.

Installation code
Installation code

Configuring NGINX Ubuntu 18.04

Currently the best way to install the 18.04 model is to use the version included in the Ubuntu repositories.

NGINX configuration files are stored in /etc/nginx/conf.d/. Typically, you will need a separate file in this directory for each domain or subdomain.

Algorithm for installing NGINX on Ubuntu:

  1. Copy the default file.
  2. Replace example.com with the site's domain name or public IP.
  3. Disable the default file by adding.disabled to his name.
  4. Open the file in the editor.
  5. Replace the domain name or site IP in the directive.
  6. For ready-made content, such as WordPress installations or static files, replace the path in the root directive with the path to the site content.
How to Install NGINX on Ubuntu
How to Install NGINX on Ubuntu

NGINX Testing:

  1. Check configuration for errors.
  2. Reload version.
  3. Go to the Linode IP in the browser and the NGINX page will appear.

Adding a new site to the server

LEMP stack software demonstrates a group that can be used to process dynamic pages and applications. Internal data is stored in MySQL and dynamic processes are handled by PHP Ubuntu Nginx.

Requirements:

  1. SSH clients like Bitvise or Putty with instructions on how to use them.
  2. Basic knowledge of common Unix commands for SSH to server.
  3. Server or VPS with 256MB minimum RAM (OpenVZ). Recommended to use 512 MB if using KVM or Xen.
  4. Ubuntu 16.04 between 32 or 64 bits.

Prerequisite:

  1. Provides the Ubuntu server.
  2. Installs full LAMP (Ubuntu Nginx Apache, MySQL, Perl / PHP / Python) on Ubuntu server.

Creating a web directory

Web directories are lists or folders on a server where all files are stored or hosted. It is logged in using Terminal, Putty and Bitvise.

Next you need a directory. Below this command creates twodirectories for two sites, for example, if you need to add only one site, they simply specify it once, instead of domain1.tld and domain2.tld, they specify the desired domain. Next, set the owner of the directory to www-data and recursively change the permissions on the directory. Place the next one, then create a test index.html file, save and exit the editor by pressing Control+O, then Cotrol+X.

Create a web directory
Create a web directory

Creating a block service

The Nginx server block file is stored internally, then put this configuration there and replace "xxxx" with your own IPv4 or IPv6 address. Save and exit the editor by pressing Control+O then Control+X. After that, perform similar actions for another site.

Building a block service
Building a block service

Next, you can activate each server block. For security purposes, edit nginx.conf and activate the option: "Name hash size", remove the comment symbol (). Press Control+O to save and Control+X to exit the editor. Now test the configuration and confirm Ok.

Finally, do a reload or restart to restart the service. Then, configure the DNS domain information or edit the local host file on the computer to test the package. Launch a web browser and try to access the VPS through the domain name.

HTTP Server Hosting

Hosting an HTTP Server
Hosting an HTTP Server

HTTP server Nginx (Engine-X) is the most popular of the analogues used on the Internet. It is lightweight, stable, ready to go, whichcan be used by anyone to create powerful and dynamic websites. It can also serve as a proxy server, making it a great choice for securing busy and popular websites.

Install Nginx HTTP server:

  1. Install the server. Packages can be obtained from the Ubuntu software repository, all you have to do is run apt.
  2. After the server is installed, configure the firewall to allow HTTP traffic. The Ubuntu firewall is not enabled by default. In some cases, it turns on automatically for security reasons.
  3. If the firewall is enabled, the given one-liners should allow HTTP traffic on both port 80 and port 443. If you check the firewall status, you can see that HTTP Full is allowed from anywhere, including v6 traffic.
  4. Open access to the default Nginx website page, or go to the IP of the server or host.
  5. Additional folders will be created when Nginx HTTP server is installed. They contain configuration files, settings and other directories mentioned above, are the most used on the server.

Using a reverse proxy

Using a reverse proxy
Using a reverse proxy

A reverse proxy is a server that sits between internal applications and external clients, redirecting client requests. While many common applications such as Node.js can run as servers on their own, NGINX has a number of advanced load balancing, security, and acceleration features,these functions are not available in most specialized programs. Using NGINX as a reverse proxy allows you to add these features to any application.

This uses plain Node.js to demonstrate how to set up NGINX as a reverse proxy.

Set proxy:

  1. These steps install Mainline on Ubuntu from the official NGINX Inc. repository. For other distributions, please refer to the admin guide for setup information for production environments.
  2. Open /etc/apt/sources.list with a text editor and add the following line at the bottom.
  3. Replace codename in this example with the codename of the installed version. For example, Ubuntu Nginx php fpm named Bionic Beaver would be bionic.
  4. Import the repository package signing key and add it to apt.
  5. Make sure the server is running and will be enabled automatically on reboot.

Sample application

Sample Application
Sample Application

Use curl to download the setup script provided by NodeSource. Replace the Node version in the curl command with the one you want to place.

Run script:

  1. The apt-get update script will run automatically so you can install Node.js right away.
  2. The Node Package Manager (NPM) will be unpacked along with Node.js.
  3. Create an application directory.
  4. Initialize the Node.js application in the directory.
  5. Accept all defaults when prompted.
  6. Install Express.js: npm install --save express.
  7. Use a text editor to create and add the following content: app.js.
  8. Start the application.
  9. Use curl in a separate terminal window to make sure the software works.

Configuring Node.js

At this point, Node.js can be configured to serve as a sample application on Linode's public IP address, allowing it to be exposed to the Internet. Instead, the section does a php installation of Ubuntu Nginx to forward all requests from the public IP to the server listening on localhost.

Basic configuration for NGINX with reverse proxy: create a file, in this example replace example.com, software domain or public IP address.

Launch the application
Launch the application

The proxy_pass directive is a reverse proxy configuration. It specifies that all requests that match the location block, in this case the root path, should be redirected to port 3000 on localhost running Node.js.

Setting procedure:

  1. Disable or remove the default page Welcome to NGINX:sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled.
  2. Check configuration: sudo nginx -t.
  3. If no errors are reported, reload a new one:sudo nginx -s reload.
  4. Browser navigates to Linode public IP address, Hello World!

Additional parameters: permalink

Additional parameters: constantlink
Additional parameters: constantlink

For a simple application, the proxy_pass directive is sufficient. However, more complex ones may require additional parameters. For example, Node.js is often used for cases that require a lot of real-time interaction. To adjust, disable the buffering feature of Nginx Ubuntu ssl. You can change or add headers that are sent along with proxy requests to proxy_set_header. This configuration uses the built-in $remote_addr variable to send the IP address of the original client to the proxy host.

One of the benefits of a reverse proxy is the ease of setting up HTTPS with a TLS certificate. Certbot is a tool to quickly get free certificates from Let's Encrypt. You can use Certbot in Ubuntu 16.04, the official site contains detailed instructions for installing and using the main distributions. After receiving the certificates through Certbot, it will automatically update the files to use the new certificate.

Certbot will request information about the site. The answers will be saved as part of the certificate.

Setting up Node.js
Setting up Node.js

Certbot will also ask if you want to automatically redirect HTTP traffic to HTTPS. It is recommended to select this option. After the tool finishes running, Certbot will store the generated keys and issued certificates in the /etc/letsencrypt/live/$domain directory, where $domain is the domain name entered during the Certbot certificate generation step.

Certbot recommends specifying the web server configuration in the default certificate directory or creatingsymbolic links. Keys and certificates should not be moved to another directory. Finally, Certbot will update the server to use the new certificate and also redirect HTTP traffic to HTTPS if this option is selected.

If a firewall is configured on Linode, you can add a firewall rule to allow incoming and outgoing connections to the HTTPS service. On Ubuntu, UFW is a widely used and simple tool for managing firewall rules.

Server Side HTTP Servers
Server Side HTTP Servers

One of the requirements to implement an application or website is to have a web or HTTP server on the server side that does the processing. The latter establishes bidirectional or unidirectional connections with the client and generates a response to requests made. Therefore, it is an integral part of the web infrastructure.

Statistically, Nginx is a growing web server that is attracting more and more webmasters around the world. Compared to Apache and other web servers, Nginx is vastly superior in terms of concurrent session processing speed, response time, and resource usage. This is due to the architecture and smart handling of connections. Another benefit offered by Nginx is its asynchronous architecture, which improves the handling of concurrent connections.

Popular topic

Editor's choice

  • IPv6 protocol: setup on Windows systems
    IPv6 protocol: setup on Windows systems

    Probably, many users of computer systems, delving into the network settings, noticed that in the list of protocols, in addition to the well-known IPv4, there is also the sixth version (IPv6)

  • View history on computer
    View history on computer

    Today you can learn about almost all the actions taken on the computer. It's about browsing history

  • Computer technology hardware: definition, description and types
    Computer technology hardware: definition, description and types

    Modern computers to ensure maximum performance and correct operation use hardware and software that are very interconnected and clearly interact in different directions. Now let's touch on the consideration of hardware, since initially it is they who occupy a dominant position in ensuring the operability of any computer or even mobile system

  • Keyboard shortcuts and the evolution of operating systems
    Keyboard shortcuts and the evolution of operating systems

    It takes about a minute to copy or move a file from one location to another using the menu system. The developers introduced a keyboard shortcut that replaced all these actions and made it possible to reduce the time of this common operation by 3 or more times. True, for this it was necessary to slightly modernize the keyboard: new keys were added - "Ctrl", and later - "Windows"

  • If the torrent does not download
    If the torrent does not download

    An article for those who consider the ability to download files from torrent trackers a convenience, not an echo of the past. Not only the unavailability of "high-speed" tariff plans is the reason for the choice, but also personal preferences