From eBower Wiki
Jump to: navigation, search

Installing Apache

There isn't much to installing Apache. I also do some php work so my installation is:

sudo apt-get install apache2 php5 libapache2-mod-php5 

That's pretty much it, everything else is included in the dependencies.

Adding SSL

See my DocBook guide here


If you want EVERYONE to use SSL (which you, and they, should) you can use the Apache rewrite engine to do this. First you need to enable it.

 sudo a2enmod rewrite

Then you'll need to edit /etc/apache2/sites-enabled/000-default and add this inside the VirtualHost tag.

RewriteEngine On
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Installing awstats

Everyone wants to know how popular they are, I use awstats for that.

sudo apt-get install awstats libgeo-ipfree-perl libnet-dns-perl libnet-ip-perl

It's pretty easy to set up. First you need to create a configuration for your website.

cd /etc/awstats
sudo cp awstats.conf
sudo vi

Now it's just edits. I change the following lines:

HostAliases="localhost ::1"

I then enable the following plugins:


Now you need to edit /etc/apache2/sites-enabled/000-default-ssl:

        Alias /awstatclasses "/usr/share/awstats/lib/"
        Alias /awstats-icon "/usr/share/awstats/icon/"
        Alias /awstatscss "/usr/share/doc/awstats/examples/css"
        ScriptAlias /awstats/ /usr/lib/cgi-bin/
        Options ExecCGI -MultiViews +SymLinksIfOwnerMatch

Now, you need to edit /etc/logrotate.d/apache2 to grab the data before you rotate the logs:

                /usr/lib/cgi-bin/ -update
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi; \

Finally, you can set up a cron to run it every 6 hours by running sudo crontab -e:

0 */6 * * * /usr/lib/cgi-bin/ -update > /dev/null

Now you can access the data by going to!

Fixing Missing Icons

There are a few missing icons in the default install that show up in /var/log/apache2/error.log. To fix those:

sudo cp /usr/share/awstats/icon/mime/unknown.png document.png
sudo cp /usr/share/awstats/icon/mime/unknown.png package.png

Of course, you can also create/find your own icons but this is cheap and easy.

Password Protecting awstats

By default, awstats is wide open. One could argue that nothing here is really secret, exposing it does expose the privacy of some end users and lets people know about the important parts of your website, but it's really just IP addresses and objects. But you never know what will show up there...

$ sudo htpasswd -c /etc/apache2/.htpasswd awstats

Just give the user whatever password you want, but remember that if you're not SSL protected (and why aren't you?) this password will be passed in the clear every time you view the page. Don't use an important password and don't assume it's really all that secure.

Now edit /etc/apache2/sites-enabled/000-default-ssl (or equivalent) and add this near the awstats stuff you had above:

        <Files "">
                AuthUserFile    /etc/apache2/.htpasswd
                AuthName "Restricted Area For Admins"
                AuthType Basic
                require user awstats

You should be a sudo apache2ctl restart away from minor security on your awstats page.

Don't forget to disallow robots.txt from viewing this, add the following line to it so they don't get errors:

Disallow: /awstats

Clearing Data

If you want to clear out the awstats data and force it to reparse the logs, just delete it:

sudo rm /var/lib/awstats/*

Installing MediaWiki

You can install mediawiki like this:

sudo apt-get install mediawiki

But it's a pretty old version. I installed from scratch by downloading it here. Now run the following:

sudo apt-get install apache2 mysql-server php5 php5-mysql libapache2-mod-php5 imagemagick
cd /var/lib
sudo tar -xzvf ~/Downloads/mediawiki-1.20.3.tar.gz # or whatever the file is
sudo ln -s mediawiki-1.20.3 mediawiki

Now you'll want to edit /etc/apache2/sites-enabled/000-default-ssl (I'm assuming you're SSL enabled!) and add the following line:

Alias /wiki /var/lib/mediawiki

You can now sudo apache2ctl restart and visit your webpage at to walk through the config wizard.

I unfortunately need to tie down my Wiki because I spend way too much time removing spam (email me if you want access). Luckily, that's now part of the wizard so my /var/lib/mediawiki/LocalSettings.php is pretty stock. Note that you may want to edit the default Skin:

$wgDefaultSkin = "modern";

Create shortened URLs

One thing I did change is to set up shortened URLs. By default you'll notice that the URLs are of the format /wiki/index.php/Page_Name which looks plain messy. Unfortunately, so is fixing it. You see, that explicit inclusion of index.php is sometimes necessary and sometimes forbidden. So we need to create two aliases (aliai?). First, we want the path structure to be /wiki/Page_Name but we also need to have a /w/index.php/Page_Name present but hidden. This /w/ link is actually for scripts instead of the main Wiki links.

First let's set up /etc/apache2/sites-enabled/000-default-ssl to remove the old Alias and replace it with this:

        Alias /w /var/lib/mediawiki
        Alias /wiki /var/lib/mediawiki/index.php
        <Directory /wiki>
                AllowOverride All
                AcceptPathInfo On

Now we need to edit /var/lib/mediawiki/LocalSettings.php and add these lines (the first line is actually a change):

$wgScriptPath       = "/w";
$wgArticlePath      = "/wiki/$1";  # what the url will look like for visitors
$wgUsePathInfo      = true;        # enable shorter url feature

If you're late to the shortened URL game, you can recover from your past mistakes by finding anything that references the /wiki/index.php/Page_Name URL and creating a Wiki for it with a redirect. For example:

 #REDIRECT [[Page_Name]]

Backing Up Your Wikis

The biggest downside of the Wiki format is that the files are not in an accessible format, they're stored in a database. To backup your wikis you can run a cron, I run mine every 8 hours and I store 30 days of data via a simple script.

First you'll want to record your database info. I created /etc/db_pass for this which has the format:


Of course, you'll need to edit this to suit your own needs. Then run chmod 400 /etc/db_pass to protect it a bit. You do run into the issue that someone who breaks into your system breaks into your mysql database, but frankly if they know the root password they can reset the mysql password anyway.

My script writes to a directory in my home which is actually a Samba mount to my storage server which backs itself up nightly. Should this be in a common place since root will likely access it? Probably, but I don't really care.

. /etc/db_pass
cd /home/jdbower/backup-data/wiki
# First, compress the latest file and any that were missed.
gzip *.xml
# Now remove any files more than 30 days old
#   backup every 8H = 3 backups per day = 90 files
rm `ls -t | awk 'NR>90'`
# Finally, dump the database
mysqldump -h localhost -u ${user} -p${passwd} --xml ${db} > /home/jdbower/backup-data/wiki/wiki_`date +%Y%m%d-%H%M`.xml

Now just run sudo crontab -e and add this line:

 0 */8 * * * /home/jdbower/bin/backup_wiki

There is semi-sensitive data in here, like a password hash. Realistically, I don't care about doing a full restore and you can view the actual text in the XML which I would probably manually cut-and-paste into a recreated Wiki.

However, in theory this implies that you can do a full restore using:

php wiki_folder/maintenance/importDump.php --dbpasspassword --quiet wikidbname path_to/dumpfile.xml 
php rebuildrecentchanges.php
php importImages.php ../path_to/images

Adding Google+ Goodness

Or, if you must, you can also add other HTML code.

I've chosen to do this by editing the skins. I edited my chosen skin (/var/lib/mediawiki/skins/Modern.php) and made two changes. First, I found that the +1 widget looked best against the grey background of the actions bar. To figure out where to add the code I used Chrome's excellent "inspect element" feature to find the <div> I wanted. You can get the most modern +1 button from here. Then I wrapped a table around it like this:

 <!-- navigation portlet -->
 <table width="100%">
       <?php $this->cactions(); ?>
     <td align="right">
       <!-- Place this tag where you want the +1 button to render. -->
       <div class="g-plusone" data-annotation="inline" data-width="300"></div>
       <!-- Place this tag after the last +1 button tag. -->
       <script type="text/javascript">
         (function() {
           var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
           po.src = '';
           var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);

Then at the footer I put a link to my Google+ Profile, I just didn't like the looks of the tiny things in the existing header rows. You can get the badge generator here:

 <!-- footer -->
 <div id="gplus_header">
   <table width="100%">
       <td align="right">
         <!-- Place this tag where you want the widget to render. -->
         <div class="g-person" data-width="273" data-href="//{G+ User ID}" data-layout="landscape" data-rel="author"></div>
         <!-- Place this tag after the last widget tag. -->
         <script type="text/javascript">
           (function() {
             var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
             po.src = '';
             var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);

Of course, you'll want to make sure your Google+ ID number is used.

Adding LastModified Extension

When browsing the Internet I hate running into a page without a date, finding detailed instructions on how to do something and then catching an odd reference to Hardy means the page is mostly worthless. So I installed the LastModified extension on my Wiki. You can find the LastModified extension here. The only real difference is the extraction command under Ubuntu Precise is this:

 sudo tar -xzvf wikimedia-mediawiki-extensions-LastModified-*.tar.gz -C /var/lib/mediawiki/extensions/

When you're done you should have text at the bottom of the page indicating when it was last touched.

Opening External Links in a New Tab

In my opinion it's best to open external links in a new tab, from a navigation perspective this allows you to leave the site and come back more cleanly. To enable this, just edit /var/lib/mediawiki/LocalSettings.php and add the following line:

 $wgExternalLinkTarget = '_blank';

Now links to your internal site should open in the same tab while links off-site will open in a new tab.

Add MobileMeta Extension

To help fix some mobile rendering issues, copy the code from here into /var/lib/mediawiki/extensions/MobileMeta.php and add the following line to your /var/lib/mediawiki/LocalSettings.php:

# Enable Mobile Meta