lnmp 架构(源码编译)

一 lnmp架构

第一步,浏览器发送http request请求到服务器(Nginx),服务器响应并处理web请求,将一些静态资源(CSS,图片,视频等)保存服务器上。

 第二步,将php脚本通过接口传输协议(网关协议)PHP-FCGI(fast-cgi)传输给PHP-FPM(进程管理程序),PHP-FPM 不做处理,然后PHP-FPM调用PHP解析器进程,PHP解析器解析php脚本信息。PHP解析器进程可以启动多个,进行并发执行。

 第三步,将解析后的脚本返回到PHP-FPM,PHP-FPM再通过fast-cgi的形式将脚本信息传送给Nginx。

 第四步,服务器再通过Http response的形式传送给浏览器。浏览器再进行解析与渲染然后进行呈现。

二 实验步骤

1 mysql 源码安装

1)源码编译

wget mysql-boost-5.7.11.tar.gz cmake-2.8.12.2-4.el6.x86_64.rpm (得到安装包)
yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel(安装软件包依赖性)
yum install cmake-2.8.12.2-4.el6.x86_64.rpm(由于版本需求安装cmake工具)
cd mysql-5.7.11/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0/ 源码编译为二进制文件
make && make install (源码安装三部曲)

2)配置文件

cd /etc
mv my.cnf my.cnf.back
cd /usr/local/mysql/support-files/
cp my-default.cnf /etc/my.cnf
vim /etc/my.cnf

basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /usr/local/mysql/data/mysql.sock

cd /usr/local/mysql/support-files/

cp mysql.server /etc/init.d/mysqld

cd ..

groupadd -g 27 mysql

useradd -u 27 -g 27 mysql

chown mysql.mysql . -R

vim ~/.bash_profile

source ~/.bash_profile

cd /usr/local/mysql/bin/

mysqld --initialize --user=mysql

mysql_secure_installation (输入初始化生成的随机数)

注意:如果出现报错 Error: Can't connect to local MySQL server through socket
'/usr/local/mysql/data/mysql.sock' (2)

重新启动服务/etc/init.d/mysqld start

chown root.mysql . -R 增加数据库权限

chown mysql data/ -R

mysql -p 登陆数据查看说明安装结束

2 PHP的源码安装

1 )源码安装

tar jxf php-5.6.35.tar.bz2

cd php-5.6.35

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --without-pear --with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-mcrypt --with-mhash


出现了上图报错我们需要继续安装软件包依赖性

yum install -y libxml2-devel
yum install curl-devel -y
yum install gd-devel-2.0.35-11.el6.x86_64.rpm  -y
yum install libmcrypt-* -y
yum install re2c-0.13.5-1.el6.x86_64.rpm -y
yum install libjpeg-turbo-* -y
yum install -y gmp-devel
yum install -y net-snmp-devel
这是我自己的系统环境,编译时根据自己的报错进行软件包安装

下图列出可能需要去下载的安装包

编译完成界面

make && make install

cd /usr/local/php/etc

cp php-fpm.conf.default php-fpm.conf

groupadd -g 500 nginx

useradd -u 500 -g 500 nginx

vim php-fpm.conf

cd ..

cp php.ini-production /usr/local/php/etc/php.ini

cd /usr/local/php/etc

vim php.ini

cd /root/php-5.6.35/sapi/fpm

[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/
[root@server1 fpm]# cd /etc/init.d/
[root@server1 init.d]# mv init.d.php-fpm php-fpm

chmod +x php-fpm

/etc/init.d/php-fpm start

查看端口

三 nginx的源码编译

1 )源码安装

tar zxf nginx-1.14.0.tar.gz

cd nginx-1.14.0

vim src/core/nginx.h #隐藏版本号

vim auto/cc/gcc

修改debug模式,减少生成的编译文件

2 )编译三部曲

[root@server1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-threads --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42

安装软件依赖性  (nginx rewrite模块需要pcre)

yum install pcre-devel  -y

继续编译

make && make install

3 ) 配置文件修改

  1 cd /usr/local/nginx/conf  添加nginx 用户和用户组 增加连接数
 

user  nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  65535;
}

2 查看内核支持的最大连接数

sysctl -a | grep file

可以看出内核是支持并发的连接数。

3 修改内核文件配置nginx 并发连接

vim /etc/security/limits.conf

4 增加发布目录文件

vim /usr/local/nginx/conf/nginx.conf

 location / {
            root   html;
            index   index.php index.html index.htm;
        }

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.conf;
        }

查看  cat fastcgi.conf 包含了我们需要的配置

5 启动脚本的软连接

ln -s /usr/local/nginx/sbin/nginx /sbin/

6 测试php

cd /usr/local/nginx/html/

当然你也可以去测试一下nginx

从这里看出nginx也已经安装完毕且和php之间的关联没有问题。

四 mysql 和php之间的配置

cd /usr/local/php/etc/

vim php.ini

[Pdo_mysql]
; If mysqlnd is used: Number of cache slots for the internal result set cache
; http://php.net/pdo_mysql.cache_size
pdo_mysql.cache_size = 2000

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket= /usr/local/mysql/data/mysql.sock  #增加pdo_mysql socket接口


[MySQL]
; Default port number for mysql_connect().  If unset, mysql_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
; http://php.net/mysql.default-port
mysql.default_port =  

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysql.default-socket
mysql.default_socket = /usr/local/mysql/data/mysql.sock  #增加mysql socket接口

; Default host for mysql_connect() (doesn't apply in safe mode).
; http://php.net/mysql.default-host
mysql.default_host =

[MySQLi]

; Default port number for mysqli_connect().  If unset, mysqli_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
; http://php.net/mysqli.default-port
mysqli.default_port = 3306

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket =  /usr/local/mysql/data/mysql.sock   #增加mysqli socket接口

; Default host for mysql_connect() (doesn't apply in safe mode).
; http://php.net/mysqli.default-host
mysqli.default_host =

/etc/init.d/php-fpm reload

五 安装论坛 与 数据库连接

 

1 )将论坛文件放到nginx默认发布目录

1 unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/nginx/html/

2 cd upload

3 将upload里面所有文件都放到默认发布目录里

4 给发布文件增加权限,不然安装的时候会出现报错

chmod 777 config/ data/ uc_client/ uc_server/ -R

5 客户端登陆http://172.25.1.2/index.php

注意 点击下一步 如果出现权限不够

到次 论坛的部署就结束了!

六 memcache缓存

1 ) 源码编译安装

1 tar zxf memcache-2.2.5.tgz

2 cd memcache-2.2.5

3 这个时候需要phpize生成configure,就需要确保环境变量中有php命令,如果没有就需要在环境变量中添加

vim ~/.bash_profile

source ~/.bash_profile

4 phpize

5 ./configure 直接编译

6 make && make install

7 vim /usr/local/php/etc/php.ini   增加memcache 模块

/etc/init.d/php-fpm reload

8 php -m | grep memcache 查看添加的模块

9 yum install memcached.x86_64 -y

/etc/init.d/memcached start

2 )php和memcache的web页面展示

cd memcache-2.2.5

cp example.php memcache.php /usr/local/nginx/html/ 将源码包里的php文件拷贝至nginx的默认发布目录下

编辑memcache.php 文件

11 浏览器测试

访问 http://172.25.1.2/memcache.php

压力测试:

可以模拟客户端高并发去访问nginx,来测试memcahe的缓冲命中情况

[root@foundation73 Desktop]# ab -c 10 -n 1000 http://172.25.1.2/example.php
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 172.25.1.2 (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:        nginx/
Server Hostname:        172.25.1.2
Server Port:            80

Document Path:          /example.php
Document Length:        123 bytes

Concurrency Level:      10
Time taken for tests:   0.725 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      280000 bytes
HTML transferred:       123000 bytes
Requests per second:    1379.84 [#/sec] (mean)
Time per request:       7.247 [ms] (mean)
Time per request:       0.725 [ms] (mean, across all concurrent requests)
Transfer rate:          377.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       6
Processing:     1    7   1.0      7      13
Waiting:        1    7   1.0      7      13
Total:          2    7   1.0      7      13

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      7
  75%      8
  80%      8
  90%      9
  95%      9
  98%     10
  99%     10
 100%     13 (longest request)

这就是典型 client -> nginx:80 ->*.php-> php-fpm:9000(memcache)

下面就提供更高效的方式

client -> nginx:80 -> cache ->*.php -> php-fpm :9000(memcache)

七  nginx 的缓存加速

OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

1 )源码安装openresty

tar zxf openresty-1.13.6.1.tar.gz

./configure --prefix=/usr/local/openresty 编译

gmake && gmake install

2 ) 修改配置文件

1 cd /usr/local/openresty/nginx/conf

2 复制nginx的主配置文件到覆盖当前的配置文件

cp /usr/local/nginx/conf/nginx.conf .

3 在openresty的sbin下启动nginx,并在浏览器上查看

[root@server1 sbin]# ./nginx

4 在openresty下添加php默认发布文件

本文直接在nginx的目录拿了一个test.php 文件

 5 编辑配置文件

/usr/local/openresty/nginx/conf

 vim nginx.conf

http {

    upstream memcache {
    server localhost:11211;
  #  keepalive 512 single;
}

   location /memc {
        internal;
        memc_connect_timeout 100ms;
        memc_send_timeout    100ms;
        memc_read_timeout    100ms;
        set  $memc_key  $query_string;
        set  $memc_exptime 300;
        memc_pass memcache;
       }

 location ~ \.php$ {
            set $key   $uri$args;
           srcache_fetch GET /memc $key;
           srcache_store PUT /memc $key;
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
         #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload


6 然后在客户端进行高并发测试

[root@foundation73 Desktop]# ab -c 10 -n 1000 http://172.25.1.2/index.php
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 172.25.1.2 (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:        openresty/1.13.6.1
Server Hostname:        172.25.1.2
Server Port:            80

Document Path:          /index.php
Document Length:        84613 bytes

Concurrency Level:      10
Time taken for tests:   0.438 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      84804770 bytes
HTML transferred:       84613000 bytes
Requests per second:    2282.37 [#/sec] (mean)
Time per request:       4.381 [ms] (mean)
Time per request:       0.438 [ms] (mean, across all concurrent requests)
Transfer rate:          189018.99 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       2
Processing:     1    4   2.3      4      31
Waiting:        1    3   1.7      3      27
Total:          1    4   2.4      4      31

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      4
  80%      5
  90%      5
  95%      6
  98%      7
  99%     15
 100%     31 (longest request)
[root@foundation73 Desktop]# 

对比上面的高并发测试访问时间提高了不少!

猜你喜欢

转载自blog.csdn.net/yangkaiorange/article/details/82989685