engine x) is one of the most popular web servers in the world and is responsible for hosting some of the largest and most popular websites on the Internet.
Nginx is known to be more resource friendly than Apache in some cases, and can be used as a web server or reverse proxy.
Compared to Apache, Nginx can handle a large number of concurrent connections and has a smaller memory footprint for each connection.
This tutorial will teach you how to install and manage Nginx on your CentOS 7 machine.
Before starting the tutorial, make sure that you are logged in as a user with sudo privileges, and that you do not have Apache or other services running on port 80 or 443.
Install Nginx on CentOS
Follow the steps below to install Nginx on your CentOS server:
The Nginx package is available in the EPEL repository. If you don’t have the EPEL repository installed, you can do so by typing:
- The Nginx package is available in the EPEL repository. If you don’t have the EPEL repository installed, you can do so by typing:
sudo yum install epel-release
- Install Nginx by typing the following yum command:
sudo yum install nginx
If this is the first time you are installing a package from the EPEL repository, yum may prompt you to import the EPEL GPG key:
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 Importing GPG key 0x352C64E5: Userid : "Fedora EPEL (7) <[email protected]>" Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5 Package : epel-release-7-9.noarch (@extras) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 Is this ok [y/N]:
- Once the installation is complete, enable and start the Nginx service with:
sudo systemctl enable nginx sudo systemctl start nginx
Check the status of the Nginx service with the following command:
sudo systemctl status nginx
The output will look like this:
● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2018-03-12 1648 UTC; 2s ago Process: 1677 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 1675 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 1673 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 1680 (nginx) CGroup: /system.slice/nginx.service ├─1680 nginx: master process /usr/sbin/nginx └─1681 nginx: worker process
- If your server is protected by a firewall, you must open the HTTP port (
80) and HTTPS (
443). Use the following command to open the desired port:
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
- To check if Nginx is installed, go to
http://alamat_ip_atau_domainIn the browser of your choice, and you will see the default welcome page for Nginx as shown in the image below:
Nginx service management with systemctl
You can use commands
systemctl To manage Nginx services, just like any other systemd module.
To stop the Nginx service, type:
sudo systemctl stop nginx
To start/start the Nginx service, type:
sudo systemctl start nginx
To restart/reload Nginx type:
sudo systemctl restart nginx
Reload the Nginx service after making some configuration changes:
sudo systemctl reload nginx
Disable Nginx to auto-start on boot:
sudo systemctl disable nginx
To enable it again:
sudo systemctl enable nginx
The basic structure of the Nginx configuration file
- Nginx configuration files are stored in the directory
- Nginx configuration file is
- The server cluster (vhost) configuration files are located in the directory
/etc/nginx/conf.d. The configuration files in this directory are not used by Nginx unless they are linked in the directory
- The server cluster is activated by creating a symbolic link (pointer) from the location configuration file in the directory
- To facilitate management, use a domain name for configuration names, such as domain
beritabebas.comThen the configuration file can be named
/etc/nginx/snippetsContains configuration fragments that can be included in a server block file. If you use recursive configuration segments, you can split the segment into chunks and include the clip file in the server block.
- The nginx log files (access.log and error.log) are located in the directory
/var/log/nginx/. It is recommended to distinguish between
errorLogs for each server cluster.
- You can map the root directory of your domain document to any location you like. The most popular sites for webroot include:
At this point, you have successfully installed Nginx on your CentOS 7 server. You are now ready to start deploying your applications and using Nginx as a web server, proxy server, or reverse proxy for Apache.
If you want to host multiple domains on your CentOS server, you can use server clusters, similar to virtual hosts on Apache.
Secure certificates are a “must have” feature for all websites today. To secure your website with Let Encrypt’s free SSL certificate, you can check out our tutorial on how to secure Nginx with Let’s Encrypt on CentOS 7.