本教程适用于以下操作系统版本

  • Ubuntu 20.04 (Focal Fossa)
  • Ubuntu 16.04 (Xenial Xerus)

本页内容

  • 先决条件
  • 开始之前
  • 安装 Nginx、MariaDB 和 PHP
  • 创建 MariaDB 数据库
  • 下载 MediaWiki
  • 为 MediaWiki 配置 Nginx
  • 访问 MediaWiki Web 界面
  • 使用 Let's Encrypt SSL 保护 MediaWiki
  • 结论

MediaWiki 是一款用 PHP 编写的开源 wiki 软件。它使你能够在服务器上创建自己的自托管 wiki 网站。由于其简单性和可定制性,它是最受欢迎的 wiki 平台之一。目前,许多公司使用它来管理其 wiki 页面。它提供了一个多功能且免费的工具,用于在互联网上发布内容。

在本教程中,我们将向你展示如何在 Ubuntu 20.04 上使用 Nginx Web 服务器和 Let's Encrypt SSL 安装 MediaWiki。

先决条件

  • 一台运行 Ubuntu 20.04 的服务器。
  • 一个指向你服务器 IP 的有效域名。
  • 服务器上已配置 root 密码。

开始之前

首先,通过运行以下命令将系统软件包更新到最新版本:

apt-get update -y

所有软件包更新完成后,你可以继续下一步。

安装 Nginx、MariaDB 和 PHP

MediaWiki 需要 Nginx Web 服务器、MariaDB 数据库服务器、PHP 以及其他扩展。你可以使用以下命令安装所有这些组件:

apt-get install nginx mariadb-server php php-fpm php-mbstring php-xml php-json php-mysql php-curl php-intl php-gd php-mbstring texlive imagemagick unzip -y

所有软件包安装完成后,使用以下命令安装 Composer:

apt-get install composer -y

接下来,编辑 php.ini 文件并更改默认设置:

nano /etc/php/7.4/fpm/php.ini

更改以下行:

memory_limit = 512M
post_max_size =32M
upload_max_filesize = 32M
date.timezone = Asia/Kolkata

保存并关闭文件,然后重启 PHP-FPM 以应用更改:

systemctl restart php7.4-fpm

完成后,你可以继续下一步。

创建 MariaDB 数据库

MediaWiki 使用 MariaDB 作为数据库后端,因此你需要为 MediaWiki 创建一个数据库和用户。

首先,使用以下命令连接到 MariaDB:

mysql

连接成功后,使用以下命令创建数据库和用户:

MariaDB [(none)]> CREATE DATABASE mediadb;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON mediadb.* TO 'mediauser'@'localhost' IDENTIFIED BY 'password';

接下来,使用以下命令刷新权限并退出 MariaDB:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

完成后,你可以继续下一步。

下载 MediaWiki

首先,访问 MediaWiki 网站并选择最新版本的 MediaWiki。然后,运行以下命令将其下载到你的服务器:

wget https://releases.wikimedia.org/mediawiki/1.35/mediawiki-1.35.2.zip

下载完成后,使用以下命令解压缩下载的文件:

unzip mediawiki-1.35.2.zip

接下来,使用以下命令将解压后的目录移动到 Nginx Web 根目录:

mv mediawiki-1.35.2 /var/www/html/mediawiki

接下来,切换到 MediaWiki 目录,并使用以下命令安装所有 PHP 依赖项:

cd /var/www/html/mediawiki
composer install --no-dev

所有依赖项安装完成后,使用以下命令设置适当的权限和所有权:

chown -R www-data:www-data /var/www/html/mediawiki
chmod -R 755 /var/www/html/mediawiki

完成后,你可以继续下一步。

为 MediaWiki 配置 Nginx

接下来,你需要为 MediaWiki 创建一个 Nginx 虚拟主机配置文件。你可以使用以下命令创建它:

nano /etc/nginx/conf.d/wiki.conf

添加以下行:

server {
        listen 80;
        server_name wiki.example.com;

        root /var/www/html/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 ~ /\.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;
        }
}

保存并关闭文件,然后使用以下命令检查 Nginx 是否存在语法错误:

nginx -t

你应该看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,重启 Nginx 服务以应用更改:

systemctl restart nginx

你还可以使用以下命令检查 Nginx 的状态:

systemctl status nginx

你应该看到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-06-02 05:06:48 UTC; 3s ago
       Docs: man:nginx(8)
    Process: 24594 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 24605 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 24606 (nginx)
      Tasks: 2 (limit: 2353)
     Memory: 2.8M
     CGroup: /system.slice/nginx.service
             ??24606 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??24607 nginx: worker process

Jun 02 05:06:48 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 02 05:06:48 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.

访问 MediaWiki Web 界面

现在,打开你的 Web 浏览器并输入 URL http://wiki.example.com 。你将被重定向到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

现在,点击 set up the wiki 按钮。你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

在这里,选择你的 wiki 语言并点击 Continue 按钮。你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

现在,点击 Continue 按钮。你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

现在,提供你的数据库详细信息并点击 Continue 按钮。你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

选择 use the same account as for installation 并点击 Continue 按钮。你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

现在,提供你的 wiki 站点名称、用户名和密码。然后,点击 Continue 按钮。你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

点击 Continue 按钮开始安装。你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

点击 Continue 按钮。安装完成后,你应该看到以下页面:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

现在,点击 download 按钮将 LocalSettings.php 文件下载到你的系统。然后,将此文件复制到服务器上 MediaWiki 根目录内,并使用以下命令设置适当的权限:

chown www-data:www-data /var/www/html/mediawiki/LocalSettings.php

接下来,返回你的 Web 浏览器并点击 enter your wiki 。你应该在以下页面上看到 MediaWiki 仪表板:

如何在 Ubuntu 20.04 上用 Nginx 和免费证书安装 MediaWiki 完整教程

使用 Let's Encrypt SSL 保护 MediaWiki

接下来,你需要安装 Certbot 客户端软件包,以安装和管理 Let's Encrypt SSL。

首先,使用以下命令安装 Certbot:

apt-get install python3-certbot-nginx -y

安装完成后,运行以下命令在你的网站上安装 Let's Encrypt SSL:

certbot --nginx -d wiki.example.com

系统将要求你提供一个有效的电子邮件地址并接受服务条款,如下所示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for wiki.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/wiki.conf

接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

输入 2 并按 Enter 键完成安装。你应该看到以下输出:

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/wiki.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://wiki.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=wiki.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/wiki.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/wiki.example.com/privkey.pem
   Your cert will expire on 2021-12-30. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

现在,你的网站已使用 Let's Encrypt SSL 进行保护。你可以使用 URL https://wiki.example.com 安全地访问它。

结论

恭喜!你已在 Ubuntu 20.04 上使用 Nginx 和 Let's Encrypt SSL 成功安装了 MediaWiki。现在你可以轻松地使用 MediaWiki 托管自己的 wiki 站点。