1.源码编译安装php
官网下载php包,并解压
tar jxf php-7.4.12.tar.bz2
解压时遇到问题:未安装对bzip2包解压的工具
yum install bzip2 -y
tar jxf php-7.4.12.tar.bz2
解压后进入文件夹查看
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd
按照功能要求安装相关依赖性文件
yum install systemd-devel -y
yum install libxml2-devel.x86_64 -y
yum install sqlite-devel.x86_64 -y
yum install libcurl-devel.x86_64 -y
yum install libpng-devel.x86_64 -y
yum install oniguruma-devel-6.8.2-1.el7.x86_64.rpm oniguruma-6.8.2-1.el7.x86_64.rpm -y
make (时间可能较长)
make install
nginx结合php-fpm
安装好的php进入主配置文件目录
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
修改文件使得.pid文件生效
拷贝修改相关配置文件,
cp php-7.4.12/php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
vim /usr/local/php/etc/php.ini
date.timezone = ‘Asia/Shanghai’
将php-fpm 设置为系统服务并开机启动。
cd php-7.4.12/sapi/fpm/
cp php-fpm.service /usr/lib/systemd/system
vim /usr/lib/systemd/system/php-fpm.service
21 #ProtectSystem=full 注释掉第21行
systemctl daemon-reload 刷新系统服务文件
systemctl start php-fpm.service 启动php-fpm
启动时发现报错,查看日志
cat /var/log/messages
会显示未创建nginx用户
useradd nginx
创建用户后,php-fpm启动成功
编辑nginx默认发布目录,
vim /usr/local/nginx/html/index.php
<?php
phpinfo()
?>
编辑nginx主配置文件,允许运行.php文件
vim /usr/local/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;
}
描述systemctl restart nginx
重启服务nginx
测试:
firefox: 172.25.9.1/index.php
若要添加到默认发布目录则需要修改nginx主配置文件
重启服务,访问172.25.9.1
2. nginx+memcache高效缓存机制
传统缓存策略
memcache安装压缩包 memcache-4.0.5.2.tgz
解压后进入目录
tar zxf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2
修改成功后查看php模块
php -m
源码编译安装memcache:
tar zxf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2/
phpize #php内置脚本安装memcache,生成cofigure加入全局变量
vim ~/.bash_profile
source ~/.bash_profile
再次进入memcache解压后的目录
执行phpize
提示缺少autoconf
yum install -y autoconf
phpize
./configure --enable-memcache #源码编译三部曲安装memcache
make
make install
修改php.ini主配置文件
vim /usr/local/php/etc/php.ini
913 extension=memcache
systemctl restart php-fpm.service
php -m |grep memcache
memcache模块安装成功
cd memcache-4.0.5.2/
cp example.php memcache.php /usr/local/nginx/html/
cd /usr/local/nginx/html/下载memcached
yum install memcached # memcached是一套分布式的高速缓存系统
启动,并查看端口 11211是否开启
systemctl enable --now memcached
netstat -antlp |grep :11211
网页访问 http://172.25.9.1/example.php即可查看变化
修改一下nginx默认发布目录里面的memcache,并修改密码、修改server1相关信息和注释server2
vim /usr/local/nginx/html/memcache.php
网页访问 http://172.25.4.1/memcache.php ----> 输入用户名和密码
做压力测试
ab -c 10 -n 1000 http://172.25.76.1/index.php
不使用缓存时,在并发量大的时候,会产成111个错误请求
ab -c 10 -n 1000 http://172.25.76.1/memcache.php
使用缓存时,在并发量大的时候,无错误
3.构建nginx高速缓存 openresty基于nginx和lua
首先
systemctl stop nginx.service
源码编译安装openresty
tar zxf openresty-1.19.3.1.tar.gz
cd openresty-1.19.3.1/
三部曲
./configure
gmake
gmake install
cd /usr/local/openresty/nginx/conf
vim nginx.conf
####keepalive 512 保持512个不立即关闭的连接用于提升性能
当所请求的uri以“.php”结尾时,首先到memcache中查询有没有以 u r i uri uriargs为key的数据,
如果有则直接返回;否则,执行location的逻辑,
如果返回的http状态码为200,则在输出前以 u r i uri uriargs为key,将输入结果存入memcache。
internal; //表示只接受内部访问 set $memc_key
q u e r y s t r i n g ; / / 使 用 内 置 的 query_string; //使用内置的 querystring;//使用内置的query_string来作为key set $memc_exptime
300; //表示缓存失效时间
/usr/local/openresty/nginx/sbin/nginx #开启openresty里的nginx
拷贝默认发布目录
cd /usr/local/openresty/nginx/html/
cp /usr/local/nginx/html/*.php .
压力测试:
ab -c10 -n1000 http://172.25.76.1/example.php
这是上面传统缓存策略的速度
这是高速缓存策略
4.goaccess日志可视化
上面打开了告诉缓存的Openresty,先将这个关闭,然后打开nginx
/usr/local/openresty/nginx/sbin/nginx -s nginx #关闭新的nginx
nginx #打开旧的nginx
tar zxf goaccess-1.4.tar.gz
解压安装包,并进入安装后的目录
cd goaccess-1.4
配置
./configure --enable-utf8 --enable-geoip=legacy
yum install GeoIP-devel-1.5.0-13.el7.x86_64.rpm -y
yum install ncurses-devel.x86_64 -y
再次配置后
编译和安装
make && make install
cd /usr/local/nginx/logs
执行启动goaccess,并打入后台
goaccess access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html &
http://172.25.21.1/report.html
4.tomcat+memcache结合jsp交叉存储
server2与3:
安装tomcat
首先安装jdk
rpm -ivh jdk-8u121-linux-x64.rpm
解压tomcat源码编译压缩包到/usr/loacl ,并进入/usr/local
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/loacl
cd /usr/local/
创建一个软链接,方便使用
ln -s apache-tomcat-7.0.37 tomcat
进入到tomcat目录 使用命令 ll
可以看到有bin目录
打开tomcat
cd tomcat/
bin/startup.sh
此时可以看到8080端口已经开启
netstat -antlp
cd webapps/ROOT/ #tomcat默认发布目录
ls
cp ~/test.jsp . 将测试文件拷贝过来
server1:
在监听localhost的server里面加入,如果访问172.25.21.1/*.jsp那么就负载均衡到tomcat新加入一个负载均衡器
nginx -t 后会报错
报错原因:缺少sticky插件
安装插件: nginx-sticky
tar zxf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz
mv nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d nginx-sticky
vim nginx-sticky/ngx_http_sticky_misc.c
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include <ngx_sha1.h>
#include <openssl/sha.h> #添加
#include <openssl/md5.h> #添加
#include "ngx_http_sticky_misc.h"
重新编译nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-sticky/
编译完成后只make,不make install
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cd nginx-1.20.1
[root@server1 nginx-1.20.1]# cp objs/nginx /usr/local/nginx/sbin/
再次 nginx -t
配置server2和3的tomcat
此时我们server1就是nginx server2就是tomcat1和memcache1 server3就是tomcat2和memcache2
安装memcached
yum install memcached -y
systemctl enable --now memcached
vim /usr/local/tomcat/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.76.2:11211,n2:172.25.76.3:11211"
failoverNodes="n1"(n2)
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
加在两个之间
memcachedNodes是写入所有的memcached n数字:ip:端口
failoverNodes比如是server2,那就是n1,server3就是n2
进入tomcat目录
然后重新启动tomcat
bin/shutdown.sh
bin/startup.sh
在server2、3都安装memcached,并设置开机自动启动
cat logs/catalina.out
如果在server的tomcat日志中看到红圈的 代表已经部署完成