/

How to Set Up Nginx on Your WHM/cPanel Server

This guide will show you how to set up Nginx on a server running WHM/cPanel. You can configure Nginx in two main ways. You can use it as a standalone web server, which replaces Apache. Or, you can use it as a reverse proxy in front of Apache, which often improves performance.

temanweb f44f4

Important Note: Before you start, it is a very good idea to create a full backup of your server. Changing your web server can affect all the websites you host.

Part 1: Setting Up Nginx as a Standalone Web Server (Replacing Apache)

In this setup, Nginx handles all web requests directly. Apache will be shut down and will not be used. This is a good option if you want to use only Nginx.

  1. Access and Navigate in WHM
    1. Log in to your WHM (WebHost Manager) account.
    2. Use the search bar in the top left to find the “EasyApache 4” tool and open it.
  2. Install the Nginx RPM Package
    1. Inside EasyApache 4, look for the “Customize” column and click the “Currently Installed Packages” button.
    2. On the next page, click the “Package Manager” tab.
    3. In the search box, type “nginx”.
    4. Find the “nginx” package in the list and toggle its switch to the “On” position.
    5. Click the “Review” button at the bottom of the page.
    6. Finally, click “Provision” to start the installation. Wait for the process to finish.
  3. Enable and Start the Nginx Service
    1. Go back to the main WHM screen.
    2. Search for and open “Service Manager”.
    3. In the Service Manager list, find “nginx”.
    4. Check the box for “nginx” to enable it.
    5. Click the “Start” button to run the Nginx service.
  4. Stop the Apache Service
    1. In the same “Service Manager” window, find the “httpd” service. This is Apache.
    2. Click the “Stop” button to stop Apache.
    3. It is also safe to uncheck the box to prevent it from starting automatically on a server reboot. Nginx is now your primary web server.
  5. Configure cPanel to Use Nginx
    1. In WHM, search for and open “Manage Feature Plugins”.
    2. Find the “Nginx Manager” plugin and enable it by checking the box.
    3. Now, search for “Nginx Manager” in WHM and open it.
    4. You will see different modes. Select the “Standalone” mode.
    5. Click “Save” to apply the configuration.

Your server is now running Nginx as its sole web server. All your cPanel accounts and websites will be served by Nginx.

Part 2: Setting Up Nginx as a Reverse Proxy for Apache

This is a very popular setup. Nginx sits in front of Apache and handles certain tasks faster. It serves static files (like images and CSS) directly, while passing requests for PHP scripts to Apache. This reduces the load on Apache and can make your websites much faster.

  1. Install Nginx
    Follow the exact same steps from Part 1 to install the Nginx RPM package using EasyApache 4 and start the service in the Service Manager.
  2. Configure the Reverse Proxy in WHM
    1. Ensure the “Nginx Manager” feature plugin is enabled (as done in Part 1, Step 5).
    2. Open the “Nginx Manager” in WHM.
    3. This time, select the “Reverse Proxy” mode.
    4. You will see several settings. Here are the common ones to configure:
      • Proxy Mode: Enable Reverse Proxy.
      • Caching: You can enable caching for even better performance.
      • Serve Stale Content: If enabled, this serves a cached version of a page if the backend Apache is busy or down.
    5. Click “Save” to apply the new configuration.
  3. Adjust Service Startup (Recommended)
    1. Go to “Service Manager” in WHM.
    2. Ensure both “httpd” (Apache) and “nginx” are running and enabled to start on boot.
    3. The correct order is for Nginx to start first, then Apache. The system manager (systemd) usually handles this correctly.

In this setup, visitors connect to Nginx. Nginx then works as a proxy:

  • If a request is for a static file, Nginx serves it directly and very quickly.
  • If a request is for a dynamic page (like a PHP script), Nginx passes it to Apache in the background.
  • Apache processes the PHP and generates the page, then sends it back to Nginx.
  • Nginx finally delivers the page to the visitor.

Final Steps and Verification

After completing either setup, you must test your work.

  1. Check Your Website
    Open your website in a web browser. It should load normally. You can also open it in a private/incognito window to avoid seeing a cached version.
  2. Verify the Server Header Using Command Line
    To confirm Nginx is working, you can check the HTTP headers of your site using command line tools. This method is reliable and doesn’t require external websites. Using curl command:
    1. Open your terminal or SSH connection to your server or any Linux/macOS computer.
    2. Run this curl command: curl -I http://yourdomain.com Replace yourdomain.com with your actual domain name.
    3. Analyze the output. Look for the “Server” header in the results: Example output for Nginx Standalone: HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Example output for Nginx Reverse Proxy:
      You might see one of these patterns: Pattern 1: Nginx header visible HTTP/1.1 200 OK Server: nginx Pattern 2: Apache header visible (but Nginx is still working) HTTP/1.1 200 OK Server: Apache
    What the results mean:
    • If you see Server: nginx, Nginx is definitely active and serving your requests.
    • If you see Server: Apache while in Reverse Proxy mode, this is normal. The request is still going through Nginx first, but Apache is adding its own header. The performance benefits are still working.
    • To be absolutely sure Nginx is involved, you can also look for other Nginx-specific headers that might be configured, like X-Powered-By: NGINX or X-Proxy: nginx.
    Alternative method for more detailed headers: For even more detailed information, use this command: curl -L -I -v http://yourdomain.com This will show you all the redirects and very verbose header information, which can help you trace the entire request path through Nginx.

By following these steps, you can successfully integrate the speed and efficiency of Nginx with your WHM/cPanel server, either as a powerful replacement for Apache or as a performance boosting partner for it.