如何使用 Nginx 安装 MediaWiki 并在 Ubuntu 20.04 上加密 SSL
本教程适用于这些操作系统版本
- Ubuntu 20.04(Focal Fossa)
- Ubuntu 16.04(Xenial Xerus)
在此页
- 先决条件
- 开始
- 安装 Nginx、MariaDB 和 PHP
- 创建 MariaDB 数据库
- 下载 MediaWiki
- 为 MediaWiki 配置 Nginx
- 访问 MediaWiki 网络用户界面
- 使用 Lets Encrypt SSL 保护 MediaWiki
- 结论
MediaWiki 是一个用 PHP 编写的开源 wiki 软件。它允许您在服务器上创建自己的自托管 wiki 网站。由于其简单性和可定制性,它是最受欢迎的 wiki 平台之一。目前,许多公司使用它来管理他们的 wiki 页面。它提供了一个多功能的免费工具,用于在 Internet 上发布内容。
在本教程中,我们将向您展示如何在 Ubuntu 20.04 上使用 Nginx Web 服务器和 Lets Encrypt SSL 安装 MediaWiki。
先决条件
- 一台运行 Ubuntu 20.04 的服务器。
- 用您的服务器 IP 指向的有效域名。
- 在服务器上配置了根密码。
入门
首先,通过运行以下命令将系统包更新到更新版本:
apt-get update -y
更新所有包后,您可以继续下一步。
安装 Nginx、MariaDB 和 PHP
MediaWiki 需要 Nginx 网络服务器、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 ubunt4 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 02 05:06:48 ubunt4 systemd[1]: Started A high performance web server and a reverse proxy server.
访问 MediaWiki 网页用户界面
现在,打开您的 Web 浏览器并输入 URL http://wiki.example.com。您将被重定向到以下页面:

现在,单击设置 wiki 按钮。您应该会看到以下页面:

在这里,选择您的 wiki 语言并单击继续按钮。您应该会看到以下页面:

现在,单击“继续”按钮。您应该会看到以下页面:

现在,提供您的数据库详细信息并单击“继续”按钮。您应该会看到以下页面:

选择使用与安装相同的帐户,然后单击继续按钮。您应该会看到以下页面:

现在,提供您的 wiki 站点名称、用户名和密码。然后,单击“继续”按钮。您应该会看到以下页面:

单击“继续”按钮开始安装。您应该会看到以下页面:

单击继续按钮。安装完成后,您应该会看到以下页面:

现在,单击下载按钮将 LocalSettings.php 文件下载到您的系统。然后,将此文件复制到 MediaWiki 根目录中的服务器,并使用以下命令设置适当的权限:
chown www-data:www-data /var/www/html/mediawiki/LocalSettings.php
接下来,返回您的网络浏览器并单击进入您的 wiki。您应该在以下页面上看到 MediaWiki 仪表板:

使用 Lets Encrypt SSL 保护 MediaWiki
接下来,您需要安装 Certbot 客户端包来安装管理 Lets Encrypt SSL。
首先,使用以下命令安装 Certbot:
apt-get install python3-certbot-nginx -y
安装完成后,运行以下命令在您的网站上安装 Lets 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):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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.
现在,您的网站已通过 Lets Encrypt SSL 得到保护。您可以使用 URL https://wiki.example.com 安全地访问它。
结论
恭喜!您已经在 Ubuntu 20.04 上成功安装了带有 Nginx 和 Lets Encrypt SSL 的 MediaWiki。您现在可以使用 MediaWiki 轻松托管自己的 wiki 站点。