环境架构:windows 7宿主机 虚拟机vmvare-11.0 CentOS6.5 x86_64
第一种:模块式
一、编译安装二进制mysql
#groupadd -r mysql
#useradd -g mysql -r -s /sbin/nologin
#tar xf mysql-5.5.42-linux2.6-x86_64.tar.gz -C /usr/local/
#cd /usr/local/
#创建链接文件,方便日后升级。
#ln -sv mysql-5.5.42-linux2.6-x86_64 mysql
#cd mysql
#验证添加用户、用户组是否成功
#id mysql
#创建数据库的存放数据目录
#mkdir /data
#这里我在虚拟机中单独添加一块硬盘进行分区创建逻辑卷,也可以在本地磁盘空间分区创建;
#fdisk /dev/sdb1
n
p
1
t
8e
w
#分别创建物理卷、卷组、逻辑卷;
#pvcreate /dev/sdb1
#vgcreate myvg /dev/sdb1
#lvcreate -n mydata -L 10G myvg
#查看创建成功与否
#lvs
#进行分区和开机挂载
#mke2fs -t ext4 /dev/myvg/mydata
#vim /etc/fstab
添加代码:
/dev/myvg/mydata /data ext4 defaults,noatime 0 0
#Mount all filesystems (of the given types) mentioned in fstab.
#mount -a
#查看挂载情况
#mount
#cd /data/
#mkdir mydata
#chown -R mysql.mysql mydata/
#切换目录更改目录及子目录属主属组
#cd /usr/local/mysql
#chown -R root.mysql ./*
#初始化数据库
#scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql
#复制服务启动文件
#cp support-files/mysql.server /etc/rc.d/init.d/mysqld
#chkconfig --add mysqld
#查看年添加是否成功
#chkconfig --list mysqld
#复制配置文件并添加mysql的数据目录;
#cp support-files/my-large.cnf /etc/my.cnf
#vim /etc/my.cnf
在thread_concurrency = 8这一行的下面,添加如下代码:
datadir = /data/mydata
#启动服务,查看成功与否。
#service mysqld start
#编辑服务脚本,并生效。
#vim /etc/profile.d/mysql.sh
添加如下代码即可:
export PATH=/usr/local/mysql/bin:$PATH
#source /etc/profile.d/mysql.sh
#为了方便php安装可能会应用到mysql的头文件,我们添加链接;
#ln -sv /usr/local/mysql/include/ /usr/include/mysql
#编辑文件,更新库文件到缓存。
#vim /etc/ld.so.conf.d/mysql.conf
#ldconfig
#Print the lists of directories and candidate libraries stored in the current cache.
#ldconfig -p | grep mysql
#最后,就可以登录mysql,进行root用户的密码设置等操作。需要注意的是远程主机登陆不了mysql的root用户。
#需要强调的是这里我并没有设置mysql的man的路径,因为系统的文件/etc/man.config已经包含在内,
当然,可以也可以添加一行:MANPATH /usr/local/mysql/man
输入mysql命令,然后进入mysql的命令行,执行的命令显示参考如下:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
Database changed
mysql> select user,host,password from user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | centos | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | centos | |
+------+-----------+----------+
6 rows in set (0.00 sec)
mysql> drop user ''@localhost ;
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,password from user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | centos | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | centos | |
+------+-----------+----------+
5 rows in set (0.03 sec)
mysql> drop user ''@centos;
Query OK, 0 rows affected (0.02 sec)
mysql> select user,host,password from user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | centos | |
| root | 127.0.0.1 | |
| root | ::1 | |
+------+-----------+----------+
4 rows in set (0.00 sec)
mysql> update user set password=password('mysql') where user='root';
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| root | centos | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| root | 127.0.0.1 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| root | ::1 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
#重读用户表
mysql> flush privileges;
二、编译安装httpd2.4
#在配置好yum源的前提下先安装pcre-devel;因为它的安装依赖次开发库;还有额外的两个包;
#yum -y install pcre-devel
#先解压这两个包
apr-1.5.2.tar.bz2
apr-util-1.5.4.tar.bz2
tar xf apr-1.5.2.tar.bz2
tar xf apr-util-1.5.4.tar.bz2
然后,分别编译安装;
cd apr-1.5.2
./configure --prefix=/usr/local/apr
make && make install
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
make && make install
# tar xf httpd-2.4.23.tar.bz2
# cd httpd-2.4.23
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd2.4 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event
# make && make install
如果报错如下:
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
需要:yum install openssl-devel后,然后再次./configure.....即可;
最后,执行make && make install。
[root@centos ~]# vim /etc/httpd2.4/httpd.conf
#在ServerRoot "/usr/local/apache"一行下面添加如下代码:
PidFile "/var/run/httpd/httpd.pid" //此行代码表示httpd服务启动后会生成一个进程的id文件;
#顺便改写ServerName www.centos.com,防止启动httpd服务时提示解析不了目标主机,也就是自己的主机名;
#如果添加最新的man文档,可编辑文件:vim /etc/man.config 添加MANPATH /usr/local/apache/man,但是需要重启,
#因为之前你如果安装过旧版本的httpd,你执行的man命令查看的是旧版本的手册,如果不重启的话,可以man -M /usr/local/apache/man httpd
此时,我们需要提供一个服务脚本,复制httpd2.2即可:
cd /etc/rc.d/init.d/
cp httpd httpd-2.4
vim httpd-2.4
修改文件的这两行即可:
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
保存退出;
[root@centos ~]# vim /etc/profile.d/httpd.sh
#添加一行
export PATH=/usr/local/apache/bin:$PATH
[root@centos ~]# source /etc/profile.d/httpd.sh
[root@centos ~]# vim /etc/hosts
#添加一行
192.168.136.136 centos
[root@centos ~]# service httpd-2.4 start
然后我们打开网页,输入192.168.136.136(自己的主机IP地址)回车,即可看见 It Works !
到此,htppd2.4安装完成。
三、编译安装php-5.6.25
如果想让php支持mcrypt,需要两个安装的开发包:
libmcrypt-2.5.8-9.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
bzip2-devel(避免预编译的时候,提示报错。)
[root@centos ~]# rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
[root@centos ~]# yum install -y bzip2-devel
[root@centos ~]# tar xf php-5.6.25.tar.bz2
[root@centos ~]# cd php-5.6.25
[root@centos php-5.6.25 ]#./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
#复制源码文件下的这个文件到/etc下,把此配置文件重命名;
[root@centos php-5.6.25]# cp php.ini-production /etc/php.ini
[root@centos ~]# vim /etc/httpd2.4/httpd.conf
#在
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#在
DirectoryIndex index.php index.html
[root@centos ~]# cd /usr/local/apache/htdocs/
[root@centos htdocs]# mv index.html index.php
[root@centos htdocs]# vim index.php
#删除原有代码,改成如下代码并保存退出:
phpinfo();
?>
#重启服务,然后打开网页输入:192.168.136.136 测试是否成功;
[root@centos ~]# service httpd-2.4 restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
当然,也可以重新编辑这个文件,测试是否链接mysql数据:
vim index.php 改成如下代码:
$link = mysql_connect('127.0.0.1','root','mysql');
if ($link)
echo "Success...";
else
echo "Failure...";
mysql_close();
?>
再重启httpd-2.4服务,刷新网页,会提示:Success...;此刻,然后关闭mysqld的服务,刷新页面,会提示:Failure...;
四、安装xcache,为php加速:
安装之前,我们先安装个phpMyAdmin,然后压力测试一下,这样好与xcache形成对比;
[root@centos ~]# tar xf phpMyAdmin-3.4.3.2-all-languages.tar.bz2
[root@centos ~]# mv phpMyAdmin-3.4.3.2-all-languages /usr/local/apache/htdocs/pma
在ab命令工具压力测试前,需要注意的是,ab -c 2000 -n 5000 http://192.168.136.136/pma/index.php
默认是1000,超过1000的话我们要使用ulimit -n 数值,提高其默认值。如果你在虚拟机上运行的话,建议两个数值设置
最多都不要超过100。否则,结果会返回很慢。
[root@centos ~]# ab -c 50 -n 100 http://192.168.136.136/pma/index.php
.......
.......
Server Software: Apache/2.4.23
Server Hostname: 192.168.136.136
Server Port: 80
Document Path: /pma/index.php
Document Length: 7747 bytes
Concurrency Level: 50
Time taken for tests: 4.406 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 859236 bytes
HTML transferred: 774700 bytes
Requests per second: 22.69 [#/sec] (mean)
Time per request: 2203.190 [ms] (mean)
Time per request: 44.064 [ms] (mean, across all concurrent requests)
Transfer rate: 190.43 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 2.9 2 7
Processing: 724 1788 485.0 1817 2895
Waiting: 724 1784 485.0 1817 2892
Total: 724 1791 485.4 1817 2895
Percentage of the requests served within a certain time (ms)
50% 1817
66% 2000
75% 2156
80% 2248
90% 2426
95% 2604
98% 2745
99% 2895
100% 2895 (longest request)
[root@centos ~]# tar xf xcache-3.2.0.tar.bz2
[root@centos ~]# cd /usr/local/php/
[root@centos php]# ls php/
man
[root@centos php]# man -M php/man/ phpize
#注意第一行的解释:为php扩展的准备工作做编译。
phpize - prepare a PHP extension for compiling
[root@centos ~]# cd xcache-3.2.0
[root@centos xcache-3.2.0]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@centos xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@centos xcache-3.2.0]# make && make install
#这是编译安装后的最后一行的显示:安装的扩展共享路径,这个很有用,我们稍后会把它添加到一个文件中;
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
[root@centos xcache-3.2.0]# mkdir /etc/php.d
[root@centos xcache-3.2.0]# cp xcache.ini /etc/php.d/
[root@centos xcache-3.2.0]# vim /etc/php.d/xcache.ini
#把上面的这个路径信息:/usr/local/php/lib/php/extensions/no-debug-zts-20131226/添加进来;
extension =/usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
[root@centos ~]# ab -c 50 -n 100 http://192.168.136.136/pma/index.php
.......
.......
Server Software: Apache/2.4.23
Server Hostname: 192.168.136.136
Server Port: 80
Document Path: /pma/index.php
Document Length: 7747 bytes
Concurrency Level: 50
Time taken for tests: 15.310 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 859264 bytes
HTML transferred: 774700 bytes
Requests per second: 6.53 [#/sec] (mean)
Time per request: 7655.132 [ms] (mean)
Time per request: 153.103 [ms] (mean, across all concurrent requests)
Transfer rate: 54.81 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 37 45.6 13 119
Processing: 687 7083 4952.9 6243 14473
Waiting: 686 7028 4975.0 6243 14470
Total: 689 7119 4970.8 6243 14558
Percentage of the requests served within a certain time (ms)
50% 6243
66% 11139
75% 12097
80% 12599
90% 14044
95% 14133
98% 14497
99% 14558
100% 14558 (longest request)
第二种:fpm式
前提条件:与lamp模块安装方式一样,首先得编译安装php-5.6.25和httpd-2.4
需要注意的是php编译安装的时候需要添加和去掉几项,具体的执行命令:
./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 --enable-maintainer-zts
编辑文件:vim /etc/httpd-2.4/httpd.conf,(注意此文件的备份)操作如下:
1)去掉#,开启加载的这两个模块:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
2)#
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
3)#此外我们为了演示效果,开启虚拟主机,关闭中心主机,即找到这两行代码,去掉#和添加#;
# Virtual hosts
Include /etc/httpd-2.4/extra/httpd-vhosts.conf
#DocumentRoot "/usr/local/apache/htdocs"
cd /etc/httpd-2.4/extra/
#备份虚拟主机的配置文件后,再编辑;
cp httpd-vhosts.conf httpd-vhosts.conf.bak
vim httpd-vhosts.conf
#修改内容如下:
ServerAdmin [email protected]
DocumentRoot "/web/a.com/htdocs"
ServerName www.a.com
ServerAlias a.com
ErrorLog "logs/a.com.err"
CustomLog "logs/a.com.access" combined
ServerAdmin [email protected]
DocumentRoot "/web/b.org/htdocs"
ServerName www.b.org
ServerAlias b.org
ErrorLog "logs/b.org.err"
CustomLog "logs/b.org.access" combined
mkdir -pv /web/{a.com,b.org}/htdocs
测试语法:httpd -t
#没问题后,简单的编辑下两个站点的测试页;
vim /web/a.com/htdocs/index.html
添加一行代码:a.com
vim /web/b.org/htdocs/index.html
添加一行代码:b.org
#编辑文件hosts添加两行代码,用来解析主机;
vim /etc/hosts
192.168.136.136 www.a.com a.com
192.168.136.136 www.b.org b.org
#安装elinks命令工具包
yum install -y elinks
#对其中的一个站点进行测试链接;
elinks -dump http://www.a.com
[root@centos extra]# elinks -dump http://www.a.com
Forbidden
You don't have permission to access / on this server.
#添加授权代码
[root@centos extra]# vim httpd-vhosts.conf
ServerAdmin [email protected]
DocumentRoot "/web/a.com/htdocs"
ServerName www.a.com
ServerAlias a.com
ErrorLog "logs/a.com.err"
CustomLog "logs/a.com.access" combined
Options None
Require all granted
ServerAdmin [email protected]
DocumentRoot "/web/b.org/htdocs"
ServerName www.b.org
ServerAlias b.org
ErrorLog "logs/b.org.err"
CustomLog "logs/b.org.access" combined
Options None
Require all granted
#然后我们再测试就ok了
[root@centos extra]# pwd
/etc/httpd2.4/extra
[root@centos extra]# elinks -dump http://www.a.com
a.com
[root@centos extra]# elinks -dump http://www.b.org
b.org
[root@centos php-5.6.25]# cd sapi/fpm/
[root@centos fpm]# cp init.d.php-fpm.in /etc/rc.d/init.d/php-fpm
[root@centos fpm]# chmod +x /etc/rc.d/init.d/php-fpm
[root@centos fpm]# chkconfig --add php-fpm
[root@centos etc]# pwd
/usr/local/php/etc
#给上述的脚本提供配置文件,只需系统自带的配置文件,在此目录下的重命名,编辑简单修改即可;
[root@centos etc]# cp php-fpm.conf.default php-fpm.conf
[root@centos etc]# vim php-fpm.conf
#这里的;是注释,只加一行,这里添加php的安装路径;此外,记录错误日志和系统日志,可开启可不开启,系统是默认的;
[global]
; Pid file
; Note: the default prefix is /usr/local/php/var
; Default Value: none
;pid = run/php-fpm.pid
pid =/usr/local/php/var/run/php-fpm.pid
..........
..........
pm.max_children = 50
; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 5
; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 2
; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 6
........
........
#最后,切换目录,编辑虚拟主机的文件,添加开启反向代理的两行代码;
cd /etc/httpd-2.4/extra/
vim httpd-vhosts.conf
ServerAdmin [email protected]
DocumentRoot "/web/a.com/htdocs"
ServerName www.a.com
ServerAlias a.com
ErrorLog "logs/a.com.err"
CustomLog "logs/a.com.access" combined
Options None
Require all granted
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/a.com/htdocs/$1
ServerAdmin [email protected]
DocumentRoot "/web/b.org/htdocs"
ServerName www.b.org
ServerAlias b.org
ErrorLog "logs/b.org.err"
CustomLog "logs/b.org.access" combined
Options None
Require all granted
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/b.org/htdocs/$1
保存退出后,我们可以打开浏览器输入www.a.com/pma进行测试。但是,可能会出现无法解析的情况。
一般在LINUX下修改完/etc/hosts文件都会立即生效的,即便偶尔不生效,只要重启一下network服务就可以了。但是windows却是会遇到这样的问题,而且在不同版本的系统中还略有区别。
所以我们需要修改windows的Hosts文件:
首先要知道hosts文件的位置,在xp,2000,win7等系统中找到如下位置C:\windows\system32\drivers\etc,hosts文件默认具有隐藏属性,系统默认设置情况下是看不到的,解决办法是打开
我的电脑-点击工具-文件夹选项-查看-取消勾选隐藏受保护的系统文件,确定即可。具体怎么修改,打开此文件看了就会,跟修改Linux的hosts基本一致;
当然,xcache还未安装,可参考之前lamp模块式中所叙述的操作。
尽管,这两种方式,在实际生产环境中不太可能同时使用,但是,依然需要全都掌握。