How to Install https and Configure Nginx on Ubuntu 20.04 (ok) full
https://phoenixnap.com/kb/how-to-install-nginx-on-ubuntu-20-04
Last updated
https://phoenixnap.com/kb/how-to-install-nginx-on-ubuntu-20-04
Last updated
doc bai viet: https://techexpert.tips/mysql/phpmyadmin-installation-nginx/ ntroduction
Nginx is a free, open-source Linux application for web servers. It works as a reverse proxy server by directing web traffic to specific servers.
Nginx is used for security and load-balancing, but can also function independently as a web server.
This guide will help you install Nginx on Ubuntu 20.04 Linux (Focal Fossa).
Prerequisites
A system running Ubuntu 20.04 Linux
A user account with sudo or root privileges
Access to a terminal window / command line (click Activities > Search > type Terminal)
It is important to refresh the repository lists before installing new software. This helps make sure that the latest updates and patches are installed.
Open a terminal window and enter the following:
Allow the process to finish.
Nginx is included in the Ubuntu 20.04 default repositories. Install it by entering the following command:
Note: If the system generates an error about the lock file, please see How To Fix Could Not Get Lock /Var/Lib/Dpkg/Lock Error for suggestions.
Verify that Nginx installed correctly by checking the software version. Enter the following:
The system should display the software version of Nginx.
The behavior of Nginx can be adjusted. Use this to start or stop Nginx, or to enable or disable Nginx at boot.
Start by checking the status of the Nginx service:
If the status displays active (running)
, Nginx has already been started. Press CTRL
+z
to exit the status display.
To set Nginx to load when the system starts, enter the following:
To stop the Nginx service, enter the following:
To prevent Nginx from loading when the system boots:
To reload the Nginx service (used to apply configuration changes):
For a hard restart of Nginx:
Nginx needs access through the system’s firewall. To do this, Nginx installs a set of profiles for the Ubuntu default ufw (UnComplicated Firewall).
Start by displaying the available Nginx profiles:
The system should display the following:
Note: Other applications may be listed. They can be ignored.
To grant Nginx access through the default Ubuntu firewall, enter the following:
The system should display Rules updated
.
Refresh the firewall settings by entering:
For encrypted (https) traffic, enter:
To allow both, enter:
Note: It is recommended that you only allow the bare minimum required traffic through the firewall. For this process, only basic HTTP traffic is needed. Other configurations may require HTTPS (encrypted) or other traffic. If the system uses a different firewall, it should be configured to allow traffic on Port 80 (HTTP), Port 443 (HTTPS), or whatever ports are required by the network.
Make sure that the Nginx service is running, as in Step 4. Open a web browser, and navigate to the following web address:
The system should display the Nginx welcome page.
Note: If the system has a specific hostname or IP address, that may be used instead.
If the system does not have a graphical interface, the Nginx Welcome page can be loaded in the terminal using curl:
The system should display the HTML code for the Nginx Welcome page.
In Nginx, a server block is a configuration that works as its own server. By default, Nginx has one server block preconfigured.
It is located at /var/www/html. However, it can be configured with multiple server blocks for different sites.
Note: This tutorial uses test_domain.com for the domain name. This may be replaced with your own domain name.
In a terminal window, create a new directory by entering the following:
Use chmod
to configure ownership and permission rules:
Note: Read our article to learn how to use chmod recursively.
Open index.html for editing in a text editor of your choice (we will use the Nano text editor):
In the text editor, enter the following HTML code:
Press CTRL
+o
to write the changes, then CTRL
+x
to exit.
Open the configuration file for editing:
Enter the following code:
Create a symbolic link between the server block and the startup directory by entering the following:
Restart Nginx by running the following command:
The system should report that the configuration file syntax is OK, and that the configuration file test is successful.
If you’re using a test domain name that isn’t registered or public, the /etc/hosts file may need to be modified to display the test_domain.com page.
Display the system’s IP address with the following command:
Make a note of the IP address displayed.
Next, open /etc/hosts for editing:
In an empty space just below the localhost information, add the following line:
Replace 127.0.1.1
with the IP address displayed above. Press CTRL
+o
to save the changes, then CTRL
+x
to exit.
Open a browser window and navigate to test_domain.com (or the domain name you configured in Nginx).
You should see the message you entered in Part 3.
By default, Nginx stores different configuration and log files in the following locations:
/var/www/html – Website content as seen by visitors.
/etc/nginx – Location of the main Nginx application files.
/etc/nginx/nginx.conf – The main Nginx configuration file.
/etc/nginx/sites-available – List of all websites configured through Nginx.
/etc/nginx/sites-enabled – List of websites actively being served by Nginx.
/var/log/nginx/access.log – Access logs tracking every request to your server.
/var/log/ngins/error.log – A log of any errors generated in Nginx.
Conclusion
You should now have a working installation of Nginx on Ubuntu 20.04. As a bonus, you should now have an introduction to setting up an Nginx server block.
If you intend to use Nginx as a reverse proxy, see our article – How to Set Up Nginx as a Reverse Proxy.
If Nginx is not running, use the following command to launch the Nginx service: