How to install nginx
How to install nginx
nginx: Linux packages
Supported distributions and versions
nginx packages are available for the following Linux distributions and versions:
Version Supported Platforms 7.4+ x86_64, aarch64/arm64 8.x x86_64, aarch64/arm64, s390x 9.x x86_64, aarch64/arm64, s390x
Version Supported Platforms 10.x “buster” x86_64, i386, aarch64/arm64 11.x “bullseye” x86_64, aarch64/arm64
Version Supported Platforms 18.04 “bionic” x86_64, aarch64/arm64 20.04 “focal” x86_64, aarch64/arm64, s390x 22.04 “jammy” x86_64, aarch64/arm64, s390x
Version Supported Platforms 12 SP5+ x86_64 15 SP2+ x86_64
Version Supported platforms 3.13 x86_64, aarch64/arm64 3.14 x86_64, aarch64/arm64 3.15 x86_64, aarch64/arm64 3.16 x86_64, aarch64/arm64
Version Supported platforms 2 (LTS) x86_64, aarch64/arm64
Installation instructions
Before you install nginx for the first time on a new machine, you need to set up the nginx packages repository. Afterward, you can install and update nginx from the repository.
RHEL/CentOS
Install the prerequisites:
To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:
To install nginx, run the following command:
Debian
Install the prerequisites:
Import an official nginx signing key so apt could verify the packages authenticity. Fetch the key:
Verify that the downloaded file contains the proper key:
The output should contain the full fingerprint 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 as follows:
If the fingerprint is different, remove the file.
To set up the apt repository for stable nginx packages, run the following command:
If you would like to use mainline nginx packages, run the following command instead:
Set up repository pinning to prefer our packages over distribution-provided ones:
To install nginx, run the following commands:
Ubuntu
Install the prerequisites:
Import an official nginx signing key so apt could verify the packages authenticity. Fetch the key:
Verify that the downloaded file contains the proper key:
The output should contain the full fingerprint 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 as follows:
If the fingerprint is different, remove the file.
To set up the apt repository for stable nginx packages, run the following command:
If you would like to use mainline nginx packages, run the following command instead:
Set up repository pinning to prefer our packages over distribution-provided ones:
To install nginx, run the following commands:
Install the prerequisites:
To set up the zypper repository for stable nginx packages, run the following command:
If you would like to use mainline nginx packages, run the following command instead:
Next, import an official nginx signing key so zypper/rpm could verify the packages authenticity. Fetch the key:
Verify that the downloaded file contains the proper key:
The output should contain the full fingerprint 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62 as follows:
Finally, import the key to the rpm database:
To install nginx, run the following command:
Alpine
Install the prerequisites:
To set up the apk repository for stable nginx packages, run the following command:
If you would like to use mainline nginx packages, run the following command instead:
Next, import an official nginx signing key so apk could verify the packages authenticity. Fetch the key:
Verify that downloaded file contains the proper key:
The output should contain the following modulus:
Finally, move the key to apk trusted keys storage:
To install nginx, run the following command:
The @nginx tag should also be specified when installing packages with dynamic modules:
Amazon Linux
Install the prerequisites:
To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:
To install nginx, run the following command:
Source Packages
Packaging sources can be found in the packaging sources repository.
The default branch holds packaging sources for the current mainline version, while stable-* branches contain latest sources for stable releases. To build binary packages, run make in debian/ directory on Debian/Ubuntu, or in rpm/SPECS/ on RHEL/CentOS/SLES/Amazon Linux, or in alpine/ on Alpine.
Packaging sources are distributed under the same 2-clause BSD-like license used by nginx.
Dynamic Modules
Main nginx package is built with all modules that do not require additional libraries to avoid extra dependencies. Since version 1.9.11, nginx supports dynamic modules and the following modules are built as dynamic and shipped as separate packages:
Signatures
Since our PGP keys and packages are located on the same server, they are equally trusted. It is highly advised to additionally verify the authenticity of the downloaded PGP key. PGP has the “Web of Trust” concept, when a key is signed by someone else’s key, that in turn is signed by another key and so on. It often makes possible to build a chain from an arbitrary key to someone’s key who you know and trust personally, thus verify the authenticity of the first key in a chain. This concept is described in details in GPG Mini Howto. Our keys have enough signatures, and their authenticity is relatively easy to check.
How To Install Nginx on Ubuntu 18.04
Introduction
Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or reverse proxy.
In this guide, you’ll learn how to install Nginx on your Ubuntu 18.04 server and about important Nginx files and directories.
Prerequisites
Before you begin this guide, you should have a regular, non-root user with sudo privileges and a basic firewall configured on your server. You can learn how to configure a regular user account by following our initial server setup guide for Ubuntu 18.04.
When you have an account available, log in as your non-root user to begin.
Step 1 – Installing Nginx
Since Nginx is available in Ubuntu’s default repositories, it is possible to install it from these repositories using the apt packaging system.
Since this may be your first interaction with the apt packaging system in this session, update the local package index so that you have access to the most recent package listings. Afterward, you can install nginx :
After accepting the procedure, apt will install Nginx and any required dependencies to your server.
Step 2 – Adjusting the Firewall
Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw upon installation, making it straightforward to allow Nginx access.
List the application configurations that ufw knows how to work with by typing the following:
Your output should be a list of the application profiles:
This list displays three profiles available for Nginx:
You can enable this by typing the following:
Then, verify the change:
You should receive a list of HTTP traffic allowed in the output:
Now that you’ve added the appropriate firewall rule, you can check that your web server is running and able to serve content correctly.
Step 3 – Checking your Web Server
At the end of the installation process, Ubuntu 18.04 starts Nginx. The web server should already be up and running.
Check with the systemd init system to make sure the service is running:
This output shows that the service has started successfully. However, the best way to test this is to actually request a page from Nginx.
You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address. If you do not know your server’s IP address, you can get it a few different ways.
Try typing the following at your server’s command prompt:
You will receive a few lines. You can try each in your web browser to confirm if they work.
An alternative is running the following command, which should generate your public IP address as identified from another location on the internet:
When you have your server’s IP address, enter it into your browser’s address bar:
You should receive the default Nginx landing page:
This page is included with Nginx to verify that the server is running correctly.
Step 4 – Managing the Nginx Process
Now that you have your web server up and running, let’s review some basic management commands.
To stop your web server, type the following:
To start the web server when it is stopped, type the following:
To stop and then start the service again, type the following:
If you are simply making configuration changes, you can often reload Nginx without dropping connections instead of restarting it. To do this, type the following:
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing the following:
To re-enable the service to start up at boot, you can type the following:
Nginx should now start automatically when the server boots again.
Step 5 – Setting Up Server Blocks (Recommended)
When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, see our Introduction to DigitalOcean DNS.
The permissions of your web roots should be correct if you haven’t modified your umask value, but you can make sure by typing the following:
Next, create a sample index.html page using nano or your favorite editor:
Inside, add the following sample HTML:
In order for Nginx to serve this content, it’s necessary to create a server block with the correct directives. Instead of modifying the default configuration file directly, make a new one at /etc/nginx/sites-available/ your_domain :
Add the following configuration block, which is similar to the default, but updated for your new directory and domain name:
Notice that we’ve updated the root configuration to the new directory, and the server_name to the domain name. Save and close the file when you are finished.
Next, enable the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup:
Two server blocks are now enabled and configured to respond to requests based on their listen and server_name directives (you can read more about how Nginx processes these directives here):
To avoid a possible hash bucket memory problem that can arise from adding additional server names, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. Open the file:
Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line:
Save and close the file when you are finished.
Next, test to make sure that there are no syntax errors in any of your Nginx files:
If there aren’t any problems, restart Nginx to enable your changes:
Step 6 – Getting Familiar with Important Nginx Files and Directories
Now that you know how to manage the Nginx service itself, you should take a few minutes to familiarize yourself with a few important directories and files.
Content
Server Configuration
Server Logs
Conclusion
Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.
If you’d like to build out a more complete application stack, check out this article on how to configure a LEMP stack on Ubuntu 18.04.
Want to learn more? Join the DigitalOcean Community!
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Installing NGINX Open Source
Install NGINX Open Source either as a prebuilt package or from source, following step-by-step instructions for all supported Linux distributions.
This article explains how to install NGINX Open Source.
Choosing Between a Stable or a Mainline Version
NGINX Open Source is available in two versions:
Choosing Between a Prebuilt Package and Compiling from Source
Both the NGINX Open Source mainline and stable versions can be installed in two ways:
Installing a Prebuilt Package
Installing NGINX Open Source from a package is much easier and faster than building from source, but building from source enables you to compile in non-standard modules. Prebuilt packages are available for most popular Linux distributions, including CentOS, Debian, Red Hat Enterprise Linux (RHEL), SUSE Linux Enterprise Server (SLES), and Ubuntu. See Linux packages at nginx.org for the list of currently supported operating systems.
Modules Included in a Prebuilt Package
See Configure Arguments at nginx.org for the list of modules included in each prebuilt package.
Installing Prebuilt CentOS and RHEL Packages
NGINX, Inc. provides packages for the following CentOS, Oracle Linux, and RHEL versions:
Version | Supported Platforms |
---|---|
6.x | x86_64, i386 |
7.4+ | x86_64, ppc64le |
8.x | x86_64 |
The package can be installed from:
Installing a Prebuilt CentOS/RHEL Package from an OS Repository
Install the EPEL repository:
Update the repository:
Install NGINX Open Source:
Verify the installation:
Installing a Prebuilt CentOS/RHEL Package from the Official NGINX Repository
Set up the yum repository for RHEL or CentOS by creating the file nginx.repo in /etc/yum.repos.d, for example using vi :
Add the following lines to nginx.repo:
The /mainline element in the pathname points to the latest mainline version of NGINX Open Source; delete it to get the latest stable version
is either rhel or centos
For example, to get the latest mainline package for CentOS 7, insert:
Save the changes and quit vi (press ESC and type wq at the : prompt).
Update the repository:
Install the NGINX Open Source package:
Start NGINX Open Source:
Verify that NGINX Open Source is up and running:
Installing Prebuilt Debian Packages
NGINX provides packages for the following Debian operating systems:
Version | Codename | Supported Platforms |
---|---|---|
9.x | stretch | x86_64, i386 |
10.x | buster | x86_64, i386 |
The package can be installed from:
Installing a Prebuilt Debian Package from an OS Repository
Update the Debian repository information:
Install the NGINX Open Source package:
Verify the installation:
Installing a Prebuilt Debian Package from the Official NGINX Repository
Download the key used to sign NGINX packages and the repository, and add it to the apt program’s key ring:
Edit the /etc/apt/sources.list file, for example with vi :
Add these lines to sources.list to name the repositories from which the NGINX Open Source can be obtained:
The /mainline element in the pathname points to the latest mainline version of NGINX Open Source; delete it to get the latest stable version
is the codename of a Debian release
For example, to get the latest mainline package for Debian 8.2 (“jessie”), insert:
Save the changes and quit vi (press ESC and type wq at the : prompt).
Install the NGINX package:
Start NGINX Open Source:
Verify that NGINX Open Source is up and running:
Installing Prebuilt Ubuntu Packages
NGINX provides packages for the following Ubuntu operating systems:
Version | Codename | Supported Platforms |
---|---|---|
16.04 | xenial | x86_64, i386, ppc64el, aarch64/arm64 |
18.04 | bionic | x86_64, aarch64/arm64 |
19.10 | eoan | x86_64 |
20.04 | focal | x86_64, aarch64/arm64 |
The package can be installed from:
Installing a Prebuilt Ubuntu Package from an Ubuntu Repository
Update the Ubuntu repository information:
Install the package:
Verify the installation:
Installing a Prebuilt Ubuntu Package from the Official NGINX Repository
Download the key used to sign NGINX packages and the repository, and add it to the apt program’s key ring:
Edit the /etc/apt/sources.list file, for example with vi :
Add these lines sources.list to name the repositories from which the NGINX Open Source source can be obtained:
For example, to get the latest mainline package for Ubuntu 14.04 (“trusty”), add:
Save the changes and quit vi (press ESC and type wq at the : prompt).
Install NGINX Open Source:
Start NGINX Open Source:
Verify that NGINX Open Source is up and running:
Installing SUSE Packages
NGINX provides packages for SUSE Linux Enterprise Server:
Version | Supported Platforms |
---|---|
SLES 12 | x86_64 |
SLES 15 | x86_64 |
To download and install the latest stable version for SLES 12, run:
To download and install the latest mainline version for SLES 12, run:
Installing Prebuilt Alpine Linux Packages
NGINX provides packages for the following Alpine Linux operating systems:
Version | Supported Platforms |
---|---|
3.8 | x86_64 |
3.9 | x86_64 |
3.10 | x86_64 |
3.11 | x86_64 |
The package can be installed from the official repo at nginx.org. You have to set up the apt-get repository the first time, but after that the provided package is always up to date.
Installing a Prebuilt Alpine Linux Package from the Official NGINX Repository
Install the prerequisites:
To set up the apk repository for stable nginx packages, run the command:
For mainline nginx packages, run the following command instead:
Import an official nginx signing key so apk could verify the packages authenticity. Fetch the key:
Verify that the downloaded file contains the proper key:
The output should contain the following modulus:
Move the key to apk trusted keys storage:
To install nginx, run the command:
Compiling and Installing from Source
Compiling NGINX Open Source from source affords more flexibility than prebuilt packages: you can add particular modules (from NGINX or third parties), and apply latest security patches.
Installing NGINX Dependencies
Prior to compiling NGINX Open Source from source, you need to install libraries for its dependencies:
PCRE – Supports regular expressions. Required by the NGINX Core and Rewrite modules.
zlib – Supports header compression. Required by the NGINX Gzip module.
OpenSSL – Supports the HTTPS protocol. Required by the NGINX SSL module and others.
Downloading the Sources
Download the source files for both the stable and mainline versions from nginx.org.
To download and unpack the source for the latest mainline version, run:
To download and unpack source files for the latest stable version, run:
Configuring the Build Options
An example of options to the configure script (should be typed as a single line):
Configuring NGINX Paths
The configure script allows you to set paths to NGINX binary and configuration files, and to dependent libraries such as PCRE or SSL, in order to link them statically to the NGINX binary.
Including Modules Not Built by Default
Many NGINX modules are not built by default, and must be listed on the configure command line to be built.
The mail, stream, geoip, image_filter, perl and xslt modules can be compiled as dynamic. See Dynamic Modules for details.
An example of the configure command that includes nondefault modules (should be typed as a single line):
Enables asynchronous I/O.
Dynamically Linked Modules
NGINX modules can also be compiled as a shared object (*.so file) and then dynamically loaded into NGINX Open Source at runtime. This provides more flexibility, as the module can be loaded or unloaded at any time by adding or removing the associated load_module directive in the NGINX configuration file and reloading the configuration. Note that the module itself must support dynamic linking.
option on the configure command, where
is the path to the source code:
The resulting *.so files are written to the prefix/modules/ directory, where the prefix is a directory for server files such as /usr/local/nginx/.
To load a dynamic module, add the load_module directive to the NGINX configuration after installation:
Install
Binary ReleasesВ¶
Prebuilt Packages for Linux and BSDВ¶
Most Linux distributions and BSD variants have NGINX in the usual package repositories and they can be installed via whatever method is normally used to install software ( apt on Debian, emerge on Gentoo, ports on FreeBSD, etc).
Be aware that these packages are often somewhat out-of-date. If you want the latest features and bugfixes, it’s recommended to build from source or use packages directly from nginx.org.
Official Red Hat/CentOS packagesВ¶
To add NGINX yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste one of the configurations below:
Official Debian/Ubuntu packagesВ¶
The available NGINX Ubuntu release support is listed at this distribution page. For a mapping of Ubuntu versions to release names, please visit the Official Ubuntu Releases page.
e.g. Ubuntu 20.04 (Focal Fossa):
To install the packages, execute in your shell:
You have now nginx installed on your server but not ready to serve web pages. you have to start the nginx. You can do this by using this command:
Ubuntu PPAВ¶
This PPA is maintained by volunteers and is not distributed by nginx.org. It has some additional compiled-in modules and may be more fitting for your environment.
You can get the latest stable version of NGINX from the NGINX PPA on Launchpad: You will need to have root privileges to perform the following commands.
For Ubuntu 20.04 and newer:
Official Win32 BinariesВ¶
As of NGINX 0.8.50, NGINX is now available as an official Windows binary.
For problems look in c:nginxlogserror.log or in EventLog.
In addition, Kevin Worthington maintains earlier Windows builds of the development branch.
Source ReleasesВ¶
How To Install Nginx on Ubuntu 20.04
Introduction
Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is a lightweight choice that can be used as either a web server or reverse proxy.
In this guide, we’ll discuss how to install Nginx on your Ubuntu 20.04 server, adjust the firewall, manage the Nginx process, and set up server blocks for hosting more than one domain from a single server.
Prerequisites
Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. You can learn how to configure a regular user account by following our Initial server setup guide for Ubuntu 20.04.
You will also optionally want to have registered a domain name before completing the last steps of this tutorial. To learn more about setting up a domain name with DigitalOcean, please refer to our Introduction to DigitalOcean DNS.
When you have an account available, log in as your non-root user to begin.
Step 1 – Installing Nginx
Because Nginx is available in Ubuntu’s default repositories, it is possible to install it from these repositories using the apt packaging system.
Since this is our first interaction with the apt packaging system in this session, we will update our local package index so that we have access to the most recent package listings. Afterwards, we can install nginx :
After accepting the procedure, apt will install Nginx and any required dependencies to your server.
Step 2 – Adjusting the Firewall
Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw upon installation, making it straightforward to allow Nginx access.
List the application configurations that ufw knows how to work with by typing:
You should get a listing of the application profiles:
As demonstrated by the output, there are three profiles available for Nginx:
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Right now, we will only need to allow traffic on port 80.
You can enable this by typing:
You can verify the change by typing:
The output will indicated which HTTP traffic is allowed:
Step 3 – Checking your Web Server
At the end of the installation process, Ubuntu 20.04 starts Nginx. The web server should already be up and running.
We can check with the systemd init system to make sure the service is running by typing:
As confirmed by this out, the service has started successfully. However, the best way to test this is to actually request a page from Nginx.
You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address. If you do not know your server’s IP address, you can find it by using the icanhazip.com tool, which will give you your public IP address as received from another location on the internet:
When you have your server’s IP address, enter it into your browser’s address bar:
You should receive the default Nginx landing page:
If you are on this page, your server is running correctly and is ready to be managed.
Step 4 – Managing the Nginx Process
Now that you have your web server up and running, let’s review some basic management commands.
To stop your web server, type:
To start the web server when it is stopped, type:
To stop and then start the service again, type:
If you are only making configuration changes, Nginx can often reload without dropping connections. To do this, type:
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:
To re-enable the service to start up at boot, you can type:
You have now learned basic management commands and should be ready to configure the site to host more than one domain.
Step 5 – Setting Up Server Blocks (Recommended)
When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name.
The permissions of your web roots should be correct if you haven’t modified your umask value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:
Next, create a sample index.html page using nano or your favorite editor:
Inside, add the following sample HTML:
In order for Nginx to serve this content, it’s necessary to create a server block with the correct directives. Instead of modifying the default configuration file directly, let’s make a new one at /etc/nginx/sites-available/ your_domain :
Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:
Notice that we’ve updated the root configuration to our new directory, and the server_name to our domain name.
Next, let’s enable the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup:
Note: Nginx uses a common practice called symbolic links, or symlinks, to track which of your server blocks are enabled. Creating a symlink is like creating a shortcut on disk, so that you could later delete the shortcut from the sites-enabled directory while keeping the server block in sites-available if you wanted to enable it.
Two server blocks are now enabled and configured to respond to requests based on their listen and server_name directives (you can read more about how Nginx processes these directives here):
To avoid a possible hash bucket memory problem that can arise from adding additional server names, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. Open the file:
Note: Commenting out lines of code – usually by putting # at the start of a line – is another way of disabling them without needing to actually delete them. Many configuration files ship with multiple options commented out so that they can be enabled or disabled, by toggling them between active code and documentation.
Save and close the file when you are finished.
Next, test to make sure that there are no syntax errors in any of your Nginx files:
If there aren’t any problems, restart Nginx to enable your changes:
Step 6 – Getting Familiar with Important Nginx Files and Directories
Now that you know how to manage the Nginx service itself, you should take a few minutes to familiarize yourself with a few important directories and files.
Content
Server Configuration
Server Logs
Conclusion
Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.
If you’d like to build out a more complete application stack, check out the article How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 20.04.
In order to set up HTTPS for your domain name with a free SSL certificate using Let’s Encrypt, you should move on to How To Secure Nginx with Let’s Encrypt on Ubuntu 20.04.
Want to learn more? Join the DigitalOcean Community!
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Источники информации:
- http://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04
- http://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/
- http://www.nginx.com/resources/wiki/start/topics/tutorials/install/
- http://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04
- How to install msixbundle
- How to install node js on mac