系统环境: RHEL6 x86-64 selinux and iptables disabled
一、lnmp简介
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统
二、Mysql的源码安装
1.解决依赖性
[root@server1 ~]# ls
mysql-boost-5.7.11.tar.gz cmake-2.8.12.2-4.el6.x86_64.rpm
[root@server1 ~]# tar zxf mysql-boost-5.7.11.tar.gz
[root@server1 ~]# yum install cmake-2.8.12.2-4.el6.x86_64.rpm -y
[root@server1 ~]# cd mysql-5.7.11/
[root@server1 mysql-5.7.11]# yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel
2.源码编译三步曲
[root@server1 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_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0/
[root@server1 mysql-5.7.11]# make
[root@server1 mysql-5.7.11]# make install
如果cmake出错则删除缓存信息再进行编译,否则编译的时候执行的是上一次cmake错误的信息
[root@server1 mysql-5.7.11]# rm -f CMakeCache.txt
3.修改mysql的配置文件
[root@server1 mysql-5.7.11]# cd /usr/local/lnmp/mysql/support-files/
[root@server1 support-files]# cp my-default.cnf /etc/my.cnf #覆盖
[root@server1 support-files]# vim /etc/my.cnf
# 指定目录位置
4.设置mysql的启动脚本
确保有可执行权限
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld
[root@server1 support-files]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 11005 Aug 5 22:11 /etc/init.d/mysqld
5.建立mysql用户
[root@server1 support-files]# groupadd -g 27 mysql
[root@server1 support-files]# useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/data -s /sbin/nologin mysql
6.设置mysql命令路径
[root@server1 ~]# vim .bash_profile
[root@server1 ~]# source .bash_profile
示图:文件编写内容
可以直接使用mysql命令
7.mysql 初始化安装
[root@server1 ~]# mysqld --initialize --user=mysql
出现warning可以跳过,但是error不行
修改目录权限,设置data目录的所有人为mysql,所有组为root
其他目录的所有人和所有组都是root
[root@server1 ~]# cd /usr/local/lnmp/mysql/
[root@server1 mysql]# chown root.root . -R
[root@server1 mysql]# chown mysql data/ -R
[root@server1 mysql]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS!
8.安全初始化:
[root@server1 data]# mysql_secure_installation
此时需要输入之前初始化安装时的默认密码
[root@server1 data]# mysql -p
设置完成
三、php的源码安装
1.解决依赖性
[root@server1 ~]# yum install -y openssl-devel libcurl-devel gmp-devel net-snmp-devel libmcrypt-devel libxml2-devel
下面的包需要在网上找资源
[root@server1 ~]# yum install re2c-0.13.5-1.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm gd-devel-2.0.35-11.el6.x86_64.rpm -y
解包
[root@server1 ~]# tar jxf php-5.6.35.tar.bz2
[root@server1 ~]# cd php-5.6.35
2.源码编译三步曲
[root@server1 php-5.6.35]# ./configure –prefix=/usr/local/lnmp/php –with-config-file-path=/usr/local/lnmp/php/etc –with-mysql=mysqlnd –enable-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
[root@server1 php-5.6.35]# make && make install
3.配置php
[root@server1 php-5.6.35]# cd /usr/local/lnmp/php/etc
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# vim php-fpm.conf
[root@server1 ~]# cd php-5.6.35/
[root@server1 php-5.6.35]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
4.创建nginx用户
[root@server1 etc]# useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin/ nginx
5.创建脚本
[root@server1 ~]# cd php-5.6.35/sapi/fpm/
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm
设置成功
[root@server1 fpm]# /etc/init.d/php-fpm start
Starting php-fpm done
四、nginx的源码编译
1.编译前的设置
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# vim src/core/nginx.h
取消版本号
关闭掉调试功能
[root@server1 nginx-1.10.1]# vim auto/cc/gcc
解决依赖性:
[root@server1 nginx-1.10.1]# yum install -y pcre-devel
2.源码编译三步曲
[root@server1 nginx-1.10.1]# ./configure –prefix=/usr/local/lnmp/nginx –with-http_ssl_module –with-http_stub_status_module –user=nginx –group=nginx –with-threads –with-file-aio
[root@server1 nginx-1.10.1]# make && make install
3.修改最大连接数
[root@server1 nginx-1.10.1]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# vim nginx.conf
events {
worker_connections 65535;
}
修改操作系统的最大文件数
[root@server1 conf]# vim /etc/security/limits.conf
nginx - nofile 65536
4.设置nginx命令路径
[root@server1 ~]# vim .bash_profile
[root@server1 ~]# source .bash_profile
示图:文件编写内容
可以直接使用nginx命令
[root@server1 ~]# nginx
测试:在浏览器访问172.25.62.1 出来nginx 欢迎界面
5.访问php页面设置
编写php页面
[root@server1 ~]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim index.php
<?php
phpinfo();
?>
当访问以.php结尾交给fastcgi处理
[root@server1 conf]# vim nginx.conf
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
[root@server1 conf]# nginx -s reload
测试:在浏览器访问:172.25.7.1/index.php
如果向直接访问php页面则:加入index.php
[root@server1 conf]# vim nginx.conf
45 index index.php index.html index.htm;
[root@server1 conf]# nginx -s reload
测试:在浏览器访问:172.25.62.1则出来php的页面
五、基于lnmp论坛的搭建
1.解压论坛的包
[root@server1 ~]# yum install unzip -y
# 添加指定解压位置
[root@server1 ~]# unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/lnmp/nginx/html/
2.给一个满权限
[root@server1 ~]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# mv upload/ bbs ##改名为bbs
[root@server1 html]# cd bbs/
[root@server1 bbs]# chmod 777 config/ data/ uc_server/ uc_client/ -R
3.指定socket的路径
[root@server1 ~]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini
# 加入scoket路径 /usr/local/lnmp/mysql/data/mysql.sock 【总共三处需要填写】
[root@server1 etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
修改访问权限:
[root@server1 mysql]# pwd
/usr/local/lnmp/mysql
[root@server1 mysql]# chmod 755 data/
测试:在浏览器访问:172.25.7.1/bbs
论坛搭建成功:
六、memcache 内存缓存
1.基础信息
memcache是一套分布式的高速缓存系统,MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据
2.php+memcache
[1]设php命令的路径
[root@server1 ~]# vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/nginx/sbin/:/usr/local/lnmp/php/bin
[root@server1 ~]# source .bash_profile
[2]预编译环境实现
[root@server1 ~]# tar zxf memcache-2.2.5.tgz
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[3]源码编译
[root@server1 memcache-2.2.5]# ./configure --prefix=/usr/local/lnmp/php/memcache
[root@server1 memcache-2.2.5]# make && make install
[4]给php添加memcache
[root@server1 memcache-2.2.5]# php -m |grep memcache #此时无结果
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini
[root@server1 etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@server1 etc]# php -m |grep memcache #此时支持memcache
memcache
示图:php.ini改写内容
安装工具
[root@server1 etc]# yum install -y memcached
[root@server1 etc]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[5]测试
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php
测试:当访问多次example.php后,再访问memcache.php命中率会提高
压力测试:
[root@foundation62 lamp]# ab -c 10 -n 1000 http://172.25.62.1/index.php
[root@foundation62 lamp]# ab -c 10 -n 1000 http://172.25.62.1/example.php
3.nginx+memcache
传统上是通过PHP操作memcache的,要执行PHP代码,Nginx就必然要和FastCGI通信,同时也要进入PHP的生命周期,因此SAPI、PHP Core和Zend Engine的一系列逻辑会被执行
更高效的缓存策略是Nginx直接访问memcache,并用uri和args 等Nginx内置变量设定缓存key规则,这样,当缓存命中时,Nginx可以跳过通过fastcgi和PHP通信的过程,直接从memcache中获取 数据并返回。memc-nginx和srcache-nginx正是利用这种策略提高了缓存的效率。下图是这种高效缓存策略的示意图(当memcache 命中时)
[root@server1 conf]# nginx -s stop # 关闭之前的nginx
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz
[root@server1 ~]# cd openresty-1.13.6.1
源码编译
[root@server1 openresty-1.13.6.1]# ./configure –prefix=/usr/local/lnmp/openresty –with-http_ssl_module –with-http_stub_status_module –user=nginx –group=nginx –with-threads –with-file-aio
[root@server1 openresty-1.13.6.1]# gmake && gmake install
修改openresty中的nginx的配置文件
[root@server1 openresty-1.13.6.1]# cd /usr/local/lnmp/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf
12 events {
13 worker_connections 65535;
14 }
17 http {
18 upstream memcache {
19 server localhost:11211;
20 keepalive 512 ;
21 }
22 include mime.types;
23 default_type application/octet-stream;
51 location /memc {
52 internal;
53 memc_connect_timeout 100ms;
54 memc_send_timeout 100ms;
55 memc_read_timeout 100ms;
56 set $memc_key $query_string;
57 set $memc_exptime 300;
58 memc_pass memcache;
59 }
77 location ~ \.php$ {
78 set $key $uri$args;
79 srcache_fetch GET /memc $key;
80 srcache_store PUT /memc $key;
81 root html;
82 fastcgi_pass 127.0.0.1:9000;
83 fastcgi_index index.php;
84 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
85 include fastcgi.conf;
86 }
[root@server1 conf]# /usr/local/lnmp/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/lnmp/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/openresty/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /usr/local/lnmp/openresty/nginx/sbin/nginx
访问测试:http://172.25.62.1/
把之前的php页面copy一份到openresty中:
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
压力测试:
[root@foundation62 lamp]# ab -c 10 -n 1000 http://172.25.62.1/index.php
[root@foundation62 lamp]# ab -c 10 -n 1000 http://172.25.62.1/example.php