Run NginX with Apache on cPanel/WHM: Quick Guide [Closed]
Running Nginx alongside Apache on a server managed by cPanel/WHM can be a bit challenging but offers performance and flexibility benefits. Here’s a detailed guide on how to achieve this setup, leveraging the strengths of both web servers and the management capabilities of cPanel/WHM.
Understanding the Setup
- Apache: The default web server used by cPanel/WHM, known for its flexibility and extensive module support.
- Nginx: A high-performance web server and reverse proxy server, often used for handling static files and acting as a load balancer.
In this setup, Nginx typically acts as a reverse proxy in front of Apache, handling incoming requests and passing them to Apache for processing.
Prerequisites
- Root Access: You need root access to the server to install and configure both web servers.
- Backup: Always back up your server before making significant changes.
- cPanel/WHM: Ensure you have the latest version of cPanel/WHM.
Step-by-Step Guide
1. Install Nginx
cPanel doesn’t natively support Nginx as a reverse proxy, but you can use third-party plugins or manual installation.
a. Using a Plugin
One of the popular plugins for integrating Nginx with cPanel/WHM is Nginx Admin or Engintron.
Install Engintron (popular and well-maintained):
- SSH into your server as the root user.
- Execute the following commands:
cd /root
wget https://raw.githubusercontent.com/engintron/engintron/master/engintron.sh
bash engintron.sh install
This script will install Nginx and integrate it with cPanel/WHM.
b. Manual Installation
For manual installation, follow these steps:
- Add the EPEL repository (if not already present):
yum install epel-release
- Install Nginx:
yum install nginx
- Start Nginx and enable it to start at boot:
systemctl start nginx
systemctl enable nginx
2. Configure Nginx as a Reverse Proxy
After installing Nginx, configure it to act as a reverse proxy for Apache.
- Edit the Nginx configuration file:
nano /etc/nginx/nginx.conf
- Add or modify the following configuration to make Nginx proxy requests to Apache (usually running on port 8080 or 81 with Nginx in front):
http {
include mime.types;
default_type application/octet-stream;
# Include additional configuration files
include /etc/nginx/conf.d/*.conf;
# Reverse proxy settings
server {
listen 80;
server_name yourdomain.com; # Replace with your domain
location / {
proxy_pass http://127.0.0.1:8080; # Apache port
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Optional: Serve static files directly by Nginx
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
root /home/username/public_html; # Replace with your web root
expires max;
log_not_found off;
}
}
}
- Test Nginx configuration:
nginx -t
- Restart Nginx to apply the changes:
systemctl restart nginx
3. Adjust Apache to Work with Nginx
Ensure Apache listens on a different port since Nginx will be handling port 80 (HTTP) and possibly 443 (HTTPS).
- Edit the Apache configuration file:
nano /etc/httpd/conf/httpd.conf
- Change the listening port to 8080 or another unused port:
Listen 8080
- Restart Apache to apply the changes:
systemctl restart httpd
4. Configure cPanel/WHM to Work with Nginx
To make sure cPanel/WHM can manage sites and handle the new Nginx setup:
- Enable proxying support in cPanel/WHM:
- Log in to WHM as root.
- Navigate to
Home
>Service Configuration
>Apache Configuration
>Include Editor
. - Under
Pre VirtualHost Include
, add:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
- Restart Apache again to apply changes from WHM:
systemctl restart httpd
5. Configure SSL
Handling SSL with Nginx as the frontend requires configuring SSL certificates within Nginx.
- Install SSL certificates in Nginx:
- Obtain SSL certificates for your domain.
- Edit your Nginx configuration to include the SSL details:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/ssl/certs/your_certificate.crt;
ssl_certificate_key /etc/ssl/private/your_private_key.key;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
- Test Nginx configuration again:
nginx -t
- Restart Nginx:
systemctl restart nginx
6. Manage SSL Certificates with cPanel
If using cPanel’s AutoSSL or another method for managing SSL certificates:
- Ensure Nginx is configured to use the same certificate files that cPanel generates and renews.
- You might need to symlink or update Nginx configurations to point to the cPanel managed certificate locations.
7. Testing and Validation
- Check the website to ensure it loads correctly over HTTP and HTTPS.
- Use tools like SSL Labs to verify SSL configurations.
- Monitor logs for any errors or issues:
# Nginx logs
tail -f /var/log/nginx/error.log
# Apache logs
tail -f /var/log/httpd/error_log
Summary
Running Nginx with Apache on a server managed by cPanel/WHM can enhance performance and scalability. By configuring Nginx as a reverse proxy, you can leverage its speed and efficiency while still using Apache’s robust features and cPanel’s management tools. This setup requires careful configuration and testing to ensure all services work harmoniously.
Note: Using third-party plugins or making significant manual changes can sometimes affect cPanel/WHM updates and operations. Always keep a backup and be cautious with updates.