源码nginx+php 7.0+mysql搭建WordPress

引言

工作中需要搭建一个web服务器用于验证谷歌最新的压缩算法Brotli,上网找到nginx有支持brotli算法的模块,于是动手搭建一个nginx + php 7.0 + mysql 5.6的WordPress环境,也可以通过这环境玩玩WordPress。


环境及准备工作

环境为:linux centos 7.0,x86_64架构,具体为Linux test-server-2 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
安装编译工具, 若源码安装mysql,需要安装cmake工具
[root@test-server-2 conf]#yum install gcc automake autoconf libtool gcc-c++ cmake

由于源码编译安装nginx、php需要依赖很多库,这边库就不直接源码安装,使用yum install安装,需要安装的库有
[root@test-server-2 conf]#yum install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel
下载相关源码,这边mysl不是源码安装,使用rpm+yum install安装
wget http://nginx.org/download/nginx-1.9.14.tar.gz
wget http://hk2.php.net/distributions/php-7.0.5.tar.gz
wget https://cn.wordpress.org/wordpress-4.4.2-zh_CN.tar.gz
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm


安装


nginx安装

这边安装的nginx有支持谷歌最新的压缩算法brotli,故需要下载brotli源码及相关依赖源码,具体如下所示:
git clone https://github.com/google/ngx_brotli.git
git clone https://github.com/bagder/libbrotli
cd libbrotli
./autogen.sh
 ./configure --prefix=/usr
 make && make install
 ldconfig
一切准备就绪了,可以开始编译安装nginx了:
./configure --prefix=/usr/local/nginx-1.9 --user=www --group=www --with-threads  --add-module=/root/LNMP/ngx_brotli
make && make install
安装过程中若出现找不到对应库的,就使用yum install进行安装。


安装php

由于php需要支持nginx和mysql,需要加上nginx和mysql的相关选项:
./configure --prefix=/usr/local/php7.0 --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-phpdbg --enable-phpdbg-webhelper --enable-mysqlnd --with-pcre-dir --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --with-pdo-mysql=mysqlnd --enable-pcntl --enable-mysqlnd --enable-zip --with-mysqli=mysqlnd
make && make install


安装mysql

mysql这边不使用源码安装,直接通过rpm+yum安装:
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
若存在旧版本的mysql,需要完成删除mysql的话,使用如下命令:
rpm -qa | grep mysql
mysql57-community-release-el7-7.noarch
mysql-community-common-5.7.11-1.el7.x86_64
mysql-community-client-5.7.11-1.el7.x86_64
mysql-community-libs-compat-5.7.11-1.el7.x86_64
mysql-community-libs-5.7.11-1.el7.x86_64
mysql-community-server-5.7.11-1.el7.x86_64
yum remove mysql-community-release mysql-community-common mysql-community-client mysql-community-libs-compat mysql-community-libs mysql-community-server
chkconfig --list | grep -i mysql
chkconfig --del mysql 
whereis mysql


配置


nginx配置

根据nginx编译安装的配置,找到配置文件,我这边的环境主要是修改了nginx.conf文件,该文件内容如下所示:
worker_processes  auto;

error_log  logs/error.log;
error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    brotli on;  
    brotli_static off;  
    brotli_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/javascript text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;  
    brotli_buffers 4 16k;  
    brotli_comp_level 9;  
    brotli_window 512k;  
    brotli_min_length 512;

    gzip  on;
    server {
        listen       80;
        server_name  192.168.24.101;

        #charset koi8-r;
        
        gzip  on;
        gzip_types  text/plain text/css application/xml application/javascript;
        gzip_comp_level 6;        
        
        access_log  logs/host.access.log  main;
        root  /home/wwwroot/wordpress;
        index index.html index.htm index.php;
        
        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
            include        fastcgi_params;
            fastcgi_param  HTTPS              off;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }


    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  192.168.24.101;

        ssl_certificate      server.crt;
        ssl_certificate_key  server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        add_header           Front-End-Https on;

        access_log  logs/ssl.access.log  main;
        root  /home/wwwroot/wordpress;
        index index.html index.htm index.php;
        include wordpress.conf;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

         location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
            include        fastcgi_params;
        }

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    }

}
其中该配置文件支持https,对于https支持谷歌压缩算法brotli,配置文件里面有server.crt和server.key,这参考 http://blog.csdn.net/chen_jianjian/article/details/51131033成SSL证书。


配置php

cp /root/LNMP/php-7.0.5/php.ini-development /usr/local/php7.0/lib/php.ini
cp /usr/local/php7.0/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php7.0/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
这边未对php进行优化,需要根据具体的环境进行优化,至于怎么优化这边就不详述了。


配置mysql

本环境安装的mysql的配置文件为/etc/my.cnf,修改该配置文件,另外网上也有根据硬件环境生成优化的mysql配置文件,可以在该网站上( https://tools.percona.com/dashboard)生成需要的mysql配置文件,我环境的配置文件为:
# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5-20120208
# Configuration name server generated for [email protected] at 2016-03-21 08:31:54

[mysql]

# CLIENT #
port                           = 3306
socket                         = /var/lib/mysql/mysql.sock

[mysqld]

# GENERAL #
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /var/lib/mysql/mysql.sock
pid-file                       = /var/lib/mysql/mysql.pid

# MyISAM #
key-buffer-size                = 32M
myisam-recover                 = FORCE,BACKUP

# SAFETY #
max-allowed-packet             = 16M
max-connect-errors             = 10000

# DATA STORAGE #
datadir                        = /var/lib/mysql/

# BINARY LOGGING #
log-bin                        = /var/lib/mysql/mysql-bin
expire-logs-days               = 14
sync-binlog                    = 1

# CACHES AND LIMITS #
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 0
query-cache-size               = 0
max-connections                = 500
thread-cache-size              = 50
open-files-limit               = 65535
table-definition-cache         = 1024
table-open-cache               = 2048

# INNODB #
innodb-flush-method            = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 128M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table          = 1
innodb-buffer-pool-size        = 2G

# LOGGING #
log-error                      = /var/lib/mysql/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1
slow-query-log-file            = /var/lib/mysql/mysql-slow.log

ps:对于mysql 5.7为了安全,初次安装使用的时候会生成一个随机密码,可以通过如下方式修改mysql的密码:
service mysqld start
mysql为了安全设置了个随机密码,要手动查看,使用grep "password" /var/log/mysqld.log
修改密码mysql shell下:SET PASSWORD = PASSWORD('123456');其中可能会出现密码不合法,只要修改配置文件,把validate-password=OFF即可修改


配置WordPress

把WordPress解压到nginx的配置目录,然后cp wp-config-sample.php wp-config.php,修改wp-config.php,下面是这个环境wp-config.php的配置内容:


启动运行

配置基本上已经完成了,下面就可以启动相关进程了,另外mysql还要创建一个wordpress数据库,具体怎么创建该数据库就不详述了。还有nginx和php没有搞开机启动脚本,需要开机启动,网上找一下开机启动脚本,这边nginx和php都要已www用户组运行,故需要创建www用户和用户组,创建www用户和用户组使用该命令:
groupadd www
useradd -g www www
启动mysql、nginx、php:
service mysqld restart
/usr/local/nginx-1.9/sbin/nginx
/usr/local/php7.0/sbin/php-fpm
都启动好了,就可以在浏览器上安装WordPress了,具体为:https://xx.xx.xx.xx/wp-admin/install.php进行安装配置。

大功告成!







你可能感兴趣的:(linux,wordpress,php,nginx,web,mysql)