Installing and Using NGINX on AlmaLinux 8
- Ubuntu 20.04
- Ubuntu 18.04
- Debian 9
- Debian 10
- CentOS 8
- Deprecated guides:
- Ubuntu 12.04
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
NGINX (pronounced “engine-X”) is an open-source web server that excels at load balancing, caching, and acting as a reverse proxy. NGINX was created to be a high-performing and scalable alternative to other popular web servers, like Apache. Its event-driven architecture continues to set it apart as one of the fastest and lightest web servers available. This guide shows you how to install NGINX on your AlmaLinux 8 server and how to get started using it.
Before You Begin
Familiarize yourself with our Getting Started with Linode guide and complete the steps for setting your Linode’s hostname and timezone.
This guide uses
sudo
wherever possible. Complete the sections of our How to Secure Your Server guide to create a standard user account, harden SSH access, and remove unnecessary network services.Update your system.
sudo yum update
NoteThis guide is written for non-root users. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, see the Users and Groups guide.
Install NGINX
Install NGINX from the package manager.
sudo yum install nginx
Enable and start the NGINX service.
sudo systemctl enable nginx sudo systemctl start nginx
Open port 80 — the HTTP port — on your system’s firewall. FirewallD is the front end typically used to manage firewall rules on AlmaLinux. You can use the following commands to open port 80 and reload the rules so they take effect.
sudo firewall-cmd --zone=public --add-service=http sudo firewall-cmd --zone=public --add-service=http --permanent sudo firewall-cmd --reload
Refer to our Introduction to FirewallD on CentOS for more on how to use FirewallD for managing your server’s firewall.
Visit the default NGINX page to see your installation in action. You can find it by navigating to your server’s domain name or its IP address.
For example, if your domain name is
example.com
, navigate tohttp://example.com
; if your IP address is192.0.2.0
, you can instead navigate tohttp://192.0.2.0
.
Manage NGINX
The NGINX service runs on systemd
, which means you can manage it using systemctl
commands.
View the current status of the NGINX service using the following command:
sudo systemctl status nginx
Stop the NGINX service using the following command:
sudo systemctl stop nginx
You can then restart the NGINX service using the following command:
sudo systemctl start nginx
To disable the NGINX service, preventing it from beginning automatically at system startup, use:
sudo systemctl disable nginx
You can enable the NGINX service again using:
sudo systemctl enable nginx
Restart the NGINX service using the following command:
sudo systemctl restart nginx
To reload NGINX’s configuration files, use the following command:
sudo systemctl reload nginx
Using NGINX
This section walks you through setting up your own website using NGINX. It also illustrates how to set up an NGINX proxy to serve static content.
NGINX Configuration
Open the main NGINX configuration file —
/etc/nginx/nginx.conf
— and disable the default website by commenting out theroot
line.- File: /etc/nginx/nginx.conf
1 2 3 4 5 6 7 8 9 10
# [...] server { listen 80 default_server; listen [::]:80 default_server; server_name _; # root /usr/share/nginx/html; # [...]
Create an NGINX configuration file for your site.
In this example, replace
example.com
with your site’s domain, in both the filename and the file’s contents. Do the same wherever you seeexample.com
throughout this guide.- File: /etc/nginx/conf.d/example.com.conf
1 2 3 4 5 6 7 8 9 10 11 12 13
server { listen 80; listen [::]:80; server_name example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri/ =404; } }
This configuration creates a new NGINX server. That server listens for requests on port 80 for the domain name
example.com
. It then defines the server’s root directory and index file name. The root directory is where NGINX maps requests to files, and the index file name is the name of the file NGINX serves for a request to the root directory.So, for a request to
example.com/
, NGINX attempts to locate anindex.html
file in the/var/www/example.com
directory, and serves the file if it finds it there.
Run NGINX’s configuration test to verify your configuration files.
sudo nginx -t
Restart NGINX for the changes to take effect.
sudo systemctl restart nginx
Launch the Site
Create a directory for your NGINX site’s content.
sudo mkdir -p /var/www/example.com
Create an
index.html
page in the new NGINX site directory.- File: /var/www/example.com/index.html
1 2 3 4 5 6 7 8
<!doctype html> <html> <body> <h1>Hello, World!</h1> <p>This is an example website running on NGINX.</p> </body> </html>
Give the NGINX user ownership of the directory for your website, and then add executable permissions to its parent directory.
sudo chown -R nginx:nginx /var/www/example.com sudo chmod -R +x /var/www
Use the
chcon
command to change the SELinux security context for the website directory to allow web content.sudo chcon -t httpd_sys_content_t /var/www/example.com -R sudo chcon -t httpd_sys_rw_content_t /var/www/example.com -R
In a browser, visit the domain you set up for your website —
example.com
above.You should see your website’s “Hello, World!” page.
Conclusion
To learn more about NGINX’s features and capabilities, check out our A Comparison of the NGINX and Apache Web Servers guide.
For more advanced configuration options, including security and performance optimizations and TLS setup, see our four-part Getting Started series on NGINX.
- Part 1: Installation and Basic Setup
- Part 2: (Slightly More) Advanced Configurations
- Part 3: Enable TLS for HTTPS Connections
- Part 4: TLS Deployment Best Practices
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on