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:
-
Install the EPEL repository if its not already present:
sudo yum install epel-release -
Install Nginx using
yum:sudo yum install nginxIf this is your first time installing a package from EPEL, you may be prompted to import the EPEL GPG key. Type
yand pressEnterto proceed. -
After the installation completes, enable Nginx to start automatically on boot and then start the service:
sudo systemctl enable nginx sudo systemctl start nginxThe
enablecommand creates a symbolic link that ensures Nginx starts when the system boots. -
Verify that Nginx is running:
sudo systemctl status nginxThe output should indicate that the service is active and running.
-
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 --reloadIf 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.
-
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.confextension. - Use a naming convention like
domain.com.confordomain.com.3000.confif 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.