LNMP架构 nginx+mysql+php+memcache+论坛

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

一.Mysql源码安装

1.安装依赖

[[email protected] mnt]# yum install cmake-2.8.12.2-4.el6.x86_64.rpm -y

[[email protected] mnt]# yum install gcc gcc-c++ -y

[[email protected] mnt]# yum install ncurses-devel -y

[[email protected] mnt]# yum install bison -y

2.安装mysql

[[email protected] mnt]# tar zxf mysql-boost-5.7.11.tar.gz

[[email protected] ~]# cd /mnt/mysql-5.7.11/

[root[email protected] mysql-5.7.11]# 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  -DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_MYISAM_STORAGE_ENGINE=1  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci  -DEXTRA_CHARSETS=all  -DWITH_BOOST=/mnt/mysql-5.7.11/boost/boost_1_59_0/

[[email protected] mysql-5.7.11]# make

[[email protected] mysql-5.7.11]# make install

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

3.配置mysql

创建mysql组和用户并指定家目录

[[email protected] lnmp]# groupadd -g 27 mysql
[[email protected] lnmp]# useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/ mysql

更改权限

[[email protected] ~]# usermod -s /sbin/nologin mysql

[[email protected] ~]# cd /usr/local/lnmp/mysql/

[[email protected] mysql]# chown mysql.mysql . -R

LNMP架构 nginx+mysql+php+memcache+论坛

添加mysql的路径

[[email protected] ~]# vim .bash_profile

[[email protected] ~]# source .bash_profile

LNMP架构 nginx+mysql+php+memcache+论坛

更改mysql脚本位置

[[email protected] support-files]# pwd

/usr/local/lnmp/mysql/support-files
[[email protected] support-files]# cp mysql.server /etc/init.d/mysqld

[[email protected] support-files]# chmod +x /etc/init.d/mysqld

LNMP架构 nginx+mysql+php+memcache+论坛

更改mysql数据路径

[[email protected] ~]# vim /etc/my.cnf

LNMP架构 nginx+mysql+php+memcache+论坛

mysql初始化

[[email protected] mysql]# mysqld --initialize --user=mysql

初始化时出错,删除data下的数据再次初始化就行


LNMP架构 nginx+mysql+php+memcache+论坛

启动使用mysql

[[email protected] data]# /etc/init.d/mysqld start

[[email protected] data]# mysql -p

登陆时需要密码,注意初始化完成后会生成一个随机密码


LNMP架构 nginx+mysql+php+memcache+论坛

4.更改mysql的密码

[[email protected] ~]# mysql_secure_installation

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

测试修改成功

LNMP架构 nginx+mysql+php+memcache+论坛

二.php

1.安装php源码包

解决依赖

[[email protected] ~]#  yum install -y net-snmp-devel -y

[[email protected] ~]# yum install libcurl-devel -y

[[email protected] ~]# yum install libxml2-devel -y

[[email protected] php-5.6.20]# yum install openssl-devel curl-devel bison gmp-devel -y

LNMP架构 nginx+mysql+php+memcache+论坛

安装

[[email protected] ~]# tar jxf php-5.6.20.tar.bz2

[[email protected] php-5.6.20]# ./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-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] php-5.6.20]# make && make install
LNMP架构 nginx+mysql+php+memcache+论坛

2.配置php文件

[[email protected] php-5.6.20]# cd /usr/local/lnmp/php/etc/
[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf
[[email protected] etc]# cd /root/php-5.6.20
[[email protected] php-5.6.20]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] etc]# vim /usr/local/lnmp/php/etc/php.ini

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] etc]# vim /usr/local/lnmp/php/etc/php-fpm.conf

LNMP架构 nginx+mysql+php+memcache+论坛

更改启动脚本位置,加执行权限

[[email protected] ~]# cd /root/php-5.6.20/sapi/fpm/

[[email protected] fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[[email protected] fpm]# chmod +x /etc/init.d/php-fpm

LNMP架构 nginx+mysql+php+memcache+论坛

增加nginx用户,启动php

[[email protected] ~]# useradd -u 800 -M -d /usr/local/lnmp/nginx nginx
[[email protected] ~]# /etc/init.d/php-fpm start

LNMP架构 nginx+mysql+php+memcache+论坛

三.nginx源码安装

1.安装

解压编译,去掉版本号,关闭gcc编译

[[email protected] mnt]# tar zxf nginx-1.10.1.tar.gz
[[email protected] mnt]# cd nginx-1.10.1

[[email protected] nginx-1.10.1]# vim src/core/nginx.h

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] nginx-1.10.1]# vim auto/cc/gcc

LNMP架构 nginx+mysql+php+memcache+论坛


LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] nginx-1.10.1]# yum install pcre-devel -y

[[email protected] nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-threads --user=nginx --group=nginx


LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] nginx-1.10.1]# make && make install

LNMP架构 nginx+mysql+php+memcache+论坛

做软链接,启动nginx

[[email protected] ~]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/

LNMP架构 nginx+mysql+php+memcache+论坛

2.配置nginx

[[email protected] ~]# cd /usr/local/lnmp/nginx/

[[email protected] nginx]# vim conf/nginx.conf

配置主页面php ,修改include 为 fastcgi.conf

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

php访问页面

[[email protected] nginx]# vim html/index.php

[[email protected] nginx]# nginx -s reload

LNMP架构 nginx+mysql+php+memcache+论坛

把数据库的数据目录加到php.ini中

[[email protected] lnmp]# vim /usr/local/lnmp/php/etc/php.ini

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

测php访问页面,mysql接口正常

[[email protected] ~]# nginx -s reload

LNMP架构 nginx+mysql+php+memcache+论坛

四.论坛Discuz的搭建

1.解压论坛包

[[email protected] mnt]# yum install unzip -y
[[email protected] mnt]# unzip Discuz_X3.2_SC_UTF8.zip

LNMP架构 nginx+mysql+php+memcache+论坛

把upload移到/bbs 文件

[[email protected] mnt]# mkdir /bbs

[[email protected] mnt]# mv upload/* /bbs

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

修改nginx的发布目录

[[email protected] upload]# vim /usr/local/lnmp/nginx/conf/nginx.conf
[[email protected] upload]# nginx -s reload

LNMP架构 nginx+mysql+php+memcache+论坛

2.配置论坛

网页进入安装配置界面

LNMP架构 nginx+mysql+php+memcache+论坛

当前状态不能写入,需要增加权限

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] bbs]# chmod 777 config/ data/ -R
[[email protected] bbs]# chmod 777 uc_client/ uc_server/ -R

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] mysql]# chmod 755 data/ -R

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] bbs]# chmod 777 /usr/local/lnmp/mysql/data/mysql.sock

[[email protected] bbs]# /etc/init.d/php-fpm reload

自己的论坛建好

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

删除index.php,以免冲突

LNMP架构 nginx+mysql+php+memcache+论坛

五.MemCache缓存

缓存体系一:缓存在php

MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRULeast Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。

LNMP架构 nginx+mysql+php+memcache+论坛

布局memcache缓存磁盘设置server2,交叉存储

1.添加php的路径

[[email protected] ~]# vim .bash_profile
[[email protected] ~]# source .bash_profile

LNMP架构 nginx+mysql+php+memcache+论坛

2.编译安装memcche

[[email protected] ~]# tar zxf memcache-2.2.5.tgz

[[email protected] memcache-2.2.5]# phpize

[[email protected] memcache-2.2.5]# ./configure --prefix=/usr/local/lnmp/php/memcache

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] memcache-2.2.5]# make && make install

LNMP架构 nginx+mysql+php+memcache+论坛

3.配置php

[[email protected] ~]# vim /usr/local/lnmp/php/etc/php.ini
[[email protected] ~]# /etc/init.d/php-fpm reload

LNMP架构 nginx+mysql+php+memcache+论坛

4.将memcache测试文件拷贝到nginx的发布目录

[[email protected] ~]# cd /root/memcache-2.2.5

[[email protected] memcache-2.2.5]# cp example.php /usr/local/lnmp/nginx/html/

[[email protected] memcache-2.2.5]# cp memcache.php /usr/local/lnmp/nginx/html/

[[email protected] memcache-2.2.5]# vim /usr/local/lnmp/nginx/html/example.php

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] memcache-2.2.5]# vim /usr/local/lnmp/nginx/html/memcache.php

LNMP架构 nginx+mysql+php+memcache+论坛

5.部署server2

[[email protected] ~]# yum install memcached -y

[[email protected] ~]# /etc/init.d/memcached start

6.测试

[[email protected] ~]# ab -c 100 -n 100 http://172.25.87.1/example.php

http://172.25.18.1/memcache.php 

密码为memcache.php中设置的密码

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛


缓存体系二:缓存在nginx上

可以看到,即使memcache命中,还是要进入PHP的生命周期。我们知道,目前很多互联网应用都使用RESTful规范进行设计,在RESTful应用下,普遍使用uri和查询参数作为缓存的key,因此一种更高效的缓存策略是Nginx直接访问memcache,并用$uri和$args等Nginx内置变量设定缓存key规则,这样,当缓存命中时,Nginx可以跳过通过fastcgi和PHP通信的过程,直接从memcache中获取数据并返回。memc-nginx和srcache-nginx正是利用这种策略提高了缓存的效率。下图是这种高效缓存策略的示意图(当memcache命中时)。

LNMP架构 nginx+mysql+php+memcache+论坛

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

OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

1.安装openresty

[[email protected] mnt]# tar zxf openresty-1.13.6.1.tar.gz

[[email protected] mnt]# cd openresty-1.13.6.1

[[email protected] openresty-1.13.6.1]# ./configure

[[email protected] openresty-1.13.6.1]# gmake && gmake install

LNMP架构 nginx+mysql+php+memcache+论坛

2.关闭原有的nginx,开启openresty自带的nginx

[[email protected] openresty-1.13.6.1]# nginx -s stop

[[email protected] openresty-1.13.6.1]# cd /usr/local/openresty/nginx/sbin/

[[email protected] sbin]# ./nginx
LNMP架构 nginx+mysql+php+memcache+论坛
3.更改openresty中nginx的配置,编写发布文件,重加载nginx

[[email protected] ~]# vim /usr/local/openresty/nginx/conf/nginx.conf

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

[[email protected] ~]# cd /usr/local/openresty/nginx/html/
[[email protected] html]# vim index.php

LNMP架构 nginx+mysql+php+memcache+论坛

4.在 nginx中设置memcache

[[email protected] sbin]# vim /usr/local/openresty/nginx/conf/nginx.conf

[[email protected] sbin]# ./nginx -s reload

LNMP架构 nginx+mysql+php+memcache+论坛

LNMP架构 nginx+mysql+php+memcache+论坛

5.server3

[[email protected] ~]# yum install memcached -y

[[email protected] ~]# /etc/init.d/memcached start

6.测试

[[email protected] ~]# ab -c 100 -n 100 http://172.25.87.1/example.php