This will install Mediawiki in Lightsail. I’m assuming you already setup SSH, and LAMP. Otherwise read my previous posts.


Useful apt packages

sudo apt install imagemagick php-cli php-apcu
sudo apt install php-mbstring
sudo apt install php-xml
sudo phpenmod mbstring
sudo phpenmod xml

The configuration is at /etc/php/7.2/apache2/php.ini. We don’t need to change anything now.

Enable Apache modules proxy, proxy_http, and rewrite. Then restart Apache.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo service apache2 restart
sudo a2enmod ssl

If you ever need to edit the Apache configuration, it is at /etc/apache2/apache2.conf.

Possibly you’ve seen Perl bitterly complaining that its language is not configured. It doesn’t really matter, but you can solve it adding this to ~/.bashrc:

export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

See that LocalSettings.php points to the database and has the correct password.

sudo nano /var/www/notes/LocalSettings.php

Decompress the mediawiki distribution in the directory /var/www/wikijano (replace wikijano with whatever you see fit).

cd /var/www/
sudo curl -o mediawiki-core-1.34.2.tar.gz
sudo tar -zxvf mediawiki-core-1.34.2.tar.gz
sudo mv mediawiki-core-1.34.2.tar.gz wikijano
sudo chown -R root:root wikijano

To point Apache to the mediawiki directory

  • Edit /etc/apache2/sites-available/000-default.conf
  • Replace DocumentRoot /var/www/html with DocumentRoot /var/www/wikijano
  • Restart Apache with sudo systemctl restart apache2.

Now open the domain in your browser. Mediawiki will initiate the configuration through a web interface. You will need the password you installed MySQL with. Once you are done the server will produce a file LocalSettings.php. To complete the installation you have to copy this file to /var/www/wikijano/LocalSettings.php. You can do this with Transmit, scp, rsync, or copy paste through the clipboard.

At this point your mediawiki should be up and running.

Change upload file size limit

Add the following to /var/www/notes/LocalSettings.php

$wgMaxUploadSize = 10485760; # 10Mb

Add the following to /etc/php/7.0/apache2/php.ini

upload_max_filesize = 10M
post_max_size = 10M

Restart Apache2

sudo systemctl restart apache2

Good to know

Dealing with MySQL

Useful commands

# remove MySQL and database
sudo apt-get purge mysql-server

# install MySQL
sudo apt-get install mysql-server

# run the security script
sudo mysql_secure_installation

# connect to mysql as root using auth_socket (works on a fresh installation)
sudo mysql

# stop mysql (works also with start/stop/restart)
sudo systemctl stop mysql

# check the mysql status
systemctl status mysql.service

# find the socket file –in Ubuntu it will be /run/mysqld/mysqld.sock
sudo find / -type s | grep mysqld

The security script issues prompts for the following:

  • Set a password for the MySQL user
  • Remove anonymous users
  • Disallow root login remotely
  • Remove the test database
  • Reload the privilege tables

Set a password for root and answer y to everything else.

Authentication in MySQL

There are several ways to authenticate a user in MySQL:

  • auth_socket. It lets you connect locally as a MySQL user that has the same user name as your Unix user, without challenging you to prove your identity providing a password or anything else. On a fresh installation this is the authentication method of root.
  • mysql_native_password
  • …others

On a fresh installation connect as root with sudo mysql and show authentication methods:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
| user             |  string        | plugin                | host      |
| root             |                | auth_socket           | localhost |
| mysql.session    | *THISISNOTA... | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTA... | mysql_native_password | localhost |
| debian-sys-maint | *DCDAECCBD3... | mysql_native_password | localhost |

Change authentication method for the root user to password secret. You need flush privileges any time you change an user, but not when you create one.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'secret';

How to reset mediawiki password

cd /var/www/wikijano
sudo php ./maintenance/changePassword.php --user="jano" --password="SECRETO"

How to backup mediawiki

Create a script with the following content:



mkdir -p $BACKUP_DIR
mkdir -p $BACKUP_WWW_DIR

ssh ubuntu@lightsail mysqldump -u root --password=yourpasswordhere --skip-extended-insert --single-transaction -B notes | gzip > $BACKUP_MYSQL_DIR/`date +%m_%d_%y`_notes.gz
rsync --delete --exclude='Maildir' --exclude='mailbox' --progress -aqhe ssh ubuntu@lightsail:/var/www $BACKUP_RSYNC_DIR
tar -czf $BACKUP_WWW_DIR/web_`date +%m_%d_%y`.tgz $BACKUP_RSYNC_DIR

echo Lightsail backup is done.

exit 0

The script will output the following

mysqldump: [Warning] Using a password on the command line interface can be insecure.
tar: Removing leading '/' from member names
Lightsail backup is done.

Note that I did the mysqldump with --single-transaction which is slower than normal, but it will avoid problems like Error Code: 2013. Lost connection to MySQL server during query.

On some cases you may still want to fine tune some parameters. For instance:

SET @@GLOBAL.max_allowed_packet=1073741824;
SET @@local.net_read_timeout=360;
SET @@local.interactive_timeout=60;
Select @@GLOBAL.max_allowed_packet;
Select @@local.net_read_timeout;
Select @@local.interactive_timeout;

It depends on your case. I had problems upgrading the mediawiki database on a Lightsail instance with 512MB.