您的位置:首页 > 运维架构 > Nginx

源码nginx+php 7.0+mysql搭建WordPress

2016-04-15 10:45 603 查看

引言

工作中需要搭建一个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[/code] 



安装

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,这参考/article/10839380.html成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 chen_jianjian@qq.com 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的配置内容:
<?php
/**
* WordPress基础配置文件。
*
* 这个文件被安装程序用于自动生成wp-config.php配置文件,
* 您可以不使用网站,您需要手动复制这个文件,
* 并重命名为“wp-config.php”,然后填入相关信息。
*
* 本文件包含以下配置选项:
*
* * MySQL设置
* * 密钥
* * 数据库表名前缀
* * ABSPATH
*
* @link https://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php *
* @package WordPress
*/

$site_url = (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT']) ? "https://" : "http://") . "192.168.24.101"; //这既支持http,也支持https,有域名,这IP改为域名
define('WP_SITEURL', $site_url);
define('WP_HOME', $site_url);
define("WP_CONTENT_URL", $site_url . "/wp-content");

/*  强制使用SSL进行登录和后台管理  */
define('FORCE_SSL_LOGIN', false);
define('FORCE_SSL_ADMIN', true);

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'root');

/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');

/** MySQL主机 */
define('DB_HOST', '127.0.0.1');

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');

/**#@+
* 身份认证密钥与盐。
*
* 修改为任意独一无二的字串!
* 或者直接访问{@link https://api.wordpress.org/secret-key/1.1/salt/ * WordPress.org密钥生成服务}
* 任何修改都会导致所有cookies失效,所有用户将必须重新登录。
*
* @since 2.6.0
*/
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

/**#@-*/

/**
* WordPress数据表前缀。
*
* 如果您有在同一数据库内安装多个WordPress的需求,请为每个WordPress设置
* 不同的数据表前缀。前缀名只能为数字、字母加下划线。
*/
$table_prefix  = 'wp_';

/**
* 开发者专用:WordPress调试模式。
*
* 将这个值改为true,WordPress将显示所有用于开发的提示。
* 强烈建议插件开发者在开发环境中启用WP_DEBUG。
*
* 要获取其他能用于调试的信息,请访问Codex。
*
* @link https://codex.wordpress.org/Debugging_in_WordPress */
define('WP_DEBUG', false);

/**
* zh_CN本地化设置:启用ICP备案号显示
*
* 可在设置→常规中修改。
* 如需禁用,请移除或注释掉本行。
*/
define('WP_ZH_CN_ICP_NUM', true);

/* 好了!请不要再继续编辑。请保存本文件。使用愉快! */

/** WordPress目录的绝对路径。 */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');

/** 设置WordPress变量和包含文件。 */
require_once(ABSPATH . 'wp-settings.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进行安装配置。

大功告成!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: