lnmp架构

一、Mysql

1、yum install -y gcc gcc-c++ ncurses-devel bison cmake-2.8.12.2-4.el6.x86_64.rpm

2、cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \ #安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \ #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \ #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \ #安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装 innodb 存储引擎
-DDEFAULT_CHARSET=utf8 \ #使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \ #校验字符
-DEXTRA_CHARSETS=all \ #安装所有扩展字符集
-DWITH_BOOST=/mnt/mysql-5.7.11/boost/boost_1_59_0/

3、make
4、make install
5、make clean #清除缓存
6、rm -rf CMakeCache.txt #编译出错时,也需要删除缓存

mysql初始化设置

7、groupadd -g 27 mysql
8、useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/ mysql
9、usermod -s /sbin/nologin mysql #安全设置,禁止交互式登陆
10、vim ~/.bash_profile #设置环境变量,方便登陆

    PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin

11、cp /usr/local/lnmp/mysql/support-files/mysql.server /etc/init.d/mysqld #添加启动脚本
12、vim /etc/my.cnf

[mysqld]
datadir=/usr/local/lnmp/mysql/data
socket=/usr/local/lnmp/mysql/data/mysql.sock
user=mysql

13、mysqld –initialize –user=mysql #初始化,复制临时密码
14、chown root.mysql /usr/local/lnmp/mysql -R
15、chown mysql /usr/local/lnmp/mysql/data -R
16、/etc/init.d/mysqld start
16、mysql_secure_installation #设置密码
17、mysql -p #登陆

测试:

这里写图片描述

二、php

1、yum install -y libxml2-devel openssl-devel curl-devel libjpeg-turbo-devel gd-devel-2.0.35-11.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm re2c-0.13.5-1.el6.x86_64.rpm gmp-devel net-snmp-devel

2、./configure –prefix=/usr/local/lnmp/php –with-config-file-path=/usr/local/lnmp/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 –with-pear –with-gettext –with-gmp –enable-inline-optimization –enable-soap –enable-ftp –enable-sockets –enable-mbstring –enable-fpm –with-fpm-user=nginx –with-fpm-group=nginx –with-mcrypt –with-mhash #添加模块功能,根据需要自己添加

3、make && make install
4、cp /usr/local/lnmp/php/etc/php-fpm.conf.default /usr/local/lnmp/php/etc/php-fpm.conf
5、cp /mnt/php-5.6.20/php.ini-production /usr/local/lnmp/php/etc/php.ini
6、cp /mnt/php-5.6.20/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
7、chmod +x /etc/init.d/php-fpm
8、vim /usr/local/lnmp/php/etc/php-fpm.conf

[global]
; Pid file
; Note: the default prefix is /usr/local/lnmp/php/var
; Default Value: none
pid = run/php-fpm.pid

9、vim /usr/local/lnmp/php/etc/php.ini

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai

10、useradd -u 800 -M -d /usr/local/lnmp/nginx nginx

测试:

这里写图片描述

三、nginx

1、vim /mnt/nginx-1.10.1/src/core/nginx.h

#define NGINX_VER          "nginx/" #为了安全,不显示版本号
# debug
#CFLAGS="$CFLAGS -g"   #关闭DEBUG,减少内存

3、yum install pcre-devel openssl-devel zlib-devel -y
4、./configure –prefix=/usr/local/lnmp/nginx –with-http_ssl_module –with-http_stub_status_module –with-file-aio –with-threads –user=nginx –group=nginx#需要的模块自定义

5、make
6、make install
7、ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
8、vim nginx.conf

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

12、vim html/index.php

<?php
phpinfo()
?>
测试:

这里写图片描述

这里写图片描述

经过上述三大步,Lnmp框架基本搭建成功了,下面来我们通过搭建论坛来测试一下,

四、搭建论坛

1、vim /usr/local/lnmp/php/etc/php.ini #设置数php和据库的相关接口

mysql.default_socket =/usr/local/lnmp/mysql/data/mysql.sock
mysqli.default_socket =/usr/local/lnmp/mysql/data/mysql.sock
pdo_mysql.default_socket=/usr/local/lnmp/mysql/data/mysql.sock

2、/etc/init.d/php-fpm reload

3、vim /usr/local/lnmp/nginx/conf/nginx.conf

    server {
            listen 80;
            server_name bbs.xinhao.com;

            location / {
                    root /bbs;
                    index index.php;
            }

            location ~ \.php$ {
                        root           /bbs;
                        fastcgi_pass   127.0.0.1:9000;
                        fastcgi_index  index.php;
                        include        fastcgi.conf;
            }
    }

4、mkdir /bbs
5、mv /mnt/discuz/upload/* /bbs
6、chmod 777 /bbs/config/ /bbs/data #添加相应的权限,论坛才可以正常安装
7、chmod 777 /bbs/config/ /bbs/data -R
8、chmod 777 /bbs/uc_client/ /bbs/uc_server/ -R
9、chmod 755 /usr/local/lnmp/mysql/data/
10、nginx -s reload

测试:(这里用自己设置的域名访问,同时做好解析)

这里写图片描述

五、为了提高网络的访问速度,一般会建立缓存,下面将演示分别在php和nginx 建立memcache缓存,来提高访问速度。

server2和server3(缓存服务器):

1、yum install memcached telnet -y
2、/etc/init.d/memcached start

server1,基于php的

1、vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin/

2、source ~/.bash_profile
3、cd memcache-2.2.5
4、phpize #生成编译文件
5、./configure –prefix=/usr/local/lnmp/php/memcache
6、make &&make install
7、vim /usr/local/lnmp/php/etc/php.ini

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll
;
; ... or under UNIX:
;
extension=memcache.so

8、/etc/init.d/php-fpm restart
9、cp example.php /usr/local/lnmp/nginx/html/
10、cp memcache.php /usr/local/lnmp/nginx/html/
11、vim /usr/local/lnmp/nginx/html/example.php

$memcache = memcache_connect('172.25.34.3', 11211);

12、vim /usr/local/lnmp/nginx/html/memcache.php #设置登陆账户密码

define('ADMIN_USERNAME','memcache');    // Admin Username
define('ADMIN_PASSWORD','admin');       // Admin Password

$MEMCACHE_SERVERS[] = '172.25.34.3:11211'; // add more as an array

13、nginx -s reload

浏览器测试:

在example.php页面刷新,会在memcache页面发现命中率在不断提高,最后接近100%

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

在终端做压力测试:

ab -c 100 -n 50000 http://172.25.34.1/example.php

这里写图片描述

六、openresty:#基于nginx开发的工具,使用起来和nginx一样。

1、cd openresty-1.13.6.1
2、./configure
3、gmake && gmake install
4、nginx -s stop #关闭原有nginx服务
5、cd /usr/local/openresty/nginx/sbin
5、./nginx #开启安装的openresty的nginx
6、./nginx -t #检测服务是否正常
7、vim /usr/local/openresty/nginx/conf/nginx.conf

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

8、vim /usr/local/openresty/nginx/html/index.php

9、./nginx -s reload

测试:

浏览器测试出现下面的情况,说明服务安装成功

这里写图片描述

这里写图片描述

在终端做压力测试:

ab -c 100 -n 50000 http://172.25.34.1/index.php

对比在php下做缓存,在nginx下做缓存不管是速度还是命中率都有所提高

这里写图片描述

七、实际应用中通常在nginx和php下都做缓存,这样就会大大提高访问体验

server1:

1、vim /usr/local/openresty/nginx/conf/nginx.conf

  http {
             upstream memcache {
                 server 172.25.34.2:11211;
             server 172.25.34.3:11211;

              }

  include       mime.types;
    default_type  application/octet-stream;

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

2、/usr/local/openresty/nginx/sbin/nginx -s reload

server2和server3:

/etc/init.d/memcached restart

测试:

ab -c 100 -n 50000 http://172.25.34.1/index.php

注意对比上面两次的结果,发现速度和命中率提高不止一个档次

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值