Nginx Ubuntu: installation and configuration

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

Two installation versions

Usually they choose two versions of Nginx Ubuntu with open source - the main and stable. The main version is a small part of the full package. The status “Development and trial version” does not mean that the main one is vulnerable, on the contrary, it is quite stable. Its advantages: you can always pre-try the optimal features and the latest features. The disadvantage is that the likelihood of errors is more than stable.

The latter (full version of Nginx Ubuntu) has analyzed its functions and security using a series of tests and have very high rates. As a result, the user gets a stable and reliable web server, which is suitable for use as an e-commerce server.

Nginx Installation Type

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

There are several installation options for Nginx Ubuntu:

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

Installing from a ready-made package (Debian / Ubuntu) with both the kernel and the default module is very easy to do along with the update process automatically. 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 simple as using the default operating system. You can choose: use the stable version or the main version. In addition, you can more flexibly select the dynamic module that you plan to use. The latter differs from the static module, but some of them can be converted into dynamic ones. For Nginx, the kernel update process is quite complicated, so it must be done manually.

Preassembled 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 the finished package:

Preassembled Package




In order to set this scheme by default, check Ubuntu or Debian Web to see which modules are built in Full. Next, install the software from the pre-assembled Repository package.

Software from the Prebuilt Repository Package




After this, the next signature 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 sources.list package file.
  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.

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

Where:

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

The code name can be seen in the following table.

Codename




Installation code.

Installation code




Configuring NGINX Ubuntu 18.04

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

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 in Ubuntu:

  1. Copy the default file.
  2. Replace example.com with the domain name of the site or public IP.
  3. Disable the default file by adding .disabled to its name.
  4. Open the file in the editor.
  5. Replace the domain name or IP of the site in the directive.
  6. For ready-made content, such as installing WordPress or static files, replace the path in the root directive with the path to the site content.
NGINX installation algorithm in Ubuntu




NGINX Testing:

  1. Check the configuration for errors.
  2. Reboot version.
  3. Go to IP Linode in the browser, after which the NGINX page appears.

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 processed by PHP Ubuntu Nginx.

Requirements:

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

Premise:

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

Create Web Directory

Web directories are lists or folders on the server where all files are stored or hosted. Log in to it using Terminal, Putty and Bitvise.

Next, you need a directory. Below this command creates two directories for two sites, for example, if you want to add only one site, they simply indicate once, instead of domain1.tld and domain2.tld, the desired domain is registered. Next, the owner of the directory is set to www-data and recursively change the access rights to the directory. They place the next one, then create a test file index.html, save and exit the editor by pressing Control + O, then Cotrol + X.

Create Web Directory




Creating a block service

The Nginx server block file is stored internally, then put this configuration there and replace “xxxx” with its 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.

Creating a block service




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

Finally, reload or restart so that the service reboots. They 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 a domain name.

Hosting an HTTP Server

Hosting an HTTP Server




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

Installing Nginx HTTP Server:

  1. Install the server. Packages can be obtained from Ubuntu's software repository, all you need to do is run the apt command.
  2. After the server is installed, configure the firewall to allow HTTP traffic. By default, the Ubuntu firewall is not enabled. In some cases, it turns on automatically for security reasons.
  3. If the firewall is enabled, the single-line commands shown 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. Access the default Nginx website page or navigate to the IP server or host.
  5. When you install the Nginx HTTP server, additional folders will be created. They contain configuration files, settings and other above directories, are the most used on the server.

Using reverse proxy

Using reverse proxy




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

To do this, use simple Node.js to demonstrate how to configure NGINX as a reverse proxy server.

Proxy Installation:

  1. These steps install Mainline on Ubuntu from the official NGINX Inc. repository. For other distributions, you need to read the administrator’s manual to get configuration information for production environments.
  2. Open the /etc/apt/sources.list text editor and add the following line at the bottom.
  3. Replace codename in this example with the code name of the installed version. For example, for Ubuntu Nginx php fpm named Bionic Beaver, they write bionic.
  4. Import the repository package signing key and add it to apt.
  5. Make sure that the server is started and will be turned on automatically upon reboot.

Application example

Application example




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

Run the script:

  1. The apt-get update script will start automatically, so you can immediately install Node.js.
  2. Node Package Manager (NPM) will be unpacked with Node.js.
  3. Create an application directory.
  4. Initialize the Node.js application in the directory.
  5. Accept all default values ​​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. Launch the application.
  9. In a separate terminal window, use curl to make sure the software works.

Setting up Node.js

At this point, you can configure Node.js to serve as an example application on the public IP address of Linode, which would allow you to put it on the Internet. Instead, the section installs php 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, replace example.com, software domain or public IP address in this example.

Launch the application




The proxy_pass directive is a reverse proxy configuration. She points out 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.

Setup Procedure:

  1. Disable or delete the default Welcome to NGINX page: sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled.
  2. Check the configuration: sudo nginx -t.
  3. If no errors are reported, restart the new one: sudo nginx -s reload.
  4. In the browser, go to the public IP address of Linode, the message Hello World!

Additional options: permalink

Additional options: permalink




For a simple proxy_pass application, directive is enough. However, for more complex ones, additional parameters may be required. For example, Node.js is often used for cases that require a lot of real-time interactions. To adapt, disable the Nginx Ubuntu ssl buffering function. You can change or add headers that are sent along with proxy requests 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 renew the files to use the new certificate.

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

Setting up Node.js




Certbot will also ask if you should automatically redirect HTTP traffic to HTTPS. It is recommended to select this option. Upon completion of the tool Certbot will save the generated keys and issued certificates in the / etc / letsencrypt / live / $ domain directory, where $ domain is the domain name entered during the creation of the Certbot certificate.

Certbot recommends specifying the web server configuration in the default certificate directory or creating symbolic 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 its rule to allow incoming and outgoing connections to the HTTPS service. In Ubuntu, UFW is a widely used and simple tool for managing firewall rules.

Server side http server




One of the requirements for implementing an application or website is to have a web or HTTP server on the server side that performs 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 attracts more and more webmasters around the world. Compared to Apache and other web servers, Nginx is significantly faster in processing concurrent sessions, response time and resource usage. This is due to the architecture and smart connection processing. Another advantage Nginx offers is an asynchronous architecture that improves concurrent connection service.




All Articles