How to use NginX as reverse proxy Apache

Apache is known as the first and most widely used http server software in the world because of its ability to handle many dynamic applications in larg

How to set up your own private RTMP server using nginx
How to Setup LEMP On Ubuntu 16.04
How To Install WordPress with Nginx on Ubuntu 14.04

Apache is known as the first and most widely used http server software in the world because of its ability to handle many dynamic applications in large scalability. Furthermore, Apache supports many programming language, some of which are commonly used are PHP, Perl, and Python (complete list can be found at this page: NginX itself today has been used by many websites generally because of its resource usage friendly. This web server which was first introduced in 2006 was intended to serve static files and as proxy server. For that reason NginX does not require too much resources and needs third party plug-ins to support programming language other than C.

In outline, Apache itself is more than enough to run many programming applications, but the problem is default server memory usage by this software is a lot higher than NginX, which is great of serving static files but does not have the important Apache feature called mod_rewrite. Therefore, this tutorial will make Apache work together with NginX so both can perform great in static file and dynamic application, while memory usage can be reduced to be lower.

OS: CentOS 6

Install Apache
By default, Apache is already installed in system, but it is recommended to use the latest version.

Because there are few softwares needed by Apache are not available from CentOS 6 repository, we will use EPEL for additional repository.

Update and install build essential tools for

Install dependencies

Both CentOS and EPEL do not provide the newest version of Apache Portable Runtime required by Apache, get it from the official download page and install.

Convert APR source packages to rpm first, after that just install it easily.

Another dependency that does not available in CentOS 6 repository, Distributed session caching software, it is needed to be rebuilt from rpm source package that already provided by Fedora Project.

Everything is set, now install and start Apache

Install MySQL
By installing mysql-devel previously, it has automatically installed mysql-server too, but it is the old version, update with the newest one.

Remove the old version first or you will experience an error like this
error: Failed dependencies:
mysql = 5.1.73-3.el6_5 is needed by (installed) mysql-devel-5.1.73-3.el6_5.i686

MySQL will automatically create root password and written inside .mysql_secret file located in the same rpm directory. The password can be used for mysql_secure_installation, don’t forget to start the daemon before securing MySQL.

Install PHP

PHP is one of many programming languages that widely used to generate dynamic web page. Install packages needed by PHP first.

Download PHP from official page, unpack, configure, and install. Check the configuration below, change any lib directory with lib64 for 64bit architecture, example: --libdir=/usr/lib to --libdir=/usr/lib64 ; --with-libdir=lib to --with-libdir=lib64

After finished installing PHP, edit httpd.conf with any text editor, add SetHandler PHP with .php extension, this is more secure than using AddType directive.

Start / restart Apache with service httpd start / restart, put one php file for example phpinfo in /var/www/html directory and try to open it from browser.
PHP Apache

Install NginX
Just like the previous, use provided rpm to ease the installation process.

Configure NginX Proxy Apache
Make sure that both webserver use the same user and group. Create one new user and group.

Change the old user to wserver from /etc/httpd/conf/httpd.conf and /etc/nginx/nginx.conf

Create user directory and domain directory for files.

Create directory for Apache virtual host configuration

Edit httpd.conf, add Include and point to the vhost configuration directory.

Change Apache listen port from 80 to 8080

Also create that directory for NginX

Edit nginx.conf, also add include to the configuration.

Create proxy.conf inside /etc/nginx/conf.d configuration folder, it will be included in all virtual host conf, the example of proxy.conf is below.

Now create vhost conf for NginX inside sites-enabled directory, don’t forget to include proxy.conf.

The configuration

Apache virtual host configuration under /etc/httpd/conf/sites-enabled.

All finished. To add another domain, create vhost configuration as above for both Apache and NginX with root user, and create folder under wserver (user) directory with wserver user, not root.

NginX configuration directory: /etc/nginx
NginX default html directory: /usr/share/nginx/html
Apache configuration directory: /etc/httpd/conf
Apache default html directory: /var/www/html