实现LNMP

时间:2023-03-09 03:15:15
实现LNMP

实现LNMP

环境:

	linux系统机器
A:一台N:nginx,ip:192.168.213.251
B:一台P:php-fpm,php-mysql ,ip:192.168.213.253
C:一台M:mysql or mariadb,ip:192.168.213.254
连接方式:
A <------------> B <-----------> C
关闭防火墙
disable掉selinux

1.在A上安装和配置nginx

	yum install nginx
cd /etc/nginx
cp nginx.conf nginx.conf.bak
vim nginx.conf
在server中添加
index index.php ;
location ~* \.php$ {
fastcgi_pass 192.168.213.254:9000;
fastcgi_param SCRIPT_FILENAME /var/www/html/php$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/(status|ping)$ {
fastcgi_pass 192.168.213.254:9000;
fastcgi_param SCRIPT_FILENAME /var/www/html/php$fastcgi_script_name;
include fastcgi_params;
}
nginx -t
systemctl start nginx

2.在B上安装和配置php-fpm,php-myql

	yum install php-fpm php-myql
vim /etc/php-fpm.d/www.conf
listen=9000
listen.allowed_clients = 127.0.0.1,192.168.213.251
pm.status_path = /status #用于查看php-fpm状态
ping.path = /ping
ping.response = pong

3.在C上安装和配置mysql 数据库

	yum install mysql mysql-server mysql-libs
chkconfig mysqld on
chkconfig --list mysqld
service mysqld start
service mysqld status
/usr/bin/mysql_secure_installation #根据需求进行配置
mysql -uroot -pxm1234
mysql>create user "shenxm"@'%' identified by 'xm1234';

4.测试

	在B上找个目录,存放数据。
cd /var/www/html/php
vim index.php
<?php
echo date("Y/m/d h:i:s");
$mysqli=new mysqli("192.168.213.253","shenxm","xm1234");
if(mysqli_connect_errno()){
echo "not ok!";
$mysqli=null;
exit;
}
echo "ok.o....kkkk!!!";
$mysqli->close();
phpinfo();
?>
在浏览器上
http://192.168.213.251/index.php #会有是否ok的显示
http://192.168.213.251/ping #会显示pong的恢复
http://192.168.213.251/status #会有状态信息显示

5.实现fastcgi缓存

	在A上
cd /etc/nginx
vim nginx.conf
在http中添加:
fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1 keys_zone=fcgicache:20m inactive=120s;
在server中location ~* \.php$中补充:
fastcgi_cache fcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
测试:
ab -c 100 -n 2000 http://192.168.213.251/index.php
可以把fastcgi_cache 关掉在测试下
修改配置文件nginx.conf ,把“fastcgi_cache fcgicache;”改为“fastcgi_cache off;”,然后在测试。