Installing NGINX on CentOS 8
- Ubuntu 20.04
- Ubuntu 18.04
- Debian 9
- Debian 10
- AlmaLinux 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.
What is NGINX?
NGINX is an open source web server with powerful load balancing, reverse proxy, and caching features. It was initially designed to solve scaling and concurrency problems with existing web servers. Its event-based, asynchronous architecture has made it one of the most popular and best-performing web servers available today.
Before You Begin
Set up your Linode in the Creating a Compute Instance and Setting Up and Securing a Compute Instance guide.
If you want a custom domain name for your site, you can set this up using our DNS Manager guide.
- Don’t forget to update your
/etc/hosts
file with your Linode’s public IP address and your site’s fully qualified domain name as explained in the Update Your System’s hosts File section of the Setting Up and Securing a Compute Instance guide.
- Don’t forget to update your
Install the SELinux core policy Python utilities. This will give you the ability to manage SELinux settings in a fine-grained way.
sudo dnf install -y policycoreutils-python-utils
Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo
. If you’re not familiar with thesudo
command, visit our Users and Groups guide.All configuration files should be edited with elevated privileges. Remember to include
sudo
before running your text editor.
Install NGINX
Currently, the best way to install NGINX on CentOS 8 is to use the version included in CentOS’s repositories:
sudo dnf clean all
sudo dnf update
sudo dnf install nginx
Add a Basic Site
Create a new directory for your site. Replace
example.com
with your site’s domain name.sudo mkdir -p /var/www/example.com
Use SELinux’s
chcon
command to change the file security context for 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 sudo ls -dZ /var/www/example.com
You can add your site’s files in your
/var/www/example.com
directory. Create an index file with a simple “Hello World” example. Using the text editor of your choice, create a new file,/var/www/example.com/index.html
. Replaceexample.com
with your website’s domain name or your Linode’s public IP address.- File: /var/www/example.com/index.html
1 2 3 4 5 6 7 8 9 10 11
<!DOCTYPE html> <html> <head> <title>My Basic Website</title> </head> <body> <header> <h1>Hello World!</h1> </header> </body> </html>
Configure NGINX
NGINX site-specific configuration files are kept in /etc/nginx/sites-available
and symlinked to /etc/nginx/sites-enabled/
. Generally, you will create a new file containing a
server block in the sites-available
directory for each domain or subdomain you will be hosting. Then, you will set up a symlink to your files in the sites-enabled
directory.
Create the directories for your configuration files:
sudo mkdir -p /etc/nginx/{sites-available,sites-enabled}
Create your site’s configuration file in the text editor of your choice. Replace
example.com
in theserver_name
directive with your site’s domain name or IP address and/var/www/example.com
in theroot
directive with your own root directory’s location.- File: /etc/nginx/sites-available/example.com
1 2 3 4 5 6 7 8 9 10 11 12
server { listen 80; listen [::]:80; server_name example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri/ =404; } }
Set up a new symlink to the
/etc/nginx/sites-enabled/
directory to enable your configuration:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Update the NGINX configuration file,
/etc/nginx/nginx.conf
, to add aninclude
directive to the/etc/nginx/sites-enabled/*
directory. Thisinclude
must be within your configuration files’http
block. Place theinclude
directive below theinclude /etc/nginx/conf.d/*.conf;
line.- File: /etc/nginx/nginx.conf
1 2 3 4 5 6 7
... http { ... include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; ... }
Open the firewall for traffic:
sudo firewall-cmd --zone=public --permanent --add-service={http,https} sudo firewall-cmd --list-all sudo firewall-cmd --reload
Test and Enable NGINX
You can test your NGINX configuration with this command:
sudo nginx -t
Start the service with the following commands:
sudo systemctl enable nginx sudo systemctl start nginx
Verify that it’s running:
sudo systemctl status nginx
Navigate to your Linode’s domain name or IP address in a browser. You should see your simple page displayed.
Advanced Configuration
For more advanced configuration options, including security and performance optimizations and TLS setup, see our four-part series on NGINX:
Changes to your NGINX configurations may require updates to your SELinux policies and contexts. For an introduction to SELinux, see our Getting Started with SELinux guide.
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