SSH & Terminal Access

Switching Between Apache and Nginx Using Terminal and cPanel/WHM in a VPS

By the Domain India teamPublished 9 min read
Knowledge base article
Contents (10 sections)

Apache and nginx are the two most common web servers on Linux. You can run either one on its own, or put nginx in front of Apache so each does what it is best at. This guide shows how to switch between them safely on a server where you have root access, from the terminal and on a cPanel & WHM server, and explains what changes for your websites when you do.

Key takeaways

Switching web servers is only possible on a server you control, such as your own VPS. Don't uninstall the old server first: stop and disable it, install and test the new one, and keep the old one ready to roll back. nginx does not read .htaccess files and runs PHP through PHP-FPM, so rewrite rules must be translated. On a cPanel & WHM server you do not replace Apache; you add nginx in front of it with cPanel's own NGINX Manager. Domain India shared hosting already runs Apache, with nginx in front on cPanel, and you can't change that.

1. Who this guide is for

You haveCan you switch web servers?What to do
Domain India shared hosting (cPanel, DirectAdmin, Webuzo)No, the server is shared and managed by usNothing: sites run on Apache, with nginx in front on cPanel, and .htaccess rules work
A Domain India VPS with no control panelYes, as rootSections 3 to 6
A cPanel & WHM server you run with your own licenceOnly by adding nginx in front of ApacheSection 7

Domain India VPS plans are self-managed and don't include cPanel, so the WHM part of this guide applies only to a cPanel server you licence and run yourself.

2. Apache and nginx: what actually changes

PointApachenginx
Per-directory configReads .htaccess files in each folderIgnores .htaccess; all rules live in the server config
PHPPHP-FPM through mod_proxy_fcgi (recommended)PHP-FPM through fastcgi_pass
Static files under loadGoodVery efficient, low memory
Best fitApps that rely on .htaccess, such as many PHP CMSsStatic sites, reverse proxy for Node.js, Python or Go apps

The biggest job in any switch is the .htaccess rules. WordPress needs only one line in nginx (try_files $uri $uri/ /index.php?$args;), but custom redirects, access rules and caching headers must each be rewritten by hand.

A third option is often the best: nginx in front of Apache. nginx takes the connections and serves static files, Apache behind it keeps reading .htaccess. Section 6 shows how.

3. Before you switch

  1. Back up.
    Copy /etc/httpd or /etc/apache2, your web roots and your databases. If your VPS plan includes snapshots, take one now.
  2. List your sites.
    Note every virtual host, its document root, its domains and its SSL certificate.
  3. Collect the .htaccess rules.
    Run find /var/www -name .htaccess and decide how each rule will look in nginx.
  4. Pick a quiet time.
    Sites are down for the seconds between stopping one server and starting the other.

Both servers want ports 80 and 443. Only one can hold them at a time, which is why the old server must be stopped before the new one starts.

4. Switch from Apache to nginx (terminal)

The commands below are for Ubuntu or Debian, and for AlmaLinux or Rocky Linux. CentOS has reached end of life; use AlmaLinux or Rocky Linux for new servers.

Ubuntu or Debian:

bash
sudo apt update
sudo systemctl disable --now apache2
sudo apt install nginx php-fpm
sudo nginx -t && sudo systemctl enable --now nginx

AlmaLinux or Rocky Linux:

bash
sudo dnf install nginx php-fpm
sudo systemctl disable --now httpd
sudo nginx -t && sudo systemctl enable --now nginx php-fpm

On Ubuntu, stop Apache before installing nginx, because the package tries to start nginx on port 80 straight away. On AlmaLinux and Rocky, edit /etc/php-fpm.d/www.conf and set user = nginx and group = nginx, then restart php-fpm, so PHP can read files nginx owns.

A basic server block for a PHP site, in /etc/nginx/conf.d/example.conf (Ubuntu also uses sites-available):

nginx
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

The PHP-FPM socket path depends on your system: on Ubuntu run ls /run/php/ to find it; on AlmaLinux and Rocky it is usually /run/php-fpm/www.sock. Then reissue SSL with Certbot's nginx plugin (sudo certbot --nginx -d example.com -d www.example.com).

Stop and disable, don't uninstall

A disabled Apache costs nothing, and if nginx misbehaves you can be back in seconds with systemctl disable --now nginx and systemctl enable --now apache2 (or httpd). Uninstall the old server only after a week or two of running cleanly.

5. Switch back from nginx to Apache

Ubuntu or Debian:

bash
sudo systemctl disable --now nginx
sudo apt install apache2
sudo a2enmod proxy_fcgi setenvif rewrite
sudo a2enconf php8.3-fpm
sudo apachectl configtest && sudo systemctl enable --now apache2

Replace php8.3-fpm with the PHP-FPM version installed on your server.

AlmaLinux or Rocky Linux:

bash
sudo dnf install httpd
sudo systemctl disable --now nginx
sudo apachectl configtest && sudo systemctl enable --now httpd php-fpm

On AlmaLinux and Rocky, php-fpm installs an Apache config that sends .php files to PHP-FPM automatically. Set the php-fpm user and group back to apache if you changed them. Recreate each site as a virtual host in /etc/apache2/sites-available/ (enable with a2ensite) or /etc/httpd/conf.d/, and reissue certificates with certbot --apache.

6. Run nginx in front of Apache

Move Apache to a local port, and let nginx pass requests to it.

  1. In Apache, listen only on the local address: Listen 127.0.0.1:8080 (in ports.conf on Ubuntu, httpd.conf on AlmaLinux), and change each virtual host's address to 127.0.0.1:8080.
  2. Enable mod_remoteip so Apache logs the visitor's real IP address, not nginx's:
apache
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1
  1. In nginx, proxy to Apache:
nginx
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;
}

Terminate SSL in nginx. The X-Forwarded-Proto header tells your application the visitor used HTTPS, which stops redirect loops in WordPress and similar apps. On AlmaLinux and Rocky with SELinux enforcing, run sudo setsebool -P httpd_can_network_connect 1 so nginx may connect to Apache.

7. On a cPanel & WHM server

cPanel manages Apache through EasyApache 4, and cPanel expects Apache to stay. The supported way to add nginx is cPanel's own NGINX Manager in WHM (the ea-nginx package), which installs nginx as a caching reverse proxy in front of Apache, so .htaccess keeps working. Uninstalling it from the same page returns you to Apache alone.

Third-party add-ons such as Engintron do a similar job; never run one alongside ea-nginx, as both want ports 80 and 443. To change Apache itself (MPM, modules, PHP versions), see EasyApache 4 with CloudLinux.

8. Troubleshooting

  • bind() to 0.0.0.0:80 failed (98: Address already in use): the other server is still running. Check with sudo ss -ltnp | grep -E ':80|:443'.
  • 502 Bad Gateway: nginx cannot reach PHP-FPM or Apache. Check the socket path or port, and that the service is running.
  • PHP files download instead of running: the PHP location block or Apache's PHP-FPM handler is missing.
  • Redirect loop after putting nginx in front: the app does not know the visitor used HTTPS; send X-Forwarded-Proto as in section 6.
  • Old rewrite rules no longer work: nginx ignores .htaccess. Translate each rule into the server block.

9. Running this on Domain India

On shared hosting, the web server is managed for you. cPanel, DirectAdmin and Webuzo servers run Apache, and on cPanel nginx sits in front of Apache as a cache. You can't switch the server, but .htaccess rules and mod_rewrite work, which covers redirects, clean URLs and access rules. See how to enable mod_rewrite.

On a Domain India VPS you have full root access and choose your own stack. The VPS is self-managed, and cPanel is not offered on VPS. If you want a web server of your own choosing, a VPS is the place to run it:

VPS Starter
₹552.65/mo + GST
  • 1 vCPU
  • 2 GB DDR4 RAM
  • 64 GB NVMe SSD Storage
  • 2 TB Monthly Bandwidth
See plan details
VPS Basic
₹1,105.30/mo + GST
  • 2 vCPU
  • 4 GB DDR4 RAM
  • 128 GB NVMe SSD Storage
  • 3 TB Monthly Bandwidth
See plan details

Prices on the cards are Domain India list prices and exclude 18% GST.

Frequently asked questions

Can I switch my shared hosting account from Apache to nginx?

No. On Domain India shared hosting the web server is shared by many accounts and managed by us. The servers run Apache, with nginx in front on cPanel, and .htaccess rules work. To choose your own web server, use a VPS.

Should I uninstall Apache before installing nginx?

No. Stop and disable Apache instead, install and test nginx, and keep Apache installed so you can roll back in seconds. Uninstall it only after the new setup has run cleanly for a while.

Does nginx support .htaccess files?

No. nginx reads its rules only from its own configuration files. Every .htaccess rule must be rewritten as nginx configuration, or you can keep Apache behind nginx so .htaccess keeps working.

How do I add nginx to a cPanel server?

Use cPanel's own NGINX Manager in WHM, which installs the ea-nginx package as a reverse proxy in front of Apache. cPanel expects Apache to stay, so you add nginx rather than replace Apache.

Can I run Apache and nginx on the same server?

Yes. Run Apache on a local port such as 127.0.0.1:8080 and let nginx listen on ports 80 and 443 and pass requests to Apache. Enable mod_remoteip so Apache logs visitors' real IP addresses.

Ready to run your own web server? Compare VPS plans, or read VPS vs shared hosting to decide whether you need one. If you are on shared hosting and a rewrite rule isn't working, open a support ticket.

Your server, your web server

Self-managed KVM VPS with full root access and your choice of Linux, so you can run Apache, nginx or both.

See VPS plans

Ready when you are

Get cPanel hosting from ₹125/mo + GST

See plans

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app
Switch Between Apache and Nginx on a VPS or WHM