Using Apache with NGINX as a reverse proxy involves configuring NGINX to forward incoming requests to Apache, which then serves the content. This setup can provide benefits like load balancing, SSL termination, and improved performance. Here's a general outline of the steps to set up Apache with NGINX as a reverse proxy:
Install and Configure Apache:
sudo apt-get install libapache2-mod-php
sudo a2enmod php
Install and Configure NGINX:
Configure Apache for the Application:
Configure NGINX as a Reverse Proxy:
.conf
file in the sites-available
directory (on Ubuntu-based systems) or the equivalent location on your distribution.server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://apache_ip_address: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;
}
}
Replace yourdomain.com
with your actual domain, and apache_ip_address
and apache_port
with the IP address and port where your Apache server is listening.
Another example of nginx server block for reverse proxy
# /etc/nginx/conf.d/mydomain.conf
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass <http://127.0.0.1:8080>; # Apache's default 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;
}
location ~ \\.php$ {
proxy_pass <http://127.0.0.1:8080>; # Apache's default port
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Use your PHP-FPM socket
}
}
In this example, the first location
block proxies non-PHP requests to Apache, while the second location
block proxies requests for PHP files. Make sure to adjust the PHP-FPM socket path to match your PHP-FPM setup.
Test and Apply Changes:
nginx -t
systemctl reload nginx
(or the appropriate command for your system).Secure Your Setup:
Monitor and Maintain:
Setting up NGINX and Apache on the same Ubuntu PC involves installing both web servers and then configuring them to work together. Here's a step-by-step guide:
Install Apache and NGINX:
Open a terminal and run the following commands to install Apache and NGINX:
sudo apt update
sudo apt install apache2 nginx
Configure Apache:
After installing Apache, it should automatically start as a service. You can test it by opening a web browser and navigating to http://localhost
.
Stop Apache Service:
Before proceeding to configure NGINX, stop the Apache service:
sudo systemctl stop apache2
Configure NGINX:
Create an NGINX configuration file to proxy requests to Apache. Create a new .conf
file in /etc/nginx/conf.d/
. For example:
sudo nano /etc/nginx/conf.d/apache.conf
Add the following configuration:
server {
listen 80;
server_name yourdomain.com; # Replace with your domain or IP
location / {
proxy_pass <http://127.0.0.1:8080>; # Apache's default 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;
}
}
Save the file and exit the text editor.
Test NGINX Configuration:
Run a configuration test to ensure there are no syntax errors:
sudo nginx -t
Start NGINX:
If the test is successful, start NGINX:
sudo systemctl start nginx
Enable Apache:
While NGINX is acting as a reverse proxy, you can run Apache on a different port. For example, let's change Apache's default port to 8080. Open the Apache ports configuration file:
sudo nano /etc/apache2/ports.conf
Change the Listen
directive to use port 8080:
Listen 8080
Save the file and exit the text editor.
Restart Apache:
Restart Apache to apply the configuration changes:
sudo systemctl restart apache2
Access the Setup:
Now, you should be able to access your application through NGINX by visiting http://yourdomain.com
, where yourdomain.com
is your server's domain or IP address.
If you've set up Apache with virtual hosts (vhosts) and you want to access these vhosted websites through NGINX as a reverse proxy on the same PC, you need to configure NGINX to proxy requests to the appropriate Apache vhost based on the domain or hostname. Here's how you can do that: