#+title: How to Set Up your Own Personal Wiki. #+author: Robert McIntyre #+email: rlm@mit.edu #+description: Empowering yourself with your own Wiki. #+keywords: freedom, personal server, wiki #+SETUPFILE: ../../aurellem/org/setup.org #+INCLUDE: ../../aurellem/org/level-0.org #+babel: :mkdirp yes :noweb yes :exports both Initially, this is going to be a guide for myself and my hacker friends to set up their own wiki. Eventually this will become a friendly guide that anyone should be able to use. If you'd like to help with this project, please send email to . [[../html/email.html][Last time]] I showed you how to set up your own server running Arch GNU/Linux. Today we're going to build off of that and make our own wiki, just like Wikipedia! I will recommend various services in this article, because they are services that I use personally. You can of course substitute these for whatever you want. * A Wiki involves three core pieces of software: ** A Database Is a set of programs and files on your server that stores data in a highly ordered way and provides fast access to the data. The data is often arranged in a structure called a /Table/. Individual entries in the table are called /Records/ or /Rows/. Programs are able to create Tables and Records in similar ways to how they create files. Like files, Tables have specific owners and permissions, so that only certain users and programs are able to access them. The important difference between a Table and a file is that a Table enforces constraints on the information inside it. A Wiki program might have many different tables in the database : one for handling users and passwords, one for handling the content of the wiki pages, and still another for handling administrative settings for the wiki as a whole. MySQL ([[http://www.mysql.com/]]), postgreSQL ([[http://www.postgresql.org/]]) and MongoDB ([[http://www.mongodb.org/]]) are all examples of database programs. We will be using MariaDB (a version of MySQL) for our media wiki installation because that is what Wikipedia uses. ** Wiki Software This is a set of programs that access the database and generate the HTML that will be delivered for any given page on the Wiki. These programs also write to the database whenever a page needs to be updated. Wiki software often includes images and CSS stylesheets to give a Wiki a basic style as well. We'll be using MediaWiki (http://www.mediawiki.org/wiki/MediaWiki) for our Wiki Software. ** Web Server A web server is like an email server in that it is always listening for input. An email server listens for emails, and a web server listens for people requesting a page. Your web server is the guardian and lens through which you present information from your server to the Web. It takes requests for pages such as http://aurellem.org/dl/constitution.txt and returns information depending on how you have configured it. You can configure a Web Server to do just about anything in response to a request for a page. A basic setup involves simply serving a folder of files from your computer to the world, and giving a [[./asdasdas][404]] error if someone requests something that isn't there. In our case, we will configure the Web Server to accept addresses like http://wiki.aurellem.org/index.php?title=Main_Page and request the Wiki Software to generate HTML appropriate for that address. You can configure a Web Server to run any program to generate responses to page requests, even one you write yourself! Whenever you visit a page on the internet, you can be sure that you are interacting with a Web Server program running on a computer connected to the internet. We will be using =lighttpd= (http://www.lighttpd.net/) because is is easy to configure. Other web servers include Apache (http://httpd.apache.org/) and Nginx (http://wiki.nginx.org/Main). * Prepare your Server Now you will get yourself your own personal server and your own domain, both of which are required for running a public wiki. If you already have a domain name and a server, or if you just want to configure a wiki for use on your own computer only, skip down to the "Install =MySQL=" section. * Get a Domain Name You can buy a domain name from namecheap.com. A domain name is like your personal name/the name of your house on the internet. My main domain name is "aurellem.org", because it means "little gold", and because it sounds like my initials, RLM. The steps to getting a domain name for yourself are as follows: 1.) Decide on a domain name. This is an important name that you might be using for a long time, so choose wisely. Be prepared to try multiple variations on your domain name as many domain names are already taken. 2.) Go to http://www.namecheap.com/ and register the domain name. The cost is about $12 per year for a domain name. * Get a Virtual Private Server I recommend Linode, since you can load the server you get with my favorite operating system, [[https://www.archlinux.org/][Arch GNU/Linux]], and because I have experienced stellar service from them for about a year so far. Steps towards getting a Virtual Private Server: 1.) Go to https://linode.com and buy a Virtual Private Server of your very own. - Select the Operating System as "Arch Linux", and set the physical location to the nearest location to you. You will then decide how to partition your server; the defaults that Linode offers are fine. Finally, you will have to set the /root password/ which is how you will access the server from your computer. Be sure to choose a secure password or your server can be easily compromised. * Link your domain name with your new server. # You are now the proud new owner of both a domain name and a virtual # private server, and will not have to spend anymore money on this # project today. This is a two step process -- you have to set your domain to point to the nameservers at linode.com, and you have to tell linode.com to point requests to your new virtual private server that you just bought. 1.) Go to namecheap.com again, select your domain, go to "Domain Name Server Setup", select "Specify Custom DNS Servers" and enter: - ns1.linode.com - ns2.linode.com - ns3.linode.com - ns4.linode.com - ns5.linode.com as the 5 DNS servers. 2.) Go back to linode.com and go to the "DNS Manager". Select "Add a domain zone" and enter your domain name. Select the defaults to get yourself started. It may take up to a day for the DNS system to sort itself out and for the entire world to recognize that your new domain name points to your new virtual private server. After that, you will be able to =ssh= into your new server using your new domain name. Once you are able to do this, you know that you have properly set up your domain name and new server. * Configure A records for your machine to create a "wiki" subdomain. The point of this section is to create the "wiki." domain. I like this setup for aesthetic reasons because it keeps your domain name open for other services you might install in the future, such as webmail at "email." or an FTP server at "ftp.". If you want your new domain name to serve as a wiki exclusively, feel free to adjust these directions accordingly. On my system visiting http://wiki.aurellem.org will bring you to the wiki, while http://aurellem.org will take you to this blog. 1.) Go to linode.com and select DNS Manager. 2.) Click "Add a domain zone" at the bottom right corner of the screen and enter the following values. | Domain: | wiki. | | SOA email: | | Select "Yes, insert a few records to get me started", using the default IP suggested. 3.) Delete the "mail" A Record, the "www" A Record, and the "mail." MX Record. (This is not strictly necessary, I just like to keep the records clean). * Set up your new sever. Login to your shiny new server by using: #+begin_src sh ssh root@ #+end_src You should fully upgrade your system by performing: #+begin_src sh pacman -Syyu #+end_src Now, you will want to create a new user for your machine. #+begin_src sh useradd -m --groups wheel #+end_src Finally, log off and log back on again as your new user. #+begin_src sh exit ssh @ #+end_src * Install =MySQL=, a database program #+begin_src sh sudo pacman -S mariadb libmariadbclient mariadb-clients #+end_src * Configure =MySQL= Tell =systemd= to start MySQL when the system starts. #+begin_src sh sudo systemctl enable mysqld.service #+end_src MySQL is like a file system, and just like the filesystem on your server, MySQL needs to have a root user that can access everything and create new users. To create sensible defaults and a root user for your MySQL database, run: #+begin_src sh sudo systemd start mysqld mysql_secure_installation #+end_src Note that when the script says: #+begin_example In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. #+end_example It is talking about the root user for the MySQL system, NOT the root user for your server. You should just press enter and not enter the password for the root user on your system. When it says: #+begin_example Set root password? [Y/n] #+end_example Select "Y" and create a new password for the root MySQL user. As with all passwords, I recommend that you set the root password on your MySQL database to a passphrase that is easy for you to remember but hard for anyone to guess. Four random English words such as "meticulous cougars flavorsome waltzing" should work very well and be essentially impossible to break. (Don't actually use "meticulous cougars flavorsome waltzing" now that I've written it here.) You should avoid using the same password for both your root server account and your MySQL account so that if someone compromises your wiki, they don't also get access to your server. Be sure to remember this root password as you will need it later to set up your wiki! Select "Y" for the rest of the options to remove unnecessary database elements created when MySQL was installed. Now, restart MySQL for your changes to take effect. #+begin_src sh sudo systemctl restart mysqld #+end_src Check to make sure that MySQL is running by executing: #+begin_src sh systemctl status mysqld #+end_src The first few lines of output on my system look like this: #+begin_example systemctl status mysqld mysqld.service - MariaDB database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since Sat 2013-08-31 18:00:38 EDT; 7min ago #+end_example Make sure that it says "Active: active (running)". * Install =php=, a "programming language". #+begin_src sh sudo pacman -S php php-cgi php-xcache php-gd php-intl #+end_src These programs provide the =php= language as well as several helper programs needed for our mediawiki. | php | base php system | | php-cgi | makes php faster by keeping php always ready | | php-xcache | makes php faster by caching common requests | | php-gd | interface for graphics in php | | php-intl | internationalization for php (displaying messages in different languages) | * Configure =/etc/php/php.ini= =php= is controlled by a global config file called php.ini. This file specifies which capabilities the php language will have on your system. Since we're setting up a wiki, we'll need to enable graphics, mysql, and a few other capabilities. Locate the "Dynamic Extensions" section of =/etc/php/php.ini=. It begins with #+begin_src conf ;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;; #+end_src Uncomment the following lines: #+begin_src conf extension=gd.so extension=intl.so extension=mysql.so extension=zip.sh #+end_src Add the following line at the end of the Dynamic Extensions section: #+begin_src conf extension=xcache.so #+end_src The Dynamic Extension section should now look like this: #+begin_src conf ;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;; ; If you wish to have an extension loaded automatically, use the ; following syntax: ; ; extension=modulename.extension ; ; For example, on Windows: ; ; extension=msql.dll ; ; ... or under UNIX: ; ; extension=msql.so ; ; ... or with a path: ; ; extension=/path/to/extension/msql.so ; ; If you only provide the name of the extension, PHP will look for it ; in its default extension directory. ; ;extension=bcmath.so ;extension=bz2.so ;extension=calendar.so extension=curl.so ;extension=dba.so ;extension=enchant.so ;extension=exif.so ;extension=ftp.so extension=gd.so extension=gettext.so ;extension=gmp.so ;extension=iconv.so ;extension=imap.so extension=intl.so ;extension=ldap.so ;extension=mcrypt.so ;extension=mssql.so ;extension=mysqli.so extension=mysql.so ;extension=odbc.so ;zend_extension=opcache.so ;extension=openssl.so ;extension=pdo_mysql.so ;extension=pdo_odbc.so ;extension=pdo_pgsql.so ;extension=pdo_sqlite.so ;extension=pgsql.so ;extension=phar.so ;extension=posix.so ;extension=pspell.so ;extension=shmop.so ;extension=snmp.so ;extension=soap.so ;extension=sockets.so ;extension=sqlite3.so ;extension=sysvmsg.so ;extension=sysvsem.so ;extension=sysvshm.so ;extension=tidy.so ;extension=xmlrpc.so ;extension=xsl.so extension=zip.so extension=xcache.so #+end_src * Install MediaWiki software and related files. #+begin_src sh sudo pacman -S imagemagick mediawiki #+end_src You may eventually host many websites and many wikis from your server, and accordingly, you should make things easier for yourself and setup your first wiki so that you can easily make more of them in the future. (Instead of setting it up so that you can only serve a single wiki from your machine.) Choose a directory on your system that will serve as the base directory for your system. On my system, my user is called =r=, and I use =/home/r/wiki/aurellem= to serve http://wiki.aurellem.org. Other wikis I host get their own directory in =/home/r/wiki=. If your domain was =lolcatsforever.com= and your username on your server was =catz=, you might consider using =/home/catz/wiki/lolcatsforever= as the base folder for the wiki at http://wiki.lolcatsforever.com. Whatever you pick is entirely your choice, and from now on I'll refer to the directory you chose as . You will now need to create and link some files to prepare your wiki. #+begin_src sh mkdir -p cd ln -s /usr/share/webapps/mediawiki/* . rm images mkdir images sudo chown :http images chmod g+w images #+end_src This creates symlinks from the files you installed from the mediawiki package to your . You create symlinks instead of copying the files so that whenever you upgrade mediawiki, the files for all of your wikis will be updated as well. You remove the symlink to =/usr/share/webapps/mediawik/images= and replace it with a folder of the same name because mediawiki stores images related to a wiki in the =images= folder and thus each wiki needs its own =images= folder. The =chown= and =chmod= commands enable lighttpd to access the images folder an put images into it whenever someone uploads an image to your wiki. * Install lighttpd, a powerful Web Server. #+begin_src sh sudo pacman -S fgci lighttpd #+end_src Instruct =systemd= to start lighttpd on boot. #+begin_src sh sudo systemctl enable lighttpd #+end_src Start the lighttpd server. #+begin_src sh sudo systemctl start lighttpd #+end_src * Configure =/etc/lighttpd/lighttpd.conf= The config file for lighttpd is the most complicated file to prepare because lighttpd can do so many different things. A minimal wiki config for lighttpd follows. Replace and with the appropriate values and replace =/etc/lighttpd/lighttpd.conf= with the following: #+begin_src conf server.modules = ( "mod_accesslog", "mod_cgi", "mod_fastcgi", "mod_rewrite", "mod_auth" ) server.document-root = "/srv/http" dir-listing.activate = "enable" server.port = 80 server.username = "http" server.groupname = "http" server.errorlog = "/var/log/lighttpd/error.log" index-file.names = ( "index.html", "index.php" ) mimetype.assign = ( ".html" => "text/html", ".txt" => "text/plain", ".jpg" => "image/jpeg", ".png" => "image/png", ".css" => "text/css", ".php" => "text/x-php", "" => "application/octet-stream" ) fastcgi.server = ( ".php" => (( "bin-path" => "/usr/bin/php-cgi", "socket" => "/tmp/php.socket"))) $HTTP["host"] =~ "^wiki.*" { server.document-root = "" } #+end_src Be sure to replace and above with the appropriate values for your system. For example, on my system, I use: #+begin_src conf $HTTP["host"] =~ "^wiki.aurellem.org*" { server.document-root = "/home/r/wiki/aurellem" } #+end_src Execute #+begin_src sh sudo systemctl restart lighttpd #+end_src To restart lighttpd with the new settings, and then check #+begin_src sh systemctl status lighttpd #+end_src To confirm that lighttpd is still running. * Configure Your Wiki Now you can visit http://wiki. and configure your new wiki. You should be greeted with something that looks like this. #+caption: Your new Wiki awaits. #+ATTR_HTML: width="400px" [[../images/wiki-greeting.png]] Click and follow the on screen directions. Below are the settings I used on for my own wiki. Modify yours accordingly. ** 'Connect to database' section | Database host: | localhost | | Database name: | aurellem | | Database table prefix: | aurellem | | Database username: | root | | Database password: | | ** 'Database settings' section | Use the same account as for installation | UNCHECKED | | Database username | aurellem | | Create the account if it does not already exist | CHECKED | | Storage Engine | InnoDB | | Database character set | Binary | ** 'Name' section | Name of wiki | aurellem | | Project namespace | Same as the wiki name: Aurellem | | Ask me more questions. | CHECKED | ** Options | Enable file uploads | CHECKED | | Enable Instant Commons | CHECKED | | Settings for object caching: | PHP object caching (APC, XCache or WinCache) | * Upload =LocalSettings.php= The configuration will end by offering you a file called =LocalSettings.php=. Save this file to your computer and then upload it by opening a terminal, changing to the directory into which you downloaded =LocalSettings.php=, and executing: #+begin_src sh scp LocalSettings.php @: #+end_src on my system, I executed: #+begin_src sh scp LocalSettings.php r@aurellem.org:/home/r/wiki/aurellem #+end_src * Configure your Wiki Logo Create a logo image and upload it to your wiki base with #+begin_src sh scp @: #+end_src I used the following image: [[../images/sun.png]] and executed: #+begin_src sh scp sun.png r@aurellem.org:/home/r/wiki/aurellem #+end_src Then edit =/LocalSettings.php= and find the part where it defines =$wgLogo= I changed mine from : #+begin_src perl $wgLogo = "$wgStylePath/common/images/wiki.png"; #+end_src to: #+begin_src perl $wgLogo = "/sun.png"; #+end_src Notice that the initial "/" is required. * Exploring Your New System Congratulations! You have a shiny new wiki that your and your friends can edit and use to collaborate. Here are a few commands that you can use to examine your new system. ** Lighttpd To get the status of lighttpd, use: #+begin_src sh systemctl status lighttpd #+end_src You can also check =/var/log/lighttpd/error.log= for useful information. #+begin_src sh cat /var/log/lighttpd/error.log #+end_src If you can't access your wiki at all for some reason, these are the first things to check. ** MySQL You can use the terminal to access your MySQL database and examine the tables and records there. Here's an example session of me interacting with my server. #+begin_example [dark-star ~] $ mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 221 Server version: 5.5.32-MariaDB-log Source distribution Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aurellem | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> use aurellem; Database changed MariaDB [aurellem]> show tables; +----------------------------+ | Tables_in_aurellem | +----------------------------+ | aurellemarchive | | aurellemcategory | | aurellemcategorylinks | | aurellemchange_tag | | aurellemexternal_user | | aurellemexternallinks | | aurellemfilearchive | | aurellemhitcounter | | aurellemimage | | aurellemimagelinks | | aurelleminterwiki | | aurellemipblocks | | aurellemiwlinks | | aurellemjob | | aurelleml10n_cache | | aurellemlanglinks | | aurellemlog_search | | aurellemlogging | | aurellemmodule_deps | | aurellemmsg_resource | | aurellemmsg_resource_links | | aurellemobjectcache | | aurellemoldimage | | aurellempage | | aurellempage_props | | aurellempage_restrictions | | aurellempagelinks | | aurellemprotected_titles | | aurellemquerycache | | aurellemquerycache_info | | aurellemquerycachetwo | | aurellemrecentchanges | | aurellemredirect | | aurellemrevision | | aurellemsearchindex | | aurellemsite_stats | | aurellemtag_summary | | aurellemtemplatelinks | | aurellemtext | | aurellemtranscache | | aurellemupdatelog | | aurellemuploadstash | | aurellemuser | | aurellemuser_former_groups | | aurellemuser_groups | | aurellemuser_newtalk | | aurellemuser_properties | | aurellemvalid_tag | | aurellemwatchlist | +----------------------------+ 49 rows in set (0.00 sec) MariaDB [aurellem]> select user_name from aurellemuser; +-----------------+ | user_name | +-----------------+ | Rlm | | Robert McIntyre | +-----------------+ 2 rows in set (0.00 sec) #+end_example At this point I went to http://wiki.aurellem.org and created a new user named "new-user". Then in this same SQL session I executed the select command again. #+begin_example MariaDB [aurellem]> select user_name from aurellemuser; +-----------------+ | user_name | +-----------------+ | New-user | | Rlm | | Robert McIntyre | +-----------------+ 3 rows in set (0.00 sec) MariaDB [aurellem]> quit; Bye #+end_example Notice how the users table changes when I add a new user to the wiki! ** == You can always check the images you and others have uploaded by going to =/images= and running =tree=. If you don't have the program =tree=, install it with. #+begin_src sh sudo pacman -S tree #+end_src When I execute #+begin_src sh tree /home/r/wiki/aurellem/images #+end_src I see: #+begin_example /home/r/wiki/aurellem/images/ |-- archive | `-- e | `-- e1 |-- deleted | `-- 3 | `-- t | `-- z | |-- 3tz2h6a0r28vsaj39rd9zd939v4ylyi.jpg | `-- index.html |-- e | `-- e1 |-- lockdir `-- thumb |-- 5 | `-- 5b | `-- Name.jpg | |-- 120px-Name.jpg | `-- Name.jpg |-- a | `-- a9 | `-- Example.jpg | |-- 116px-Example.jpg | `-- Example.jpg `-- e `-- e1 19 directories, 6 files #+end_example You can delete images for good by going to the images directory and removing them. * How to Backup your Wiki Disks fail, companies go out of business, and you may be hacked at some point, so you should know how to backup your wiki and also how to restore it from the backup. In contrast to normal files, in order to backup your wiki you have to use a specialized MySQL "data dumper" to get the information contained in your MySQL database. To backup your database, use: #+begin_src sh mysqldump -u root -p > #+end_src On my system, this command is: #+begin_src sh mysqldump -u root -p aurellem > aurellem-wiki-backup.sql #+end_src The wiki database name is the name you selected for the database when installing your wiki. If you don't remember it then you can execute: #+begin_src sh mysql -u root -p #+end_src And then enter: #+begin_src sql show databases; #+end_src at the SQL prompt. The backup file is a human readable sequence of SQL statements that together restores the wiki's tables when executed. Take a look at it -- it's quite interesting. You can only run this command from your server, but once you have generated the backup file, you should copy it to another location to protect from the possibility of your server being destroyed. To restore your wiki after a catastrophic failure, run the following commands. First, an empty database with the same name as your wiki database. #+begin_src sh mysql -u root -p #+end_src At the sql prompt, type: #+begin_src sh create database ; #+end_src On my machine I typed: #+begin_src sh create database aurellem; #+end_src The output should be: #+begin_example Query OK, 1 row affected (0.00 sec) #+end_example Then, restore the contents of your wiki database from backup: At a terminal prompt, type: #+begin_src sh mysql -u root -p < #+end_src On my machine this command would be: #+begin_src sh mysql -u root -p aurellem < aurellem-wiki-backup.sql #+end_src Your wiki database will be restored. You will also want to backup the files in ==. You can do this by simply copying them to another computer. One nice way to do this is: #+begin_src sh tar cvjf "wiki-backup-`date`.tar.bz2" #+end_src This will create a dated archive file with all your wiki files (including symlinks) which you can restore anytime by unpacking the archive file with: #+begin_src sh cd cd .. tar xvf #+end_src * How to create more Wikis Now that you've created one wiki, creating another one can be done in 5 minutes or less. Here are the steps. ** Create an appropriate A record for your new wiki's subdomain For the purposes of this we'll use wiki.new-site.org. ** Decide on a directory to serve as the base for your new wiki. For example =/home/r/wiki/new-site= ** Add an entry to =/etc/lighttpd/lighttpd.conf= In this case we would use the following: #+begin_src conf $HTTP["host"] =~ "^wiki.new-site.org*" { server.document-root = "/home/r/wiki/new-site" } #+end_src ** Symlink files from =/usr/share/webapps/mediawiki/= to your wiki base You also have to set proper permissions in the =images= folder in your new wiki base. In this case, I would execute: #+begin_src sh mkdir -p /home/r/wiki/new-site cd /home/r/wiki/new-site ln -s /usr/share/webapps/mediawiki/* . rm images mkdir images sudo chown :http images chmod g+w images #+end_src ** Complete wiki configuration from your browser. I would go to http://wiki.new-site.org and complete configuration, then upload the generated LocalSettings.php to my server with: #+begin_src sh scp LocalSettings.php r@aurellem.org:/home/r/wiki/new-site #+end_src * Source Listing #+html: * COMMENT Test =php= to be sure it works. run the following : #+begin_src sh php -nr 'echo "hello world!\n";' #+end_src You should see "hello world!" displayed on the screen, like this: #+begin_example [dark-star ~] $ php -nr 'echo "hello world!\n";' hello world! #+end_example * COMMENT resources - https://wiki.archlinux.org/index.php/Lighttpd - https://wiki.archlinux.org/index.php/Mediawiki - http://www.mediawiki.org/wiki/Manual:Running_MediaWiki_on_Arch_Linux - http://sharkysoft.com/wiki/how_to_configure_multiple_MediaWiki_instances_on_a_single_host http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/ * COMMENT bugs When installing mediawiki, "mail" is misspelled as "meail" #+begin_example Optional dependencies for mediawiki texvc: for math rendering python2 [installed] pcre: for regular expressions support [installed] php-intl: to handle Unicode normalization [installed] php-mysql: for MySQL database support php-pgsql: for PostgreSQL database support php-sqlite: for sqlite database support php-apc: for cache support php-xcache: for cache support [installed] memcached: for cache support php-gd: for thumbnails rendering [installed] imagemagick: for thumbnails rendering [installed] sendmail: for meail sending #+end_example