环境:
OS: CentOS 6.6
IP: 172.16.66.100
Nginx: nginx-1.6.2
PHP: php-5.4.40
Xcache: xcache-3.2.0
Mysql: mariadb-5.5.43
一、前期环境准备:
1、根据官方ISO 创建本地yum源
[root@1inux ~]# mkdir /mnt/cd
[root@1inux ~]# mount /dev/cdrom /mnt/cd
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@1inux ~]# cd /etc/yum.repos.d/
[root@1inux yum.repos.d]# vim cd.repo
1
2
3
4
5
|
[CD] name=CentOS- $releasever - Base
baseurl=file: ///mnt/cd
enable=1 gpgcheck=0 |
2、安装开发环境及必要的安装包
1
2
|
[root@1inux yum.repos.d]# yum -y groupinstall "Development tools" "Server Platform Development"
[root@1inux yum.repos.d]# yum -y install pcre-devel |
二、安装Nginx
1、创建用户
1
2
|
[root@1inux ~]# groupadd -g 60 nginx [root@1inux ~]# useradd -g 60 -u 60 -r -s /sbin/nologin nginx |
2、创建编译安装时需要的目录
[root@1inux nginx-1.6.2]# mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
3、编译安装nginx
1
2
3
|
[root@1inux tmp]# tar xf nginx-1.6.2.tar.gz [root@1inux tmp]# cd nginx-1.6.2 [root@1inux nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/ var /log/nginx/error.log --http-log-path=/ var /log/nginx/access.log --pid-path=/ var /run/nginx/nginx.pid --lock-path=/ var /lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/ var /tmp/nginx/client --http-proxy-temp-path=/ var /tmp/nginx/proxy --http-fastcgi-temp-path=/ var /tmp/nginx/fastcgi --http-uwsgi-temp-path=/ var /tmp/nginx/uwsgi --http-scgi-temp-path=/ var /tmp/nginx/scgi
|
4、为nginx提供SysV init脚本
[root@1inux nginx-1.6.2]# vim /etc/rc.d/init.d/nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#!/bin/sh # # nginx - this script starts and stops the nginx daemon
# # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server
# processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: / var /run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0
nginx= "/usr/local/nginx/sbin/nginx"
prog=$( basename $nginx )
NGINX_CONF_FILE= "/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/ var /lock/nginx.lock
make_dirs() { # make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=` $nginx -V 2>&1 | grep 'configure arguments:' `
for opt in $options ; do
if [ ` echo $opt | grep '.*-temp-path' ` ]; then
value=` echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
} start() { [ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $ "Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
} stop() { echo -n $ "Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
} restart() { configtest || return $?
stop
sleep 1
start
} reload() { configtest || return $?
echo -n $ "Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
} force_reload() { restart
} configtest() { $nginx -t -c $NGINX_CONF_FILE
} rh_status() { status $prog
} rh_status_q() { rh_status >/dev/null 2>&1
} case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart| try -restart)
rh_status_q || exit 0
;;
*)
echo $ "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac |
添加执行权限
1
|
[root@1inux nginx-1.6.2]# chmod +x /etc/rc.d/init.d/nginx
|
6、修改PATH环境变量,让系统可以直接使用nginx的相关命令
1
2
3
|
[root@1inux nginx-1.6.2]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
重载文件 [root@1inux nginx-1.6.2]# . /etc/profile.d/nginx.sh |
7、添加至服务管理列表,并让其开机自动启动
1
2
|
[root@1inux nginx-1.6.2]# chkconfig --add nginx [root@1inux nginx-1.6.2]# service nginx start |
三、安装Mariadb
1、创建数据存放的文件系统--LVM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@1inux nginx-1.6.2]# fdisk /dev/sda WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c' ) and change display units to
sectors (command 'u' ).
Command (m for help): n
Command action e extended
p primary partition (1-4)
p Partition number (1-4): 3 First cylinder (7859-10443, default 7859):
Using default value 7859
Last cylinder, +cylinders or +size{K,M,G} (7859-10443, default 10443): +6G
Command (m for help): t
Partition number (1-4): 3 Hex code (type L to list codes): 83 Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks. |
让内核识别分区表
1
2
|
[root@1inux nginx-1.6.2]# partx -a /dev/sda [root@1inux nginx-1.6.2]# partx -a /dev/sda |
创建LVM
1
2
3
4
|
[root@1inux nginx-1.6.2]# pvcreate /dev/sda3 [root@1inux nginx-1.6.2]# vgcreate myvg /dev/sda3 [root@1inux nginx-1.6.2]# lvcreate -L 4G -n mydata myvg [root@1inux nginx-1.6.2]# mke2fs -t ext4 -L MYDATA /dev/myvg/mydata |
2、编辑fstab文件自动挂载
[root@1inux nginx-1.6.2]# vim /etc/fstab
添加如下
1
2
3
4
|
---------------------- /dev/myvg/mydata /data ext4 defaults 0 0 或者 LABEL=MYDATA /data ext4 defaults 0 0 |
---------------------------
1
2
3
4
5
6
|
[root@1inux nginx-1.6.2]# mkdir /data
[root@1inux nginx-1.6.2]# mount -a //自动挂载
[root@1inux nginx-1.6.2]# mount ... /dev/mapper/myvg-mydata on /data type ext4 (rw) ... |
3、创建存放数据的目录,并创建系统mysql用户,以便安全运行进程
1
2
3
4
|
[root@1inux data]# mkdir /data/mydata
[root@1inux tmp]# groupadd -r mysql [root@1inux tmp]# useradd -g mysql -r -s /sbin/nologin -M -d /data/mydata mysql [root@1inux tmp]# chown -R mysql:mysql /data/mydata
|
4、安装并初始化mariadb-5.5.43
1
2
3
4
|
[root@1inux tmp]# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local [root@1inux tmp]# cd /usr/local [root@1inux local]# ln -sv mariadb-5.5.43-linux-x86_64/ mysql [root@1inux local]# cd mysql/ |
1
2
|
[root@1inux mysql]# chown -R mysql:mysql . //修改当前目录权限
[root@1inux mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mydata //初始化数据
|
4、为mariadb-5.5.43 提供配置文件
1
2
3
|
【MySQL的配置文件查找次序:/etc/my.cnf --> /etc/mysql/my.cnf --> ~/.my.cnf】 [root@1inux mysql]# mkdir /etc/mysql
[root@1inux mysql]# cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf |
并修改此文件中thread_concurrency的值为你的CPU个数乘以2 【其中cpu个数可以使用lscpu查看】
# vim /etc/mysql/my.cnf
1
2
3
4
|
thread_concurrency = 2 //并添加以下内容: datadir = /data/mydata innodb_file_per_table = on |
5、为mariadb 提供sysv服务脚本
1
2
|
[root@1inux data]# cd /usr/local/mysql [root@1inux mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld |
添加至服务列表
1
2
3
|
[root@1inux mysql]# chkconfig --add mysqld [root@1inux mysql]# chkconfig mysqld on [root@1inux mysql]# |
而后就可以启动服务使用了。
为了使用mariadb的安装符合系统使用规范,这里还需要进行如下步骤:
6、输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
1
|
MANPATH /usr/local/mysql/man |
7、输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
1
|
[root@1inux mysql]# ln -sv /usr/local/mysql/ include /usr/ include /mysql
|
8、输出mysql的库文件给系统库查找路径:
1
|
[root@1inux mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
|
然后让系统重新载入系统库:
1
|
[root@1inux mysql]# ldconfig |
9、修改PATH环境变量,让系统可以直接使用mysql的相关命令
1
2
|
[root@1inux mysql]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@1inux mysql]# . /etc/profile.d/mysql.sh |
四、编译安装php-5.4.4
1、解决依赖关系并编译安装:
安装依赖程序包
1
2
3
4
5
6
7
|
[root@1inux php-5.4.40]# tar xf php-5.4.40.tar.bz2 [root@1inux php-5.4.40]# cd php-5.4.40 [root@1inux php-5.4.40]# yum install bzip2 bzip2-devel -y [root@1inux php-5.4.40]# yum install mhash mhash-devel mcrypt -y [root@1inux php-5.4.40]# yum -y install libxml2 libxml2-devel [root@1inux php-5.4.40]# yum -y install curl-devel [root@1inux php-5.4.40]# yum install libmcrypt libmcrypt-devel -y |
编译安装
1
2
|
[root@1inux php-5.4.40]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl [root@1inux php-5.4.40]# make && make install |
2、为php提供配置文件
1
|
[root@1inux php-5.4.40]# cp php.ini-production /etc/php.ini |
3、为php-fpm提供Sysv init脚本
1
2
3
|
[root@1inux php-5.4.40]# cp php.ini-production /etc/php.ini [root@1inux php-5.4.40]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@1inux php-5.4.40]# chmod +x /etc/rc.d/init.d/php-fpm
|
4、并将其添加至服务列表
1
2
|
[root@1inux php-5.4.40]# chkconfig --add php-fpm [root@1inux php-5.4.40]# chkconfig php-fpm on |
5、为php-fpm提供配置文件
1
|
[root@1inux php-5.4.40]# cp /usr/local/php/etc/php-fpm.conf. default /usr/local/php/etc/php-fpm.conf
|
6、编辑php-fpm的配置文件:
[root@1inux php-5.4.40]# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
1
2
3
4
5
|
pm.max_children = 150 pm.start_servers = 8 pm.min_spare_servers = 5 pm.max_spare_servers = 10 pid = /usr/local/php/ var /run/php-fpm.pid
|
7、现在就可以启动php-fpm了
1
|
[root@1inux php-5.4.40]# service php-fpm start |
8、验证是否启动成功 (如果此命令输出有中几个php-fpm进程就说明启动成功了):
1
2
3
4
5
|
[root@1inux php-5.4.40]# ps aux | grep php-fpm root 121717 0.0 0.4 253684 4308 ? Ss 03:19 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nobody 121800 0.7 2.2 259076 20700 ? S 03:24 0:17 php-fpm: pool www nobody 121816 0.4 2.5 261892 23372 ? S 03:28 0:08 php-fpm: pool www nobody 121817 0.3 2.2 259076 20700 ? S 03:28 0:07 php-fpm: pool www |
五、整合nginx和php5
1、编辑/etc/nginx/nginx.conf,启用如下选项:
1
2
3
4
5
6
7
|
location ~ \.php$ { root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts $fastcgi_script_name ;
include fastcgi_params;
}
|
2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string ;
fastcgi_param REQUEST_METHOD $request_method ;
fastcgi_param CONTENT_TYPE $content_type ;
fastcgi_param CONTENT_LENGTH $content_length ;
fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ;
fastcgi_param SCRIPT_NAME $fastcgi_script_name ;
fastcgi_param REQUEST_URI $request_uri ;
fastcgi_param DOCUMENT_URI $document_uri ;
fastcgi_param DOCUMENT_ROOT $document_root ;
fastcgi_param SERVER_PROTOCOL $server_protocol ;
fastcgi_param REMOTE_ADDR $remote_addr ;
fastcgi_param REMOTE_PORT $remote_port ;
fastcgi_param SERVER_ADDR $server_addr ;
fastcgi_param SERVER_PORT $server_port ;
fastcgi_param SERVER_NAME $server_name ;
|
并在所支持的主页面格式中添加php格式的主页,类似如下:
1
2
3
4
|
location / { root html;
index index.php index.html index.htm;
}
|
3、然后重新加载配置文件
1
|
[root@1inux php-5.4.40]# service nginx reload |
4、接下来创建index.php页面,测试php是否能正常工作
1
2
3
4
|
[root@1inux nginx]# vim /usr/local/nginx/html/index.php <?php
phpinfo();
?>
|
5、访问主页
能够正常访问,说明 nginx+ php 已经配置完成
6、在nginx中添加虚拟主机配置WordPress blog
编辑配置文件/etc/nginx/nginx.conf,添加如下内容:server {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
listen 80;
server_name blog.1inux.com;
access_log logs/blog.access; //其日志目录为: /usr/local/nginx/logs/blog.access
error_log logs/blog.error;
location / {
root /vhost/blog;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /vhost/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts $fastcgi_script_name ;
fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ;
include fastcgi_params;
}
} |
创建目录
1
2
3
4
|
# mkdir -pv /vhost/blog/
# unzip wordpress-3.2.1-zh_CN.zip # mv wordpress/* /vhost/blog/ # chown -R nginx:nginx /vhost/blog
|
创建数据库及数据库用户密码
1
2
3
4
|
[root@1inux ~]# mysql MariaDB [(none)]> create database wpdb; MariaDB [(none)]> GRANT ALL ON wpdb.* TO wpuser@ 'localhost' IDENTIFIED BY 'wppass' ;
MariaDB [wpdb]> GRANT ALL ON wp.* TO wpuser@ '172.16.66.%' IDENTIFIED BY 'wppass' ;
|
然后安装.......访问
六、安装xcache,为php加速:
1、安装 Xcache
1
2
3
4
5
6
7
8
|
[root@1inux tmp]# tar xf xcache-3.2.0.tar.bz2 [root@1inux tmp]# cd xcache-3.2.0 [root@1inux xcache-3.2.0]# /usr/local/php/bin/phpize Configuring for :
PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 [root@1inux xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config |
安装结束时,会出现类似如下行:
1
|
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/ |
2、编辑php.ini,整合php和xcache:
1
2
|
[root@1inux xcache-3.2.0]# mkdir /etc/php.d
[root@1inux xcache-3.2.0]# cp xcache.ini /etc/php.d |
注:xcache.ini文件在xcache的源码目录中
3、编辑/etc/php.d/xcache.ini,找到extension开头的行,修改为如下行:
将 extension = xcache.so 修改为如下:
1
|
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so |
注:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。
4、重新启动php-fpm
1
|
[root@1inux xcache-3.2.0]# service php-fpm restart |
OK 至此 Xcache就安装完成了接下来我们看下效果
七、使用ab进行压力测试
[root@1inux ~]# ab -n 1000 -c 10 http://blog.1inux.com/index.php //总请求1000 并发10
1、安装Xcache加速前进行的压力测试 效果如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
Server Software: nginx/1.6.2 Server Hostname: blog.1inux.com Server Port: 80 Document Path: /index.php Document Length: 0 bytes Concurrency Level: 10 //并发请求数
Time taken for tests: 30.660 seconds //处理请求所花费的总时间
Complete requests: 1000 //总请求数
Failed requests: 0 Write errors: 0 Non-2xx responses: 1000 Total transferred: 257000 bytes HTML transferred: 0 bytes Requests per second: 32.62 [#/sec] (mean) //服务器并发吞吐量
Time per request: 306.602 [ms] (mean) //用户平均等待时间
Time per request: 30.660 [ms] (mean, across all concurrent requests) //服务器平均请求处理时间
Transfer rate: 8.19 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max
Connect: 0 0 0.1 0 1 Processing: 192 306 129.9 253 1246 Waiting: 192 306 129.9 253 1246 Total: 192 306 129.9 253 1246 Percentage of the requests served within a certain time (ms) 50% 253
66% 308
75% 350
80% 421
90% 483
95% 492
98% 596
99% 737
100% 1246 (longest request)
|
2、安装Xcache加速并启用后 进行压力测试结果如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
Server Software: nginx/1.6.2 Server Hostname: blog.1inux.com Server Port: 80 Document Path: /index.php Document Length: 0 bytes Concurrency Level: 10 Time taken for tests: 11.158 seconds
Complete requests: 1000 Failed requests: 0 Write errors: 0 Non-2xx responses: 1000 Total transferred: 257000 bytes HTML transferred: 0 bytes Requests per second: 89.62 [#/sec] (mean) Time per request: 111.577 [ms] (mean) Time per request: 11.158 [ms] (mean, across all concurrent requests) Transfer rate: 22.49 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max
Connect: 0 0 1.0 0 33 Processing: 62 111 20.3 105 207 Waiting: 62 111 20.3 105 207 Total: 63 111 20.3 105 207 Percentage of the requests served within a certain time (ms) 50% 105
66% 114
75% 125
80% 131
90% 142
95% 149
98% 157
99% 161
100% 207 (longest request)
|
可以明显看到 服务器并发吞吐量以及用户请求时长等性能都有了明显的提升........
不足之处还请各位看官指正。。。。。。
本文转自 1inux 51CTO博客,原文链接:http://blog.51cto.com/1inux/1655591