在本页
- 先决条件
- 第 1 步:安装所需的依赖项
- 第 2 步:安装 Nginx 和 MariaDB
- 第 3 步:安装 PHP 7.4
- 第 4 步:创建 Drupal 数据库
- 第 5 步:下载 Drupal
- 第 6 步:配置 Nginx
- 第 7 步:访问 Drupal
- 结论
Drupal 是一个免费、可扩展且开放的平台,用于创建和管理网站内容。它拥有庞大的社区,被全球数以百万计的个人和组织使用。Drupal 提供了一个简单且用户友好的 Web 界面,可帮助您创建各种各样的网站,从个人网站到大型社区驱动的网站。
在本指南中,我们将向您展示如何在 Ubuntu 20.04 上使用 Nginx 部署多个 Drupal 实例。
先决条件
- 一台运行 Ubuntu 20.04 的服务器。
- 两个指向您服务器 IP 的有效域名。
- 服务器上已配置 root 密码。
第 1 步:安装所需的依赖项
首先,您需要将系统软件包更新到最新版本,并在系统中安装所需的依赖项。您可以使用以下命令来完成:
apt-get update -y
apt-get install gnupg2 software-properties-common unzip git wget -y
所有依赖项安装完成后,您可以继续下一步。
第 2 步:安装 Nginx 和 MariaDB
这里,我们将使用 Nginx 来托管 Drupal。因此您的服务器上必须安装 Nginx。如果尚未安装,可以使用以下命令安装:
apt-get install nginx -y
安装 Nginx 之后,您需要在服务器上安装最新版本的 MariaDB。默认情况下,Ubuntu 20.04 默认存储库不包含最新版本的 MariaDB。因此您需要将 MariaDB 存储库添加到 APT。
首先,使用以下命令添加 GPG 密钥:
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
接下来,使用以下命令添加 MariaDB 存储库:
add-apt-repository "deb [arch=amd64,arm64,ppc64el] http://mariadb.mirror.liquidtelecom.com/repo/10.4/ubuntu $(lsb_release -cs) main"
添加存储库后,使用以下命令更新存储库并安装最新版本的 MariaDB:
apt-get update -y
apt-get install mariadb-server -y
MariaDB 服务器安装完成后,您可以继续下一步。
第 3 步:安装 PHP 7.4
Drupal 不支持 PHP 8.0。因此您需要在系统中安装 PHP 7.4。您可以使用以下命令安装 PHP 7.4 版本以及其他所需的扩展:
apt-get install php7.4 php7.4-fpm php7.4-gd php7.4-common php7.4-mysql php7.4-apcu php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip -y
所有软件包安装完成后,编辑 php.ini 文件并进行一些调整:
nano /etc/php/7.4/fpm/php.ini
更改以下几行:
date.timezone = Asia/Kolkata memory_limit = 256M upload_max_filesize = 64M max_execution_time = 600 cgi.fix_pathinfo = 0
保存并关闭文件,然后重启 PHP-FPM 以使更改生效:
systemctl restart php7.4-fpm
第 4 步:创建 Drupal 数据库
接下来,您需要为两个 Drupal 实例创建一个数据库和用户。
首先,使用以下命令连接到 MySQL:
mysql
连接后,使用以下命令为两个 Drupal 实例创建数据库:
MariaDB [(none)]> create database drupal1db;
MariaDB [(none)]> create database drupal2db;
接下来,使用以下命令为两个 Drupal 实例创建数据库用户:
MariaDB [(none)]> create user drupal1user@localhost identified by 'password';
MariaDB [(none)]> create user drupal2user@localhost identified by 'password';
接下来,使用以下命令为两个 Drupal 数据库授予所有权限:
MariaDB [(none)]> grant all privileges on drupal1db.* to drupal1user@localhost identified by 'password';
MariaDB [(none)]> grant all privileges on drupal2db.* to drupal2user@localhost identified by 'password';
接下来,使用以下命令刷新权限并退出 MySQL:
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;
完成后,您可以继续下一步。
第 5 步:下载 Drupal
接下来,您需要从其官方网站下载最新版本的 Drupal。
首先,使用以下命令将目录切换到 Nginx Web 根目录:
cd /var/www/html
接下来,使用以下命令下载最新版本的 Drupal:
wget -q https://www.drupal.org/download-latest/tar.gz -O drupal-latest.tar.gz
接下来,使用以下命令解压缩下载的文件:
tar -xf drupal-latest.tar.gz
接下来,将解压后的目录复制到 drupal1 和 drupal2:
cp -r drupal-9.1.5 drupal1
cp -r drupal-9.1.5 drupal2
接下来,使用以下命令设置适当的所有权:
chown -R www-data:www-data /var/www/html/drupal*
完成后,您可以继续下一步。
第 6 步:配置 Nginx
接下来,您需要为每个 Drupal 实例创建两个 Nginx 虚拟主机配置文件。
首先,为第一个 Drupal 实例创建一个 Nginx 虚拟主机配置文件:
nano /etc/nginx/conf.d/drupal1.conf
添加以下几行:
server {
listen 80;
root /var/www/html/drupal1;
index index.php index.html index.htm;
server_name drupal1.example.com;
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ [^/]\.php(/|$) {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
try_files $uri @rewrite;
}
location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
try_files $uri /index.php?$query_string;
}
}
完成后保存并关闭文件。
接下来,为第二个 Drupal 实例创建一个 Nginx 虚拟主机配置文件:
nano /etc/nginx/conf.d/drupal2.conf
添加以下几行:
server {
listen 80;
root /var/www/html/drupal2;
index index.php index.html index.htm;
server_name drupal2.example.com;
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ [^/]\.php(/|$) {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
try_files $uri @rewrite;
}
location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
try_files $uri /index.php?$query_string;
}
}
完成后保存并关闭文件,然后使用以下命令检查 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 Tue 2021-03-23 08:50:47 UTC; 6s ago
Docs: man:nginx(8)
Process: 19946 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 19959 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 19964 (nginx)
Tasks: 2 (limit: 2353)
Memory: 2.7M
CGroup: /system.slice/nginx.service
??19964 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??19965 nginx: worker process
Mar 23 08:50:47 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 23 08:50:47 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.
第 7 步:访问 Drupal
现在,打开您的 Web 浏览器并输入 URL http://drupal1.example.com 或 http://drupal2.example.com 。您将被重定向到 Drupal Web 安装向导,如下所示:

选择您的语言并单击 Save and continue 按钮。您应该看到以下页面:

选择 standard 安装类型并单击 Save and continue 按钮。您应该看到以下页面:

提供您的数据库详细信息并单击 Save and continue 按钮。您应该看到以下页面:


提供您的站点信息并单击 Save and continue 按钮。您将在以下页面中看到 Drupal 仪表板:

结论
在上述文章中,您学习了如何在 Ubuntu 20.04 服务器上使用 Nginx 部署多个 Drupal 实例。现在您可以在服务器上部署任意数量的 Drupal 实例。您只需为每个新的 Drupal 实例定义域名和 Drupal 安装路径。如果您有任何问题,请随时问我。