Nginx Specific-Version & Latest Install Guide: Ubuntu / CentOS, Fully Organized
1. What is Nginx?
Nginx is a lightweight, high-performance open-source web server, currently the most widely used in the world. Beyond a simple web-server role, it performs a core role in solving complex architectural problems such as a reverse proxy, a load balancer, and a mail proxy.
In this post, I'll organize the install methods for Ubuntu (Debian-based) — the most used in cloud-server (AWS EC2, etc.) environments — and CentOS/RedHat (RPM-based), which is preferred in enterprises.
💡 Note
This guide assumes an environment where the server is connected to the internet (the public network). (using the package manager)
In a closed-network (Private Subnet) environment, a separate manual install via binary files is needed.
2. Installing Nginx on Ubuntu (Debian-based)
Package manager: apt
sudo apt update
sudo apt install nginx
* In practice, cases arise where you have to install a specific version.
apt-cache madison nginx # query the nginx versions installable from the current repository
sudo apt install nginx=1.24.0-2ubuntu7.5 # install a specific version of nginx
Service management:
sudo systemctl start nginx
sudo systemctl enable nginx # auto-run on boot
2.1 Detailed explanation of the procedure
Figure 1. (left) apt package update / (right) nginx install
2.1.1 Running sudo apt update / sudo apt install nginx
First, update the apt repository (especially to reflect the latest version, it's good to update the repository).
The left image in Figure 1 is the result of apt update, and the right image shows installing nginx through the apt repository. A (Y/n) will appear in the middle; enter 'Y' and continue.
Figure 2. result of installing nginx via apt
When you install nginx via apt, it automatically registers it as a linux service, so you can directly check the status or turn it off and on through systemctl (the overall manager of linux services (daemons)).
Figure 3. checking the nginx status via systemctl
You can check the nginx status via sudo systemctl status nginx. When you install nginx via apt, it runs with the default config.
2.2 Understanding the Nginx configuration file structure and paths
Figure 4. nginx config file paths
When you install Nginx with the package manager, all config files are located by default under the /etc/nginx/ path. Nginx's configuration approach is structured so that a 'central control room' loads 'detailed instruction sheets.'
2.2.1 The central config file: nginx.conf
This is Nginx's topmost config file. It determines how the whole server operates, and inside it there are statements that include config files at other paths, as below.
include /etc/nginx/modules-enabled/*.conf; # include module configs
include /etc/nginx/conf.d/*.conf; # include user-defined configs
include /etc/nginx/sites-enabled/*; # include individual site configs
The reason for managing files separately like this is for maintenance convenience. Instead of cramming all settings into one file, you split files by service, manage them, and then load them with include.
2.2.2. Site config: sites-available vs sites-enabled
These are the two most core directories in the Nginx config structure.
- sites-available: where the config files of actual individual services (ports, routing, etc.) are kept. (the config-original repository)
- sites-enabled: Nginx actually reads and runs only the config files linked in this directory. (the activated configs)
Usually a default file exists in sites-available, and inside it are settings like port (Listen), domain (Server Name), routing (Location), etc.
2.2.3. Summary: the flow of configuration
- nginx.conf: sets the overall framework and oversees the detailed configs.
- the detailed .conf files: define the actual service's port numbers, addresses, etc., and are included by nginx.conf to start up.
3. Installing on CentOS / RedHat (RPM-based)
CentOS and RedHat-based (RHEL, Rocky Linux, etc.) use the yum or dnf package manager. The install process is very similar to Ubuntu, but there's one thing to watch out for.
3.1 Enabling the EPEL repository
In many cases, CentOS 7 and above's default repository doesn't include Nginx. Therefore you have to first install EPEL (Extra Packages for Enterprise Linux), an additional package repository for enterprise Linux.
- Package manager: yum or dnf
- Caveat: on CentOS 7 and above, you often have to install the epel-release repository first.
sudo yum install epel-release -y
sudo yum install nginx -y
Service management: (same as Ubuntu)
sudo systemctl start nginx
sudo systemctl enable nginx
The detailed configuration and service management after install are the same as the Ubuntu environment explained earlier, so please refer to that content.
4. Firewall setup
If you've finished installing Nginx but the browser shows 'can't connect to the site,' it's most likely a firewall problem. You have to check both the server's own firewall and the AWS Security Group.
4.1 ① Linux OS firewall setup
Depending on the OS you're using, open ports 80 (HTTP) and 443 (HTTPS) with the commands below.
Ubuntu (UFW):
sudo ufw allow 'Nginx Full'
CentOS (Firewalld):
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
4.2 ② AWS Security Group setup (when using AWS)
If you're using a cloud environment (AWS), the Linux internal settings alone aren't enough. In the AWS Console's instance settings, check the Inbound Rules.
- HTTP (port 80): Source 0.0.0.0/0 (allow all)
- HTTPS (port 443): Source 0.0.0.0/0 (allow all)
5. Verifying the install result
Once all settings are done, enter the server's public IP address in the web browser's address bar and try to connect.
Figure 5. Nginx connection complete
If a page with the text "Welcome to nginx!" appears as above, the install and network setup are perfectly complete.
📦 Migrated from the Tistory blog I used to run. Original: taehyuklee.tistory.com/30
Comments