<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hostmedic &#187; apache</title>
	<atom:link href="http://www.hostmedic.com/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hostmedic.com</link>
	<description>Emergency Medicine for Hosting &#38; Server Admins</description>
	<lastBuildDate>Sat, 24 Sep 2011 04:51:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>nginx + apache = happy &amp; fast cPanel server</title>
		<link>http://www.hostmedic.com/admin/network_administration/nginx-apache-happy-fast-cpanel-server/</link>
		<comments>http://www.hostmedic.com/admin/network_administration/nginx-apache-happy-fast-cpanel-server/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 03:30:25 +0000</pubDate>
		<dc:creator>Glenn Kelley</dc:creator>
				<category><![CDATA[cPanel]]></category>
		<category><![CDATA[Network Admin]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[SysAdmin]]></category>
		<category><![CDATA[apache]]></category>

		<guid isPermaLink="false">http://www.hostmedic.com/?p=459</guid>
		<description><![CDATA[Nginx &#8211; the small, lightning fast and very efficient web server is usually used to serve static content or as a reverse proxy/load balancer for Apache.  Till now &#8211; the issue has been that many folks have not figured out how to use nginx due to their control panels lack of support. Case in point [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.hostmedic.com%2Fadmin%2Fnetwork_administration%2Fnginx-apache-happy-fast-cpanel-server%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.hostmedic.com%2Fadmin%2Fnetwork_administration%2Fnginx-apache-happy-fast-cpanel-server%2F&amp;source=churchmedic&amp;style=normal&amp;service=TinyURL.com&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.nginx.net/" target="_blank"><span style="font-family: arial,sans-serif;"><img class="alignright" src="http://nginx.net/nginx.gif" alt="" width="121" height="32" /></span></a><span style="font-family: arial,sans-serif;"><a href="http://www.nginx.net/" target="_blank">Nginx</a> &#8211; the small, lightning fast and very efficient web server is usually used to serve static content or as a reverse proxy/load balancer for Apache.  Till now &#8211; the issue has been that many folks have not figured out how to use nginx due to their control panels lack of support. </span></p>
<p><span style="font-family: arial,sans-serif;">Case in point -<a href="http://www.cpanel.net" target="_blank"> cPanel</a>.</span></p>
<p><span style="font-family: arial,sans-serif;">cPanel is an awesome control panel &#8211; and while not the least expensive &#8211; the support of Dan Muey and his team is second to none !</span></p>
<p><span style="font-family: arial,sans-serif;">the cPanel forums like many &#8211; are host to flamings &#8211; but what is odd &#8211; unlike all the others &#8211; even the flames come with solutions &#8211; the community lacks nothing &#8211; except &#8230; nginx and openldap (room for another posting later)&#8230;</span></p>
<p>In order to get the cPanel server ready for nginx &#8211; you must first install an apache module called mod_rpaf  (written by Thomas Eibner &#8211; and he deserves some serious kudos for his excellent work.)</p>
<p>Mod_rpaf will in short &#8211; allow for apache to see the visitors ip address rather than the ip address of the nginx front end running on your server. &#8211; In short &#8211; failure to include this simple module will break the ability for your users to have meaningful logs.</p>
<p><span style="font-family: arial,sans-serif;"><br />
Download (from here: </span>http://stderr.net/apache/rpaf/ ) <span style="font-family: arial,sans-serif;">untar, cd to the newly created directory and run this command as root:</span></p>
<blockquote><p><span style="font-family: arial,sans-serif;"><strong>/usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c</strong><br />
</span></p></blockquote>
<p><span style="font-family: arial,sans-serif;">Doing so will install the module into the Apache module directory.</span></p>
<p>Then in your  Web Host Manager (WHM) follow the tree here:  <strong>Main &gt;&gt; Service Configuration &gt;&gt; Apache Configuration &gt; Include Editor &gt; Pre Main Include</strong> and add this section there, replacing <strong>(place your ips here w/o the brakets)</strong> with the list of IP addresses on this Cpanel server:</p>
<blockquote><p><span style="font-family: arial,sans-serif;"> </span></p>
<pre style="padding-left: 60px;">LoadModule rpaf_module modules/mod_rpaf-2.0.so 

RPAFenable On
# Enable reverse proxy add forward

RPAFproxy_ips 127.0.0.1  (place your ips here w/o the brakets) 

RPAFsethostname On
# let rpaf update vhost settings allowing to have
# the same hostnames as in the "actual" configuration for the
# forwarding apache installation

RPAFheader X-Real-IP
# Allows you to change which header we have mod_rpaf looking for
# when trying to find the ip the that is forwarding our requests</pre>
<p><span style="font-family: arial,sans-serif;"> </span></p></blockquote>
<p>Once this is completed &#8211; we are ready to move Apache to another port, let&#8217;s take 81 for example.  This &#8211; thank goodness to the excellent work of the cPanel team in coding an excellent product is quite simple.  In WHM navigate to the &#8220;Tweak Settings&#8221; page and replace</p>
<blockquote>
<p style="padding-left: 30px;"><em>0.0.0.0:80</em> with <em>0.0.0.0:81</em></p>
</blockquote>
<p>While many like doing it the gui method &#8211; others might like using the command line interface (cli)  -so instructions are posted here for those wishing to complete the task that way instead.</p>
<blockquote>
<p style="padding-left: 30px;">vi <strong>/var/cpanel/cpanel.config</strong> and change port 80 in <em>apache_port</em> assignment to 81:  apache_port=0.0.0.0:81</p>
</blockquote>
<p>Next &#8211; you need to Run<strong>: </strong></p>
<blockquote><p><strong>/usr/local/cpanel/whostmgr/bin/whostmgr2 &#8211;updatetweaksettings</strong></p>
<p>Next &#8211; check <strong>/usr/local/apache/conf/httpd.conf</strong> for any occurrences of port 80, and run <strong>/scripts/rebuildhttpdconf</strong> to make sure your httpd.conf file is up to date.</p></blockquote>
<p>Since Nginx is going to be in the front feeding the data requested from browsers to the sites you host &#8211; you can now reduce the number of Apache children.   This is done by editing <span style="font-family: arial,sans-serif;"> <strong>/usr/local/apache/conf/httpd.conf</strong> and replacing the <em>prefork.c</em> section with settings similar to those below&#8230;  It is important to note these values may need to be tweaked to fit your needs:</span></p>
<blockquote><p><span style="font-family: arial,sans-serif;"> </span></p>
<pre>&lt;IfModule prefork.c&gt;
    StartServers 8
    MinSpareServers 2
    MaxSpareServers 5
    MaxClients 80
    MaxRequestsPerChild 0
&lt;/IfModule&gt;</pre>
</blockquote>
<p><span style="font-family: arial,sans-serif;">Make sure to run <strong>/usr/local/cpanel/bin/apache_conf_distiller &#8211;update &#8211;main</strong> so that the changes are picked up and then run  <strong>/scripts/rebuildhttpdconf</strong></span></p>
<p>I promise &#8211; were almost there next &#8211; we are going to move onto the NGINX settings:</p>
<p><span style="font-family: arial,sans-serif;">In this final step we are going to build the nginx configuration files based on the domains hosted on your cpanel server.  There are two files: </span><span style="font-family: arial,sans-serif;"><strong>/usr/local/nginx/conf/nginx.conf</strong> which is the main configuration file &#8211; and the include file with all virtual hosts: <strong>/usr/local/nginx/conf/vhost.conf</strong></span></p>
<blockquote><p><span style="font-family: arial,sans-serif;"> </span></p>
<pre>#!/bin/sh

cat &gt; "/usr/local/nginx/conf/nginx.conf" &lt;&lt;EOF
user  nobody;
# no need for more workers in the proxy mode
worker_processes  1;

error_log  logs/error.log info;

worker_rlimit_nofile  8192;

events {
 worker_connections  512; # you might need to increase this setting for busy servers
 use rtsig; #  Linux kernels 2.6.x change to epoll
}

http {
 server_names_hash_max_size 2048;

 include    mime.types;
 default_type  application/octet-stream;

 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;

 keepalive_timeout  10;

 gzip on;
 gzip_min_length  1100;
 gzip_buffers  4 32k;
 gzip_types    text/plain text/html application/x-javascript text/xml text/css;
 ignore_invalid_headers on;

 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 connection_pool_size  256;
 client_header_buffer_size 4k;
 large_client_header_buffers 4 32k;
 request_pool_size  4k;
 output_buffers   4 32k;
 postpone_output  1460;

 include "/usr/local/nginx/conf/vhost.conf";
}

EOF

/bin/cp /dev/null /usr/local/nginx/conf/vhost.conf

cd /var/cpanel/users
for USER in *; do
 for DOMAIN in `cat $USER | grep ^DNS | cut -d= -f2`; do
  IP=`cat $USER|grep ^IP|cut -d= -f2`;
  ROOT=`grep ^$USER: /etc/passwd|cut -d: -f6`;
  echo "Converting $DOMAIN for $USER";

  cat &gt;&gt; "/usr/local/nginx/conf/vhost.conf" &lt;&lt;EOF
   server {
  access_log off;

  error_log  logs/vhost-error_log warn;
  listen    80;
  server_name  $DOMAIN www.$DOMAIN;

  # uncomment location below to make nginx serve static files instead of Apache
  # NOTE this will cause issues with bandwidth accounting as files wont be logged
  #location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ {
  # root   $ROOT/public_html;
  #}

  location / {
   client_max_body_size    10m;
   client_body_buffer_size 128k;

   proxy_send_timeout   90;
   proxy_read_timeout   90;

   proxy_buffer_size    4k;
   # you can increase proxy_buffers here to suppress "an upstream response
   #  is buffered to a temporary file" warning
   proxy_buffers     16 32k;
   proxy_busy_buffers_size 64k;
   proxy_temp_file_write_size 64k;

   proxy_connect_timeout 30s;

   proxy_redirect  http://www.$DOMAIN:81   http://www.$DOMAIN;
   proxy_redirect  http://$DOMAIN:81   http://$DOMAIN;

   proxy_pass   http://$IP:81/;

   proxy_set_header   Host   \$host;
   proxy_set_header   X-Real-IP  \$remote_addr;
   proxy_set_header   X-Forwarded-For \$proxy_add_x_forwarded_for;
  }
 }
EOF
 done
done</pre>
</blockquote>
<p><span style="font-family: arial,sans-serif;">Now &#8211; Run <strong>/usr/local/nginx/sbin/nginx -t</strong> to check the configuration, and then <strong>/usr/local/nginx/sbin/nginx</strong> to start nginx.<br />
</span></p>
<p><span style="font-family: arial,sans-serif;"><br />
We have setup a hook to the cPanel scripts to rerun this script anytime a new account is created as well as anytime a domain is parked however we will leave that for another post.<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hostmedic.com/admin/network_administration/nginx-apache-happy-fast-cpanel-server/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

