从架构根源为avada究极加速,Centos7系统搭建openresty+php7+mariadb+redis环境

1、下载安装openresty

https://openresty.org/cn/
这里以openresty-1.11.2.3.tar.gz 2017年4月21日版本为例
安装之前的准备工作:

1
yum install readline-devel pcre-devel openssl-devel gcc wget

开始安装

1
2
3
tar -xzvf openresty-1.11.2.3.tar.gz
cd openresty-1.11.2.3

编译之前需要设置好nginx的用户组www

1
2
groupadd www
useradd -g www www

开始编译,因为我们要用到redis缓存技术的一些必要组件,所以需要再编译后面添加一些参数

1
2
3
./configure --user=www --group=www --with-http_v2_module --with-http_ssl_module
make
make install

默认安装路径:/usr/local/openresty

2、添加nginx启动、重启等脚本

命令:

1
vim /usr/lib/systemd/system/nginx.service

添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

脚本使用方法:

1
2
3
4
5
6
7
8
9
systemctl is-enabled nginx.service #查询nginx是否开机启动
systemctl enable nginx.service #开机运行nginx
systemctl disable nginx.service #取消开机运行nginx
systemctl start nginx.service #启动nginx
systemctl stop nginx.service #停止nginx
systemctl restart nginx.service #重启nginx
systemctl reload nginx.service #重新加载nginx配置文件
systemctl status nginx.service #查询nginx运行状态
systemctl --failed #显示启动失败的服务

 

3、安装php7及组件

3 yum -y install php70u-fpm php70u-pdo php70u-mysqlnd php70u-opcache php70u-xml php70u-mcrypt php70u-gd php70u-devel php70u-mysql php70u-intl php70u-mbstring php70u-bcmath php70u-json php70u-iconv

相关配置:

1
sudo vi /etc/php.ini

设置cgi.fix_pathinfo=0
然后:

1
sudo vi /etc/php-fpm.d/www.conf

设置

1
2
3
4
5
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = www
listen.group = www
user = www
group = www

最后启动php-fpm

1
sudo systemctl start php-fpm

设置开机启动php-fpm

1
sudo systemctl enable php-fpm

 

4、安装mysql这里我们选择开源的mariadb最新版mariadb10.1版本

准备工作,默认centos7含有mairadb5.5版本需要将其卸载
查找本机mariadb具体版本

1
rpm -qa | grep mariadb

卸载rpm -e mariadb-libs-5.5.52-1.el7.x86_64
提示关联postfix,这个一般用不到 卸载他!

1
yum remove postfix

再运行

1
rpm -e mariadb-libs-5.5.52-1.el7.x86_64

开始安装mariadb10.1

1
yum install mariadb101u mariadb101u-server

启动mariadb

1
sudo systemctl start mariadb

配置mariadb密码及其他

1
sudo mysql_secure_installation

第一步提示输入密码,这里直接回车
然后提示是否设置新密码输入Y
输入新密码、再次输入新密码后回车
其他选项直接输入Y回车即可。

到这里LNMP的nginx php mysql(mariadb)就安装好了 下面开始把他们关联起来!
因为本教程主要是用redis缓存技术加速wordpress下面就以安装配置一个WordPress这点为例说明。

安装redis新版本

1
yum install redis

本教程中同时启用SSL服务及https服务下面一同讲解

1、切换到openresty下的nginx目录
1
cd /usr/local/openresty/nginx

 

2、创建文件夹用于存储虚拟主机配置文件、伪静态文件、SSL文件(分开写文件更容易管理)
1
mkdir vhost ssl rewrite

 

3、修改nginx配置检索虚拟主机配置文件

找到/usr/local/openresty/nginx/conf下的nginx.conf文件
清空文件内容替换如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
user  www www;
worker_processes auto;
events
{
    use epoll;
    worker_connections 51200;
}
http
{
    include  mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 4k;
    large_client_header_buffers 4 4k;
    client_max_body_size 50m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60 60;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 16 16k;
    fastcgi_busy_buffers_size 16k;
    fastcgi_temp_file_write_size 16k;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    server_tokens off;
    gzip             on;
    gzip_min_length  1000;
    gzip_proxied     expired no-cache no-store private auth;
    gzip_types       text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/xml application/rss+xml application/xhtml+xml application/atom_xml;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer"  "$http_user_agent" $http_x_forwarded_for';
    include /usr/local/openresty/nginx/vhost/*.conf;
}

 

虚拟主机配置文件

路径:/usr/local/openresty/nginx/vhost下新建abc.com.conf文件
内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
server {
            listen   80;
            server_name abc.com www.abc.com;
            rewrite ^(.*)$ https://www.abc.com$1 permanent;
       }
server
{
    listen 443 ssl;
    server_name abc.com;
    index index.html index.htm index.php;
    root   /usr/local/openresty/nginx/html;
    include /usr/local/openresty/nginx/rewrite/re-adc.conf;
    ssl on;
    ssl_certificate_key  /usr/local/openresty/nginx/ssl/xxx.key;
    ssl_certificate  /usr/local/openresty/nginx/ssl/xxx.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;
    
    set $skip_cache 0;
    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 1;
    }  
    if ($query_string != "") {
        set $skip_cache 1;
    }  
    # Don't cache uris containing the following segments
    if ($request_uri ~* "/wp-admin/|/shop/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }  
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }
    location /redis-fetch {
        internal  ;
        set  $redis_key $args;
        redis_pass  127.0.0.1:6379;
    }
    location /redis-store {
        internal  ;
        set_unescape_uri $key $arg_key ;
        redis2_query  set $key $echo_request_body;
        redis2_query expire $key 14400;
        redis2_pass  127.0.0.1:6379;
    }
    location ~ \.php$ {
        
        set $key "nginx-cache:$scheme$request_method$host$request_uri";
        try_files $uri =404;
        srcache_fetch_skip $skip_cache;
        srcache_store_skip $skip_cache;
        srcache_response_cache_control off;
        set_escape_uri $escaped_key $key;
        srcache_fetch GET /redis-fetch $key;
        srcache_store PUT /redis-store key=$escaped_key;
        more_set_headers 'X-Cache $srcache_fetch_status';
        more_set_headers 'X-Cache-2 $srcache_store_status';
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

上面包含了redis配置内容

配置WordPress伪静态规则

路径:/usr/local/openresty/nginx/rewrite/re-adc.conf
内容如下:

1
2
3
4
5
6
7
8
9
location / {
    try_files $uri $uri/ /index.php;
}
location /shop {
    try_files $uri $uri/ /index.php;
        if (!-f $request_filename){
        rewrite (.*) /shop/index.php;
    }
}

将您的SSL放置到/usr/local/openresty/nginx/ssl/路径下
一般有.pem和.key两个文件文件名一定要和虚拟机abc.com.conf配置文件里的一致!

4、创建mysql数据库

命令:

1
mysql -uroot -p

输入密码登录mysql
创建WordPress的数据库

1
create database webdata;

最后exit;
退出mysql

5、安装WordPress

切换到网站根目录

1
cd  /usr/local/openresty/nginx/html

下载解压WordPress

1
2
tar -xzvf latest.tar.gz
1
2
3
cd wordpress
mv * ../
cd ../

完成WordPress文件转移
给WordPress权限

1
chown -R www:www /usr/local/openresty/nginx/html

最后打开浏览器输入您绑定的域名开始wordpress安装吧。

下面开始介绍如何给WordPress提速

  • WordPress后台->设置->取消显示头像(根据需要)
  • 安装Remove Google Fonts References 插件禁用谷歌字体
  • 安装nginx helper管理redis缓存
  • 在设置->nginx helper下选择Enable Purge
  • Caching Method选择Redis cache
  • 其他默认即可最后点save all changes按钮
  • 关于如何清空站点缓存,登录WordPress后台后最上方工具条有个 Purge Cache按钮
  • 需要手动更新的时候点此即可。
  • 判断缓存是否生效
  • 谷歌浏览器右键->检查->Network
  • 刷新页面后,从列表中点选当前页面网址
  • 点选左侧窗口Headers选框
  • 在如下信息中看到类似
1
2
3
4
5
6
7
8
9
10
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Thu, 18 May 2017 04:13:37 GMT
Keep-Alive:timeout=60
Server:openresty
Transfer-Encoding:chunked
X-Cache:HIT
X-Cache-2:BYPASS
X-Powered-By:PHP/7.0.18

这里看到X-Cache:HIT说明缓存成功了!

注意:登录后台的状态下X-Cache:BYPASS

最后提示一点默认上传限制2M需要修改的话请自行修改php.ini文件

原文:https://bobo.vc/centos7-openresty-php7-mariadb-redis/

前往《用 WordPress和 Avada做网站》完整版教程

发表回复

请填写评论
请填写您的名字

最近流行

热点内容