Configure Nginx as a Reverse Proxy for Apache

Install nginx

sudo apt-get install nginx

Configure nginx

sudo nano /etc/nginx/sites-available/example

Make the file as bellows:

server {
    listen   80;

    root /var/www/example.com/public_html;
    index index.php index.html index.htm;

    server_name example.com www.example.com; 

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

    location ~[^?]*/$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;
    }

    location ~ \.php$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;
    }

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

Activate the virtual host.

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example

and delete the default one.

sudo rm /etc/nginx/sites-enabled/default

Install Apache

sudo apt-get install apache2

Configure Apache

sudo nano /etc/apache2/ports.conf

Find and replace the port 80 to 8080 like bellow:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Create apache virtual host.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.conf
sudo nano /etc/apache2/sites-available/example.conf

Change the port 80 to 8080 including the IP address like bellow:

<VirtualHost 127.0.0.1:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/var/www/html">
        AllowOverride ALL
    </Directory>
</VirtualHost>

Activate the virtual host’s site.

sudo a2ensite example

Now Install the php.

sudo apt-get install php
sudo apt-get install libapache2-mod-php

Restart both servers.

sudo service apache2 restart
sudo service nginx restart

Congratulations you’re done.

Configure X-Debug:

sudo apt-get install php-xdebug

add these lines at the end of the file /etc/php/7.1/apache2/php.ini

# Added for xdebug
zend_extension="/usr/lib/php/20151012/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.max_nesting_level=300

Turn on error reporting

sudo nano /etc/php/7.1/apache2/php.ini

Install MySQL

sudo apt-get install mysql-server php-mysql
sudo mysql_install_db
sudo mysql_secure_installation

Install phpMyAdmin

sudo apt-get update
sudo apt-get install phpmyadmin
sudo ln -s /usr/share/phpmyadmin /var/www/html
sudo service apache2 restart
sudo service nginx restart