Apache and Nginx both on port 80

20,236

Solution 1

I'm not quite sure what your hosting company means by their comment but you won't be able to run BOTH Apache and Nginx on port 80. Once one is bound to port 80 the other will be unable to bind to it.

Probably the best configuration in your current situation would be to put Nginx on port 80 and Apache on 8000 or similar.

Use nginx to serve static files (see try_files because "if" is evil) and then proxy all requests for PHP to port 8000 using the HTTP proxy module.

The other common configuration for PHP with Nginx is to use PHP-FPM and proxy via FastCGI, just google "PHP-FPM Nginx {Your OS} tutorial" for a tutorial.

There much debate about the performance of PHP-FPM/mod_php but in my personal experience I have found PHP-FPM more performant.

Solution 2

I would use nginx as the web facing server on port 80 and proxy pass to apache which would be running on a different port. Many sites run this configuration. Serving static files with nginx is much more efficient than with apache. It is actually a lot simpler than it may sound.

This document explains in detail.

Solution 3

You should bind your externally facing webserver to the public IP address and the internally facing webserver to localhost (127.0.0.1:80).

So if you are using Nginx publicly then bind that to the public IP address and have it proxy to Apache at localhost.

Its better to use separate ports to make it easier to debug.

Share:
20,236
Passionate Engineer
Author by

Passionate Engineer

Passionate engineer who loves everything IT related with particular interest in: Javascript, Python, Go, Kubernetes, Cloud Computing, DevOps and AI

Updated on July 05, 2022

Comments

  • Passionate Engineer
    Passionate Engineer almost 2 years

    I'm trying to install Nginx on my current cloud Cent OS server which has Apache httpd installed and running.

    My hosting company tells me that Nginx and Apache can both run on port 80 at the same time so my plan was to transform .htaccess and Apache conf of Wordpress sites to Nginx after installing it via Yum.

    I also Googled about this and some people suggest using Nginx as a reverse proxy and serve static files only but run Apache with PHP because Apache has PHP embedded and would consume less memory even though it doesn't support multiple concurrent requests like Nginx.

    My gut feeling is that switching everything over to Nginx would be beneficial but unsure at this stage.

    Also, is there anything I should watch out for when doing this switch over?

    What would you do if it was you in this situation?

  • Get Off My Lawn
    Get Off My Lawn over 11 years
    I agree, PHP-FPM is way faster on my server than mod_php was.
  • Passionate Engineer
    Passionate Engineer over 11 years
    @Thom Seddon: Thanks for this. Is there other implications for changing over for either method on caching such as APC? Would it not have any effect as it's a PHP package?
  • Thom Seddon
    Thom Seddon over 11 years
    You're quite right, as it's a PHP extension it wouldn't make a difference. The only difference I can think of are that mod_php does expose a few extra PHP functions (php.net/manual/en/ref.apache.php) but you can definitely work without them (PHP 5.4 actually brings a few of them into core so they are always available, e.g. php.net/manual/en/function.apache-request-headers.php)
  • Vagari
    Vagari over 10 years
    Unless each service is attached to a different IP. Then it is possible to use port 80 for both Apache and Nginx.
  • Kevin Ford The Submariner
    Kevin Ford The Submariner almost 8 years
    TRENT your link is currently broken. Do you know where the content has moved to? Would it be possible for you to summarize that content in your answer?
  • Works for a Living
    Works for a Living about 4 years
    Can you explain precisely how to do this?