Skip to content
This repository has been archived by the owner on Feb 5, 2020. It is now read-only.

Commit

Permalink
Configuration for phpmotion.
Browse files Browse the repository at this point in the history
  • Loading branch information
restorer committed Jan 15, 2013
1 parent 3a0a084 commit 3bab1ba
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 88 deletions.
10 changes: 0 additions & 10 deletions README

This file was deleted.

116 changes: 116 additions & 0 deletions README.md
@@ -0,0 +1,116 @@
What is it?
===========

Virtual hosts + per-directory config for lighttpd.

Project goal is to automatically convert .htaccess files into lighttpd config,
but currently .htaccess is **NOT** supported.

But you can convert .htaccess files into .lighttpd, it's easy
(except "RewriteCond %{REQUEST_FILENAME} !-f" - there is no easy way to convert it).

**examples** folder contains ready-to-go configuration files for some popular projecs.

How to install
==============

- Ensure that mod_rewrite and mod_redirect included into server.modules
- Set server.document-root to "/var/www/default"
- Ensure that ".lighttpd", ".enable-www", and ".aliases" is not readable from browser
- Put enable-sites.pl in /usr/share/lighttpd
- Append include_shell "/usr/share/lighttpd/enable-sites.pl" to the end of lighttpd.conf

Directory structure
===================

|- var
| |- www
| | |- somehost.com
| | | |- .aliases
| | | |- .lighttpd
| | | |- somefile.php
| | | |- sources
| | | | |- .lighttpd
| | | | |- nextfile.php
| | | |- ...
| | |- anotherhost.net
| | | |- .enable-www
| | | |- .lighttpd
| | | |- anotherfile.php
| | | |- ...

.aliases
========

Put domain aliases one per line.
If domain is "somehost.com" and .aliases is:

```
www
temp
m
```

Than folder will be accessible at somehost.com, www.somehost.com, temp.somehost.com, and m.somehost.com

.enable-www
===========

Empty file. Same as append "www" to .aliases.
If domain is "anotherhost.net" and there is .enable-www in it's folder,
than folder will be accessible at anotherhost.net and www.anotherhost.net

.lighttpd
=========

Per-directory configuration file.
Following substitutions available:

**{root}** - root path of current virtual host folder with trailing slash (for example "/var/www/somehost.com/")
**{dir}** - current directory path with trailing slash (for example "/var/www/somehost.com/sources/")
**{url}** - current directory url with trailing slash (for example "/sources/")

Example of lighttpd.conf
========================

```
server.modules = (
"mod_access",
"mod_alias",
"mod_rewrite", # Ensure that mod_rewrite and mod_redirect included into server.modules
"mod_redirect" # Ensure that mod_rewrite and mod_redirect included into server.modules
)
server.document-root = "/var/www/default" # Set server.document-root to "/var/www/default"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
index-file.names = (
"index.php",
"index.html",
"index.htm",
"default.htm"
)
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
include_shell "/usr/share/lighttpd/use-ipv6.pl"
# Ensure that ".lighttpd", ".enable-www", and ".aliases" is not readable from browser
# Easy way - just deny all hidden files
$HTTP["url"] =~ "/\." {
url.access-deny = ( "" )
}
dir-listing.encoding = "utf-8"
server.dir-listing = "disable"
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
# Append include_shell "/usr/share/lighttpd/enable-sites.pl" to the end of lighttpd.conf
include_shell "/usr/share/lighttpd/enable-sites.pl"
```
99 changes: 21 additions & 78 deletions enable-sites.pl
Expand Up @@ -13,8 +13,7 @@
my @confs = ();
my $find_name = '';

sub find_confs
{
sub find_confs {
my $file = $File::Find::name;
my $name = basename($file);

Expand All @@ -25,16 +24,14 @@ sub find_confs

# -------------------------------------------------------------------

sub print_lighttpd
{
sub print_lighttpd {
my $root = shift;

@confs = ();
$find_name = '.lighttpd';
find(\&find_confs, $root);

for my $conf_name (@confs)
{
for my $conf_name (@confs) {
my $url = dirname(substr($conf_name, length($root) - 1));
$already{$url} = 1;

Expand All @@ -52,32 +49,33 @@ sub print_lighttpd
my @inner = ();
my @outer = ();

for my $line (@lines)
{
for my $line (@lines) {
chomp($line);
$line =~ s/^\s\s*//;
$line =~ s/\s\s*$//;

$line =~ s/\{dir\}/$dir/g;
$line =~ s/\{url\}/$url_sl/g;
$line =~ s/\{root\}/$root/g;

if ($line =~ /^\s*url\.rewrite/) {
push(@outer, $line);
} else {
push(@inner, $line);
if (($line =~ /^\s*url\.rewrite/) || ($line =~ /^\s*url\.redirect/)) {
push(@outer, $line);
} elsif (($line ne '') && !($line =~ /^#/)) {
push(@inner, $line);
}
}

my $conf = join("\n", @inner);
my $outer_conf = join("\n", @outer);

if ($url ne '/') {
print "\t\$HTTP[\"url\"] =~ \"^$url_esc/\" {\n";
print "\$HTTP[\"url\"] =~ \"^$url_esc/\" {\n";
}

print "$conf\n";

if ($url ne '/') {
print "\t}\n";
print "}\n";
}

if ($outer_conf ne '') {
Expand All @@ -88,61 +86,13 @@ sub print_lighttpd

# -------------------------------------------------------------------

my @ht_indexes = ();
my %ht_errors = ();
my $ht_deny_all = 0;
my @ht_rules = ();
my $ht_charset = '';

sub read_htaccess
{
my $file = shift;

open(F_CONF, $file);
my @lines = <F_CONF>;
close(F_CONF);

@ht_indexes = ();
%ht_errors = ();
$ht_deny_all = 0;
@ht_rules = ();
$ht_charset = '';

# for my $line (@lines)
# {
# }
}

sub print_htaccess
{
my $root = shift;

@confs = ();
$find_name = '.htaccess';
find(\&find_confs, $root);

for my $conf_name (@confs)
{
my $url = dirname(substr($conf_name, length($root) - 1));

if (!$already{$url})
{
read_htaccess($conf_name);
}
}
}

# -------------------------------------------------------------------

sub print_fastcgi
{
sub print_fastcgi {
my $root_name = shift;

open(F_ORIG_INI, "/etc/php5/cgi/php.ini");
open(F_INI, ">/var/run/lighttpd-runner/php-${root_name}.ini");

while (<F_ORIG_INI>)
{
while (<F_ORIG_INI>) {
chomp;
my $line = $_;

Expand All @@ -156,7 +106,7 @@ sub print_fastcgi
close(F_INI);
close(F_ORIG_INI);

print<<EOF
print<<EOF
fastcgi.server = ( ".php" =>
((
"bin-path" => "/usr/bin/php5-cgi -c /var/run/lighttpd-runner/php-${root_name}.ini",
Expand All @@ -183,29 +133,23 @@ sub print_fastcgi
chdir($document_root);
my @files = bsd_glob('*');

for my $root_name (@files)
{
if ($root_name =~ /\./)
{
for my $root_name (@files) {
if ($root_name =~ /\./) {
my $root = $document_root . $root_name . '/';

my $root_name_esc = $root_name;
$root_name_esc =~ s/\./\\./g;

my @aliases = ();

if (-e "$root/.enable-www")
{
if (-e "$root/.enable-www") {
push @aliases, 'www';
}
elsif (-e "$root/.aliases")
{
} elsif (-e "$root/.aliases") {
open(F_ALIASES, "$root/.aliases");
my @lines = <F_ALIASES>;
close(F_ALIASES);

for my $line (@lines)
{
for my $line (@lines) {
chomp($line);
push @aliases, $line if $line ne '';
}
Expand All @@ -219,14 +163,13 @@ sub print_fastcgi
}

unless (-e "$root/.disable-def") {
print "\tserver.document-root = \"$root\"\n";
print "server.document-root = \"$root\"\n";
}

# print_fastcgi($root_name);

%already = ();
print_lighttpd($root);
# print_htaccess($root);

print "}\n";
}
Expand Down
71 changes: 71 additions & 0 deletions examples/phpmotion.domain.tld/.lighttpd
@@ -0,0 +1,71 @@
# Allow pictures, videos and swf files

url.rewrite-once += ( "\.(?:css|jpe?g|gif|png|flv|swf)(?:\?.*)?$" => "$0" )

# Main Menu Links

url.rewrite-once += ( "^{url}videos/load/([^/?]*)(?:\?(.*))?$" => "{url}seemore.php?load=$1&$2" )
url.rewrite-once += ( "^{url}videos/load/([^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}seemore.php?load=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}audios/load/([^/?]*)(?:\?(.*))?$" => "{url}audio.php?load=$1&$2" )
url.rewrite-once += ( "^{url}audios/load/([^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}audio.php?load=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}audios/album/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}audio.php?album=$1&$2" )
url.rewrite-once += ( "^{url}audios/album/([^/?]*)/(?:[^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}audio.php?album=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}blogs/load/([^/?]*)(?:\?(.*))?$" => "{url}blogs.php?load=$1&$2" )
url.rewrite-once += ( "^{url}blogs/load/([^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}blogs.php?load=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}albums/load/([^/?]*)(?:\?(.*))?$" => "{url}albums.php?load=$1&$2" )
url.rewrite-once += ( "^{url}albums/([^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}albums.php?load=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}people_ajax(?:\?(.*))?" => "{url}people_ajax.php?$1" )
url.rewrite-once += ( "^{url}people/([^/?]*)/([^/?]*)(?:\?(.*))?" => "{url}people.php?load=$1&page=$2&$3" )
url.rewrite-once += ( "^{url}people(?:\?(.*))?" => "{url}people.php?$1" )
url.rewrite-once += ( "^{url}upload-media(?:\?(.*))?" => "{url}upload.php?$1" )

url.rewrite-once += ( "^{url}groups(?:\?(.*))?" => "{url}groups.php?$1" )
url.rewrite-once += ( "^{url}search(?:\?(.*))?" => "{url}search.php?$1" )

# Action Links

url.rewrite-once += ( "^{url}videos/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}play.php?vid=$1&$2" )
url.rewrite-once += ( "^{url}audio/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}play_audio.php?audio=$1&$2" )

url.rewrite-once += ( "^{url}category/([^/?]*)(?:\?(.*))?$" => "{url}category_home.php?cid=$1&$2" )
url.rewrite-once += ( "^{url}category/([^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}category_home.php?cid=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}subcategory/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}category_home.php?sub=$1&$2" )
url.rewrite-once += ( "^{url}subcategory/([^/?]*)/(?:[^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}category_home.php?sub=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}genre/([^/?]*)(?:\?(.*))?$" => "{url}genre_home.php?cid=$1&$2" )
url.rewrite-once += ( "^{url}genre/([^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}genre_home.php?cid=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}view-album/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}album_view.php?album=$1&$2" )
url.rewrite-once += ( "^{url}view-image/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}album_view.php?image=$1&$2" )
url.rewrite-once += ( "^{url}read_blog/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}read_blog.php?id=$1&$2" )

url.rewrite-once += ( "^{url}blogs/category/([^/?]*)/(?:[^/?]*)(?:\?(.*))?$" => "{url}blogs.php?cat_id=$1&$2" )
url.rewrite-once += ( "^{url}blogs/category/([^/?]*)/(?:[^/?]*)/([^/?]*)(?:\?(.*))?$" => "{url}blogs.php?cat_id=$1&page=$2&$3" )

url.rewrite-once += ( "^{url}members/([^/?]*)(?:\?(.*))?$" => "{url}memberprofile.php?user=$1&$2" )

# Redirect directory browsing attempts

url.rewrite-once += ( "^{url}videos/?(?:\?.*)?$" => "{url}seemore.php" )
url.rewrite-once += ( "^{url}audios/?(?:\?.*)?$" => "{url}audio.php" )
url.rewrite-once += ( "^{url}blogs/?(?:\?.*)?$" => "{url}blogs.php" )
url.redirect += ( "^{url}members/?(?:\?.*)?$" => "{url}people" )
url.rewrite-once += ( "^{url}albums/?(?:\?.*)?$" => "{url}albums.php" )

# Some simple blocks

url.rewrite-once += ( "^{url}[^?]*\?.*?(?i)(?:<|%3C).*script.*(?:>|%3E)" => "{url}index.php" )
url.rewrite-once += ( "^{url}[^?]*\?.*?GLOBALS(?:=|\[|%[0-9A-Za-z]{0,2})" => "{url}index.php" )
url.rewrite-once += ( "^{url}[^?]*\?.*?_REQUEST(?:=|\[|%[0-9A-Za-z]{0,2})" => "{url}index.php" )
url.rewrite-once += ( "^{url}[^?]*\?.*?SELECT(?:=|\[|%[0-9A-Za-z]{0,2})" => "{url}index.php" )
url.rewrite-once += ( "^{url}[^?]*\?.*?UNION(?:=|\[|%[0-9A-Za-z]{0,2})" => "{url}index.php" )

# Error handlers

server.error-handler-404 = "/404.php"
1 change: 1 addition & 0 deletions examples/phpmotion.domain.tld/fckeditor/.lighttpd
@@ -0,0 +1 @@
server.dir-listing = "disable"
@@ -0,0 +1,2 @@
url.access-deny += ( ".htm", ".html" )
server.error-handler-404 = "/404.php"

0 comments on commit 3bab1ba

Please sign in to comment.