Drupal 8在CentOS上安装配置Nginx

译者:anxin
日期:2017-08-14

本教程介绍在CentOS使用Yum安装Nginx,因为使用Yum安装方便简单,安装Nginx较新版本,需要安装Nginx官方软件源。

导入Nginx RPM包GPG秘钥

使用rpm命令导入GPG秘钥

sudo rpm --import https://nginx.org/keys/nginx_signing.key

添加Nginx Yum源

这是Nginx官方提供的使用数字签名验证Nginx软件包的完整性和来源的Yum源代码,我们已经在第一步导入了Nginx的GPG秘钥。

CentOS 7 Yum源:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1

CentOS 6 Yum源:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=1
enabled=1

使用Yum源安装Nginx服务器

在CentOS中添加Yum软件源后,安装Nginx Http服务器就非常简单啦:

yum install nginx

使用PHP-Fpm套接字处理PHP文件

php-fpm.conf定义了一个upstream,即使用fastcgi_pass命令可以指定的server;修改此文件注释server 127.0.0.1:9000;,取消server unix:/run/php-fpm/www.sock;前面的#,修改后的形式如下:

# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        #server 127.0.0.1:9000;
        server unix:/run/php-fpm/www.sock;
}

启动Nginx Http服务器

sudo systemctl start nginx

配置Apache服务器运行Drupal 8网站

Nginx服务器安装后,虚拟机文件存放在/etc/nginx/conf.d/,首先备份/etc/nginx/conf.d/default.conf文件,然后编辑/etc/nginx/conf.d/default.conf文件(或者新建一个文件),删除所有内容,添加如下内容:

server {
    listen       80;
    server_name  localhost;
    root /var/www/drupal_site; ## <-- Your only path reference.

    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:/run/php-fpm/www.sock;
        # PHP 7 socket location.
        #fastcgi_pass   127.0.0.1:9000;
    }

    # 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)$ {
        expires max;
        log_not_found off;
    }
}

代码解释:

1)使用root指令指定你自己的Drupal 8站点的位置

root /path/to/drupal8;

2)设置fastcgi_pass指令把php文件处理传递给Unix套接字,此处需要设置为php-fpm,使它能够引用在php-fpm.conf文件定义的upstream

fastcgi_pass php-fpm;

3)location定义的是相应的URL集合,即处理URL时,Nginx查找location,找到最长匹配的location,如果只匹配 location / {},那么使用location / {} 处理请求。

一般的Drupal 8节点请求处理顺序:

  1. 查找location,只匹配location / {}
  2. 查看$uri文件是否存在,不存在
  3. 把请求重定向到/index.php?$query_string;
  4. 使用location ~ '\.php$|^/update.php' {}处理请求
  5. 把请求传递给php-fpm套接字,使用php-fpm处理php请求。

做完如上配置后,如何PHP-Fpm已经安装配置好,把Drupal 8源码放在/path/to/drupal8文件夹中,在浏览器访问localhost就能够出现Drupal 8安装界面。

本文链接:/tutorial/drupal-8/for-drupal-8-anzhuang-nginx-in-centos

本文版权归知站所有,未经站长同意不得转载,谢谢尊重作者劳动成果!