Apache and Nginx are two popular open-source web servers often used with PHP. It can be useful to run both of them on the same virtual machine when hosting multiple websites that have varied requirements. The general solution for running two web servers on a single system is to either use multiple IP addresses or different port numbers.
Servers that have both IPv4 and IPv6 addresses can be configured to serve Apache sites on one protocol and Nginx sites on the other, but this isn’t currently practical, as IPv6 adoption by ISPs is still not widespread. Having a different port number like 81
or 8080
for the second web server is another solution, but sharing URLs with port numbers (such as http://``your_domain``:81
) isn’t always reasonable or ideal.
In this tutorial you’ll configure Nginx as both a web server and as a reverse proxy for Apache – all on a single server.
Depending on the web application, code changes might be required to keep Apache reverse-proxy-aware, especially when SSL sites are configured. To avoid this, you will install an Apache module called mod_rpaf
which rewrites certain environment variables so it appears that Apache is directly handling requests from web clients.
We will host four domain names on one server. Two will be served by Nginx: nginx1.``your_domain
(the default virtual host) and nginx2.``your_domain
. The remaining two, apache1.``your_domain
and apache2.``your_domain
, will be served by Apache. We’ll also configure Apache to serve PHP applications using PHP-FPM, which offers better performance over mod_php
.
To complete this tutorial, you’ll need the following:
Let’s start by installing Apache and PHP-FPM.
In addition to Apache and PHP-FPM, we will also install the PHP FastCGI Apache module, libapache2-mod-fastcgi
, to support FastCGI web applications.
First, update your package list to ensure you have the latest packages.
sudo apt update
Next, install the Apache and PHP-FPM packages:
sudo apt install apache2 php-fpm
The FastCGI Apache module isn’t available in Ubuntu’s repository so download it from kernel.org and install it using the dpkg
command.
#apache #configure #web server