Posts Tagged ‘apache’

linux server memory management

February 27th, 2011 Comments off

It has been long time since last post, but finally something useful shown up, today I want to present simple script for monitoring memory in Linux.

But first happy news: our server is now sucessfully migrated to nginx, no more apache, now maximal memory usage is 20MB per process.

So here is script, it is almost simple, find some processes, and kill them if to much RAM used:


memory=$(free -m | awk '{if (FNR==2) print $2}')
count=$(ps aux | awk -v threshold=$threshold '{if (FNR==1 || $4>=threshold) print}' | tee $log | wc -l)
[ $count -gt 1 ] && echo "Showing processes over ${threshold_mb}MB, killing processes over ${kill_at_mb}MB" && cat $log

cat $log | awk -v kill_at=$kill_at '{if ($4>kill_at) print $2" "$4 }' | while read pid mem;
used_mb=$(($(echo $mem | sed 's/\.//')*memory/1000))
# fix rounding problem in awk
if [ $used_mb -gt $kill_at_mb ]; then
kill -9 $pid
echo "pid $pid used ${used_mb}MB - killed"
rm -f $log

Most important in this script are first two variables, threshold_mb which is a limit for showing processes (it is rough comparison) and kill_at_mb which is limit for killing processes, by default this limit is 5 times bigger then showing threshold. Cause killing is more important operation additional check for used memory is more strict to be sure, only processes over the limit are killed.

Save it to disk and in crontab:

sudo crontab -e

add the following lines:

MAILTO=<your @email>
* * * * * /root/bin/

That’s all, now add some filter in your mailbox to ignore mails form server – this is all automatic, but you might want to have this for later check if some important processes are missing.

Preparing Ruby on Rails environment – using apache for development

February 6th, 2009 Comments off

In previous post we have created first rails application, it was tested using WEBrick which is part of rails environment. To have environment more similar to server environment and to get rid of keeping track of started projects we will switch to running applications with apache and passenger. There are some benefits of this configuration:

  1. Possibility to run all applications with only one server,
  2. Easy to share applications on local network or even to Internet,
  3. Fast detection of problems with compatibility to passenger,
  4. There are probably more benefits, If You know them leave me a comment.

So lets start with configuration, on beginning turn off apache:

/etc/init.d/apache2 stop

As a central point of configuration we will use ~/public in my case it is /home/mpapis/public, we need to create this directory:

mkdir /home/mpapis/public

Configure Your /etc/hosts to have some meaningful name for running it in the browser, so edit /etc/hosts and modify Your localhost line to look similar to this one (added rails):       localhost  rails

We need to configure apache to respond to our requests, first remove or backup default vhost configuration:

cd /etc/apache2/vhosts.d

mv 00_default_vhost.conf 00_default_vhost.conf.old

After getting rid of default configuration we can edit apache/passenger configuration file created in one of previous posts /etc/apache2/vhosts.d/01_passenger.conf:

LoadModule passenger_module /usr/lib64/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/
PassengerRoot /usr/lib64/ruby/gems/1.8/gems/passenger-2.0.6
PassengerRuby /usr/bin/ruby18

UseCanonicalName Off
DocumentRoot /home/mpapis/public
ServerName rails
ServerAlias localhost
RailsEnv development
Include /home/mpapis/public/paths.conf
<Directory “/home/mpapis/NetBeansProjects”>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
<Directory “/home/mpapis/public”>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all

One of the lines defines that we will use /home/mpapis/public/paths.conf as part of the configuration, and this is the contents of this file:

RailsBaseURI /RailsApplication1

As we pointed /home/mpapis/public in previous file as document root and /RailsApplication1 as path of our application we need to create symbolic link to our application:

ln -s /home/mpapis/NetBeansProjects/RailsApplication1/public/ /home/mpapis/public/RailsApplication1

Last step is to notify our application that it will no more run from the / path, so we add the following entry to /home/mpapis/NetBeansProjects/RailsApplication1/config/environments/development.rb:

config.action_controller.relative_url_root = “/RailsApplication1”

That’s all, we can start appache now:

/etc/init.d/apache2 start

And if it is desired add it to autostart (to have our applications available just after system boot):

rc-update add apache2 default

Finally we can check work of our application by navigating in web browser window to the following path:


Looks pretty nice, in next step we will need to automate this a bit to avoid manual work each new project:

Rails try to follow DRY which stands for “don’t repeat yourself”.

Categories: Development, Linux Tags: , , , ,

Preparing Ruby on Rails environment – apache, ruby

January 24th, 2009 Comments off

In previous article I have described how to setup postgress for development, now is time to continue with the environment. At the beginning we need:

  • ruby as it is base for rails,
  • apache For serving pages,
  • java + netbeans for development.

So installation of required system packages:

emerge -qDNu ruby rubygems apache jdk

/etc/init.d/apache2 start

Install ruby gems that will be needed shortly:

UPDATE: 2009.01.26 added postgres

gem install rails capistrano capistrano-ext passenger postgres

Now we need to integrate passenger into apache – please run:


UPDATED 2009-02-06:

The installation should go without nay problems (If You followed all instructions), at the end You will be asked to modify apache configuration file – few Lines of the output – starting with LoadModule, PassengerRoot and PassengerRuby – should be copied to new apache configuration file, easiest way is to use this commands:

echo “LoadModule passenger_module /usr/lib64/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/” > /etc/apache2/vhosts.d/01_passenger.conf

echo “PassengerRoot /usr/lib64/ruby/gems/1.8/gems/passenger-2.0.6” >> /etc/apache2/vhosts.d/01_passenger.conf

echo “PassengerRuby /usr/bin/ruby18” >> /etc/apache2/vhosts.d/01_passenger.conf

Some of the lines may differ from the output form yours passenger so please use Yours as it is better for you.

In next part we will continue with development environment.

domain redirect with mod_rewrite

January 16th, 2009 Comments off

There are at least two methods of an Internet address/domain to point to another location:

  • writing small frame and hosting it under originally address/domain – this way our original source will be hidden from visitors eyes.
  • writing small mod_rewrite rule to forward originally address/domain to another one, this is the case if we like both domain names, but think that the second one is more important.

Rules of modrewrite can be writen almost everywhere in apache configuration, but most convinient places are:

  • directory vhosts.d in apache configuration directory under etc, this location varies between Linux distributions but most likely it is something like /etc/apache2/vhosts.d/
  • file .htaccess that can be found at yours project directory served over apache, this can be something like /srv/www/

In all locations You have to write the same content

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} www\.niczsoft\.com [NC]
RewriteRule . [NC]

With this rule I have asked my apache to answer all requests with Please take care for escaping dots in RewriteCond it is required as dot would be matched as any character. In the given example NC stands for no case, so and WwW.NICZSoft.CoM will match and return the requesting person to