Bilal Alam
5 min readJun 18, 2018

Replacing Apache HTTP server with nginx in Bitnami AWS Virtual Machine for faster client-server communication

As we all know, the Apache HTTP server and NGINX are the two most popular open source web servers powering the Internet today and there is a lot of discussions already done on their comparison by experts.This article is not about comparing both of them or getting into pros and cons of both technologies rather, the goal is to share a small experience/solution that might help someone finding himself in a similar situation.

In the world of web applications developement specially single page web apps developement ,it feels like there is framework’s world war going on, each one trying to be the best.There are obviously numerous parameters that defines the ‘best’ here,one of the most important is performance and optimization.Clearly everyone wants the application to load fast and move fast.No matter how complex work you have done or how fantastic your UI is, if it is slow it is ugly.

My story starts with a project that I was very excited to build , it was a enhanced clone of Apex ping which is a network monitoring app that sends ICMP pings at regular interval to a list of IP addresses and provides real-time insight, data visualization and email reporting on the connections.The project requirement was pretty detailed and involved many use-cases.The little inborn architect inside me was very excited, as a beginner i was happy to build something from the scratch all by myself.I carefully sketched out the application architecture and started development,everything went well ,the first phase of the application was ready to deploy on the server but It did not take me long to realize why becoming an architect needs a lot of core experience.

I deployed the application to bitnami AWS cloud with MEAN stack which used Apache as the HTTP server and surprisingly the application was dead slow.Although I was using server-side rendering and my whole application was not more than 19.6 MB, The application still was taking atleast 2–3 minutes before it lands to the first page which should not be the case in server-side rendering since it does not loads the whole app in the first go.Even after the app is loaded , every http request to the server ( same origin) was taking on average 5 seconds to response.Clearly the problem was with the Apache HTTP server that i was using as a reverse proxy to redirect all requests to port 80 to port 3000( where my application was running).Looking at the CPU usage by running “top” command I realized that the nodejs instance was eating almost all of it leaving behind nothing for the HTTP server.

I immediately added extra 2 GB ram in the hope to resolve this issue but in vain. Monitoring the CPU usage for a while, I could see even when the node’s CPU consumption dropped from 99% to 48% on average after adding RAM ,I still could only see the apache’s HTTP process appearing after every 20 to 30 seconds with just 0.3% CPU usage and lasting for only 3–4 seconds, and only then I could see the pending HTTP requests getting their responses. I spent a lot of time searching for the solution and explanation of this behavior but failed, eventually I decided to by pass the built-in apache HTTP server and use nginx instead which i should have been using since the beginning.So heres what I did to stop the Apache’s server and replace it with nginx.

1-Move to the /opt/ folder
2-Stop all services by running command ‘sudo /opt/bitnami/ctlscript.sh start’.

3-There were two folders in /opt/ one was bitnami (contains all services and configurations) and the other was my project’s folder, I renamed the bitnami folder : ‘ sudo mv /opt/bitnami/ /opt/bitnami_bck/’

4-Download the nginx isntaller next in the current folder i.e /opt/ by running the command ‘sudo wget https://bitnami.com/redirect/to/133526/bitnami-nginxstack-1.10.2-0-linux-x64-installer.run

5-Change the access permission
sudo chmod +x bitnami-nginxstack-1.10.2–0-linux-x64-installer.run’

6-Run the nginx installer
sudo ./bitnami-nginxstack-1.10.2–0-linux-x64-installer.run — prefix /opt/bitnami/’

Press Y on all prompts and press enter enter when the installer asks you to choose the folder where you want to install.

This will create a new folder ‘bitnami’ in /opt/ and install the bitnami’s Nginx stack there, Now we need to move only the nginx component from the newly created folder to the original one.

7-Moving Nginx component to the original folder
sudo mv /opt/bitnami/nginx /opt/bitnami_bck/
Moving phpMyadmin to the apps folder
sudo mv /opt/bitnami/apps/phpmyadmin /opt/bitnami_bck/apps/

8-Since we donot need the bitnami folder anymore ,it has done its job, we can remove it now.

sudo /opt/bitnami/uninstall’

and ‘ sudo rm -rf /opt/bitnami/

9-Now we can restore the original folder back to its previous name

sudo mv /opt/bitnami_bck/ /opt/bitnami/

10-Now we need to configure the nginx so that we can use it as a reverse proxy for port 3000
we need to open nginx.conf file which is usually found here
/opt/bitnami/nginx/conf/
look for the http following curly braces, these are called directives, we need to add a server directive inside http directive , add the following lines inside http curly braces:


server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://127.0.0.1:3000;
}
}

10-Now if you run the start script i.e ‘sudo /opt/bitnami/ctlscript.sh start’ which starts all the services in the following sequence mongodb, php-fpm, apache,nginx. Nginx will throw error because apache will occupy port 80 before nginx does, since we dont need apache anymore , once you run the start script , stop the apache by running ‘sudo /opt/bitnami/ctlscript.sh stop apache’

and start nginx again by running ‘sudo /opt/bitnami/ctlscript.sh start nginx’

and here we go, My application went from snail to rabbit, the first page started landing in 500- 600 ms and the average response time for all the requests were 150–200 ms.

Bilal Alam

just another Javascript enthusiast, computer science graduate from University Of Karachi and Software Engineer at VentureDive .