Optimizing Apache Performance – Part 2

Optimizing Apache Performance Part 2

In my previous post, you saw some Apache configuration tips to optimize your Apache web server in Ubuntu. Now I am going to show you more Apache performance tuning tips and tricks. This will improve the overall performance of your Apache web server.

Enable mod_deflate Compression

Mod_deflate is an Apache module that allows output from your Apache server to be compressed before being sent to the browser. This will reduce the size of the content that is required to be sent and will decrease the time and the amount of data transmitted to the browser. Mod_deflate module uses gzip compression to compress data, text, HTML, or XML files to approximately 30% of their original sizes.

In Ubuntu 14.04, by default, the mod_deflate module is included and enabled in Apache.

To confirm this, run the following command:

sudo apachectl -t -D DUMP_MODULES | grep deflate

You should see deflate_module (shared) if mod_deflate is enabled.


By default, the gzip compression level is 9. You can set the gzip compression level from 1 to 9 as necessary.

For example, you can set the gzip compression level to 8 by editing the “/etc/apache2/mods-enabled/deflate.conf” file.

sudo nano /etc/apache2/mods-enabled/deflate.conf

Add the following line:

DeflateCompressionLevel 8

Save the file and restart Apache.

sudo /etc/init.d/apache2 restart

Set Up Varnish Cache

Varnish cache is an HTTP accelerator and reverse proxy for speeding up an Apache server. It is used to speed up a website by storing a copy of a page served by an Apache web server. It makes the website load faster because it stores the content in RAM.

Note: the following is only a simple example of how to install, configure and start Varnish with an Apache web server. In most cases, you will have to configure it further to suit your own needs.

Install Varnish:

First, you need to add the varnish repository to install varnish on your system.

You can do this by running the following commands:

sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
sudo echo "deb http://repo.varnish-cache.org/ubuntu/ trust varnish-3.0" >> /etc/apt/sources.list
sudo apt-get update
sudo apt-get install varnish

Configure Varnish Cache:

Varnish will serve the content of the website using port 80 while getting that information from Apache on port 8080.

Now you need to change the varnish configuration file.

sudo nano /etc/default/varnish

Change the lines from

DAEMON_OPTS="-a :6081 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"


DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Save the file and open the “default.vcl” file. This file tells varnish to look for the server content.

sudo nano /etc/varnish/default.vcl

Now tell varnish to get the content on port 8080.

backend default {
    .host = "";
    .port = "8080";

By default, Apache listens on port 80; you need to change this port to 8080.

You can change the Apache listening port from 80 to 8080 by editing the ports.conf file.

sudo  nano /etc/apache2/ports.conf

Change the value from 80 to 8080.

Listen 8080

Save the file and restart Apache and varnish.

sudo /etc/init.d/apache2 restart
sudo  /etc/init.d/varnish restart

Now you can now check the status of varnish by running

sudo varnishstat

Set Up APC

APC (Alternative PHP Cache) is a free open-source opcode caching plugin for PHP that can help speed up your site. It caches the output of the PHP bytecode compiler in shared memory; this will reduce parsing and disk I/O overhead for later requests.

You can install APC using the following command:

sudo apt-get install php-apc

To verify if APC is now enabled, you can create a info.php file.

sudo nano /var/www/html/info.php

Add the following line:

<?php phpinfo(); ?>

Save the file and restart Apache.

sudo /etc/init.d/apache2 restart

Now open that file in the browser. You should see APC enabled as shown in the below image.



Now, you have enough knowledge to tune up an Apache web server for the best performance. You can also experiment with various available options and measure the web server performance by using tools like ab and httperf. Feel free to comment if you have any questions.

Hitesh Jethva
Hitesh Jethva

Over 5 years of experience as IT system administrator for IT company in India. My skills include a deep knowledge of Rehat/Centos, Ubuntu nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, squied proxy, NFS, FTP, DNS, Samba, ldap, Openvpn, Haproxy, Amazon web services, WHMCS, Openstack Cloud, Postfix Mail Server, Security etc.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox