Ubuntu 16.04 + PHP7.2 + Nginx + MariaDB + Durpal8.4.3 環境構築


事前準備

$ sudo apt-get update
$ sudo apt-get upgrade

PHP7.2 のインストール

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install php7.2
$ sudo apt-get install php7.2-mbstring php7.2-mysql php7.2-xml php7.2-gd php7.2-zip

Apacheの削除

php7.2をインストール時にapacheがインストールされるのでそれを削除

$ dpkg -l | grep apache
ii  apache2                             2.4.18-2ubuntu3.5                          amd64        Apache HTTP Server
ii  apache2-bin                         2.4.18-2ubuntu3.5                          amd64        Apache HTTP Server (modules and other binary files)
ii  apache2-data                        2.4.18-2ubuntu3.5                          all          Apache HTTP Server (common files)
ii  apache2-utils                       2.4.18-2ubuntu3.5                          amd64        Apache HTTP Server (utility programs for web servers)
ii  libapache2-mod-php7.2               7.2.0-2+ubuntu16.04.1+deb.sury.org+2       amd64        server-side, HTML-embedded scripting language (Apache 2 module)
$ sudo apt-get purge apache2 apache2-utils apache2-bin apache2-data
$ sudo apt-get autoremove
$ whereis apache2
apache2: /etc/apache2
$ sudo rm -rf /etc/apache2

Nginxのインストール

$ sudo apt-get install nginx

各種設定

$ php --ini | grep Loaded
Loaded Configuration File:         /etc/php/7.2/cli/php.ini

$ sudo vi /etc/php/7.2/cli/php.ini
672 post_max_size = 10M
776 cgi.fix_pathinfo=0
825 upload_max_filesize = 10M

$ sudo vi /etc/php/7.2/fpm/php.ini
672 post_max_size = 10M
776 cgi.fix_pathinfo=0
825 upload_max_filesize = 10M

$ sudo vi /etc/nginx/sites-available/default
51         location ~ \.php    $ {
52                 include snippets/fastcgi-php.conf;
53
54         #       # With php7.0-cgi alone:
55         #       fastcgi_pass 127.0.0.1:9000;
56         #       # With php7.0-fpm:
57                 fastcgi_pass unix:/run/php/php7.2-fpm.sock;
58         }

$ sudo vi /etc/nginx/nginx.conf
# httpの中に追加
client_max_body_size 10m; # defaule 1m

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

起動確認

$ sudo systemctl restart nginx
$ sudo systemctl restart php7.2-fpm
$ sudo su -c 'echo "<?php phpinfo(); ?>" > /var/www/html/info.php'

http://<your host or ip>/info.php にアクセス。

MariaDBのインストール

$ sudo apt-get install mariadb-client mariadb-server
$ sudo systemctl start mysql
$ sudo mysql_secure_installation
Enter current password for root (enter for none): Enter

Set root password? [Y/n] Y
New password:
Re-enter new password:

Remove anonymous users? [Y/n] Y

Disallow root login remotely? [Y/n] Y

Remove test database and access to it? [Y/n] Y

Reload privilege tables now? [Y/n] Y

$ sudo mysql -u root -p
Enter password:

MariaDB [(none)]> create database drupaldb;
MariaDB [(none)]> create user drupaluser@localhost identified by 'drupaluser@';
MariaDB [(none)]> grant all privileges on drupaldb.* to drupaluser@localhost identified by '*************';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> \q

$ sudo systemctl status mysql

自己署名証明書準備

$ sudo mkdir -p /etc/nginx/ssl
$ sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/nginx/ssl/drupal.key -out /etc/nginx/ssl/drupal.crt

各種質問はすべて空でEnter

$ sudo chmod 600 /etc/nginx/ssl/drupal.key

Drupal用のVirtualHostの設定

$ sudo mkdir -p /var/www/drupal8
$ sudo vi /etc/nginx/sites-available/drupal8
server {
    server_name drupal.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;
    }

    # Allow "Well-Known URIs" as per RFC 5785
    location ~* ^/.well-known/ {
        allow all;
    }

    # 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;
    }

    # Don't allow direct access to PHP files in the vendor directory.
    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    # 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)(|/.*)$;
        # Security note: If you're running a version of PHP older than the
        # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
        # See http://serverfault.com/q/627903/94922 for details.
        include fastcgi_params;
        # Block httpoxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        # PHP 5 socket location.
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        # PHP 7 socket location.
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    # Fighting with Styles? This little gem is amazing.
    # location ~ ^/sites/.*/files/imagecache/ { # For Drupal <= 6
    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
        try_files $uri @rewrite;
    }

    # Handle private files through Drupal. Private file's path can come
    # with a language prefix.
    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;
    }
}

$ sudo ln -s /etc/nginx/sites-available/drupal8 /etc/nginx/sites-enabled/
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl restart nginx

Durpalのインストールと設定

$ sudo apt-get install git drush
$ cd /var/www/drupal8
/var/www/drupal8$ sudo wget https://ftp.drupal.org/files/projects/drupal-8.4.3.tar.gz
/var/www/drupal8$ sudo tar -xzf drupal-8.4.3.tar.gz
/var/www/drupal8$ sudo mv drupal-8.4.3/* .
/var/www/drupal8$ sudo rm -rf drupal-8.4.3 drupal-8.4.3.tar.gz

/var/www/drupal8$ sudo cp sites/default/default.services.yml sites/default/services.yml
/var/www/drupal8$ sudo cp sites/default/default.settings.php sites/default/settings.php

/var/www/drupal8$ sudo mkdir -p sites/default/files
/var/www/drupal8$ sudo chmod a+w * sites/default/*

https でサイトにアクセス











一部エラーが表示されましたが、無事にインストール終了。

セットアップ完了後

$ sudo chmod 644 /var/www/drupal8/sites/default/settings.php
$ sudo chmod 644 /var/www/drupal8/sites/default/services.yml
$ sudo chown -R www-data /var/www/drupal8/sites/default

コンテンツ / コメント が正しく表示されない場合

https://<your host or ip>/admin/content/comment にアクセスした際に正しく表示されない場合、Nginxのエラーログが出力されます。

[error] 23384#23384: *383 FastCGI sent in stderr: "PHP message: PHP Fatal error:
Declaration of Drupal\comment\Plugin\Menu\LocalTask\UnapprovedComments::getTitle() must be compatible
with Drupal\Core\Menu\LocalTaskDefault::getTitle(?Symfony\Component\HttpFoundation\Request $request = NULL)
in /var/www/drupal8/core/modules/comment/src/Plugin/Menu/LocalTask/UnapprovedComments.php on line 14"
while reading response header from upstream

PHPを直接修正します。

$ sudo vi /var/www/drupal8/core/modules/comment/src/Plugin/Menu/LocalTask/UnapprovedComments.php

追加

use Symfony\Component\HttpFoundation\Request;

編集

public function getTitle() {
↓
public function getTitle(Request $request = NULL) {

新規コンテントが追加できない場合

https://<your host or ip>/node/add/**にアクセスした際に正しく表示されない場合、Nginxのエラーログが出力されます。

2017/12/27 06:57:40 [error] 1317#1317: *12 FastCGI sent in stderr: "PHP message: PHP Fatal error:
Declaration of Drupal\Core\TypedData\ComputedItemListTrait::getValue() must be compatible
with Drupal\Core\Field\FieldItemList::getValue($include_computed = false)
in /var/www/drupal8/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php on line 13"
while reading response header from upstream

PHPを直接修正します

$ sudo vi /var/www/drupal8/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php

/**
 * {@inheritdoc}
 * @todo Revisit the need when all entity types are converted to NG entities.
 */
public function getValue($include_computed = FALSE) {
  $values = [];
  foreach ($this->list as $delta => $item) {
    $values[$delta] = $item->getValue($include_computed);
  }
  return $values;
}

これを追加です。