Installing NGINX on CentOS 8
Updated by Linode Written by Linode
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 Getting Started and Securing your Server guides.
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/hostsfile 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 Getting Started 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 yum install -y policycoreutils-python-utilsNote
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo. If you’re not familiar with thesudocommand, visit our Users and Groups guide.All configuration files should be edited with elevated privileges. Remember to include
sudobefore 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 yum update
sudo dnf install @nginx
Add a Basic Site
Create a new directory for your site. Replace
example.comwith your site’s domain name.sudo mkdir -p /var/www/example.comUse SELinux’s
chconcommand 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 -RYou can add your site’s files in your
/var/www/example.comdirectory. 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.comwith your website’s domain name or your Linode’s public IP address.- /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.comin theserver_namedirective with your site’s domain name or IP address and/var/www/example.comin therootdirective with your own root directory’s location.- /etc/nginx/sites-available/example.com
-
1 2 3 4 5 6 7 8 9 10 11 12server { 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 anincludedirective to the/etc/nginx/sites-enabled/*directory. Thisincludemust be within your configuration files’httpblock. Place theincludedirective below theinclude /etc/nginx/conf.d/*.conf;line.- /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 sudo firewall-cmd --zone=public --permanent --add-service=https sudo firewall-cmd --reload
Test and Enable NGINX
You can test your NGINX configuration with this command:
sudo nginx -tStart the service with the following commands:
sudo systemctl enable nginx sudo systemctl start nginxVerify that it’s running:
sudo systemctl status nginxNavigate 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.
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.