如何在 Ubuntu 15.10 上使用 Nginx、PHP-FPM 和 SSL 安装 Drupal 8
Drupal 是一个著名的基于 PHP 的开源内容管理系统。它是免费提供的,并根据 GNU 通用公共许可证发布。 Drupal 可用于各种规模的网站,从大型国际网站到个人博客和公司或政府网站。 drupal的核心部分称为“Drupal Core”,包含基本的内容管理系统、用户管理、菜单管理、布局定制和系统管理。 Drupal Core 可以通过插件进行扩展,到目前为止,drupal 社区已经为 Drupal 提供了超过 31,000 个模块。当前的 Drupal 版本是 8.0。
在本教程中,我们将安装带有 Nginx 网络服务器的 Drupal 8,MariaDB 作为数据库服务器和 PHP-FPM 来处理 PHP 请求。我还将向您展示如何安装 SSL 证书来保护 Drupal 网站。
先决条件
- Ubuntu 15.10 - 64 位版本。
- 根特权。
第 1 步 - 更新 Ubuntu 系统
使用 ssh 登录到 ubuntu 服务器,然后成为 root 用户并更新 ubuntu 存储库:
sudo su
sudo apt-get update
第 2 步 - 安装 Nginx 和 PHP-FPM
Nginx 或 \engine-x\ 是一种快速的 HTTP 服务器,专注于高性能和低内存/RAM 使用率。我们也可以使用 Nginx 作为反向代理,用于 HTTP、HTTPS、POP3 和 IMAP 协议。在本教程中,我们将使用 Nginx 作为启用 SSL 的 HTTP 服务器。
以 sudo/root 用户身份使用以下 apt 命令安装 Nginx:
sudo apt-get install nginx -y
接下来,使用 drupal 核心所需的 php-gd 安装 php-fpm:
apt-get install php5-fpm php5-cli php5-gd php5-mysql -y
在下一步中,我们将配置 Nginx。
第 3 步 - 配置 Nginx 和 PHP-FPM
在此步骤中,我们将配置 Nginx 以使用 php-fpm 为 PHP 页面提供 HTTP 请求。进入php-fpm目录“/etc/php/fpm”,编辑“php.ini”文件:
cd /etc/php/fpm/
vim php.ini
在 773 行,取消注释 cgi.fix_pathinfo 行并将值更改为 \0\。
cgi.fix_pathinfo=0
保存文件并退出编辑器。
现在我们修改默认的 nginx 虚拟主机配置。编辑“默认”文件并启用 php-fpm 指令。
cd /etc/nginx/sites-available/
vim default
取消注释行 45 - 52 以将 php-fpm 与 nginx 一起使用。
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
保存文件并退出。
然后使用命令\nginx -t\测试Nginx配置,确保其有效:
nginx -t
如果没有报错,重启nginx和php-fpm服务:
systemctl restart nginx
systemctl restart php5-fpm
接下来,通过在 Web 目录 \/var/www/html\ 中创建新的 php 信息文件来测试 php-fpm 是否与 nginx 一起正常工作。
cd /var/www/html/
echo "<?php phpinfo(); ?>" > info.php
访问服务器 IP:192.168.1.101/info.php 浏览器。结果应该类似于下面的屏幕截图。

第 4 步 - 安装和配置 MariaDB
MariaDB 是 MYSQL 关系数据库管理系统的数据库服务器分支,它提供了 mysql 的直接替换功能。
使用 apt-get 命令安装最新版本的 MariaDB:
sudo apt-get install mariadb-client mariadb-server -y
安装完成后,启动 MariaDB:
systemctl start mysql
MariaDB 服务名称是 \mysql\,这就是 systemctl 命令在这里使用 mysql 一词的原因。
现在我们将使用以下命令配置 MariaDB 用户名和密码:
mysql_secure_installation
使用您的密码登录 MariaDB shell 并为名为 \drupaldb\ 的 drupal 创建新数据库,并使用密码\创建新的 MariaDB/MySQL 用户 \drupaluser\ “[email \”供您安装!
mysql -u root -p
create database drupaldb;
create user ';
flush privileges;
\q

已创建用户为 \drupaluser\ 的数据库 \drupaldb\。
第 5 步 - 生成自签名 SSL 证书
转到 /etc/nginx 目录并为 SSL 配置文件创建一个名为 \ssl\ 的新目录。
mkdir -p /etc/nginx/ssl
cd /etc/nginx/ssl
然后使用下面的 openssl 命令生成一个新的自签名 SSL 证书:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/drupal.key -out /etc/nginx/ssl/drupal.crt
填写要求的信息。 SSL证书文件已经生成,现在修改私钥文件“drupal.key”的权限为600:
chmod 600 drupal.key
第 6 步 - 为 Drupal 配置 VirtualHost
我们将Drupal 8 安装在目录\/var/www/drupal8\ 中,域名为\drupal8.me\。请将安装中的域名替换为您要使用此 drupal 安装的网站的域名。所以让我们创建目录:
mkdir -p /var/www/drupal8
然后进入Nginx虚拟主机目录\/etc/nginx/sites-available/\,用vim编辑器新建一个文件\drupal8\:
cd /etc/nginx/sites-available/
vim drupal8
将下面 drupal 的 Nginx 配置粘贴到文件 drupal8 中:
server {
server_name drupal8.me;
root /var/www/drupal8; ## <-- Your only path reference.
listen 80;
listen [::]:80;
listen 443 default ssl;
ssl_certificate /etc/nginx/ssl/drupal.crt;
ssl_certificate_key /etc/nginx/ssl/drupal.key;
# Redirect HTTP to HTTPS
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
# Block access to "hidden" files and directories whose names begin with a
# period. This includes directories used by version control systems such
# as Subversion or Git to store control files.
location ~ (^|/)\. {
return 403;
}
location / {
# try_files $uri @rewrite; # For Drupal <= 6
try_files $uri /index.php?$query_string; # For Drupal >= 7
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
# In Drupal 8, we must also match new paths where the '.php' appears in the middle,
# such as update.php/selection. The rule we use is strict, and only allows this pattern
# with the update.php front controller. This allows legacy path aliases in the form of
# blog/index.php/legacy-path to continue to route to Drupal nodes. If you do not have
# any paths like that, then you might prefer to use a laxer rule, such as:
# location ~ \.php(/|$) {
# The laxer rule will continue to work if Drupal uses this new URL pattern with front
# controllers other than update.php in a future release.
location ~ '\.php$|^/update.php' {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# Fighting with Styles? This little gem is amazing.
# location ~ ^/sites/.*/files/imagecache/ { # For Drupal <= 6
location ~ ^/sites/.*/files/styles/ { # For Drpal >= 7
try_files $uri @rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
保存文件并退出编辑器。
Drupal 虚拟主机文件已创建,现在我们必须通过在 \sites-enabled\ 目录中创建文件的符号链接来激活它:
ln -s /etc/nginx/sites-available/drupal8 /etc/nginx/sites-enabled/
测试 Nginx 配置并重启 Nginx:
nginx -t
systemctl restart nginx
第 7 步 - 安装和配置 Drupal
在开始安装 Drupal 之前,我们必须安装 git 和 drush。 Drush 是 Drupal 的命令行 shell 和 Unix 脚本界面。使用以下命令安装它:
sudo apt-get install git drush -y
然后进入我们之前创建的drupal8目录,使用git或者drush命令下载Drupal:
cd /var/www/drupal8
git clone --branch 8.0.x http://git.drupal.org/project/drupal.git
如果您想使用 drush,请改为运行以下命令:
drush dl drupal-8
等到下载完成,然后将所有 Drupal 文件移动到主目录:
mv drupal/* /var/www/drupal8/
在下一步中,我们将配置 Drupal 设置文件。从 Drupal 主目录,转到 \sites/default\ 目录并复制两个配置文件 \settings.php\ 和 \services.yml\ “:
cd sites/default
cp default.settings.php settings.php
cp default.services.yml services.yml
然后我们必须在 \sites/default\ 目录中创建一个名为 \files\ 的新目录。 \chmod\ 命令确保配置文件和 \files\ 目录对于 Drupal 安装是可写的:
mkdir files/
chmod a+w *
现在使用网络浏览器访问您的 Drupal 站点(在我的例子中是http://drupal8.me),您将被自动重定向到 https 连接并显示 Drupal 安装程序页面。
选择您的语言,我将在此处使用\English\。
点击“保存并继续”。

现在选择安装类型。您可以使用标准或最小类型。我将使用\标准\。

现在您将看到数据库配置页面。为我们为 Drupal 创建的数据库填写数据库详细信息。

等到安装完成。

现在我们必须配置站点配置文件设置,例如 站点名称、管理员用户和密码、电子邮件 等。

现在安装了 Drupal。您将被重定向到 Drupal 主页。

您将看到有关 drupal 配置文件 \settings.php\ 和 \services.yml\ 的权限的错误消息。只需使用以下命令更改它们的权限:
cd sites/default/
chmod 644 settings.php services.yml
现在 Drupal 8 在 ubuntu 15.10 上安装了 Nginx 和 SSL。
结论
Drupal 是基于 PHP 的免费开源内容管理框架。它可用于个人博客、私人主页或公司网站。 Drupal 在 GNU 许可证下发布。 Drupal 的主要组件称为“Drupal 核心”,到目前为止,Drupal 社区开发了超过 31,000 个模块来扩展它。当前的稳定版本是 8.0。 Drupal 易于安装和配置,我们可以使用 Nginx 或 Apache 作为 Web 服务器和 PHP-FPM 来处理 PHP 请求。