一、PHP简介
PHP是通用服务器端脚本编程语言,其主要用于web开发以实现动态web页面,它也是最早实现将脚本嵌入HTML源码文档中的服务器端脚本语言之一。同时,php还提供了一个命令行接口,因此,其也可以在大多数系统上作为一个独立的shell来使用。
PHP 作为一种强大的语言,无论是以模块还是 CGI 的方式安装,它的解释器都可以在服务器上访问文件、运行命令以及创建网络连接等。这些功能也许会给服务器添加很多不安全因素,但是只要正确地安装和配置 PHP,以及编写安全的代码,那么 PHP 相对于 Perl 和 C 来说,是能创建出更安全的 CGI 程序的。而且,也可以在可用性和安全性之间找到一个很好的平衡点。
PHP 可能会被用在很多不同的方面,因此,PHP 内置的选项以方便用户对其进行配置。虽然众多的选项可以使 PHP 完成很多工作,但是对这些选项的设定以及对服务器的配置很可能会产生安全问题。
PHP 的选项与其语法一样,具有很高的灵活性。使用 PHP,可以在只有 shell 用户权限的环境下创建完善的服务器端程序,或者在被严格限制环境下使用它来完成服务器端包含(Server-Side Includes)而无需承但太大的风险。如何建立这样一种环境,其安全性如何,很大程度上取决于 PHP 的开发者。
二、PHP Zend Engine
Zend Engine是开源的、PHP脚本语言的解释器。Zend Engine为PHP提供了内存和资源管理的功能以及其它的一些标准服务,其高性能、可靠性和可扩展性在促进PHP成为一种流行的语言方面发挥了重要作用。Zend Engine的出现将PHP代码的处理过程分成了两个阶段:首先是分析PHP代码并将其转换为称作Zend opcode的二进制格式(类似Java的字节码),并将其存储于内存中;第二阶段是使用Zend Engine去执行这些转换后的Opcode。
三、PHP的Opcode
Opcode是一种PHP脚本编译后的中间语言,就像Java的ByteCode,或者.NET的MSL。PHP执行PHP脚本代码一般会经过如下4个步骤(确切的来说,应该是PHP的语言引擎Zend):
1、Scanning(Lexing) —— 将PHP代码转换为语言片段(Tokens)
2、Parsing —— 将Tokens转换成简单而有意义的表达式
3、Compilation —— 将表达式编译成Opocdes
4、Execution —— 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能
四、PHP的加速器
基于PHP的特殊扩展机制如opcode缓存扩展也可以将opcode缓存于php的共享内存中,从而可以让同一段代码的后续重复执行时跳过编译阶段以提高性能。由此也可以看出,这些加速器并非真正提高了opcode的运行速度,而仅是通过分析opcode后并将它们重新排列以达到快速执行的目的。
1、APC (Alternative PHP Cache)
遵循PHP License的开源框架,PHP opcode缓存加速器,目前的版本不适用于PHP 5.4。项目地:http://pecl.php.net/package/APC。
2、eAccelerator
源于Turck MMCache,早期的版本包含了一个PHP encoder和PHP loader,目前encoder已经不在支持。项目地址, http://eaccelerator.net/。
3、XCache
快速而且稳定的PHP opcode缓存,经过严格测试且被大量用于生产环境。项目地址,http://xcache.lighttpd.net/
4、Zend Optimizer和Zend Guard Loader
Zend Optimizer并非一个opcode加速器,它是由Zend Technologies为PHP5.2及以前的版本提供的一个免费、闭源的PHP扩展,其能够运行由Zend Guard生成的加密的PHP代码或模糊代码。 而Zend Guard Loader则是专为PHP5.3提供的类似于Zend Optimizer功能的扩展。项目地址,http://www.zend.com/en/products/guard/runtime-decoders
5、NuSphere PhpExpress
NuSphere的一款开源PHP加速器,它支持装载通过NuSphere PHP Encoder编码的PHP程序文件,并能够实现对常规PHP文件的执行加速。项目地址,http://www.nusphere.com/products/phpexpress.htm
五、构建LAMP平台
前面已经学习源码安装httpd-2.4,以及通用二进制格式MySQL-5.5.44的安装,这里就不做介绍了,下面就是如何基于源码安装PHP-5.5.28,在安装前先介绍下httpd-2.4与PHP-5.5.28相结合的通信方法。
基于module 的通信:安装配置方便但速度较慢。
prefork模式需要的模块:libphp5.so
worker, event模式需要的模块:libphp5-zts.so
基于CGI 通信:不建议使用
基于fastcgi 通信:安装配置脚复杂,这需要将PHP独立出来专门执行.php的代码。相较于模块安装响应速度快。
php-fpm: 启动为服务进程
声明:基于源码安装httpd-2.4我前面的博文已经写过,这里就不做介绍了,连接为:
http://cuchadanfan.blog.51cto.com/9940284/1688002
基于通用二进制格式安装的MySQL我在前面也安装过,这里也不做介绍,连接为:
http://cuchadanfan.blog.51cto.com/9940284/1688364
实验环境介绍
Linux | httpd | MySQL | PHP | phpMyAdmin | xcache |
CentOS 6.6 | httpd-2.4 | MySQL-5.5.44 | php-5.5.28 | phpMyAdmin-4.1.4 | xcache-3.1.2 |
安装PHP
[root@example.com ~]# tar xf php-5.5.28.tar.gz [root@example.com ~]# cd php-5.5.28 [root@example.com php-5.5.28]# yum groupinstall Development tools Server Platform Development -y [root@example.com php-5.5.28]# yum -y groupinstall Desktop Platform Development [root@example.com php-5.5.28]# yum -y install bzip2-devel libmcrypt-devel libxml2-devel openssl-devel [root@example.com php-5.5.28]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts [root@example.com php-5.5.28]# make && make install
说明:
1、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
2、如果使用PHP5.3以上版本,为了链接别处MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
配置httpd
[root@example.com php-5.5.28]# vim /etc/httpd24/httpd.conf LoadModule php5_module modules/libphp5.so //查看此模块是否加载 #在文件内添加下面的内容,位置最好在AddType下面的行: AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps #在此行中添加index.php DirectoryIndex index.php index.html
测试页环境
[root@example.com php-5.5.28]# cd /usr/local/apache/htdocs/ [root@example.com htdocs]# vim index.php <?php $link = mysql_connect('192.168.1.8','root','redhat'); if ($link) echo "Success..."; else echo "Failure..."; mysql_close(); ?>
为避免未知错误,最好将mysql进行相对应的授权:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY 'redhat' ; Query OK, 0 rows affected (0.20 sec)
在浏览器输入IP地址测试php能否正常启动:
[root@example.com ~]# /etc/init.d/mysqld stop Shutting down MySQL.... SUCCESS!
安装phppMyAdmin工具
[root@example.com ~]# unzip phpMyAdmin-4.1.4-all-languages.3715384168.zip [root@example.com ~]# mv phpMyAdmin-4.1.4-all-languages /usr/local/apache/htdocs/pma
在浏览器输入IP/pma,后在输入mysql数据库的帐号密码就能登录phpMyAdmin了。
压力测试
[root@example.com ~]# ulimit -n 6666 [root@example.com ~]# ab -c 10 -n 1000 http://192.168.1.8/pma/index.php This is ApacheBench, Version 2.3 <$Revision: 1663405 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.1.8 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Apache/2.4.16 Server Hostname: 192.168.1.8 Server Port: 80 Document Path: /pma/index.php Document Length: 8123 bytes Concurrency Level: 10 Time taken for tests: 94.122 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 9404560 bytes HTML transferred: 8123000 bytes Requests per second: 10.62 [#/sec] (mean) Time per request: 941.223 [ms] (mean) Time per request: 94.122 [ms] (mean, across all concurrent requests) Transfer rate: 97.58 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 2 19.5 0 398 Processing: 278 938 637.7 761 7461 Waiting: 263 867 606.1 704 7177 Total: 278 940 637.6 762 7461 Percentage of the requests served within a certain time (ms) 50% 762 66% 840 75% 946 80% 1058 90% 1462 95% 1975 98% 2477 99% 3846 100% 7461 (longest request)
测试中发现机器反应速度极慢,每个请求的平均时间为94ms,为了在有限的资源中若想提高性能就安装xcache缓存软件。
安装xcache加速器
[root@example.com ~]# tar -xf xcache-3.1.2.tar.gz [root@example.com ~]# cd xcache-3.1.2 [root@example.com xcache-3.1.2]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20121113 Zend Module Api No: 20121212 Zend Extension Api No: 220121212 [root@example.com xcache-3.1.2]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@example.com xcache-3.1.2]# make && make install ************************ Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20121212/ [root@example.com xcache-3.1.2]# mkdir /etc/php.d [root@example.com xcache-3.1.2]# mv xcache.ini /etc/php.d/ [root@example.com xcache-3.1.2]# vim /etc/php.d/xcache.ini #为避免出现查找不到xcache.so文件这里加上绝对路径 extension = /usr/local/php/lib/php/extensions/no-debug-zts-20121212/xcache.so [root@example.com ~]# vim /usr/local/apache/htdocs/index.html <?php phpinfo(); ?> [root@example.com ~]# service httpd24 restart
在浏览器输入IP就会出现测试页,查询xcache就可以看到php已加载加速器。
压力测试
[root@example.com ~]# ab -c 10 -n 1000 http://192.168.1.8/pma/index.php This is ApacheBench, Version 2.3 <$Revision: 1663405 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.1.8 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Apache/2.4.16 Server Hostname: 192.168.1.8 Server Port: 80 Document Path: /pma/index.php Document Length: 8123 bytes Concurrency Level: 10 Time taken for tests: 22.267 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 9404566 bytes HTML transferred: 8123000 bytes Requests per second: 44.91 [#/sec] (mean) Time per request: 222.666 [ms] (mean) Time per request: 22.267 [ms] (mean, across all concurrent requests) Transfer rate: 412.46 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.4 0 18 Processing: 46 222 153.4 187 1486 Waiting: 35 181 145.7 143 1475 Total: 46 222 153.3 188 1486 Percentage of the requests served within a certain time (ms) 50% 188 66% 226 75% 260 80% 286 90% 360 95% 468 98% 688 99% 910 100% 1486 (longest request)
有ab压测可以看到每个请求有每个94ms,下降到22ms,可见加速器已见效。
六、基于fastcgi方式搭建LAMP环境
此种方法安装是将PHP从httpd中独立出来,这样httpd用来处理用户的请求,而遇到需要执行PHP代码就将其转到后方的php-fpm服务器,此种方法大大释放了httpd服务器的压力,从而不用处理php程序,专门处理静态的内容,工作与这种方式的httpd就叫做proxy,即反向代理,若想工作于此种方式需要安装proxy-fcgi模块。然而生产环境中有专门做方向代理的服务器有HA-proxy和nginx,这两种的处理请求方面的速度远远大于httpd,所以下面的实验只是学习下httpd是如何工作于反向代理的模式,这里要注意httpd-2.2所带的模块没有proxy-fcgi,需要从新下载proxy-fcgi源码编译安装,而httpd-2.4就直接安装的有此模块,此实验就是基于httpd-2.4的。
实验环境介绍
Linux | httpd | MySQL | PHP | phpMyAdmin | xcache |
CentOS 6.6 | httpd-2.4 | MySQL-5.5.44 | php-5.6.12 | phpMyAdmin-4.1.4 | xcache-3.2.0 |
注意:XCache 3.2.0 支持PHP_5_6 ,而3.1不支持php-5.6,这点要注意。
安装PHP
[root@example.com ~]# tar -xf php-5.6.12.tar.xz [root@example.com ~]# cd php-5.6.12 [root@example.com ~]# yun install -y libxml2-level openssl-devel bzip2-devel libmcrypt-devel [root@example.com php-5.6.12]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 [root@example.com php-5.6.12]# make && make install
配置httpd服务
[root@example.com ~]# vim /etc/httpd24/httpd.conf #在配置文件内将基于proxy的模块取消注释 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so #在文件内添加下面的内容,位置最好在AddType下面的行: AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps #下面是关于基于域名进行测试所以为了建立多个虚拟主机,就把中心主机给取消了 #DocumentRoot "/usr/local/apache/htdocs" #在此行中添加index.php DirectoryIndex index.php index.html #将虚拟机的配置文件取消注释 Include /etc/httpd24/extra/httpd-vhosts.conf
配置虚拟主机
[root@example.com ~]# vim /etc/httpd24/extra/httpd-vhosts.conf <VirtualHost *:80> ServerAdmin webadmin@a.com DocumentRoot "/www/a.com/htdoc" ServerName www.a.com ServerAlias a.com ErrorLog "logs/a.com.err" CustomLog "logs/a.com.access" combined <Directory "/www/a.com/htdoc"> Options None Require all granted </Directory> ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/a.com/htdoc/$1 </VirtualHost> <VirtualHost *:80> ServerAdmin webadmin@b.org DocumentRoot "/www/b.org/htdoc" ServerName www.b.org ServerAlias b.org ErrorLog "logs/b.org.err" CustomLog "logs/b.org.access" combined <Directory "/www/b.org/htdoc"> Options None Require all granted </Directory> ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/b.org/htdoc/$1 </VirtualHost> <VirtualHost *:80> ServerAdmin webadmin@c.net DocumentRoot "/www/c.net/htdoc" ServerName www.c.net ServerAlias c.net ErrorLog "logs/c.net.err" CustomLog "logs/c.net.access" combined <Directory "/www/c.net/htdoc"> Options None Require all granted </Directory> ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/c.net/htdoc/$1 </VirtualHost> [root@example.com ~]# httpd -t Syntax OK [root@example.com ~]# /etc/init.d/httpd24 reload [root@example.com ~]# httpd -M |grep proxy //查看proxy模块是否加载 proxy_module (shared) proxy_fcgi_module (shared)
配置php-fpm
为php提供配置文件:
[root@example.com php-5.6.12]# mkdir /etc/php [root@example.com php-5.6.12]# cp php.ini-production /etc/php/php.ini
为php-fpm提供SysV init脚本,并将其添加至服务列表:
[root@example.com php-5.6.12]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@example.com php-5.6.12]# chmod +x /etc/init.d/php-fpm [root@example.com php-5.6.12]# chkconfig --add php-fpm
为php-fpm提供配置文件:
[root@example.com php-5.6.12]# cd /usr/local/php/etc/ [root@example.com etc]# cp php-fpm.conf.default php-fpm.conf [root@example.com etc]# vim php-fpm.conf #启用pid文件,将php-fpm的pid文件加上绝对路径 pid = /usr/local/php/var/run/php-fpm.pid #配置fpm的相关选项为你所需要的值 m.max_children = 50 #最大子进程数 pm.start_servers = 5 #开机预启动子进程数 pm.min_spare_servers = 2 #最小空闲子进程数 pm.max_spare_servers = 8 #最大空闲子进程数 启动php-fpm,查看是否正常启动: [root@example.com etc]# service php-fpm start [root@example.com etc]# ss -tunl |grep 9000 tcp LISTEN 0 128 127.0.0.1:9000
*:*
测试php-fpm
[root@example.com ~]# vim /www/b.org/htdoc/index.php <h1> <?php phpinfo(); ?>
安装phpMyAdmin工具
[root@example.com ~]# unzip phpMyAdmin-4.1.4-all-languages.3715384168.zip [root@example.com ~]# cp -r phpMyAdmin-4.1.4-all-languages /www/a.com/htdoc/pm
为避免未知错误,最好将mysql进行相对应的授权:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY 'redhat' ; Query OK, 0 rows affected (0.20 sec)
压力测试
[root@example.com ~]# ab -c 10 -n 1000 http://www.a.com/pma/index.php This is ApacheBench, Version 2.3 <$Revision: 1663405 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Apache/2.4.16 Server Hostname: www.a.com Server Port: 80 Document Path: /pma/index.php Document Length: 8121 bytes Concurrency Level: 10 Time taken for tests: 49.576 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 9341600 bytes HTML transferred: 8121000 bytes Requests per second: 20.17 [#/sec] (mean) Time per request: 495.761 [ms] (mean) Time per request: 49.576 [ms] (mean, across all concurrent requests) Transfer rate: 184.01 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.8 0 23 Processing: 200 495 108.0 453 844 Waiting: 177 474 102.6 434 817 Total: 214 495 107.8 453 844 Percentage of the requests served within a certain time (ms) 50% 453 66% 522 75% 573 80% 598 90% 658 95% 703 98% 740 99% 775 100% 844 (longest request)
从测试结果来看虽然基于fastcgi安装的LAMP环境单个请求为49ms,比基于模块安装的LAMP+xcache速度满了进一倍,但比基于模块安装的LAMP快了进一倍,所以单从性能而言还是fastcgi方式安装的LAMP较好。
安装xcache
[root@example.com ~]# tar -xf xcache-3.2.0.tar.gz [root@example.com ~]# cd xcache-3.2.0 [root@example.com xcache-3.2.0]# /usr/local/php/bin/phpize [root@example.com xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@example.com xcache-3.2.0]# make & make install /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
配置xcache服务:
[root@example.com xcache-3.2.0]# mkdir /etc/php.d [root@example.com xcache-3.2.0]# cp xcache.ini /etc/php.d [root@example.com xcache-3.2.0]# vim /etc/php/xcache.ini #为避免出现查找不到xcache.so文件这里加上绝对路径 extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so
测试xcache加速器是否启动:
[root@example.com ~]# cat /www/a.com/htdoc/index.php <h1>www.a.com</h1> <?php phpinfo(); ?>
压力测试
[root@example.com ~]# ab -c 10 -n 1000 http://www.a.com/pma/index.php This is ApacheBench, Version 2.3 <$Revision: 1663405 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Apache/2.4.16 Server Hostname: www.a.com Server Port: 80 Document Path: /pma/index.php Document Length: 8121 bytes Concurrency Level: 10 Time taken for tests: 10.106 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 9341598 bytes HTML transferred: 8121000 bytes Requests per second: 98.95 [#/sec] (mean) Time per request: 101.063 [ms] (mean) Time per request: 10.106 [ms] (mean, across all concurrent requests) Transfer rate: 902.67 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.8 0 28 Processing: 32 101 34.2 96 469 Waiting: 14 83 26.7 80 365 Total: 32 101 34.2 96 469 Percentage of the requests served within a certain time (ms) 50% 96 66% 107 75% 115 80% 120 90% 137 95% 150 98% 183 99% 204 100% 469 (longest request)
通过测试可以看到响应单个请求已经达到了10ms了,是基于模块安装的LAMP+xcache速度的两倍,所以在性能上基于fastcgi方法安装的LAMP+xcache性能是最好的,所以今后能用到xcache的地方尽量使用,最后上述实验所得到的结果均是虚拟机上的,关于真实环境的性能如何还需要自己摸索。
四种方式安装的结果比较:
模块安装LAMP | 模块安装LAMP+xcache | fastcgi安装LAMP | fastcgi安装 LAMP+xcache | |
每秒事务数 Requests per second | 10.62 | 44.91 | 20.17 | 98.95 |
平均事务响应时间 Time per request | 941.223 | 222.666 | 495.761 | 101.063 |
七、安装WordPress
[root@example.com ~]# unzip wordpress-4.2-zh_CN.zip [root@example.com ~]# cp wordpress /www/c.net/htdoc/ -r
修改WordPress配置文件
[root@example.com htdoc]# vim wp-config.php define('DB_NAME', 'wpdb'); /** MySQL数据库用户名 */ define('DB_USER', 'wpuser'); /** MySQL数据库密码 */ define('DB_PASSWORD', 'redhat'); /** MySQL主机 */ define('DB_HOST', '192.168.1.8');
为WordPress创建数据库及登录用户:
[root@example.com ~]# mysql -uroot -hlocalhost -pEnter password: mysql> CREATE DATABASE wpdb; Query OK, 1 row affected (0.18 sec) mysql> GRANT ALL PRIVILEGES on wpdb.* TO 'wpuser'@'192.168.1.%' IDENTIFIED BY 'redhat'; Query OK, 0 rows affected (0.07 sec) mysql> GRANT ALL PRIVILEGES on wpdb.* TO 'wpuser'@'localhost' IDENTIFIED BY 'redhat'; Query OK, 0 rows affected (0.03 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.05 sec)
提示:在给wpuser用户授权时,由于自己是基于IP方式登录MySQL服务端的,所以授权会出现未知错误,为避免出现此问题还是以localhost方式登录服务器。
配置httpd服务:
[root@example.com ~]# vim /etc/httpd24/httpd.conf #在文件内添加下面的内容,位置最好在AddType下面的行: AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps #下面是关于基于域名进行测试所以为了建立多个虚拟主机,就把中心主机给取消了 #DocumentRoot "/usr/local/apache/htdocs" #在此行中添加index.php DirectoryIndex index.php index.html #将虚拟机的配置文件取消注释 Include /etc/httpd24/extra/httpd-vhosts.conf
配置虚拟主机:
[root@example.com ~]# vim /etc/httpd24/extra/httpd-vhosts.conf <VirtualHost *:80> ServerAdmin webadmin@c.net DocumentRoot "/www/c.net/htdoc" ServerName www.c.net ServerAlias c.net ErrorLog "logs/c.net.err" CustomLog "logs/c.net.access" combined <Directory "/www/c.net/htdoc"> Options None Require all granted </Directory> ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/c.net/htdoc/$1 </VirtualHost>
浏览器登录测试:
为WordPress安装phpMyAdmin管理数据库:
[root@example.com ~]# unzip phpMyAdmin-4.1.4-all-languages.3715384168.zip [root@example.com ~]# cp -r phpMyAdmin-4.1.4-all-languages /www/c.net/htdoc/pma