Server Ubuntu 22.04 64-bit installation Nginx

1. Compared to Apache, Nginx can handle a large number of concurrent connections and has a smaller memory footprint per connection. This tutorial describes how to install and manage Nginx on Ubuntu 20.04.

Nginx, pronounced "engine x," is an open source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. It works as a standalone web server, load balancer, content cache, and reverse proxy for HTTP and non-HTTP servers.

2. Install Nginx

Nginx is available in the default Ubuntu repositories. To install it, run the following command:

sudo apt update
sudo apt install nginx

After the installation is complete, the Nginx service will start automatically. You can verify this by running the following command:

sudo systemctl status nginx

The output will look like this:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-02 20:25:43 UTC; 13s ago
...

Nginx is installed on your Ubuntu computer. You can manage Nginx services in the same way as any other systemd unit.

3. Configure firewall

Now that you have Nginx installed and running on your server, you now need to ensure that your firewall is configured to allow communication on HTTP (80) and HTTPS (443) ports. Assuming you are using UFW, you can do this by enabling the "Nginx Full" profile, which includes rules for both ports:

sudo ufw allow 'Nginx Full'

To verify the status type:

sudo ufw status

The output will be similar to the following:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Test installation

To test a new Nginx installation, open http://YOUR_IP in a browser of choice and you should see the default Nginx login page, as shown below:

Insert image description here

4.Nginx configuration file structure and best practices

All Nginx configuration files are located in the /etc/nginx directory.
The main configuration file of Nginx is /etc/nginx/nginx.conf.
To make Nginx configuration easier to maintain, it is recommended to create a separate configuration file for each domain. You can have as many server blocking files as you want.
Nginx server blocks files from being stored in the /etc/nginx/sites-available directory. Nginx will not use configuration files found in this directory unless they are linked to the /etc/nginx/sites-enabled directory.
To activate the server block, you need to create a symbolic link (pointer) from the configuration file site in the sites-available directory to the directory sites-enabled.
It is recommended to follow standard naming conventions. For example, if your domain name is, mydomain.com then your configuration file should be named /etc/nginx/sites-available/mydomain.com.conf
The /etc/nginx/snippets directory contains the Configuration snippet. If you use repeatable configuration segments, you can refactor the segments into fragments and include the fragment files into the server block.
Nginx log files (access.log and error.log) are located in the /var/log/nginx directory. It is recommended to have different access and error log files for each server module.
You can set the root of your domain document to any location you want. The most common locations for webroot include:

/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>
Summarize

We have shown you how to install Nginx on Ubuntu 20.04. Now you can start deploying your application and use Nginx as a web or proxy server.

If you have any questions or feedback, please feel free to leave a comment.

Guess you like

Origin blog.csdn.net/weixin_45500785/article/details/129403525
Recommended