1 整体要求
- php-fpm、httpd、mysql三者分别安装在三台虚拟机上;
- 第一台虚拟主机用于安装Mariadb,第二台虚拟主机安装php-fpm;第三台虚拟主机安装httpd。三台主机安装完之后部署phpMyAdmin和wordpress;
- 利用xcache来为php提供缓存加速页面的访问速度;
vhost1: 10.207.51.76,mariadb
vhost3: 10.207.51.77,httpd静态网页服务
vhost2: 10.207.51.37,php-fpm和phpMyAdmin和wordpress
提前将防火墙、selinx关掉
注意:整个安装过程,php5是依赖于mariadb的,所以要先装mariadb,后装php5;如果mariadb不使用通用二进制格式安装,而使用在centos 7系统自带时,要安装mariadb-devel;
2 本次所使用的软件版本
mariadb-5.5.44.tar.gz
php-5.3.27.tar.gz
xcache-3.2.0.tar.bz2
apr-1.6.3.tar.bz2
apr-util-1.6.1.tar.bz2
httpd-2.4.6.tar.bz2
3 vhost1 Mariadb-Server
3.1 安装依赖
[root@mariadb ~]# yum install -y gcc gcc-c++ ncurses ncurses-devel bison cmake man
3.2 编译安装mariadb-5.5.44
``
[root@mariadb ~]# tar -xf mariadb-5.5.44.tar.gz
[root@mariadb ~]# cd mariadb-5.5.44/
[root@mariadb mariadb-5.5.44]# useradd -r -M -s /sbin/nologin mysql
[root@mariadb mariadb-5.5.44]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=boost -DWITH_SYSTEMD=1
(注意:如果在CMAKE的过程中有报错,当报错解决后,需要把源码目录中的
CMakeCache.txt文件删除,然后再重新CMAKE,否则错误依旧)
选项描述
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql //定义安装目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock //连接数据库socket路径
-DSYSCONFDIR=/etc //指定初始化参数文件目录(my.cnf)
-DSYSTEMD_PID_DIR=/usr/local/mysql //数据库目录
-DDEFAULT_CHARSET=utf8 //指定默认使用的字符集编码
-DDEFAULT_COLLATION=utf8_general_ci //指定默认使用的字符集校对规则,utf8_general_ci是适用于UTF-8字符集的通用规则
-DWITH_INNOBASE_STORAGE_ENGINE=1 //支持InnoDB引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 //安装支持数据库分区
-DMYSQL_DATADIR=/usr/local/mysql/data
-DWITH_BOOST=boost
-DWITH_SYSTEMD=1
[root@mariadb mariadb-5.5.44]# make
[root@mariadb mariadb-5.5.44]# make install
## 3.3 编辑配置文件
[root@mariadb mariadb-5.5.44]# vim /etc/my.cnf
(里面内容全部删除,替换成以下内容)
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
skip_name_resolv=ON
## 3.4 更改数据库目录权限以及配置文件权限
``
[root@mariadb mariadb-5.5.44]# chown -R mysql.mysql /usr/local/mysql/
[root@mariadb mariadb-5.5.44]# chown mysql:mysql /etc/my.cnf
3.5 导出二进制程序目录至PATH变量
[root@mariadb mariadb-5.5.44]# echo 'PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mariadb.sh
[root@mariadb mariadb-5.5.44]# source /etc/profile.d/mariadb.sh
4.6 导出man手册
[root@mariadb ~]# vim /etc/man.config 添加以下路径
MANPATH_MAP /usr/local/mysql/bin /usr/local/mysql/man/
[root@mariadb ~]# manpath
/usr/local/share/man:/usr/share/man/en:/usr/share/man:/usr/local/mysql/man/
3.7 导出头文件
[root@mariadb ~]# ln -sv /usr/local/mysql/include /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include'
3.8 导出库文件
[root@mariadb ~]# vim /etc/ld.so.conf.d/mariadb.conf
/usr/local/mysql/lib/
[root@mariadb ~]# ldconfig -v
[root@mariadb ~]# ldconfig -p | grep mysql
libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient_r.so.16
libmysqlclient.so.18 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.18
libmysqlclient.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.16
libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so
3.9 执行脚本,创建系统数据库
[root@mariadb mariadb-5.5.44]# cd /usr/local/mysql
[root@mariadb mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
执行脚本完成后就会在/data/mysqldb/生成mysql系统数据库
3.10 复制启动服务脚本,添加自启,启动程序
[root@mariadb mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@mariadb mysql]# chkconfig --add mysqld
[root@mariadb mysql]# chkconfig --list mysqld
[root@mariadb mysql]# service mysqld start
[root@mariadb ~]# ss -antu | grep 3306 | column -t
tcp LISTEN 0 50 *:3306 *:*
3.11 执行mysql_secure_installation
[root@mariadb mysql]# mysql_secure_installation
确定输入密码;回车
输入y,密码为123123,再确认密码为123123;
Remove anonymous users? [Y/n] y 是否删除数据库匿名用户
Disallow root login remotely? [Y/n] n 是否禁止管理员root远程登录
Remove test database and access to it? [Y/n] n 是否移除test库
Reload privilege tables now? [Y/n] y 是否重载授权
3.12 创建所需数据库
[root@mariadb ~]# mysql -h127.0.0.1 -uroot -p123123
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123123');
MariaDB [(none)]> SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('123123');
MariaDB [(none)]> GRANT ALL ON wordpress.* TO 'wordpress'@'10.207.51.%' IDENTIFIED BY '123123';
MariaDB [(none)]> GRANT ALL ON *.* TO 'root'@'10.207.%.%' IDENTIFIED BY '123123';
MariaDB [(none)]> CREATE DATABASE wordpress;
MariaDB [(none)]> exit
4 vhost2 HTTPD-Server
4.1 编译安装httpd-2.4.6
过程参考我的另一篇文章
[Centos7编译安装apache 2.4.6](https://www.cnblogs.com/jzbgltb/p/9623079.html#centos7%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85apache-2.4.6)
4.2 编辑配置文件/etc/httpd/conf/httpd.conf
增加下列内容
DirectoryIndex index.html index.php
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
3ProxyRequests Off
ProxyPassMatch ^/(.*.php)$ fcgi://10.207.51.37:9000/data/www/$1
DocumentRoot "/data/www/"
<Directory /data/www/>
AllowOverride None
Options None
Require all granted
</Directory>
5 vhost3 php-fpm Server
5.1 安装依赖
[root@php-fpm ~]# yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel openssl-devel libmcrypt-devel libtool-ltdl-devel gcc gcc-c++
5.2 编译php
[root@php-fpm ~]# tar -xf php-5.3.27.tar.gz
[root@php-fpm ~]# cd php-5.3.27
[root@php-fpm php-5.3.27]# useradd -r -M -s /sbin/nologin php
[root@ php-fpm php-5.5.27]# ./configure
--prefix=/usr/local/php
--with-mysql=mysqlnd
--with-pdo-mysql=mysqlnd
--with-iconv-dir=/usr/local/libiconv
--with-freetype-dir
--with-jpeg-dir
--with-png-dir
--with-zlib
--with-libxml-dir=/usr
--enable-xml
--disable-rpath
--enable-bcmath
--enable-shmop
--enable-sysvsem
--enable-inline-optimization
--with-curl
--enable-mbregex
--enable-fpm
--enable-mbstring
--with-mcrypt
--with-openssl
--with-gd
--enable-gd-native-ttf
--with-openssl
--with-mhash
--enable-pcntl
--enable-sockets
--with-xmlrpc
--enable-soap
--enable-short-tags
--enable-static
--with-xsl
--with-fpm-user=php
--with-fpm-group=php
--enable-ftp
--with-config-file-path=/etc/php
--with-config-file-scan-dir=/etc/php.d
[root@ php-fpm php-5.5.27]# make
[root@ php-fpm php-5.5.27]# make test
[root@ php-fpm php-5.5.27]# make install
./configure选项说明:
如果本地没有mysql数据库的话要配置成--with-mysql=mysqlnd,如果本地有mysql数据库的话要配成--with-mysql=PATH
--enable-fpm 以fpm模式安装,这个还可以改成 --with-apxs2=PATH(生成libphp5.so来解码php),两者只能取其一
--with-config-file-path=/etc/php 设置php.ini的存放目录
--with-config-file-scan-dir=/etc/php.d 扩展目录
5.3 导出二进制程序目录至PATH变量
[root@php-fpm php-5.3.27]# echo 'PATH=$PATH:/usr/local/php/bin' > /etc/profile.d/php.sh
[root@php-fpm php-5.3.27]# source /etc/profile.d/php.sh
5.4 导出man手册
[root@php-fpm php-5.3.27]# vim /etc/man.config 添加已下路径
MANPATH_MAP /usr/local/php/bin /usr/local/php/man
[root@php-fpm php-5.3.27]# manpath
/usr/local/share/man:/usr/share/man/en:/usr/share/man:/usr/local/mysql/man/
5.5 导出头文件
[root@php-fpm php-5.3.27]# ln -sv /usr/local/php/include /usr/include/php
`/usr/include/mysql' -> `/usr/local/mysql/include'
5.6 添加开机自启动
[root@php-fpm php-5.3.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php-fpm php-5.3.27]# chmod +x /etc/init.d/php-fpm
[root@php-fpm etc]# chkconfig --add php-fpm
[root@php-fpm etc]# chkconfig --list | grep php-fpm
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
5.7 创建并编辑配置文件,启动php-fpm
[root@php-fpm php-5.3.27]# mkdir /etc/php
[root@php-fpm php-5.3.27]# cp php.ini-production /etc/php/php.ini
[root@php-fpm php-5.3.27]# cd /usr/local/php/etc/
[root@php-fpm etc]# cp php-fpm.conf.default php-fpm.conf
注意:在centos 7 中php-fpm.conf.default 和www.conf.default这两个文件都需要cp,但centos 6把这两个文件合在php-fpm.conf.default 中了。
[root@php-fpm etc]# vim php-fpm.conf 修改下列配置
listen = 0.0.0.0:9000
[root@php-fpm etc]#chown -R php:php /etc/php
[root@php-fpm etc]#chown -R php:php /usr/local/php
[root@php-fpm etc]# service php-fpm start
[root@php-fpm etc]# ss -antu | grep 9000 | column -t
tcp LISTEN 0 128 *:9000 *:*
5.8 部署wordpress
[root@php-fpm etc]# mkdir -p /data/www/wordpress
[root@php-fpm etc]# cd /data/www/
[root@php-fpm www]# tar -xf wordpress-4.7.4-zh_CN.tar.gz
[root@php-fpm www]# cp wordpress/wp-config-sample.php wordpress/wp-config.php
[root@php-fpm www]# vim wordpress/wp-config.php ##修改以下内容,其余配置保持默认;
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '123123');
define('DB_HOST', '10.207.51.73');
[root@php-fpm www]# chown -R php:php wordpress/
[root@wordpress wordpress]# service php-fpm start
[root@wordpress www]# scp -r wordpress root@10.207.51.77:/data/www/ ##使用scp需要安装openssh-clients
需要把wordpress目录拷贝到http服务器上,原因如下:
用户通过访问http服务器上查看网页,如果是动态请求,http服务器就会通过fcgi协议交给php-fpm,如果是静态请求就会由http服务器自己处理。在访问wordpress的站点,肯定需要用到静态文件,所以需要保证http服务器上有那些静态文件,而实现这个需求,最简单的方法就是将同样的内容也放到http服务器一份。
上面这个情况是因为wordpress 程序 里面 静态资源和php文件默认是没有分离的。
安装完之后,又发现一个问题,我在wordpress编译一篇文章,文章里有一个图片或一个附件,我点击上传时会上传到php-fpm服务器上,而且当我发布了之后,除了文章里的文字部分,所有图片和附件是无法打开的状态,我觉得这和我分开部署有关系,我认为我只要往httpd服务器上的同样的目录中上传图片和附件,就没有这个问题了,但这只是临时方案。
至于为什么会上传到php-fpm服务器而非http服务器,我认为原因是:httpd本身并不能支持上传和下载, 这都是通过php实现的,所以会上传到php-fpm服务器中。
生产环境中一般很少php-fpm和apache做分离。
浏览器中输入http://10.207.51.74/wordpress/wp-admin/setup-config.php,开始安装
输入数据库信息
然后点击开始会提会出现下面的提示,选择“立即使用当前配置进行安装”即可;
其余步骤略
最后在浏览器中输入http://10.207.51.74/wordpress/即可访问wordpress;
5.9 部署phpMyAdmin
[root@php-fpm www]# mkdir ./pam
[root@php-fpm www]# unzip phpMyAdmin-4.0.10.20-all-languages.zip
[root@php-fpm www]# cp -r phpMyAdmin-4.0.10.20-all-languages/* ./pma/
[root@php-fpm www]# cd pma/
[root@php-fpm pma]# cp config.sample.inc.php config.inc.php
[root@php-fpm pma]# vim config.inc.php ##修改下面这行
$cfg['blowfish_secret'] = '27283askdhfnbaidksdf9'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
[root@php-fpm pma]# vim libraries/config.default.php ##修改下面这行
$cfg['AllowArbitraryServer'] = true; //默认是false,改成true
[root@php-fpm pma]# service httpd start
[root@php-fpm pma]# service php-fpm start
[root@php-fpm pma]# cd ..
将phpMyAdmin拷贝到http服务器上
[root@php-fpm www]# scp -r pma root@10.207.51.77:/data/www/
至此phpMyAdmin已经可以登陆了,访问http://10.207.51.72/pma即可;
5.10 编译安装xcache
[root@php-fpm ~]# cd xcache-3.2.0/
[root@php-fpm ~]# phpize ##要在php扩展的目录里运行phpize命令,作用是把当前目录生成一些config脚本,及php环境;
[root@php-fpm xcache-3.2.0]# ./configure --prefix=/usr/local/xcache --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@php-fpm xcache-3.2.0]# make
[root@php-fpm xcache-3.2.0]# make test
[root@php-fpm xcache-3.2.0]# make install
[root@php-fpm xcache-3.2.0]# mkdir /etc/php.d/
[root@php-fpm xcache-3.2.0]# cp /root/xcache-3.2.0/xcache.ini /etc/php.d/
[root@php-fpm xcache-3.2.0]# chown -R php:php /etc/php.d/
[root@php-fpm xcache-3.2.0]#vim /etc/php.d/xcache.ini ##如果以下配置已存在,则不用更改;
[xcache-common]
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xcache.so
[xcache]
xcache.size = 200M
[xcache.admin]
xcache.admin.enable_auth = On
5.11 重新启动测试xcache
[root@php-fpm xcache-3.2.0]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
./configure的选项说明
--enable-xcache 表示启用xcahe
-with-php-config=/usr/bin/php-config 指明php配置程序文件,如果是rpm安装,可省略加等号后面的内容,自动能找到,如果是编译安装的php就要指明php程序的配置接口文件;
5.12 压测
[root@centos7 blog]# ab -c 1000 -n10000 http://10.207.51.77/wordpress
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 10.207.51.77 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: Apache/2.4.6
Server Hostname: 10.207.51.77
Server Port: 80
Document Path: /wordpress
Document Length: 238 bytes
Concurrency Level: 1000
Time taken for tests: 3.536 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Non-2xx responses: 10000
Total transferred: 4650000 bytes
HTML transferred: 2380000 bytes
Requests per second: 2828.00 [#/sec] (mean)
Time per request: 353.607 [ms] (mean)
Time per request: 0.354 [ms] (mean, across all concurrent requests)
Transfer rate: 1284.20 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 101 488.3 1 3103
Processing: 1 126 60.6 146 427
Waiting: 1 126 60.6 146 427
Total: 2 227 516.2 148 3529
Percentage of the requests served within a certain time (ms)
50% 148
66% 152
75% 156
80% 159
90% 164
95% 1127
98% 3323
99% 3342
100% 3529 (longest request)