Installing and Setting Up Nginx on CentOS 7

Prerequisites

Log in to your CentOS 7 server using an account that has sudo privileges. Ensure that no other service (such as Apache) is occupying port 80 or 443, as this would prevent Nginx from starting.

Installing Nginx

Nginx is available from the EPEL (Extra Packages for Enterprise Linux) repository. Follow these steps to install it:

  1. Install the EPEL repository if its not already present:

    sudo yum install epel-release
    
  2. Install Nginx using yum:

    sudo yum install nginx
    

    If this is your first time installing a package from EPEL, you may be prompted to import the EPEL GPG key. Type y and press Enter to proceed.

  3. After the installation completes, enable Nginx to start automatically on boot and then start the service:

    sudo systemctl enable nginx
    sudo systemctl start nginx
    

    The enable command creates a symbolic link that ensures Nginx starts when the system boots.

  4. Verify that Nginx is running:

    sudo systemctl status nginx
    

    The output should indicate that the service is active and running.

  5. If your server uses a firewall, allow HTTP (port 80) and HTTPS (port 443) traffic:

    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    

    If your server is hosted on a platform like Alibaba Cloud or Tencent Cloud, you may also need to open these ports in your security group settings.

  6. Test the installation by opening a web browser and navigating to http://YOUR_SERVER_IP. You should see the default Nginx welcome page.

Managing Nginx with systemctl

You can control Nginx using systemctl commands:

  • Start Nginx: sudo systemctl start nginx
  • Stop Nginx: sudo systemctl stop nginx
  • Restart Nginx: sudo systemctl restart nginx
  • Reload configuration changes: sudo systemctl reload nginx
  • Enable auto-start at boot: sudo systemctl enable nginx
  • Disable auto-start: sudo systemctl disable nginx

Configuration Files and Best Practices

  • All Nginx configuration files are stored in /etc/nginx/. The main configuration file is /etc/nginx/nginx.conf.
  • To keep configurations manageable, create a separate configuration file for each service or domain. Place these files in /etc/nginx/conf.d/ and ensure they have a .conf extension.
  • Use a naming convention like domain.com.conf or domain.com.3000.conf if the service runs on a custom port.
  • For reusable configuration blocks, create a directory /etc/nginx/snippets/ and store common code there. Include these snippets in your server blocks as needed.
  • Nginx logs are located at /var/log/nginx/. Configure separate access and error logs for each service to simplify debugging.
  • Store your website files in a directory of your choice. Recommended locations include:
    • /home/<user>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>
    • /usr/share/nginx/html

Next Steps

With Nginx installed, you can now deploy your applications. To host multiple domains on a single server, consider configuring Nginx server blocks. For secure connections, set up HTTPS using a free SSL certificate from Let's Encrypt.

Tags: nginx CentOS 7 Web Server installation System Administration

Posted on Fri, 31 Jul 2026 16:43:08 +0000 by dougmcc1