SlideShare a Scribd company logo
1 of 27
Download to read offline
Scaling Apache for LAMP




                 Buddhika Siddhisena
        (Co-Founder & CTO of THINKCube Systems)
            bud@thinkcube.com | twitter @geekaholic
What is Apache
The folk story is that Apache was named after "A-patchy-server", which was the result of NCSA httpd
server being patched a lot. The project was started by Brian Behlendorf




Today, Apache is still the most popular web server out there running more than half of the websites
on the net. It is actively developed by the Apache Software Foundation along with many other software
projects.

Besides its primary function of being a website, Apache can also be configured as a reverse proxy for
load balancing.
Installing Apache
The easiest method of installing Apache along with PHP and MySQL (aka LAMP) is to use the tasksel
command.

tasksel

Alternatively install each package manually:

apt-get install apache2 libapache2-mod-php5 mysql-server

Installing a sample LAMP app - Drupal
In order to test out Apache performance as we tune it, it is good to setup a real world full fledged CMS
such as Drupal.

    Download the latest version of Drupal from drupal.org

    Follow the Drupl setup guide

    Install the Devel module into Drupal modules directory

    Login to Drupal as admin and using the devel plugin, populate Drupal with sample data for testing
    (Configuration -> Development -> Generate Content)
Setting up Benchmarking tools
Setup Autobench
Autobench is a handy script to stress test a webserver by sending an increasing number of requests. It
works by calling the httperf tool iteratively with increasing parameters.

Download autobench and follow directions to compile.

In order to plot graphs, you need to install gnuplot via apt. As of this writing, the script used to plot
the graph has a bug calling the current version of gnuplot and requires the following minor
modification.

$ sudo vi which bench2graph

line ~78 should be

echo set style data linespoints >> gnuplot.cmd
Baseline benchmark with Autobench
Lets benchmark our standard Apache setup to get an idea of default performance.

 autobench --single_host --host1 localhost --uri1 /drupal --quiet       
        --low_rate 20 --high_rate 200 --rate_step 20 --num_call 10 
        --num_conn 5000 --timeout 5 --file results.tsv

Basically the above will test a single host, localhost/drupal by sending it 20 connections per second,
each having 10 requests up to 200 connections per second incrementing by 20. The total number of
connections are capped at 5000 while any request that takes more than 5 seconds to respond is
considered unsuccessful.

Plotting the results
Using the result.tsv file and the included bench2graph utility, you can plot a graph into a postscript
file.

 bench2graph results.tsv results.ps
Tuning Apache - Enable GZip
You can decrease network overhead and make pages load faster, there by reducing the amount of
time a client is connected by compressing pages using gzip. All modern browser support rendering
compressed files.

In order to benchmark its effect, you can install a tool such as Firebug on the client side.
Tuning Apache - Enable GZip
Enable the mod_deflate module. On Ubuntu :

a2enmod deflate && a2enmod headers

Then we'll configure deflate to compress everything except images.

sudo vi /etc/apache2/modules-enabled/deflate.conf

 1 <Location />
 2     # Insert filter
 3     SetOutputFilter DEFLATE
 4
 5     # Don't compress images
 6     SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
 7
 8     # Make sure proxies don't deliver the wrong content
 9     Header append Vary User-Agent env=!dont-vary
10
11 </Location>
Apache configuration tuning
There are a few key parameters that can be tuned:

    KeepAlive - By default its set to ON which is good. Clients will make all requests in one shot via
    http 1.1.

    KeepAliveTimeout - Better to keep it low. Defaults to 15 sec. Make sure thats enough. Rule is 1.5
    to 2 times your page load speed.

    TimeOut - The default is 5 minutes which might be long to allow for one process. Adjust
    accordingly.

    StartServers, MinSpareServers, MaxSpareServers - Generally even on a busy site you may not need
    to tweak. Apache can self regulate.

    MaxClients - The maximum number of clients (threads) Apache will handle simultaneously.
Calculating MaxClients
ps -eafly |grep apache2|awk '{print $8}'|sort -n

Use free to figure out how much memory is available. Cache is also considered free memory but you
might want to leave some and not assume all cache will be used.

free

By deviding free memory by the average memory used by an Apache thread, you can estimate the
number of MaxClients.

e.g: Assuming Apache memory usage and free memory are as follows

$ ps -eafly |grep apache2|awk '{print $8}'|sort -n

816
3896
3896
3896
3896
20844

$ free
             total          used        free       shared   buffers   cached
Mem:        508904        447344       61560            0    141136   213468
-/+ buffers/cache:         92740      416164
Swap:       407544          4364      403180

Memory avail ~= 60000 (free) + 100000 (cached) ~= 160 MB and Memory per thread ~= 4 MB Then a
safe value for MaxClients = 40
Improving PHP performance
We can improve PHP performance by

1. Caching pages (useful if dynamic content doesn't change often)

2. PHP Opcode optimizations (pre-compile php)

Fortunately we can get the benefit of both using PHP APC, which is a PHP accellerator!

apt-get install php-apc

You can verify installation by loading a php page having phpinfo(); and searching for apc. Or if you
have php5-cli installed:

php -r "phpinfo();" | grep apc

Using memcached
Memcached is a distributed cache for storing key-value pairs in memory for faster access with
reduced trips to the database. Some popular PHP apps can use memcache if available. memcached
does not instantly accellerate PHP!

apt-get install memcached php5-memcache

service memcached start
More Tips for improving performance
  Keep DirectoryIndex file list as short as possible.

  Whenever possible disable .htaccess via AllowOverride none

  Use Options FollowSymLinks to simplify file access process in Apache

  Minimize the use of mod_rewrite or at least complex regexs

  If logs are unnecessary disable them or log to another server via syslog.

  For Deny/Allow rules use IPs rather then domains. (prevents superfluous DNS lookups).

  Do not enable HostnameLookups (DNS is slow).

  For dynamic sites see if you can separate dynamic vs static content into two servers
Scaling Architectures




                  Buddhika Siddhisena
         (Co-Founder & CTO of THINKCube Systems)
             bud@thinkcube.com | twitter @geekaholic
Architecture overview
In terms of scaling the web server there are few options.

1. Single machine (Scale vertically)
Basically the easiest to setup. Scaling is a matter of buying a better server or upgrading it!




2. App-DB machines (2-Tier)
Separate DB from App, as a result each can be scaled separately.
Architecture overview contd...
3. Load balancer + App-DB machines (3-Tier)
Load balancer (aka reverse proxy) will route requests betwen multiple backend HTTP servers while
caching results.
Data Independence
It is good to isolate the data from the app by hosting it on a separate server. This was the two aspects
can be scaled independantly. Some methods to consider:

    Store DB data on MYSQL running on a separate server

    Enable file sharing to share data files using NFS, rsync

    Clustering MYSQL across multiple servers using mysqlcluster

    Cluster file system via DRDB, GFS2 or as Facebook does using Bittorrent
Setting up an HTTP accellerator
        using Apache
Apache as a reverse proxy
In this setup, the reverse server is what the user will contact while the real webserver can be hidden
behind a private network.

1. On the reverse proxy server :
Enable required modules for caching reverse proxy.

a2enmod proxy

a2enmod proxy_connect

a2enmod proxy_http

a2enmod cache

2. Configure proxy module
vi /etc/apache2/modules-enabled/proxy.conf

1 <Proxy *>
2          AddDefaultCharset off
3          Order deny,allow
4          Deny from all
5          Allow from all
6 </Proxy>
7 ProxyVia On
Apache as a reverse proxy contd...
3. Setup (public) virtual host
Next we configure an empty virtual host that is configured to the public site. But instead of showing
the document root we do a reverse proxy.

vi /etc/apache2/sites-available/public-domain.com

 1 <VirtualHost *:80>
 2
 3     ServerName your-public-domain.com
 4
 5     <Proxy *>
 6         Order deny,allow
 7         Allow from all
 8     </Proxy>
 9
10     ProxyPass / http://your-private-domain.com/
11     ProxyPassReverse / http://your-private-domain.com/
12
13 </VirtualHost>

a2ensite public-domain.com
service apache2 reload
Using Nginx
What is Nginx?




   Nginx was designed as a reverse proxy first, and an HTTP server second

   Unlike Apache, Nginx uses a non blocking process model

Two modes of operation for Nginx:
1. Use Nginx for the static content and Apache for PHP

2. Use FastCGI to embed PHP
Nginx process model in a nutshell
  Receive request, trigger events in a process

  The process handles all the events and returns the output

  Process handles events in parallel

  Limitation is PHP can no longer be embedded ( mod_php ) inside process as PHP is not asynchronous

  Unlike Apache, Nginx doesn't not have an .htaccess equivelant. You need to reload server after
  making any chage, making it difficult to use for shared hosting
Using Nginx and Apache side-by-side
In this setup we put Nginx as the frontend http accellerator and Apache as the backend app server. If
you want to run this on the same physical server you'll need to either change the Apache port from 80
to another value or bind and Nginx to their own IP addresses with the same server.

Listen 8080

or using the ip address

Listen 127.0.0.1:8080

Now we're ready to install Nginx

sudo apt-get install nginx
Apache style virtual host in Nginx
Nginx uses a different format for defining virtual hosts than Apahche.

1 <VirtualHost>
2       DocumentRoot "/usr/local/www/mydomain.com"
3       ServerName mydomain.com
4       ServerAlias www.mydomain.com
5       CustomLog /var/log/httpd/mydomain_access.log common
6       ErrorLog /var/log/httpd/mydomain_error.log
7       ...
8 </VirtualHost>

becomes...

 1 server {
 2       root /usr/local/www/mydomain.com;
 3       server_name mydomain.com www.mydomain.com;
 4
 5       # by default logs are stored in nginx's log folder
 6       # it can be changed to a full path such as /var/log/...
 7       access_log logs/mydomain_access.log;
 8       error_log logs/mydomain_error.log;
 9       ...
10 }
Redirecting all PHP requests to Apache
The following example will server all static content via nginx while redirect dynamic content (php) to
Apache

 1 server {
 2     listen    80 default;
 3     server_name localhost;
 4
 5     access_log /var/log/nginx/localhost.access.log;
 6
 7     location / {
 8          root   /var/www;
 9          index index.html index.htm;
10     }
11
12       ## Parse all .php file in the /var/www directory
13       location ~ .php$ {
14          # these two lines tell Apache the actual IP of the client being forwarded
15
16          proxy_set_header X-Real-IP $remote_addr;
17          proxy_set_header X-Forwarded-For $remote_addr;
18
19          # this next line adds the Host header so that apache knows which vHost to serve
20
21          proxy_set_header Host $host;
22
23          # And now we pass back to apache
24          proxy_pass http://127.0.0.1:8080;
25
26     }
27 }
References
 How To Improve Website Performance (With Drupal, LAMP)

 PHP Performance tuning

 HipHop compiler by Facebook

 How the code you write affects PHP benchmarks

 Scaling Facebook with OpenSource tools

 Scaling drupal

 DRBD for HA distributed file system
Thank you



bud@thinkcube.com | twitter @geekaholic

More Related Content

What's hot

Benchmarking NGINX for Accuracy and Results
Benchmarking NGINX for Accuracy and ResultsBenchmarking NGINX for Accuracy and Results
Benchmarking NGINX for Accuracy and ResultsNGINX, Inc.
 
WordPress + NGINX Best Practices with EasyEngine
WordPress + NGINX Best Practices with EasyEngineWordPress + NGINX Best Practices with EasyEngine
WordPress + NGINX Best Practices with EasyEngineNGINX, Inc.
 
Proxysql use case scenarios plam 2016
Proxysql use case scenarios    plam 2016Proxysql use case scenarios    plam 2016
Proxysql use case scenarios plam 2016Alkin Tezuysal
 
Nginx internals
Nginx internalsNginx internals
Nginx internalsliqiang xu
 
Nginx - Tips and Tricks.
Nginx - Tips and Tricks.Nginx - Tips and Tricks.
Nginx - Tips and Tricks.Harish S
 
How to improve your apache web server’s performance
How to improve your apache web server’s performanceHow to improve your apache web server’s performance
How to improve your apache web server’s performanceAndolasoft Inc
 
Web Server Load Balancer
Web Server Load BalancerWeb Server Load Balancer
Web Server Load BalancerMobME Technical
 
ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016Derek Downey
 
Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...
Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...
Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...Severalnines
 
ProxySQL - High Performance and HA Proxy for MySQL
ProxySQL - High Performance and HA Proxy for MySQLProxySQL - High Performance and HA Proxy for MySQL
ProxySQL - High Performance and HA Proxy for MySQLRené Cannaò
 
under the covers -- chef in 20 minutes or less
under the covers -- chef in 20 minutes or lessunder the covers -- chef in 20 minutes or less
under the covers -- chef in 20 minutes or lesssarahnovotny
 
Drupal 8 and NGINX
Drupal 8 and NGINX Drupal 8 and NGINX
Drupal 8 and NGINX NGINX, Inc.
 
MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...
MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...
MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...Severalnines
 
NGINX: High Performance Load Balancing
NGINX: High Performance Load BalancingNGINX: High Performance Load Balancing
NGINX: High Performance Load BalancingNGINX, Inc.
 
Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...
Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...
Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...Severalnines
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90minsLarry Cai
 
Rate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX PlusRate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX PlusNGINX, Inc.
 
NGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA BroadcastNGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA BroadcastNGINX, Inc.
 
Generic Parse Server
Generic Parse ServerGeneric Parse Server
Generic Parse Serverdavidolesch
 

What's hot (19)

Benchmarking NGINX for Accuracy and Results
Benchmarking NGINX for Accuracy and ResultsBenchmarking NGINX for Accuracy and Results
Benchmarking NGINX for Accuracy and Results
 
WordPress + NGINX Best Practices with EasyEngine
WordPress + NGINX Best Practices with EasyEngineWordPress + NGINX Best Practices with EasyEngine
WordPress + NGINX Best Practices with EasyEngine
 
Proxysql use case scenarios plam 2016
Proxysql use case scenarios    plam 2016Proxysql use case scenarios    plam 2016
Proxysql use case scenarios plam 2016
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
 
Nginx - Tips and Tricks.
Nginx - Tips and Tricks.Nginx - Tips and Tricks.
Nginx - Tips and Tricks.
 
How to improve your apache web server’s performance
How to improve your apache web server’s performanceHow to improve your apache web server’s performance
How to improve your apache web server’s performance
 
Web Server Load Balancer
Web Server Load BalancerWeb Server Load Balancer
Web Server Load Balancer
 
ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016
 
Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...
Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...
Webinar slides: MySQL & MariaDB load balancing with ProxySQL & ClusterControl...
 
ProxySQL - High Performance and HA Proxy for MySQL
ProxySQL - High Performance and HA Proxy for MySQLProxySQL - High Performance and HA Proxy for MySQL
ProxySQL - High Performance and HA Proxy for MySQL
 
under the covers -- chef in 20 minutes or less
under the covers -- chef in 20 minutes or lessunder the covers -- chef in 20 minutes or less
under the covers -- chef in 20 minutes or less
 
Drupal 8 and NGINX
Drupal 8 and NGINX Drupal 8 and NGINX
Drupal 8 and NGINX
 
MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...
MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...
MySQL Load Balancers - Maxscale, ProxySQL, HAProxy, MySQL Router & nginx - A ...
 
NGINX: High Performance Load Balancing
NGINX: High Performance Load BalancingNGINX: High Performance Load Balancing
NGINX: High Performance Load Balancing
 
Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...
Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...
Webinar slides: How to deploy and manage HAProxy, MaxScale or ProxySQL with C...
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 
Rate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX PlusRate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX Plus
 
NGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA BroadcastNGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA Broadcast
 
Generic Parse Server
Generic Parse ServerGeneric Parse Server
Generic Parse Server
 

Similar to Scale Apache with Nginx

Using aphace-as-proxy-server
Using aphace-as-proxy-serverUsing aphace-as-proxy-server
Using aphace-as-proxy-serverHARRY CHAN PUTRA
 
Nginx 0.8.x + php 5.2.13 (fast cgi) setup web server
Nginx 0.8.x + php 5.2.13 (fast cgi) setup web serverNginx 0.8.x + php 5.2.13 (fast cgi) setup web server
Nginx 0.8.x + php 5.2.13 (fast cgi) setup web serverwruben
 
Performance and Scalability
Performance and ScalabilityPerformance and Scalability
Performance and ScalabilityMediacurrent
 
DrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performanceDrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performanceAshok Modi
 
Apache Traffic Server
Apache Traffic ServerApache Traffic Server
Apache Traffic Serversupertom
 
How to install and configure LEMP stack
How to install and configure LEMP stackHow to install and configure LEMP stack
How to install and configure LEMP stackRootGate
 
High Performance Web Sites
High Performance Web SitesHigh Performance Web Sites
High Performance Web SitesRavi Raj
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisationgrooverdan
 
Supercharging your PHP pages with mod_lsapi in CloudLinux OS
Supercharging your PHP pages with mod_lsapi in CloudLinux OSSupercharging your PHP pages with mod_lsapi in CloudLinux OS
Supercharging your PHP pages with mod_lsapi in CloudLinux OSCloudLinux
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
Joomla! Performance on Steroids
Joomla! Performance on SteroidsJoomla! Performance on Steroids
Joomla! Performance on SteroidsSiteGround.com
 
Configuring Your First Hadoop Cluster On EC2
Configuring Your First Hadoop Cluster On EC2Configuring Your First Hadoop Cluster On EC2
Configuring Your First Hadoop Cluster On EC2benjaminwootton
 
wordpress-performance-presentation
wordpress-performance-presentationwordpress-performance-presentation
wordpress-performance-presentationArun Janarthanan
 
How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04Sanjary Edu
 

Similar to Scale Apache with Nginx (20)

Using aphace-as-proxy-server
Using aphace-as-proxy-serverUsing aphace-as-proxy-server
Using aphace-as-proxy-server
 
Nginx 0.8.x + php 5.2.13 (fast cgi) setup web server
Nginx 0.8.x + php 5.2.13 (fast cgi) setup web serverNginx 0.8.x + php 5.2.13 (fast cgi) setup web server
Nginx 0.8.x + php 5.2.13 (fast cgi) setup web server
 
Performance and Scalability
Performance and ScalabilityPerformance and Scalability
Performance and Scalability
 
DrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performanceDrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performance
 
Apache Traffic Server
Apache Traffic ServerApache Traffic Server
Apache Traffic Server
 
How to install and configure LEMP stack
How to install and configure LEMP stackHow to install and configure LEMP stack
How to install and configure LEMP stack
 
High Performance Web Sites
High Performance Web SitesHigh Performance Web Sites
High Performance Web Sites
 
Apache - Quick reference guide
Apache - Quick reference guideApache - Quick reference guide
Apache - Quick reference guide
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisation
 
Supercharging your PHP pages with mod_lsapi in CloudLinux OS
Supercharging your PHP pages with mod_lsapi in CloudLinux OSSupercharging your PHP pages with mod_lsapi in CloudLinux OS
Supercharging your PHP pages with mod_lsapi in CloudLinux OS
 
Installing lemp with ssl and varnish on Debian 9
Installing lemp with ssl and varnish on Debian 9Installing lemp with ssl and varnish on Debian 9
Installing lemp with ssl and varnish on Debian 9
 
Performance_Up.ppt
Performance_Up.pptPerformance_Up.ppt
Performance_Up.ppt
 
Scaling PHP apps
Scaling PHP appsScaling PHP apps
Scaling PHP apps
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Apache
ApacheApache
Apache
 
Joomla! Performance on Steroids
Joomla! Performance on SteroidsJoomla! Performance on Steroids
Joomla! Performance on Steroids
 
Configuring Your First Hadoop Cluster On EC2
Configuring Your First Hadoop Cluster On EC2Configuring Your First Hadoop Cluster On EC2
Configuring Your First Hadoop Cluster On EC2
 
Its3 Drupal
Its3 DrupalIts3 Drupal
Its3 Drupal
 
wordpress-performance-presentation
wordpress-performance-presentationwordpress-performance-presentation
wordpress-performance-presentation
 
How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04How to Install LAMP in Ubuntu 14.04
How to Install LAMP in Ubuntu 14.04
 

More from Bud Siddhisena

Building apis that don’t suck!
Building apis that don’t suck!Building apis that don’t suck!
Building apis that don’t suck!Bud Siddhisena
 
Why should you android (archived)
Why should you android (archived)Why should you android (archived)
Why should you android (archived)Bud Siddhisena
 
Virtualization, The future of computing (archived)
Virtualization, The future of computing (archived)Virtualization, The future of computing (archived)
Virtualization, The future of computing (archived)Bud Siddhisena
 
Building the Next big thing (archived)
Building the Next big thing (archived)Building the Next big thing (archived)
Building the Next big thing (archived)Bud Siddhisena
 
GNU/Linux for a better home (archived)
GNU/Linux for a better home (archived)GNU/Linux for a better home (archived)
GNU/Linux for a better home (archived)Bud Siddhisena
 
Recipe of a linux Live CD (archived)
Recipe of a linux Live CD (archived)Recipe of a linux Live CD (archived)
Recipe of a linux Live CD (archived)Bud Siddhisena
 
Gaming on linux (archived)
Gaming on linux (archived)Gaming on linux (archived)
Gaming on linux (archived)Bud Siddhisena
 
FOSS in Sri Lanka (archived)
FOSS in Sri Lanka (archived)FOSS in Sri Lanka (archived)
FOSS in Sri Lanka (archived)Bud Siddhisena
 
Contributing to FOSS (archived)
Contributing to FOSS (archived)Contributing to FOSS (archived)
Contributing to FOSS (archived)Bud Siddhisena
 
Choosing your GNU/Linux distribution (archived)
Choosing your GNU/Linux distribution (archived)Choosing your GNU/Linux distribution (archived)
Choosing your GNU/Linux distribution (archived)Bud Siddhisena
 
Beyond desktop/server with GNU/Linux (archived)
Beyond desktop/server with GNU/Linux (archived)Beyond desktop/server with GNU/Linux (archived)
Beyond desktop/server with GNU/Linux (archived)Bud Siddhisena
 
Opensource opportunity
Opensource opportunityOpensource opportunity
Opensource opportunityBud Siddhisena
 
Introduction to firewalls through Iptables
Introduction to firewalls through IptablesIntroduction to firewalls through Iptables
Introduction to firewalls through IptablesBud Siddhisena
 
Secure your IT infrastructure with GNU/Linux
Secure your IT infrastructure  with GNU/LinuxSecure your IT infrastructure  with GNU/Linux
Secure your IT infrastructure with GNU/LinuxBud Siddhisena
 
Kernel Configuration and Compilation
Kernel Configuration and CompilationKernel Configuration and Compilation
Kernel Configuration and CompilationBud Siddhisena
 

More from Bud Siddhisena (20)

JIT qa-docker
JIT qa-dockerJIT qa-docker
JIT qa-docker
 
Building apis that don’t suck!
Building apis that don’t suck!Building apis that don’t suck!
Building apis that don’t suck!
 
Why should you android (archived)
Why should you android (archived)Why should you android (archived)
Why should you android (archived)
 
Virtualization, The future of computing (archived)
Virtualization, The future of computing (archived)Virtualization, The future of computing (archived)
Virtualization, The future of computing (archived)
 
Building the Next big thing (archived)
Building the Next big thing (archived)Building the Next big thing (archived)
Building the Next big thing (archived)
 
GNU/Linux for a better home (archived)
GNU/Linux for a better home (archived)GNU/Linux for a better home (archived)
GNU/Linux for a better home (archived)
 
Recipe of a linux Live CD (archived)
Recipe of a linux Live CD (archived)Recipe of a linux Live CD (archived)
Recipe of a linux Live CD (archived)
 
Gaming on linux (archived)
Gaming on linux (archived)Gaming on linux (archived)
Gaming on linux (archived)
 
FOSS in Sri Lanka (archived)
FOSS in Sri Lanka (archived)FOSS in Sri Lanka (archived)
FOSS in Sri Lanka (archived)
 
Contributing to FOSS (archived)
Contributing to FOSS (archived)Contributing to FOSS (archived)
Contributing to FOSS (archived)
 
Choosing your GNU/Linux distribution (archived)
Choosing your GNU/Linux distribution (archived)Choosing your GNU/Linux distribution (archived)
Choosing your GNU/Linux distribution (archived)
 
Beyond desktop/server with GNU/Linux (archived)
Beyond desktop/server with GNU/Linux (archived)Beyond desktop/server with GNU/Linux (archived)
Beyond desktop/server with GNU/Linux (archived)
 
UX talk
UX talkUX talk
UX talk
 
Opensource opportunity
Opensource opportunityOpensource opportunity
Opensource opportunity
 
Remembering steve
Remembering steveRemembering steve
Remembering steve
 
Introduction to firewalls through Iptables
Introduction to firewalls through IptablesIntroduction to firewalls through Iptables
Introduction to firewalls through Iptables
 
FOSS and Security
FOSS and SecurityFOSS and Security
FOSS and Security
 
Secure your IT infrastructure with GNU/Linux
Secure your IT infrastructure  with GNU/LinuxSecure your IT infrastructure  with GNU/Linux
Secure your IT infrastructure with GNU/Linux
 
Kernel Configuration and Compilation
Kernel Configuration and CompilationKernel Configuration and Compilation
Kernel Configuration and Compilation
 
Foss Gadgematics
Foss GadgematicsFoss Gadgematics
Foss Gadgematics
 

Recently uploaded

Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 

Recently uploaded (20)

Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 

Scale Apache with Nginx

  • 1. Scaling Apache for LAMP Buddhika Siddhisena (Co-Founder & CTO of THINKCube Systems) bud@thinkcube.com | twitter @geekaholic
  • 2. What is Apache The folk story is that Apache was named after "A-patchy-server", which was the result of NCSA httpd server being patched a lot. The project was started by Brian Behlendorf Today, Apache is still the most popular web server out there running more than half of the websites on the net. It is actively developed by the Apache Software Foundation along with many other software projects. Besides its primary function of being a website, Apache can also be configured as a reverse proxy for load balancing.
  • 3. Installing Apache The easiest method of installing Apache along with PHP and MySQL (aka LAMP) is to use the tasksel command. tasksel Alternatively install each package manually: apt-get install apache2 libapache2-mod-php5 mysql-server Installing a sample LAMP app - Drupal In order to test out Apache performance as we tune it, it is good to setup a real world full fledged CMS such as Drupal. Download the latest version of Drupal from drupal.org Follow the Drupl setup guide Install the Devel module into Drupal modules directory Login to Drupal as admin and using the devel plugin, populate Drupal with sample data for testing (Configuration -> Development -> Generate Content)
  • 4. Setting up Benchmarking tools Setup Autobench Autobench is a handy script to stress test a webserver by sending an increasing number of requests. It works by calling the httperf tool iteratively with increasing parameters. Download autobench and follow directions to compile. In order to plot graphs, you need to install gnuplot via apt. As of this writing, the script used to plot the graph has a bug calling the current version of gnuplot and requires the following minor modification. $ sudo vi which bench2graph line ~78 should be echo set style data linespoints >> gnuplot.cmd
  • 5. Baseline benchmark with Autobench Lets benchmark our standard Apache setup to get an idea of default performance. autobench --single_host --host1 localhost --uri1 /drupal --quiet --low_rate 20 --high_rate 200 --rate_step 20 --num_call 10 --num_conn 5000 --timeout 5 --file results.tsv Basically the above will test a single host, localhost/drupal by sending it 20 connections per second, each having 10 requests up to 200 connections per second incrementing by 20. The total number of connections are capped at 5000 while any request that takes more than 5 seconds to respond is considered unsuccessful. Plotting the results Using the result.tsv file and the included bench2graph utility, you can plot a graph into a postscript file. bench2graph results.tsv results.ps
  • 6.
  • 7. Tuning Apache - Enable GZip You can decrease network overhead and make pages load faster, there by reducing the amount of time a client is connected by compressing pages using gzip. All modern browser support rendering compressed files. In order to benchmark its effect, you can install a tool such as Firebug on the client side.
  • 8. Tuning Apache - Enable GZip Enable the mod_deflate module. On Ubuntu : a2enmod deflate && a2enmod headers Then we'll configure deflate to compress everything except images. sudo vi /etc/apache2/modules-enabled/deflate.conf 1 <Location /> 2 # Insert filter 3 SetOutputFilter DEFLATE 4 5 # Don't compress images 6 SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary 7 8 # Make sure proxies don't deliver the wrong content 9 Header append Vary User-Agent env=!dont-vary 10 11 </Location>
  • 9. Apache configuration tuning There are a few key parameters that can be tuned: KeepAlive - By default its set to ON which is good. Clients will make all requests in one shot via http 1.1. KeepAliveTimeout - Better to keep it low. Defaults to 15 sec. Make sure thats enough. Rule is 1.5 to 2 times your page load speed. TimeOut - The default is 5 minutes which might be long to allow for one process. Adjust accordingly. StartServers, MinSpareServers, MaxSpareServers - Generally even on a busy site you may not need to tweak. Apache can self regulate. MaxClients - The maximum number of clients (threads) Apache will handle simultaneously.
  • 10. Calculating MaxClients ps -eafly |grep apache2|awk '{print $8}'|sort -n Use free to figure out how much memory is available. Cache is also considered free memory but you might want to leave some and not assume all cache will be used. free By deviding free memory by the average memory used by an Apache thread, you can estimate the number of MaxClients. e.g: Assuming Apache memory usage and free memory are as follows $ ps -eafly |grep apache2|awk '{print $8}'|sort -n 816 3896 3896 3896 3896 20844 $ free total used free shared buffers cached Mem: 508904 447344 61560 0 141136 213468 -/+ buffers/cache: 92740 416164 Swap: 407544 4364 403180 Memory avail ~= 60000 (free) + 100000 (cached) ~= 160 MB and Memory per thread ~= 4 MB Then a safe value for MaxClients = 40
  • 11. Improving PHP performance We can improve PHP performance by 1. Caching pages (useful if dynamic content doesn't change often) 2. PHP Opcode optimizations (pre-compile php) Fortunately we can get the benefit of both using PHP APC, which is a PHP accellerator! apt-get install php-apc You can verify installation by loading a php page having phpinfo(); and searching for apc. Or if you have php5-cli installed: php -r "phpinfo();" | grep apc Using memcached Memcached is a distributed cache for storing key-value pairs in memory for faster access with reduced trips to the database. Some popular PHP apps can use memcache if available. memcached does not instantly accellerate PHP! apt-get install memcached php5-memcache service memcached start
  • 12. More Tips for improving performance Keep DirectoryIndex file list as short as possible. Whenever possible disable .htaccess via AllowOverride none Use Options FollowSymLinks to simplify file access process in Apache Minimize the use of mod_rewrite or at least complex regexs If logs are unnecessary disable them or log to another server via syslog. For Deny/Allow rules use IPs rather then domains. (prevents superfluous DNS lookups). Do not enable HostnameLookups (DNS is slow). For dynamic sites see if you can separate dynamic vs static content into two servers
  • 13. Scaling Architectures Buddhika Siddhisena (Co-Founder & CTO of THINKCube Systems) bud@thinkcube.com | twitter @geekaholic
  • 14. Architecture overview In terms of scaling the web server there are few options. 1. Single machine (Scale vertically) Basically the easiest to setup. Scaling is a matter of buying a better server or upgrading it! 2. App-DB machines (2-Tier) Separate DB from App, as a result each can be scaled separately.
  • 15. Architecture overview contd... 3. Load balancer + App-DB machines (3-Tier) Load balancer (aka reverse proxy) will route requests betwen multiple backend HTTP servers while caching results.
  • 16. Data Independence It is good to isolate the data from the app by hosting it on a separate server. This was the two aspects can be scaled independantly. Some methods to consider: Store DB data on MYSQL running on a separate server Enable file sharing to share data files using NFS, rsync Clustering MYSQL across multiple servers using mysqlcluster Cluster file system via DRDB, GFS2 or as Facebook does using Bittorrent
  • 17. Setting up an HTTP accellerator using Apache
  • 18. Apache as a reverse proxy In this setup, the reverse server is what the user will contact while the real webserver can be hidden behind a private network. 1. On the reverse proxy server : Enable required modules for caching reverse proxy. a2enmod proxy a2enmod proxy_connect a2enmod proxy_http a2enmod cache 2. Configure proxy module vi /etc/apache2/modules-enabled/proxy.conf 1 <Proxy *> 2 AddDefaultCharset off 3 Order deny,allow 4 Deny from all 5 Allow from all 6 </Proxy> 7 ProxyVia On
  • 19. Apache as a reverse proxy contd... 3. Setup (public) virtual host Next we configure an empty virtual host that is configured to the public site. But instead of showing the document root we do a reverse proxy. vi /etc/apache2/sites-available/public-domain.com 1 <VirtualHost *:80> 2 3 ServerName your-public-domain.com 4 5 <Proxy *> 6 Order deny,allow 7 Allow from all 8 </Proxy> 9 10 ProxyPass / http://your-private-domain.com/ 11 ProxyPassReverse / http://your-private-domain.com/ 12 13 </VirtualHost> a2ensite public-domain.com service apache2 reload
  • 21. What is Nginx? Nginx was designed as a reverse proxy first, and an HTTP server second Unlike Apache, Nginx uses a non blocking process model Two modes of operation for Nginx: 1. Use Nginx for the static content and Apache for PHP 2. Use FastCGI to embed PHP
  • 22. Nginx process model in a nutshell Receive request, trigger events in a process The process handles all the events and returns the output Process handles events in parallel Limitation is PHP can no longer be embedded ( mod_php ) inside process as PHP is not asynchronous Unlike Apache, Nginx doesn't not have an .htaccess equivelant. You need to reload server after making any chage, making it difficult to use for shared hosting
  • 23. Using Nginx and Apache side-by-side In this setup we put Nginx as the frontend http accellerator and Apache as the backend app server. If you want to run this on the same physical server you'll need to either change the Apache port from 80 to another value or bind and Nginx to their own IP addresses with the same server. Listen 8080 or using the ip address Listen 127.0.0.1:8080 Now we're ready to install Nginx sudo apt-get install nginx
  • 24. Apache style virtual host in Nginx Nginx uses a different format for defining virtual hosts than Apahche. 1 <VirtualHost> 2 DocumentRoot "/usr/local/www/mydomain.com" 3 ServerName mydomain.com 4 ServerAlias www.mydomain.com 5 CustomLog /var/log/httpd/mydomain_access.log common 6 ErrorLog /var/log/httpd/mydomain_error.log 7 ... 8 </VirtualHost> becomes... 1 server { 2 root /usr/local/www/mydomain.com; 3 server_name mydomain.com www.mydomain.com; 4 5 # by default logs are stored in nginx's log folder 6 # it can be changed to a full path such as /var/log/... 7 access_log logs/mydomain_access.log; 8 error_log logs/mydomain_error.log; 9 ... 10 }
  • 25. Redirecting all PHP requests to Apache The following example will server all static content via nginx while redirect dynamic content (php) to Apache 1 server { 2 listen 80 default; 3 server_name localhost; 4 5 access_log /var/log/nginx/localhost.access.log; 6 7 location / { 8 root /var/www; 9 index index.html index.htm; 10 } 11 12 ## Parse all .php file in the /var/www directory 13 location ~ .php$ { 14 # these two lines tell Apache the actual IP of the client being forwarded 15 16 proxy_set_header X-Real-IP $remote_addr; 17 proxy_set_header X-Forwarded-For $remote_addr; 18 19 # this next line adds the Host header so that apache knows which vHost to serve 20 21 proxy_set_header Host $host; 22 23 # And now we pass back to apache 24 proxy_pass http://127.0.0.1:8080; 25 26 } 27 }
  • 26. References How To Improve Website Performance (With Drupal, LAMP) PHP Performance tuning HipHop compiler by Facebook How the code you write affects PHP benchmarks Scaling Facebook with OpenSource tools Scaling drupal DRBD for HA distributed file system
  • 27. Thank you bud@thinkcube.com | twitter @geekaholic