Skip to content

Commit bababa3

Browse files
committed
fully functional
1 parent f7eeec5 commit bababa3

8 files changed

+346
-0
lines changed

Dockerfile

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#Drupal
2+
3+
FROM ubuntu
4+
5+
RUN echo 'deb http://archive.ubuntu.com/ubuntu precise main universe' > /etc/apt/sources.list
6+
RUN echo 'deb http://archive.ubuntu.com/ubuntu precise-updates universe' >> /etc/apt/sources.list
7+
RUN apt-get update
8+
9+
#Prevent daemon start during install
10+
RUN echo '#!/bin/sh\nexit 101' > /usr/sbin/policy-rc.d && chmod +x /usr/sbin/policy-rc.d
11+
12+
#Supervisord
13+
RUN apt-get install -y supervisor && mkdir -p /var/log/supervisor
14+
CMD ["/usr/bin/supervisord", "-n"]
15+
16+
#SSHD
17+
RUN apt-get install -y openssh-server && mkdir /var/run/sshd && echo 'root:root' |chpasswd
18+
19+
#Utilities
20+
RUN apt-get install -y vim less ntp net-tools inetutils-ping curl git
21+
22+
#All pkgs required by Drupal
23+
RUN apt-get -y install git mysql-client mysql-server apache2 libapache2-mod-php5 pwgen python-setuptools vim-tiny php5-mysql php-apc php5-gd php5-memcache memcached php-pear mc varnish
24+
25+
#Drush
26+
RUN pear channel-discover pear.drush.org && pear install drush/drush
27+
28+
#Install Drupal
29+
RUN rm -rf /var/www/ ; cd /var ; drush dl drupal ; mv /var/drupal*/ /var/www/
30+
RUN chmod a+w /var/www/sites/default ; mkdir /var/www/sites/default/files ; chown -R www-data:www-data /var/www/
31+
32+
#Varnish
33+
ADD ./drupal.vcl /etc/varnish/drupal.vcl
34+
ADD ./status.php /var/www/status.php
35+
36+
#Cleanup agt-get to reduce disk
37+
RUN apt-get clean
38+
39+
#Configurations
40+
41+
#Apache
42+
RUN sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/sites-available/default
43+
RUN a2enmod rewrite vhost_alias
44+
ENV APACHE_RUN_USER www-data
45+
ENV APACHE_RUN_GROUP www-data
46+
ENV APACHE_LOG_DIR /var/log/apache2
47+
48+
#Supervisor starts everything
49+
ADD ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
50+
51+
#MySql
52+
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
53+
RUN supervisord & sleep 3 && mysql -e "CREATE DATABASE drupal; GRANT ALL ON drupal.* TO 'drupal'@'localhost';" && cd /var/www/ && drush site-install standard -y --account-name=admin --account-pass=admin --db-url="mysql://drupal@localhost:3306/drupal" && cd /var/www && drush dl varnish memcache && drush en varnish memcache memcache_admin -y && drush vset cache 1 && drush vset page_cache_maximum_age 3600 && drush vset varnish_version 3 && mysqladmin shutdown
54+
55+
#Drupal Settings
56+
ADD ./settings.php.append /tmp/settings.php.append
57+
RUN cat /tmp/settings.php.append >> /var/www/sites/default/settings.php
58+
59+
EXPOSE 80 22 6081
60+
61+

build

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker build -t="drupal" .

drupal.vcl

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#
2+
# Customized VCL file for serving up a Drupal site with multiple back-ends.
3+
#
4+
# For more information on this VCL, visit the Lullabot article:
5+
# http://www.lullabot.com/articles/varnish-multiple-web-servers-drupal
6+
#
7+
8+
# Define the internal network subnet.
9+
# These are used below to allow internal access to certain files while not
10+
# allowing access from the public internet.
11+
acl internal {
12+
"127.0.0.1";
13+
}
14+
15+
# Define the list of backends (web servers).
16+
# Port 80 Backend Servers
17+
backend web1 { .host = "127.0.0.1"; .probe = { .url = "/status.php"; .interval = 5s; .timeout = 1s; .window = 5;.threshold = 3; }}
18+
19+
# Define the director that determines how to distribute incoming requests.
20+
director default_director round-robin {
21+
{ .backend = web1; }
22+
}
23+
24+
# Respond to incoming requests.
25+
sub vcl_recv {
26+
set req.backend = default_director;
27+
28+
# Use anonymous, cached pages if all backends are down.
29+
if (!req.backend.healthy) {
30+
unset req.http.Cookie;
31+
}
32+
33+
# Allow the backend to serve up stale content if it is responding slowly.
34+
set req.grace = 6h;
35+
36+
# Do not cache these paths.
37+
if (req.url ~ "^/status\.php$" ||
38+
req.url ~ "^/update\.php$" ||
39+
req.url ~ "^/ooyala/ping$" ||
40+
req.url ~ "^/admin/build/features" ||
41+
req.url ~ "^/info/.*$" ||
42+
req.url ~ "^/flag/.*$" ||
43+
req.url ~ "^.*/ajax/.*$" ||
44+
req.url ~ "^.*/ahah/.*$") {
45+
return (pass);
46+
}
47+
48+
# Pipe these paths directly to Apache for streaming.
49+
if (req.url ~ "^/admin/content/backup_migrate/export") {
50+
return (pipe);
51+
}
52+
53+
# Do not allow outside access to cron.php or install.php.
54+
if (req.url ~ "^/(cron|install)\.php$" && !client.ip ~ internal) {
55+
# Have Varnish throw the error directly.
56+
error 404 "Page not found.";
57+
# Use a custom error page that you've defined in Drupal at the path "404".
58+
# set req.url = "/404";
59+
}
60+
61+
# Handle compression correctly. Different browsers send different
62+
# "Accept-Encoding" headers, even though they mostly all support the same
63+
# compression mechanisms. By consolidating these compression headers into
64+
# a consistent format, we can reduce the size of the cache and get more hits.=
65+
# @see: http:// varnish.projects.linpro.no/wiki/FAQ/Compression
66+
if (req.http.Accept-Encoding) {
67+
if (req.http.Accept-Encoding ~ "gzip") {
68+
# If the browser supports it, we'll use gzip.
69+
set req.http.Accept-Encoding = "gzip";
70+
}
71+
else if (req.http.Accept-Encoding ~ "deflate") {
72+
# Next, try deflate if it is supported.
73+
set req.http.Accept-Encoding = "deflate";
74+
}
75+
else {
76+
# Unknown algorithm. Remove it and send unencoded.
77+
unset req.http.Accept-Encoding;
78+
}
79+
}
80+
81+
# Always cache the following file types for all users.
82+
if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9\=\.]+)?$") {
83+
unset req.http.Cookie;
84+
}
85+
86+
# Remove all cookies that Drupal doesn't need to know about. ANY remaining
87+
# cookie will cause the request to pass-through to Apache. For the most part
88+
# we always set the NO_CACHE cookie after any POST request, disabling the
89+
# Varnish cache temporarily. The session cookie allows all authenticated users
90+
# to pass through as long as they're logged in.
91+
if (req.http.Cookie) {
92+
set req.http.Cookie = ";" + req.http.Cookie;
93+
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
94+
set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|NO_CACHE)=", "; \1=");
95+
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
96+
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
97+
98+
if (req.http.Cookie == "") {
99+
# If there are no remaining cookies, remove the cookie header. If there
100+
# aren't any cookie headers, Varnish's default behavior will be to cache
101+
# the page.
102+
unset req.http.Cookie;
103+
}
104+
else {
105+
# If there is any cookies left (a session or NO_CACHE cookie), do not
106+
# cache the page. Pass it on to Apache directly.
107+
return (pass);
108+
}
109+
}
110+
}
111+
112+
# Routine used to determine the cache key if storing/retrieving a cached page.
113+
sub vcl_hash {
114+
# Include cookie in cache hash.
115+
# This check is unnecessary because we already pass on all cookies.
116+
# if (req.http.Cookie) {
117+
# set req.hash += req.http.Cookie;
118+
# }
119+
}
120+
121+
# Code determining what to do when serving items from the Apache servers.
122+
sub vcl_fetch {
123+
# Don't allow static files to set cookies.
124+
if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9\=\.]+)?$") {
125+
# beresp == Back-end response from the web server.
126+
unset beresp.http.set-cookie;
127+
}
128+
129+
# Allow items to be stale if needed.
130+
set beresp.grace = 6h;
131+
}
132+
133+
# In the event of an error, show friendlier messages.
134+
sub vcl_error {
135+
# Redirect to some other URL in the case of a homepage failure.
136+
#if (req.url ~ "^/?$") {
137+
# set obj.status = 302;
138+
# set obj.http.Location = "http://backup.example.com/";
139+
#}
140+
141+
# Otherwise redirect to the homepage, which will likely be in the cache.
142+
set obj.http.Content-Type = "text/html; charset=utf-8";
143+
synthetic {"
144+
<html>
145+
<head>
146+
<title>Page Unavailable</title>
147+
<style>
148+
body { background: #303030; text-align: center; color: white; }
149+
#page { border: 1px solid #CCC; width: 500px; margin: 100px auto 0; padding: 30px; background: #323232; }
150+
a, a:link, a:visited { color: #CCC; }
151+
.error { color: #222; }
152+
</style>
153+
</head>
154+
<body onload="setTimeout(function() { window.location = '/' }, 5000)">
155+
<div id="page">
156+
<h1 class="title">Page Unavailable</h1>
157+
<p>The page you requested is temporarily unavailable.</p>
158+
<p>We're redirecting you to the <a href="/">homepage</a> in 5 seconds.</p>
159+
<div class="error">(Error "} + obj.status + " " + obj.response + {")</div>
160+
</div>
161+
</body>
162+
</html>
163+
"};
164+
return (deliver);
165+
}
166+
167+
sub vcl_deliver {
168+
if (obj.hits > 0) {
169+
set resp.http.V-Cache = "HIT";
170+
} else {
171+
set resp.http.V-Cache = "MISS";
172+
}
173+
}

run

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker run -p 49800:80 -p 49801:6081 drupal

settings.php.append

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#Varnish
2+
$conf['cache_backends'][] = 'sites/all/modules/varnish/varnish.cache.inc';
3+
$conf['cache_class_cache_page'] = 'VarnishCache';
4+
5+
#Memcache
6+
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
7+
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
8+
$conf['cache_default_class'] = 'MemCacheDrupal';
9+
$conf['memcache_key_prefix'] = 'key1';

shell

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker run -i -t -p 49800:80 -p 49801:6081 drupal /bin/bash

status.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
error_reporting(E_ALL);
4+
ini_set('display_errors', '1');
5+
6+
define('DRUPAL_ROOT', getcwd());
7+
8+
// Register our shutdown function so that no other shutdown functions run before this one.
9+
// This shutdown function calls exit(), immediately short-circuiting any other shutdown functions,
10+
// such as those registered by the devel.module for statistics.
11+
register_shutdown_function('status_shutdown');
12+
function status_shutdown() {
13+
exit();
14+
}
15+
// Drupal bootstrap.
16+
require_once './includes/bootstrap.inc';
17+
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
18+
// Build up our list of errors.
19+
$errors = array();
20+
21+
// Check that the main database is active.
22+
$result = db_query('SELECT * FROM {users} WHERE uid = 1');
23+
$result = $result->fetchAssoc();
24+
if (!$result['uid'] == 1) {
25+
$errors[] = 'Master database not responding.';
26+
}
27+
28+
// Check that the slave database is active.
29+
if (function_exists('db_query_slave')) {
30+
$result = db_query_slave('SELECT * FROM {users} WHERE uid = 1');
31+
$account = db_fetch_object($result);
32+
if (!$account->uid == 1) {
33+
$errors[] = 'Slave database not responding.';
34+
}
35+
}
36+
37+
// Check that all memcache instances are running on this server.
38+
if (isset($conf['cache_inc'])) {
39+
foreach ($conf['memcache_servers'] as $address => $bin) {
40+
list($ip, $port) = explode(':', $address);
41+
if (!memcache_connect($ip, $port)) {
42+
$errors[] = 'Memcache bin <em>' . $bin . '</em> at address ' . $address . ' is not available.';
43+
}
44+
}
45+
}
46+
47+
// Check that the files directory is operating properly.
48+
if ($test = tempnam(variable_get('file_directory_path', conf_path() .'/files'), 'status_check_')) {
49+
// Uncomment to check if files are saved in the correct server directory.
50+
//if (!strpos($test, '/mnt/nfs') === 0) {
51+
// $errors[] = 'Files are not being saved in the NFS mount under /mnt/nfs.';
52+
//}
53+
if (!unlink($test)) {
54+
$errors[] = 'Could not delete newly create files in the files directory.';
55+
}
56+
}
57+
else {
58+
$errors[] = 'Could not create temporary file in the files directory.';
59+
}
60+
61+
// Print all errors.
62+
if ($errors) {
63+
$errors[] = 'Errors on this server will cause it to be removed from the load balancer.';
64+
header('HTTP/1.1 500 Internal Server Error');
65+
print implode("<br />\n", $errors);
66+
}
67+
else {
68+
// Split up this message, to prevent the remote chance of monitoring software
69+
// reading the source code if mod_php fails and then matching the string.
70+
print 'CONGRATULATIONS' . ' 200';
71+
}
72+
73+
74+
// Exit immediately, note the shutdown function registered at the top of the file.
75+
exit();

supervisord.conf

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[supervisord]
2+
nodaemon=true
3+
4+
[program:apache2]
5+
command=/usr/sbin/apache2 -DFOREGROUND
6+
stdout_logfile=/var/log/supervisor/%(program_name)s.log
7+
stderr_logfile=/var/log/supervisor/%(program_name)s.log
8+
autorestart=true
9+
10+
[program:mysql]
11+
command=/usr/bin/mysqld_safe
12+
autorestart=true
13+
14+
[program:sshd]
15+
command=/usr/sbin/sshd -D
16+
autorestart=true
17+
18+
[program:memcached]
19+
command=/usr/bin/memcached -p 11211 -u www-data -m 64 -c 1024 -t 4
20+
autorestart=true
21+
22+
[program:varnishd]
23+
command=/usr/sbin/varnishd -s malloc,1G -T 127.0.0.1:6082 -a 0.0.0.0:6081 -F -f /etc/varnish/drupal.vcl
24+
autorestart=true
25+

0 commit comments

Comments
 (0)