[Alt](https://res.cloudinary.com/practicaldev/image/fetch/s--GHHeWDzh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev- to-uploads.s3.amazonaws.com/i/sm9qejlpv8py4a9asn9g.png)

当您使用 php 作为后端技术时,您总是会听到人们谈论网络环境的灯堆栈(linux - apache - mysql - php)。

有很多工具可以根据您的操作系统为您提供该堆栈的增强版本:

  • XAMPP

  • WAMP

  • MAMP

  • 阿奎亚

这些工具通常是预先配置的,因此您可以开始动态开发。

这些工具并不意味着存在于您的生产服务器中,这意味着您至少需要具备一些关于如何设置自己的堆栈的基本知识或至少一些

基本配置技巧。

当您想托管一个 php web 应用程序时,您将使用的服务器通常是一台机器(基于云的服务的虚拟机),其中 Apache 或 nginx 作为 web 服务器。

在本系列文章中,我将介绍使用 nginx - php-fpm - mariadb for docker 的 drupal 8 堆栈的基本设置。

首先,我们需要给 nginx 一些配置,让他知道我们使用它作为 Web 服务器而不是代理服务器,我们通常使用 *.conf 文件来做到这一点:

user www-data;
pid /run/nginx.pid;
worker_processes auto;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        types_hash_max_size 2048;
        server_tokens off;


    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_connect_timeout 1200s;
    proxy_send_timeout 1200s;
    proxy_read_timeout 1200s;
    proxy_busy_buffers_size 256k;
    gzip on;
    gzip_disable "msie6";

    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    brotli on;
    brotli_static on;
    brotli_buffers 32 8k;
    brotli_comp_level 7;
    brotli_types *;

    include /etc/nginx/conf.d/*.conf;

}

进入全屏模式 退出全屏模式

http 指令是此配置中最重要的指令,您始终可以使用缓冲区大小等值...请注意,对于这种情况,我的网络服务器配置为提供 gzip 和 brotli 压缩资产,您可以了解网络服务器此处压缩:Gzip 和 Brotli 压缩

此外,我们需要添加站点的 conf ... 通常是您放置域名和其他站点特定 conf 的位置,例如浏览器缓存、文件访问、重定向...等。

该文件将具有项目的特定配置,但请注意某些指令永远不会更改:

upstream fastcgi_backend {
server php:9000;
keepalive 8;
}
server {
listen 80;
server_name domain.dev *.dev;
root /var/www/html/;

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)$ {
    deny all;
}

location ~ \..*/.*\.php$ {
    return 403;
}

location ~ ^/sites/.*/private/ {
    return 403;
}

location / {
    # try_files $uri @rewrite; # For Drupal <= 6
    gzip_static on;
    proxy_cache cache;
    proxy_cache_revalidate on;
    proxy_cache_min_uses 3;
    proxy_cache_valid 200 1s;
    proxy_cache_use_stale updating error timeout;
    proxy_cache_background_update on;
    proxy_cache_lock on;
    try_files $uri /index.php?$query_string; # For Drupal >= 7
}

进入全屏模式 退出全屏模式

我们可以很容易地发现 nginx 和 apache 作为网络服务器的主要区别:

[Alt](https://res.cloudinary.com/practicaldev/image/fetch/s--_V2-hdZh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https:// dev-to-uploads.s3.amazonaws.com/i/t9vvklawykxnzt1q69mo.png)

**Apache 可以通过激活 modphp 来执行您的 php 脚本,这是一个 apache 扩展,但在 nginx 的情况下,您被迫使用 php-fpm(快速 cgi)来运行您的 php 脚本和

将其传递给服务器,服务器只会将执行结果呈现给浏览器。**

他会怎么做?

遵循此指令:

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_intercept_errors on;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 64k;
    fastcgi_send_timeout 1200s;
    fastcgi_read_timeout 1200s;
    # PHP 5 socket location.
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    # PHP 7 socket location.
    # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

    fastcgi_pass fastcgi_backend;
    fastcgi_keep_conn on;
    # fastcgi_pass fpm:9000;
}

进入全屏模式 退出全屏模式

现在我们已经使用 php-fpm 介绍了 nginx 配置,接下来我们将设置 docker 容器来运行我们的堆栈(nginx - php-fpm - mariadb)。

编辑:第 2 部分。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐