Running with Ruby

Category: PHP (page 1 of 6)

Apache (Passenger) Maintenance mode for Ruby on Rails application with Capistrano

Same of Nginx and Puma: Nginx (and Puma behind) maintenance mode for Rack/Rails applications with Capistrano

There is a time, when we need to switch our apps into maintenance mode. Maybe it is because of some data processing stuff, maybe because of backups, Capistrano or whatever good reason you might have. To be honest it doesn’t matter why. What does matter, is how we should handle working users of our apps. Of course all the downtimes should take place when there is the smallest amount of users online. In most cases it might be a good idea to switch application off in the middle of the night (or on Sunday, etc.), but this won’t solve our primary problem: what should we show users that are already online?

The worst scenario ever would be showing them nothing (for example by shutting down whole application server). Users probably will think, that something bad happened. Much better idea is to show users a maintenance page with some sort of information like “Temporary down for maintenance”. It would be even better, it such a page would automatically show when needed.

To do so, we can use Apache Mod Rewrite module and a static 503.html page.

Mod Rewrite for Maintenance mode detection

How to determine if we are in maintenance mode? Let’s check if maintenance.txt file exists in tmp/ dir of our app:

RewriteCond %{DOCUMENT_ROOT}/../tmp/maintenance.txt !-f

When it exists, we need to redirect user to our 503.html static page:

RewriteRule ^(.*)$ /503.html [NC,R,L]

Of course the whole .htaccess should include also enabling RewriteEngine, ignoring redirects of CSS files and redirecting from 503.html to root, when the maintenance is off:

RewriteEngine On
# Set error 503 static page
ErrorDocument 503 /503.html

# Don't redirect when someone requests assets used in 503.html
RewriteCond %{REQUEST_URI} !^/assets/layouts/portal/favicon.ico$
RewriteCond %{REQUEST_URI} !^/assets/libraries/bootstrap/bootstrap.min.css$
RewriteCond %{REQUEST_URI} !^/assets/layouts/portal/application.css$
RewriteCond %{REQUEST_URI} !^/503.html$
RewriteCond %{DOCUMENT_ROOT}/../tmp/maintenance.txt -f
# Set 503 status for all requests
RewriteRule ^(.*)$ /503.html [NC,R=503,L]

# When it is not maintenance mode redirect to root_path from 503.html
RewriteCond %{DOCUMENT_ROOT}/../tmp/maintenance.txt !-f
RewriteCond %{REQUEST_URI} ^/503.html
RewriteRule ^503.html http://www.project.domain/ [R=302,L]

Of course, you need to remember to create your own 503.html file, put it in public/ dir of your project and customize all the htaccess rules based on your needs.

Capistrano hookup

To automate enabling and disabling my projects, I use a set of simple Capistrano tasks, enclosed in an Apache namespace:

namespace :apache do

  desc 'Restarts the current Passenger project'
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"

  desc 'Sets project server in dev mode - so the 503 page is served'
  task :lock do
    run "touch #{current_path}/tmp/maintenance.txt"

  desc 'Sets project to a standard mode'
  task :unlock do
    run "rm -f #{current_path}/tmp/maintenance.txt"


Usage example:

before 'deploy:update' do

after 'deploy:update' do

That’s all. Good luck and as few maintenance downtime as possible! P.S. With a bit of modifications, this code might be used also for PHP/Python Passenger based projects.

Prosta (bardzo) księga gości w PHP

Przyszło mi dzisiaj napisać możliwie najprostszą możliwą księgę gości w PHP. Stwierdziłem, że się z wami podzielę :) Miała nie korzystać z bazy danych i nie mieć walidacji :) także jest naprawdę prosta. Na dodatek całość zawiera się aż w jednym pliku.

Najpierw zdefiniujemy sobie stałą, w której będziemy trzymać nazwę pliku przechowującego wpisy:

define("DB_FILE_NAME", 'baza.dat');

Następnie stworzymy metodę, która sprawdzi czy plik DB_FILE_NAME istnieje, a jeśli nie – to go utworzy:

function gb_init(){
  // sprawdzamy czy plik istnieje
  if (!file_exists(DB_FILE_NAME)){
    // jesli nie, to go tworzymy
    $f = fopen(DB_FILE_NAME, 'w');

Następnie utworzymy metodę która będzie dodawać nam wpisy. Jeśli ktoś nie będzie miał nicka, to jako nick wpisze “Gość”:

function add_entry($nick, $tresc){
  $nick = HTMLSpecialChars($nick);
  $tresc = HTMLSpecialChars($tresc);
  if($nick=="")  $nick =  "Gość";
  $date = date("j.m.Y G:i");
  $text = ereg_replace("\n", "<br />", $tresc);
  $wynik = '<b>'. $nick . '</b>'. "     " . $date . '|||' .$text.  "\n";

  $fp = fopen(DB_FILE_NAME, "a");
  fwrite ($fp, $wynik);

Następnie całość inicjujemy (przed htmlem – ponieważ to jest obsługa dodawania wpisów, a nie ich wyświetlania):

  if ($_POST['nick'] && $_POST['tresc']){
    add_entry($_POST['nick'], $_POST['tresc']);

Teraz pozostaje nam wyświetlić pliki:

$myFile = fopen(DB_FILE_NAME, "r");
$i = -1;
while (!feof($myFile)) {
  $myLine = fgets($myFile, 2000);

echo "Ilość wpisów w Księdze: $i<br /><br />";
// ładujemy pliki w odwrotnej kolejnosci
$file = array_reverse(file('baza.dat'));
// I wyświetlamy rozdzielajac dane - seperator to |||
foreach ($file as $key => $value) {
  list($imie, $tresc) = explode("|||", $value);
  echo $imie . '<br />';
  echo $tresc . '<br /><br />';
  echo '<hr>';

Pozostaje nam jeszcze tylko formularz dodawania wpisów:

<form method="post" action="ksiega.php?id=zapisz">
   Imię: <input type="text" name="nick" /><br />
   Treść:<br />
    <textarea cols="35" name="tresc" rows="12"></textarea>  <br />
   <input type="submit" value="Wyślij"/>

Copyright © 2018 Running with Ruby

Theme by Anders NorenUp ↑