How to Install MediaWiki on Ubuntu 20.04 with Apache/Nginx
This tutorial is going to show you how to install MediaWiki on Ubuntu 20.04. MediaWiki is the backend software that powers Wikipedia. It’s free open-source and the most widely used wiki software today. Well-known users of MediaWiki include Wikipedia.org and wikia.com.
You can use MediaWiki to create your own private or public wiki site. MediaWiki has a lot of useful extensions that have been created both for Wikipedia and for other wiki sites.
Prerequisites
MediaWiki is a program written in PHP. To follow this tutorial, first you need to have a LAMP or LEMP stack installed on your Ubuntu 20.04 server.
If you prefer Apache web server, then set up LAMP stack.
If you prefer Nginx web server, then set up LEMP stack.
You also need a domain name. I registered my domain name from NameCheap because the price is low and they give whois privacy protection free for life.
After installing LAMP or LEMP stack, read the following instructions to install MediaWiki.
Step 1: Downloading MediaWiki
Download the latest stable version of MediaWiki:
wget https://releases.wikimedia.org/mediawiki/1.35/mediawiki-1.35.1.tar.gz
Extract the archive to /var/www/
.
sudo mkdir -p /var/www/ sudo tar xvf mediawiki-1.35.1.tar.gz -C /var/www/
Rename the directory.
sudo mv /var/www/mediawiki-1.35.1 /var/www/mediawiki
Then we need to install some PHP extensions required by MediaWiki.
sudo apt install php7.4-mbstring php7.4-xml php7.4-fpm php7.4-json php7.4-mysql php7.4-curl php7.4-intl php7.4-gd php7.4-mbstring texlive imagemagick
If you use Apache web server, then you need to restart Apache.
sudo systemctl restart apache2
Next, we need to install external dependencies via Composer (a PHP dependency manager).
sudo apt install composer cd /var/www/mediawiki/ sudo composer install --no-dev
Note that MediaWiki currently doesn’t support PHP8.0. If you have installed PHP8.0 on your Ubuntu server, then you should run sudo update-alternatives --config php
command to set PHP7.4 as the default version.
Once all dependencies are installed, run the following command to set web server user (www-data
) as the owner of this directory.
sudo chown www-data:www-data /var/www/mediawiki/ -R
Step 2: Creating a Database
Log into MariaDB server with the command below.
sudo mysql -u root
Create a database for MediaWiki. This tutorial name the database mediawiki
, but you can use whatever name you like.
CREATE DATABASE mediawiki;
Then run the following command at MariaDB prompt to create a database user and grant privileges to this user. Replace mediawiki
, wikiuser
and password
with your preferred database name, database username and user password respectively.
GRANT ALL PRIVILEGES ON mediawiki.* TO 'wikiuser'@'localhost' IDENTIFIED BY 'password';
Next, flush MariaDB privileges and exit.
flush privileges; exit;
Step 3: Create Apache Virtual Host or Nginx Config File for MediaWiki
Apache
If you use Apache web server, create a virtual host for MediaWiki.
sudo nano /etc/apache2/sites-available/mediawiki.conf
Copy and paste the following text into the file. Replace wiki.your-domain.com
with your actual domain name. Don’t forget to create DNS A record for this domain name.
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www//mediawiki/ ServerName wiki.your-domain.com <Directory /var/www/html/mediawiki/> Options FollowSymLinks AllowOverride All Order allow,deny allow from all </Directory> ErrorLog /var/log/apache2/mediawiki_error CustomLog /var/log/apache2/mediawiki_access common </VirtualHost>
Save and close the file. Then enable this virtual host.
sudo a2ensite mediawiki.conf
Reload Apache for the above changes to take effect.
sudo systemctl reload apache2
Nginx
If you use Nginx web server, create a server block file for MediaWiki under /etc/nginx/conf.d/
directory.
sudo nano /etc/nginx/conf.d/mediawiki.conf
Copy the following text and paste it into the file. Replace wiki.your-domain.com
with your actual domain name. Don’t forget to create DNS A record for this domain name.
server {
listen 80;
listen [::]:80;
server_name wiki.your-domain.com;
root /var/www/mediawiki;
index index.php;
error_log /var/log/nginx/mediawiki.error;
access_log /var/log/nginx/mediawiki.access;
location / {
try_files $uri $uri/ /index.php;
}
location ~ /.well-known {
allow all;
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}
}
Save and close the file. Then test Nginx configuration.
sudo nginx -t
If the test is successful, reload Nginx web server.
sudo systemctl reload nginx
Step 4: Enabling HTTPS
To encrypt the HTTP traffic, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 20.04 server.
sudo apt install certbot
If you use Apache, install the Certbot Apache plugin.
sudo apt install python3-certbot-apache
And run this command to obtain and install TLS certificate.
sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d wiki.your-domain.com
If you use Nginx, then you also need to install the Certbot Nginx plugin.
sudo apt install python3-certbot-nginx
Next, run the following command to obtain and install TLS certificate.
sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d wiki.your-domain.com
Where
--nginx
: Use the nginx plugin.--apache
: Use the Apache plugin.--agree-tos
: Agree to terms of service.--redirect
: Force HTTPS by 301 redirect.--hsts
: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.--staple-ocsp
: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.
The certificate should now be obtained and automatically installed.
Step 5: Running the Web-based Installation Script
Go to https://wiki.you-domain.com
in your web browser to start the web-based installation script. Click Complete the installation link to start the setup wizard.
Next, select a language to use for the MediaWiki installation and for the wiki itself.
The setup wizard will then check your server’s environment, such as PHP extensions.
Go to the next step. You will need to configure the database settings. Select MariaDB, MySQL, or compatible
for Database type
. Enter localhost
for Database host and enter the name of the database mediawiki
as well as the database username and password that are created in step 2.
In the next step, you can choose to use the same database account for web access.
Then enter the name of your Wiki and create an admin account.
In the Options
page, you have the option to set up your wiki as Open wiki, account creation required, authorized editors only and a private wiki. Then you can choose a license.
Click Continue
to begin the installation of Mediawiki.
A LocalSettings.php
file will be generated in the installation process. You need to download this file and put it in /var/www/mediawiki/
directory of your Ubuntu 20.04 server. Once that’s done, you will be able to access your newly-installed MediaWiki at http://wiki.your-domain.com
.
To create a page for a term, simply go to
wiki.your-domain.com/index.php/your-term
or
wiki.your-domain.com/index.php?title=your-term
How to Set Up SMTP to Send Emails
If your MediaWiki instance will be used by more than one person, it’s important that your MediaWiki server can send transactional emails, such as password-resetting email, notification email, etc.
Edit the LocalSettings.php
file.
sudo nano /var/www/mediawiki/LocalSettings.php
Add the following lines at the end of this file.
$wgSMTP = [ 'host' => 'tls://mail.your-domain.com', // could also be an IP address. Where the SMTP server is located. If using SSL or TLS, add the prefix "ssl://" or "tls://". 'port' => 587, // Port to use when connecting to the SMTP server 'auth' => true, // Should we use SMTP authentication (true or false) 'username' => '[email protected]', // Username to use for SMTP authentication (if being used) 'password' => 'user_password' // Password to use for SMTP authentication (if being used) ];
Save and close the file.
For how to set up an email server, please check out the following tutorial. Note that I highly recommend running iRedMail mail server on a fresh clean OS. Installing iRedMail on an OS that has other web applications can fail, and likely break existing applications.
Wrapping Up
This tutorial described how to install MediaWiki on Ubuntu 20.04. I hope this helped you. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂
Never ever install composer with
sudo apt install composer
because it will mess up the system with a lot of files from Symfony and other packages. Always use composer-setup.php, as described on getcomposer.org.
I never had a problem with
sudo apt install composer
.Anybody have issues when migrating a wikidb running on an older version of mediawiki from a CentOS server to this setup?
Thanks!