wqee2 发表于 2015-5-28 08:48:10

CentOS 6.6 环境下 编译安装LNMP

环境:
    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源
# mkdir /mnt/cd
# mount /dev/cdrom /mnt/cd
mount: block device /dev/sr0 is write-protected, mounting read-only
# cd /etc/yum.repos.d/
# vim cd.repo



1
2
3
4
5

name=CentOS-$releasever - Base
baseurl=file:///mnt/cd
enable=1
gpgcheck=0





2、安装开发环境及必要的安装包


1
2
# yum -y groupinstall "Development tools" "Server Platform Development"
# yum -y install pcre-devel




二、安装Nginx
1、创建用户


1
2
# groupadd -g 60 nginx
# useradd -g 60 -u 60 -r -s /sbin/nologin nginx




2、创建编译安装时需要的目录
# mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}

3、编译安装nginx


1
2
3
# tar xf nginx-1.6.2.tar.gz
# cd 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脚本
# 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
# chmod +x /etc/rc.d/init.d/nginx




6、修改PATH环境变量,让系统可以直接使用nginx的相关命令


1
2
3
# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
重载文件
# ./etc/profile.d/nginx.sh




7、添加至服务管理列表,并让其开机自动启动


1
2
# chkconfig --add nginx
# 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
# 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
# partx -a /dev/sda
# partx -a /dev/sda




创建LVM


1
2
3
4
# pvcreate /dev/sda3
# vgcreate myvg /dev/sda3
# lvcreate -L 4G -n mydata myvg
# mke2fs -t ext4 -L MYDATA /dev/myvg/mydata




2、编辑fstab文件自动挂载
# 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
# mkdir /data
# mount -a    //自动挂载
# mount
...
/dev/mapper/myvg-mydata on /data type ext4 (rw)
...





3、创建存放数据的目录,并创建系统mysql用户,以便安全运行进程


1
2
3
4
# mkdir /data/mydata
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /data/mydata mysql
# chown -R mysql:mysql /data/mydata




4、安装并初始化mariadb-5.5.43


1
2
3
4
# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-5.5.43-linux-x86_64/ mysql
# cd mysql/





1
2
# chown -R mysql: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】
# mkdir /etc/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
# cd /usr/local/mysql
# cp support-files/mysql.server/etc/rc.d/init.d/mysqld




添加至服务列表


1
2
3
# chkconfig --add mysqld
# chkconfig mysqld on
#




而后就可以启动服务使用了。

为了使用mariadb的安装符合系统使用规范,这里还需要进行如下步骤:

6、输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:


1
MANPATH/usr/local/mysql/man




7、输出mysql的头文件至系统头文件路径/usr/include:

这可以通过简单的创建链接实现:


1
# ln -sv /usr/local/mysql/include/usr/include/mysql




8、输出mysql的库文件给系统库查找路径:


1
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf




然后让系统重新载入系统库:


1
# ldconfig




9、修改PATH环境变量,让系统可以直接使用mysql的相关命令


1
2
# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
# . /etc/profile.d/mysql.sh





四、编译安装php-5.4.4

1、解决依赖关系并编译安装:
安装依赖程序包


1
2
3
4
5
6
7
# tar xf php-5.4.40.tar.bz2
# cd php-5.4.40
# yum install bzip2 bzip2-devel -y
# yum install mhash mhash-devel mcrypt -y
# yum -y install libxml2 libxml2-devel
# yum -y install curl-devel
# yum install libmcrypt libmcrypt-devel -y




编译安装


1
2
# ./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
# make && make install




2、为php提供配置文件


1
# cp php.ini-production /etc/php.ini




3、为php-fpm提供Sysv init脚本


1
2
3
# cp php.ini-production /etc/php.ini
# cp sapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm




4、并将其添加至服务列表


1
2
# chkconfig --add php-fpm
# chkconfig php-fpm on




5、为php-fpm提供配置文件


1
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf




6、编辑php-fpm的配置文件:
# 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
# service php-fpm start




8、验证是否启动成功 (如果此命令输出有中几个php-fpm进程就说明启动成功了):


1
2
3
4
5
# ps aux | grep php-fpm
root   1217170.00.4 2536844308 ?      Ss   03:19   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)                                                                  
nobody   1218000.72.2 259076 20700 ?      S    03:24   0:17 php-fpm: pool www                                                                                                            
nobody   1218160.42.5 261892 23372 ?      S    03:28   0:08 php-fpm: pool www                                                                                                            
nobody   1218170.32.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_indexindex.php;
            fastcgi_paramSCRIPT_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_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWARE    nginx;
fastcgi_paramQUERY_STRING       $query_string;
fastcgi_paramREQUEST_METHOD   $request_method;
fastcgi_paramCONTENT_TYPE       $content_type;
fastcgi_paramCONTENT_LENGTH   $content_length;
fastcgi_paramSCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME      $fastcgi_script_name;
fastcgi_paramREQUEST_URI      $request_uri;
fastcgi_paramDOCUMENT_URI       $document_uri;
fastcgi_paramDOCUMENT_ROOT      $document_root;
fastcgi_paramSERVER_PROTOCOL    $server_protocol;
fastcgi_paramREMOTE_ADDR      $remote_addr;
fastcgi_paramREMOTE_PORT      $remote_port;
fastcgi_paramSERVER_ADDR      $server_addr;
fastcgi_paramSERVER_PORT      $server_port;
fastcgi_paramSERVER_NAME      $server_name;




并在所支持的主页面格式中添加php格式的主页,类似如下:


1
2
3
4
location / {
            root   html;
            indexindex.php index.html index.htm;
      }




               
3、然后重新加载配置文件



1
# service nginx reload




4、接下来创建index.php页面,测试php是否能正常工作


1
2
3
4
# 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_indexindex.php;
            #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
            fastcgi_paramSCRIPT_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
# mysql      
MariaDB [(none)]> create database wpdb;      
MariaDB [(none)]> GRANT ALL ON wpdb.* TO wpuser@'localhost' IDENTIFIED BY 'wppass';      
MariaDB > GRANT ALL ON wp.* TO wpuser@'172.16.66.%' IDENTIFIED BY 'wppass';




然后安装.......访问



六、安装xcache,为php加速:
1、安装 Xcache


1
2
3
4
5
6
7
8
# tar xf xcache-3.2.0.tar.bz2
# cd 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
# ./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
# mkdir /etc/php.d
# 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
# service php-fpm restart






OK至此 Xcache就安装完成了接下来我们看下效果

七、使用ab进行压力测试

# 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 (mean)      //用户平均等待时间
Time per request:       30.660 (mean, across all concurrent requests)    //服务器平均请求处理时间
Transfer rate:          8.19 received
Connection Times (ms)
            minmean[+/-sd] median   max
Connect:      0    0   0.1      0       1
Processing:   192306 129.9    253    1246
Waiting:      192306 129.9    253    1246
Total:      192306 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 (mean)
Time per request:       11.158 (mean, across all concurrent requests)
Transfer rate:          22.49 received
Connection Times (ms)
            minmean[+/-sd] median   max
Connect:      0    0   1.0      0      33
Processing:    6211120.3    105   207
Waiting:       6211120.3    105   207
Total:         6311120.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)





可以明显看到 服务器并发吞吐量以及用户请求时长等性能都有了明显的提升........

不足之处还请各位看官指正。。。。。。
页: [1]
查看完整版本: CentOS 6.6 环境下 编译安装LNMP