Blog

Enable keep-alive in Apache

What is keep alive?

keep-alive-enabled

Keep alive is a method to allow the same tcp connection for HTTP conversation instead of opening a new one with each new request. More simply put, it is a communication between the web server and the web browser that says “you can grab more than just one file at a time”. Keep alive is also known as a persistant connection.

– Source

Advantages of KeepAlive

  • Improves website speed: It reduces latency associated with HTTP transfers and delivers a better user experience.
  • Reduces CPU usage: On the server side enabling KeepAlive reduces CPU usage. Consider that a typical web page has dozens of different files such as images, stylesheets, javascript files etc. If KeepAlive is disabled a separate connection must be made for each of those files. Creating and closing connections has an overhead and doing it for every single file wastes CPU time.

Disadvantages of Keepalive

  • Increases memory usage: Enabling KeepAlive  increases memory usage on the server. Apache processes have to keep connections open waiting for new requests from established connections. While they are waiting they are occupying RAM that could be used to service other clients. If you turn off KeepAlive fewer apache processes will remain active. This will lower memory usage and allow Apache to serve more users.

Source

Edit httpd.conf.

vi /etc/httpd/conf/httpd.conf

Edit the following section – KeepAlive On.

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 100

Restart httpd.

/etc/init.d/httpd restart

Browser Caching via .htaccess

I added simple browser caching through my .htaccess file.

vi .htaccess

IMPORTANT: Add the following to the top of the file.

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

Enable mod_deflate On CentOS 6 (Enable Compression)

Add to .htaccess.

## ENABLE COMPRESSION ##
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/opentype

# For Olders Browsers Which Can't Handle Compression
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
## ENABLE COMPRESSION ##

WordPress 5 Minute Install

This setup assumes that you are running a web server and mysql on the same server.

Step 1

cd into your root directory.

cd /your/root/directory

Step 2

Download and unzip.

wget https://wordpress.org/latest.tar.gz && tar -xzvf latest.tar.gz && rm -rf latest.tar.gz && cd wordpress && mv * .. && cd .. && rm -rf wordpress

Step 3

Create database and user within mysql.

Note: Update database_name, username and password fields.

mysql -u root -p
mysql> CREATE DATABASE database_name;
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON databasename.* TO '<username>'@'localhost';

Step 4

Complete WordPress install.

http://example.com/wp-admin/install.php

You should see the following screen (click Let’s go!).

wordpess-setup01

Fill out Database Name, User Name and Password. Leave the rest as default.

wordpess-setup02

Once you’ve filled out your database information, it will prompt you to create a  wp-config.php file within your root directory. Copy and paste the auto-generated code into wp-config.php and save it.

vi wp-config.php

Step 5

Setup Permalinks.

This is not really part of the install steps, but I’m adding it here because I think it’s pretty important and convenient to setup at this point of the installation.

Once logged in to WordPress, go to Settings > Permalinks.

Select Day and name.

wordpress-permalink

You will need to create an .htaccess file within your root directory and copy the code from the bottom of the page.

vi .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

That’s it!

WordPress Full Width Page Template twentysixteen-child

If you’ve read my previous blogs, you’d know I am using a child theme. I noticed that the theme didn’t come with a full width page template, so here are the simple steps to create one within your child theme.

Step 1

Copy page.php from the twentysixteen folder into your twentysixteen-child theme folder as a new file named page-wide.php.

cp /var/www/itsmetommy.com/public_html/wp-content/themes/twentysixteen/page.php /var/www/itsmetommy.com/public_html/wp-content/themes/twentysixteen-child/page-wide.php

Step 2

Edit page-wide.php within your child theme folder.

Add the following at the top of the page – remove all other lines between /** and */.

/**
Template Name: Wide Template
*/

Delete the following line.

<?php get_sidebar(); ?>

Change the class from content-area to content-area-wide.

<div id="primary" class="content-area-wide">

Step 3

Add the following to style.css within your child theme folder.

.content-area-wide {
 float: left;
 margin-right: -100%;
 width: 100%;
 }

You will now have the option to choose Wide Template within WordPress Pages.

wordpress-wide-template