How to install and configure Nginx on CentOS 7

nginx (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.

precondition

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:

  1. 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
  2. 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]:

    just write y and press Enter to complete.

  3. 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
  4. 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
  5. To check if Nginx is installed, go to http://alamat_ip_atau_domain In the browser of your choice, and you will see the default welcome page for Nginx as shown in the image below:
See also  Shellinabox enable SSL using Letsencrypt Certificate

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 /etc/nginx.
  • Nginx configuration file is /etc/nginx/nginx.conf.
  • 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 /etc/nginx/sites-enabled.
  • The server cluster is activated by creating a symbolic link (pointer) from the location configuration file in the directory /etc/nginx/conf.d for evidence sites-enabled.
  • To facilitate management, use a domain name for configuration names, such as domain beritabebas.com Then the configuration file can be named /etc/nginx/conf.d/beritabebas.com.conf
  • Guide /etc/nginx/snippets Contains 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 accessand files error Logs 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:
    • /home/<user_name>/<nama_situs>
    • /var/www/<nama_situs>
    • /var/www/html/<nama_situs>
    • /opt/<nama_situs>

conclusion

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.

See also  How to install LibModsecurity WAF app on Nginx CentOS 8

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.

Source link

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply