zoukankan      html  css  js  c++  java
  • CentOS8 + Nginx 安装部署 WordPress 5.4

    获取 WordPress

    访问个人中文镜像站点 cn.wp101.net,下载最新的安装包到服务器

    mkdir /home/downloads
    cd /home/downloads
    
    wget http://cn.wp101.net/latest-zh_CN.tar.gz
    # 解压获得一个 wordpress 目录
    tar -xzvf latest-zh_CN.tar.gz
    

    处理 WordPress

    wordpress 目录就是整个项目的根目录,这个目录名字是可以更改的,本例中将其命名为 website-wp 即修改目录名为 website-wp 并将整个目录移动(复制)到打算放置 web 项目的路径下

    # 假设现在在 downloads 目录下
    mv wordpress website-wp
    
    # 假设存在 /var/website 并将整个目录移动到 website 目录下
    mv website-wp /var/website
    

    按照官方教程,执行一些其它必要的配置,比如配置项目根目录下 wp-config.php 中需要的数据库连接信息

    配置 Nginx

    没想到 Nginx 访问 WordPress 会有坑,默认设置了 location 之后会导致各种 404,百度一番给出的答案是 WordPress 是依靠 .htaccess 伪静态的,所以移植到 Nginx 访问的时候路径访问就会有问题,需要做一点修改,首先在 /etc/nginx/conf.d 目录下新建一个配置文件,专门用来配置 website-wp 项目,配置内容如下

    # website-wp 项目配置
    # 更新: 2020/04/29
    #
    server {
        #使用 one 这个缓存
            #proxy_cache    one;
            #proxy_cache_valid   any  10m;
    
            listen         80;
            server_name    139.139.139.139    www.website-wp.com;
    
            location / {
                root     /var/website/wwwsite-wp;
                index    index.php;
    
                # 关键句 !!
                try_files  $uri  $uri/  /index.php?$args;
            }
    
            # 关键句 !!
            rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    
            error_page   401 402 403 404 500 502 503 504  /oops.html;
            location = /oops.html {
                root     /var/website/wwwsite-wp;
            }
    
            location  ~.php$ {
                root            /var/website/wwwsite-wp;
                fastcgi_pass    unix:/run/php-fpm/www.sock;
                #fastcgi_pass    127.0.0.1:9000;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include         fastcgi_params;
            }
        }
    

    保存好后,在 /etc/nginx/nginx.conf 配置中包含进这个配置文件即可,值得一提的是 location 中对 PHP 的配置一段,fastcgi_pass 使用的是 socket 方式,与其对应 php-fpm 的配置文件也需要如此配置才能对应,具体如下

    # 在/etc/nginx/conf.d 中默认有一个 php-fpm.conf,配置内容如
    upstream php-fpm {
            server unix:/run/php-fpm/www.sock;
    }
    

    配置 PHP

    找到本机的 PHP-FPM 自己的配置文件,如果是 yum 安装通常会在 /etc/php-fpm.conf 和 /etc/php-fpm.d/.conf 两个地方(whereis php-fpm* 查看一下),它们的关系如 Nginx 的配置文件关系一样,第一个文件是主配置,使用 include 语句说明包含某个目录下哪些相关联的其它配置文件,因此可以在 /etc/php-fpm.d 下新建一个名为 for-wwwsite-wp.conf 的文件

    # 部分配置内容
    ; Start a new pool named 'www'.
    ; the variable $pool can be used in any directive and will be replaced by the
    ; pool name ('www' here)
    [www]
    
    ; Per pool prefix
    ; It only applies on the following directives:
    ; - 'access.log'
    ; - 'slowlog'
    ; - 'listen' (unixsocket)
    ; - 'chroot'
    ; - 'chdir'
    ; - 'php_values'
    ; - 'php_admin_values'
    ; When not set, the global prefix (or @php_fpm_prefix@) applies instead.
    ; Note: This directive can also be relative to the global prefix.
    ; Default Value: none
    ;prefix = /path/to/pools/$pool
    
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    ; RPM: apache user chosen to provide access to the same directories as httpd
    user = nginx
    ; RPM: Keep a group allowed to write in log dir.
    group = nginx
    
    ; The address on which to accept FastCGI requests.
    ; Valid syntaxes are:
    ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
    ;                            a specific port;
    ;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
    ;                            a specific port;
    ;   'port'                 - to listen on a TCP socket to all addresses
    ;                            (IPv6 and IPv4-mapped) on a specific port;
    ;   '/path/to/unix/socket' - to listen on a unix socket.
    ; Note: This value is mandatory.
    listen = /run/php-fpm/www.sock
    

    [www]用来标识 pid,可以改成具体项目名以区别于其它项目(多项目时使用挺好),user=nginxgroup=nginx 是要改成和 Nginx 配置使用相同的 user 和 group 便于访问,关键是 listen=/run/php-fpm/www.sock 说明了 www 这个(这个什么呢)池监听的不是 127.0.0.1:9000 这样的本地ip,而是通过 sock ,这个地方与 Nginx 一致即可,其实应该说 Nginx 要和 PHP 看齐,否则不在一条路就通讯不上了

    chown 使 Nginx 拥有所有访问权限

    到此处时 wwwsite-wp 目录所有操作都是在 root 用户下进行过的,Nginx 也就是 WordPress 开始工作了也没有权限访问(主要是没有权限写入),使用一句命令将目录过户给 Nginx 用户(yum 安装的 Nginx 默认就向系统添加了 nginx 用户和 nginx 组)

    # -R 表示包含目录下所有子目录和子文件
    chown -R nginx:nginx /var/website/wwwsite-wp
    

    准备启动

    所有准备工作完毕,先启动 PHP,再启动 Nginx,然后用 IP 或域名访问 /readme.html 或直接访问 /wp-admin/install.php

    总结

    • Nginx 搭配 WordPress 是有坑的,要注意 location 部分的配置
    • Nginx + PHP 的通讯可以是 sock 也可以是 ip+port,保持一致能通上话就行
    • 云服务器 + 云数据库没准儿会遇到安全策略方面的阻碍,也要注意防火墙、端口等的设置,保证互相通讯正确
  • 相关阅读:
    快速排序算法
    DirectX9(翻译):介绍
    奇葩的面试题
    新博客
    OpenCV2:幼儿园篇 第八章 视频操作
    编程规范:位运算
    编程规范:allocator
    深浅copy和浅copy
    模块和包
    递归函数
  • 原文地址:https://www.cnblogs.com/cinlap/p/12803073.html
Copyright © 2011-2022 走看看