wordpress从apache迁移到nginx

目录

一、安装nginx

二、配置文件准备

2.1、进程运行用户

2.2、虚拟主机

2.3、重定向

三、迁移


庚子鼠年最后几天,贫僧发现了内存不足的问题,并在Apache2.4.x下proxy_module、proxy_fcgi_module结合PHP-FPM解决内存不足问题一文中阐述了解决方案。最近和几位开发朋友聊天,发现如果直接使用nginx,就不存在这个内存不足的问题。所以,研究了几天,决定从apache迁移到nginx。

一、安装nginx

在nginx的官网,有编译好的nginx版本,RHEL/CentOS发行版yum源地址https://nginx.org/en/linux_packages.html#RHEL-CentOS ,配置好了以后就可以安装了,目前的稳定版本是nginx-1.18.0。

To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

二、配置文件准备

2.1、进程运行用户

nginx默认的进程运行用户是nginx。由于本站从apache迁移过来,之前文件系统所属用户、所属组是apache:apache。为了保证文件读写顺利,不引起不必要的麻烦,修改/etc/nginx/nginx.conf文件,用user指令指定进程运行用户。

Syntax: user user [group];

2.2、虚拟主机

根据官网https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/ 的描述应当这样配置wordpress建站。

首先,要安装php-fpm服务,php-fpm默认在127.0.0.1:9000端口提供服务,我们为php设置一个命名的upstream来抽象后端,这样做将来更改端口或添加更多的后端会很容易。

之后,我们为stackoperator.top设置虚拟主机配置。模板很全面,我们只需要修改三个部分即可。server_name来指定虚拟主机域名。root指定虚拟主机的文件路径。ssl相关选项指定证书、私钥、加密算法等,具体请参考各云服务商的建议值。

# Upstream to abstract backend connection(s) for php
upstream php {
    #server unix:/tmp/php-cgi.socket;
    server 127.0.0.1:9000;
}

server {
    ## https is working on this port
    listen                      443 ssl;
    ## Your website name goes here.
    server_name                 example.org www.example.org;
    ## Your only path reference.
    root                        /your/website/root/absolute/path;
    ## This should be in your http block and if it is, it's not needed here.
    index                       index.php;

    ssl_certificate             /absolute/path/to/your/certificate.crt;
    ssl_certificate_key         /absolute/path/to/your/certificate.key;
    ssl_session_timeout         5m;
    ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                 ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers   on;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass php;
        #The following parameter can be also included in fastcgi_params file
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

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


2.3、重定向

根据官网https://nginx.org/en/docs/http/converting_rewrite_rules.html ,应当配置一个监听80端口的http虚拟主机,用return 301进行重定向保证https。

This is a wrong, cumbersome, and ineffective way. The right way is to define a separate server for example.org:

server {
    listen       80;
    server_name  example.org;
    return       301 https://www.example.org$request_uri;
}

server {
    listen       80;
    server_name  www.example.org;
    return       301 https://www.example.org$request_uri;
}

因此stackoperator.top的配置是酱紫的。

server {
    ## rewrite http to https
    listen       80;
    server_name  stackoperator.top www.stackoperator.top;
    return       301 https://www.stackoperator.top$request_uri;
}

三、迁移

关闭apache服务,禁止开机启动。设置nginx开机启动,启动nginx服务。不要同时启动,否则端口冲突就好玩了。

systemctl stop httpd
systemctl disable httpd
systemctl enable nginx
systemctl start nginx

 

你可能感兴趣的:(#,Linux实践与随笔,Linux,linux,nginx,apache,centos,运维)