loindoin
Posts: 4
Joined: Wed Jun 27, 2012 10:01 am

n00b RPi webserver setup with Lighttpd and Mediawiki

Wed Jun 27, 2012 6:14 pm

How I set up my Pi as a webserver using LIghttpd, MySQL, PHP and mediawiki.

SD card
SD card setup taken from this URL http://elinux.org/RPi_Easy_SD_Card_Setu ... nd_line.29

I chose to run the debian squeeze image from http://www.raspberrypi.org/downloads the rpi download site to my laptop and then checked the hash using the command:

Code: Select all

sha1sum ~/debian6-19-04-2012.zip
As the instructions state I unzipped the zip file to a directory that i made, ~/rpi, on my laptop. The image is already in a directory so does not require the separate directory that i made. If you placed the zip file in your home dir then the command will be:

Code: Select all

unzip ~/debian6-19-04-2012.zip
Before inserting the SD card I ran the command:

Code: Select all

df -h
which gave me a read out of the partitions on my laptop. I inserted the SD card and ran df -h again.
I then saw the addition of the SD card as something like /dev/mmcblk0p1. This refers to partition 1 of mmcblk0. Take note of this file path for later - make sure that this is the one difference between the two times that you run df -h as this positively idents the SD card.

I then followed the instructions to un-mount the drive to allow write access to it. Include the full path in this command including the partition number

Code: Select all

sudo umnount /dev/mmcblk0p1
Write the image to the SD card

Code: Select all

sudo dd  bs=1M if=~/rpi/debian6-19-04-2012/debian6-19-04-2012.img of=/dev/mmcblk0
noting that path to the image on my latop is listed after if= and the location on the sd card is the full path minus the partition number after the of=

After this was complete I ran the following command to flush any cache and prepare the SD card for removal from the laptop

Code: Select all

sudo sync
Once complete I removed the card from the reader.

Note that the debian squeeze img is a 2GB img. if you have an SD card larger than this you will need to manually resize the card to use all of the space and not just 2GB

Resize the SD Card image
As noted above you may need to resize the image on the card if you have one that is larger than 2GB. I have a 16GB card and I missed this step originally and had to come back to it later. There was a possibility of loosing all the work I had done so I backed up my install and ran the following instructions. If you are doing this in the order that I've written it then you wont need to worry about backing up as it is a clean image.

The instructions I followed were from the Raspberry Pi Beginners channel on YouTube http://www.youtube.com/watch?v=R4VovMDnsIE, but I will reproduce the commands here.

You can run these commands on the RPi itself after you boot it up. So place the SD card in the pi, add your HDMI output and keyboard, then power it up. On login prompt you can now sign in using the default credentials provided from http://www.raspberrypi.org/downloads raspberry pi downloads page

As I did this out of order I already had a different user made than the default Pi user. The default will suffice for now if it can sudo or you can get into root. I notice that the video linked above is in root but if its sudo-ing then it shouldn't need to be root. Lets stick with default user and sudo.

When you have logged in run:

Code: Select all

 df -h
to get a snapshot of the filesystem. You should have the good old /dev/mmcblik0p1 available. What the video talks about is using fdisk to delete the partition table and recreate it at a larger size. just so you know :) Now run

Code: Select all

sudo fdisk -uc /dev/mmcblk0
Note that we take the whole card and not a partition - mmcblk0 as opposed to mmcblk0p1.

You will now get a new prompt (the fdisk prompt)

Code: Select all

Command (m for help):
at this point type in

Code: Select all

p
to print the partition table. You will seee a bunch of info and a list of partitions p1 (FAT32 for boot), p2 (the system files), p3 (swap). Take note of the exact start and end point numbers in this table too. It should be visible above the following commands but just in case, write them down EXACTLY how you see them...we will need them later.

Now to delete the system bulk and the swap. At the prompt choose:

Code: Select all

d
to delete, then choose:

Code: Select all

2
for partition 2. One down and one to go

Code: Select all

d
again, and to remove partition 3 choose:

Code: Select all

3
Now you need create the new tables. At the prompt type:

Code: Select all

n
then

Code: Select all

p
for primary

Code: Select all

2
for partition 2, now choose the start position of partition 2. This will be the one you wrote down above. Mine was the same as the video 157696 , type that in

Code: Select all

157696
Then choose where the end of this partition will be. Choose the default by pressing enter.
Now to commit the changes and type:

Code: Select all

w
You will drop out to the Pi prompt. Now reboot the system.

Code: Select all

sudo reboot
When the systme has booted up and you have logged in check the filesystem table again

Code: Select all

df -h
You now need to run a resize to use the new partition table, in particular partition 2. This could take a while to do depending on the size of your SD card, leave it and come back in a few mins.

Code: Select all

sudo resize2fs /dev/mmcblk0p2
Check filestructure again

Code: Select all

df -h
Look at the rootfs line and you should see that it has a bunch of nice new space!

Swap File Space
Now to create a small swap file space

Code: Select all

cd /var
then to create a 128MB swap file.

Code: Select all

sudo dd if=/dev/zero of=swapfile bs=1M count=128
then make the linux swap area

Code: Select all

 sudo mkswap /var/swapfile
 sudo swapon /var/swapfile
This last command could take some time so give it a little bit and come back.

Now to adjust the fstab (the file system table file) to use the new swap file space

Code: Select all

sudo nano /etc/fstab
Btw, feel free to use any other text editor, I think RPi comes with vim already

The video tutorial suggests using a nicer formatted fstab file which takes into account the boot partition as well. You can find this at http://pastebin.com/r5myRnYS this pastebin address but here it is for your reading pleasure. Paste this into your fstab file overwriting the content already there.

Code: Select all

proc            /proc   proc    defaults                        0 0
 /dev/mmcblk0p1  /boot   vfat    defaults                        0 0
 /dev/mmcblk0p2  /       ext4    defaults,noatime,nodiratime     0 0
 /var/swapfile   none    swap    sw                              0 0
save and close the file using your preferred text editor. Then reboot the pi.

Code: Select all

sudo reboot
Once the system has come back up then you can check things out. Some of the things to use are top and df -h.

Code: Select all

top 
will show you the amount of swap available - this should show you that you have 128MB available.

Code: Select all

 df -h
will show you the file system with the bulk of the system that you resized to.

Keyboard layout and Timezone
During setup you probably wont get asked where you currently are and what keyboard you have plugged in so we need to do this ourselves. Run

Code: Select all

sudo nano /etc/default/keyboard
and choose the English (US) keybooard layout. The UK version has some screwy thing with the location of the @ for some reason. Now the time zone

Code: Select all

sudo dpkg-reconfigure tzdata
follow the on-screen info and choose your country.

Static IP address and Host
So one thing I like to do with my home network is to give all things on it a static IP address. I did this in my router DHCP server and is quite handy to do.

The other thing that I did was to add the following into my /etc/hosts file
192.168.0.140 rpi rpi.local

That way I'm able to use a name instead of an IP address. There may be a better way of doing this but this seems to work for me. If folks know better setups let me know :)

LAMP
I originally intended to pop the good old LAMP server on the RPi and I set these up out of the repo's. LAMP stands for Linux, Apache, MySQL and PHP, all are needed for running a standard webserver.

I soon found out that Apache was quite heavy footed when it came to the RPi and moved across to using Lighttpd. If you wanted to give Apache a go then I originally followed http://fusionstrike.com/2012/installing ... -pi-debian this blog on the setup and then adjust the apache2.conf file by using http://serverfault.com/questions/112361 ... mory-usage this forum hint on limiting memory usage.

So I suppose i am using LLMP instead of LAMP in my current setup.

Initial Setup
My first thoughts stemmed from a few handy hints that I had when learning how to set up Apache. This used symlinks from /srv to /var. I'll run through this in a tick, but the other setup thing is the www-data group and user which the server will use to crank out the files.

So lets get a location to put our future webs, as stated above I chose /srv as my default location and as i'm going to be running a few things like Media wiki and Wordpress as well as my D3 stuff i need to create directorys for these.

Code: Select all

sudo mkdir /srv/w
for the wiki

Code: Select all

sudo mkdir /srv/wp
for wordpress, and

Code: Select all

 sudo mkdir /srv/d3
well, for my d3 stuff.

Now to create the generic web location that the server will use in /var

Code: Select all

sudo mkdir /var/www
and to symlink the directorys in /srv use

Code: Select all

sudo ln -s /srv/* /var/www/
This should pick up all the directorys the /srv/ path and symbolically link them to the /var/www/ location. Now as a n00b i'm still yet to learn about these things fully. For example, if you update the /srv/ path with say an index.html file (which we will do for lulz and testing) then you may need to re-run the command above to pick upt he new files. Maybe some one out there could fill that one in for me or even suggest a time to run this command (or a better one)

Now lets setup this www-data stuff.

Code: Select all

sudo groupadd www-data
to add the new group

Code: Select all

 sudo adduser www-data www-data
to add the user

Code: Select all

sudo usermod -a -G www-data www-data
to add the user and mod them to be in the group www-data :)
and last but not least give /var/www to www-data

Code: Select all

sudo chown -R www-data:www-data /var/www
Right now lets get ready to install some stuff

Code: Select all

 sudo apt-get update
to update from the repo's

lighttpd
Good old RPi wiki was the key herehttp://elinux.org/RPi_webserver which has a nice setup for lighttpd Quite simply we do the following to get the initial webserver going

Code: Select all

sudo apt-get install lighttpd
Part of the install is to start the service as well so lets check that its running ok

Code: Select all

ps -u www-data u
this should list something like

Code: Select all

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
 www-data  7329  0.0  1.0   7028  1972 ?        S    10:23   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.
If you see this then its all up and running, to test it it though in your browser visit the following address

Code: Select all

http://your-raspberry-pi-ip/index.lighttpd.html
where ''your-raspberry-pi-ip'' is what it says. If you set a host name above then swap out the ip for the host name. If that doesn't work then try putting .local after the host name. For my host settings above my address looks like

Code: Select all

http://rpi.local/index.lighttpd.html
The webserver will need some PHP support as it doesn't natively handle PHP, but we will get to that later.

MySQL
Lets get MySQL working

Code: Select all

 sudo apt-get install mysql-server mysql-client php5-mysql
Once done, it was recommended to me to run the mysql_secure_installation script, so

Code: Select all

sudo /usr/bin/mysql_secure_installation
and answer Y to each step - set aiooood password :)

We will be coming back to MySQL later to create some users for the wiki and wp setups but for now that is it. If you need to you should now be able to use the following commands to start, stop and restart the server, though it should now be running and also start up on boot.

Code: Select all

sudo /etc/init.d/mysql start
 sudo /etc/init.d/mysql stop
 sudo /etc/init.d/mysql restart
PHP
So I'm not so strong on PHP-foo and tend to rely on googling certain errors or issues specifically so as to find solutions. Having said that i'll step through some of the steps I followed to set up PHP on the RPi. I followed http://fusionstrike.com/2012/setting-ph ... -pi-debian this little blog post on setting up PHP5.

First lets install PHP5 and some extras - what I will do is follow the commands that I used and then if these extras are not needed we will do a clean up :) (once again sorry for the n00b trick)

Code: Select all

sudo apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-fpm php5-cgi php5-cli php5-common php5-suhosin
Yeah I know there is a lot there, and yeah I know that some of these things may not be needed...i'd be happy for your help on this if you have any suggestions.

Lets see if it works by creating a small phpinfo page

Code: Select all

cd /var/www
 sudo nano phpinfo.php
this will open a new file into which you can paste

Code: Select all

 <?php
 phpinfo();
 ?>
Then in your browser head on over to

Code: Select all

http://your-raspberry-pi-ip/phpinfo.php
all things working you should see a nice page with info about the PHP :)

PHP support on lighttpd
Right this is where we need to do some edits on config files etc to make PHP work a bit more with media wiki and wordpress.

From http://programisiai.lt/index.php?topic=1315.0 this link we need to enable PHP5 in Lighttpd, we must modify /etc/php5/cgi/php.ini and uncomment the line

Code: Select all

cgi.fix_pathinfo=1
.

Now to edit a conf file

Code: Select all

sudo nano /etc/lighttpd/conf-enabled/10-cgi-php.conf
and add the following

Code: Select all

server.modules += ("mod_cgi")
 cgi.assign = (".php" => "/usr/bin/php5-cgi")
There is a nice perl script, which i didn't write, that we can run to make this happen

Code: Select all

perl -p -i -e 's/http:\/\/php.net\/cgi.fix-pathinfo$/http:\/\/php.net\/cgi.fix-pathinfo\ncgi.fix_pathinfo = 1/' '/etc/php5/cgi/php.ini'
or we can just edit the file itself and uncomment the line as suggested above.
Now enable the mods

Code: Select all

sudo lighttpd-enable-mod fastcgi fastcgi-php
And finally reload the conf files inlighttpd

Code: Select all

 sudo /etc/init.d/lighttpd force-reload
Hopefully that gets everything all up and ready for Wiki and Wordpress. Lets install phpMyadmin now so that we can check it all runs nicely and give us a great in for our MySQL management.

PHPMyAdmin
Install this by using the awesome apt-get command

Code: Select all

 sudo apt-get install phpmyadmin
Follow the on screen steps that pop up. Make sure you choose lighttpd instead of apache2. I chose yes to everything else.

Once that is done we need to symlink the install to our web directory in /var/www

Code: Select all

sudo ln -s /usr/share/phpmyadmin/ /var/www/phpmyadmin
restart the webserver and mysql

Code: Select all

sudo /etc/init.d/lighttpd force-reload
 sudo /etc/init.d/mysql force-reload
and now in your browser visit

Code: Select all

http://[your-rpi-ip-address-or-host]/phpmyadmin
fingers crossed you dont get a 404 error :)

MediaWiki
My main reason for running this as I mentioned a bit before was to run MediaWiki and my D3 stuff as well as Wordpress for developement. MediaWiki provide some pretty nice instructions for install on their site and wiki. Firstly lets grab the file and pop it in the /srv/w/ location we made before.

Code: Select all

cd /srv/w
Copy the URL of the Download MediaWiki 1.x link from [http://www.mediawiki.org/wiki/Download http://www.mediawiki.org/wiki/Download]. This will be a tar.gz file and you will paste it in the following command

Code: Select all

 sudo wget paste the URL here
This will show a progress bar and download the file. The bash prompt will appear when the download is complete. Now to decompress the tar ball.

Code: Select all

sudo tar xvzf the full file name
This will likely extract the files to a directory within the /srv/w/ directory so we need to mv these files to the /srv/w/ location. It will be something like, but possibly not exactly

Code: Select all

sudo mv mediawiki* /srv/w/
So if you run ls on /srv/w/ then you'll see a nice list of files and directorys.
If you do then you can clean up by removing the empty mediawiki directory. Make sure it is empty first though ;)

Code: Select all

ls /srv/w/mediawiki....
 sudo rm -rf /srv/w/mediawiki....
That should about do the initial install. Now to run the config setup through the browser. This is actually pretty simple to follow using the on screen instructions.

One of the questions that you'll get when you run through the setup in a minute is how to access your mySQL db.

You do not want to be leaving your root password to the db in the config file of your wiki because what ends up happening is that the credientials get stored in the clear in the LocalSettings.php config file in the wiki directory. You'll see what this looks like in a few moments when the setup generates the file itself. For now though let us create the wiki tables using the root user just to set everything up and then we will change it. I'll come back to this later.

In your browser hear along to the following location

Code: Select all

http://your-rpi-ip-address-or-host/mediawiki/index.php
You should now see the http://www.mediawiki.org/wiki/File:Medi ... script.png media wiki setup or something like this anyway. Follow it through giving your db a name like my_wiki (remember this name for later), pop in a prefix like wp_ and then pop in your db credentials for root access to the db.

You will then get to a point where you may need to generate the LocalSettings.php file. It will generate this for you and you will need to create a file with the LocalSettings.php file name. This is easily done by first making sure you are in the /srv/w directory and then

Code: Select all

 sudo nano LocalSettings.php
This will open a new blank file with that name. Now copy the guts that were generated on the browser and paste it into the file, save and exit.

The wiki setup in the browser will give instructions on what to do next but essentially you'll have your wiki :)

Let us get back to those db credentials. Lets sign into MySQL and create a new user then grant that user privileges to the wiki db.

Code: Select all

mysql -u root -p
This drops you into a mysql prompt that looks like

Code: Select all

 mysql>
At the prompt type the following but make sure that you know the name of the db, using the name example above:

Code: Select all

use my_wiki;

Code: Select all

GRANT ALL PRIVILEGES ON * to "user"@"localhost" IDENTIFIED BY "new_password";
where ''user'' is a new user name that is not root and ''new_password'' is a password for that user, oh and leave the "'s in there too. They are needed.

Code: Select all

FLUSH PRIVILEGES;

Code: Select all

 quit
Now pop on back into the LocalSettings.php file

Code: Select all

sudo nano /srv/w/LocalSettings.php
and update the db settings to change the username and password to the ones that you just created in Mysql.

The db section should look something like:

Code: Select all

...
 ## Database settings
 $wgDBtype           = "mysql";
 $wgDBserver         = "localhost";
 $wgDBname           = "my_wiki";
 $wgDBuser           = "user";
 $wgDBpassword       = "new_password";
 ...
Save and exit and reload the wiki page :) All should be running as it was but no longer on the root db credentials.

Short URLs
One of the cool things about mediaiki is that you can shorten the URLs to avoid having to have the ugly as index.php used in every single address. URLs can now just be /wiki/page_title making it much easier to recall URLs quickly. To shorten URLs is quite a simple set of steps to do but we need to alter a couple of conf files to do so. I grabbed the instructions from http://www.mediawiki.org/wiki/Manual:Sh ... oot_access

The first step of installing the wiki in a directory of /w/ is key. If you've installed it in a different directory then you will best be served moving it to a /w/ directory.

The next are two config files that you need to adjust, these are the /srv/w/LocalSettings.php file and the /etc/lighttpd/lighttpd.conf file

Lets get into the LocalSettings.php file first but first create a backup of the file first.

Code: Select all

cd /srv/w
 sudo cp LocalSettings.php LocalSettings.php.bak
 sudo nano /srv/w/LocalSettings.php
and look for the following section near the top

Code: Select all

...
 $wgScriptPath
 ...
What we need to do is edit it to look like this

Code: Select all

 $wgScriptPath       = "/w";
 $wgArticlePath      = "/wiki/$1";
 $wgUsePathInfo      = true;
save and exit

Now truck on over to the /etc/lighttpd

Code: Select all

cd /etc/lighttpd
and create a backup of the conf file

Code: Select all

sudo cp lighttpd.conf lighttpd.conf.bak
and edit

Code: Select all

sudo nano lighttpd.conf
two things to adjust here - at the very top we need to enable by uncommenting a few of the server modules, rewrite for one. The server modules list in my conf file looks like

Code: Select all

server.modules = (
 	"mod_access",
 	"mod_alias",
 	"mod_compress",
  	"mod_redirect",
 	"mod_rewrite",
 )
Now at the very bottom of this file add the following

Code: Select all

 #rewrite URLs for wiki
 url.rewrite-if-not-file += ( 
     "^/wiki/(mw-)?config/?" => "$0",
     "^/wiki/([^?]*)(?:\?(.*))?" => "/w/index.php?title=$1&$2",
     "^/wiki/([^?]*)" => "/w/index.php?title=$1" 
 )
Save and exit

Now restart the webserver

Code: Select all

sudo /etc/init.d/lighttpd restart
and then in your browser you can either reload the main page or visit the following address

Code: Select all

http://[your domain]/wiki/Main_Page
You are now done!

sleipner42
Posts: 5
Joined: Tue Jun 26, 2012 7:12 pm

Re: n00b RPi webserver setup with Lighttpd and Mediawiki

Mon Jul 02, 2012 12:36 pm

Very nice guide;)

loindoin
Posts: 4
Joined: Wed Jun 27, 2012 10:01 am

Re: n00b RPi webserver setup with Lighttpd and Mediawiki

Tue Jul 03, 2012 8:55 am

Cheers, I figured that as all the basic info was found in across a multitude of sites that made it more tricky to get a complete guide. So this ended up helping me and then I figured that it might also help other folks too. :)

FrankEvil
Posts: 2
Joined: Sun Jul 01, 2012 4:31 pm

Re: n00b RPi webserver setup with Lighttpd and Mediawiki

Sat Jul 07, 2012 6:22 pm

I can't get past the 404 error on MediaWiki. Any ideas?

I'm an even bigger n00b than you, so you might need to spell it out for me.

loindoin
Posts: 4
Joined: Wed Jun 27, 2012 10:01 am

Re: n00b RPi webserver setup with Lighttpd and Mediawiki

Sat Jul 07, 2012 6:50 pm

FrankEvil wrote:I can't get past the 404 error on MediaWiki. Any ideas?

I'm an even bigger n00b than you, so you might need to spell it out for me.
Which step did you get up to before the 404?

FrankEvil
Posts: 2
Joined: Sun Jul 01, 2012 4:31 pm

Re: n00b RPi webserver setup with Lighttpd and Mediawiki

Sat Jul 07, 2012 9:26 pm

loindoin wrote:
FrankEvil wrote:I can't get past the 404 error on MediaWiki. Any ideas?

I'm an even bigger n00b than you, so you might need to spell it out for me.
Which step did you get up to before the 404?
Ended up getting past it. I was setting my browser to MediaWiki, but it actually made me use W instead. So the url is http://xxx.xxx.xxx.xxx/w/index.php.

I got everything setup and after I put together the LocalSettings.php file, everything stopped working again. Didn't realize how tough this is to setup.

So for now, the wiki isn't working. Can't seem to access it. Going to look again in the LocalSettings.php file. Anything specifically I should look for?

loindoin
Posts: 4
Joined: Wed Jun 27, 2012 10:01 am

Re: n00b RPi webserver setup with Lighttpd and Mediawiki

Mon Jul 09, 2012 9:01 am

FrankEvil wrote:
loindoin wrote:
FrankEvil wrote:I can't get past the 404 error on MediaWiki. Any ideas?

I'm an even bigger n00b than you, so you might need to spell it out for me.
Which step did you get up to before the 404?
Ended up getting past it. I was setting my browser to MediaWiki, but it actually made me use W instead. So the url is http://xxx.xxx.xxx.xxx/w/index.php.

I got everything setup and after I put together the LocalSettings.php file, everything stopped working again. Didn't realize how tough this is to setup.

So for now, the wiki isn't working. Can't seem to access it. Going to look again in the LocalSettings.php file. Anything specifically I should look for?
Hmm looks like it came unstuck on the short URLs part. If you return your LocalSettings.php backup to be the actual LocalSettings.php file does that sort things out for you? At least by doing this we can determine the steps where things start breaking.

I'd also recommend reading the mediawiki setup for short URLs at http://www.mediawiki.org/wiki/Manual:Sh ... oot_access through. I read it a few times myself before I got it working, but it may also help just to check to see if everything was written correctly.

Once we get it working again we can look at specifics on getting Short URLs going for you.

User avatar
Alexander Brown
Posts: 97
Joined: Thu Jan 23, 2014 2:36 pm

Re: n00b RPi webserver setup with Lighttpd and Mediawiki

Tue Apr 08, 2014 8:58 am

ok may have missed it but i need to set up MediaWiki on httpd running from pidora on a 4gb card
I am

Return to “Beginners”