概述
今天阿坤带大家一起遨游LNMP-Redis服务海洋!!
LNMP搭建参考:https://blog.csdn.net/qq_43417559/article/details/100773904
Redis搭建参考:https://blog.csdn.net/qq_43417559/article/details/100775040
问题
具体要求如下:
在主机192.168.4.57部署LNMP 环境
配置PHP支持redis
编写网站脚本,把数据存储到redis服务器192.168.4.50
步骤
实现此案例需要按照如下步骤进行。
步骤一:在主机192.168.4.57部署LNMP 环境
1)安装源码nginx软件及php-fpm
]#yum -y install gcc pcre-devel zlib-devel //安装依赖
]#tar -zxvf nginx-1.12.2.tar.gz //解压
]#cd nginx-1.12.2 //进源码目录
]#./configure //配置
……
……
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.12.2]# make //编译
……
……
objs/src/http/modules/ngx_http_upstream_zone_module.o
objs/ngx_modules.o
-ldl -lpthread -lcrypt -lpcre -lz
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|"
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|"
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|"
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|"
< man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/root/lnmp/nginx-1.12.2”
[root@localhost nginx-1.12.2]#make install //安装
……
……
test -d '/usr/local/nginx/logs'
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html'
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs'
|| mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/root/lnmp/nginx-1.12.2”
[root@localhost nginx-1.12.2]# ls /usr/local //查看安装目录
bin etc games include lib lib64 libexec nginx sbin share src
[root@localhost nginx-1.12.2]#
[root@localhost nginx-1.12.2]# ls /usr/local/nginx //查看目录列表
conf html logs sbin
[root@localhost nginx-1.12.2]#
]#yum -y install php-fpm //安装php-fpm
……
……
已安装:
php-fpm.x86_64 0:5.4.16-45.el7
作为依赖被安装:
libzip.x86_64 0:0.10.1-8.el7 php-common.x86_64 0:5.4.16-45.el7
完毕!
2)修改配置nginx.conf
] # vim +65 /usr/local/nginx/conf/nginx.conf
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
:wq
]# /usr/local/nginx/sbin/nginx -t //测试修改
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3)启动服务
启动php-fpm服务
]# systemctl start php-fpm //启动服务
]# netstat -utnlp | grep :9000 //查看端口
启动nginx服务
]# /usr/local/nginx/sbin/nginx
]# netstat -utnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23505/nginx: master
4)测试配置
# vim /usr/local/nginx/html/test.php //编写php文件
<?php
echo "hello world!!!";
?>
:wq
]# curl http://localhost/test.php //访问nginx服务
hello world!!!
步骤二:配置PHP支持redis
1)安装php扩展
[root@host71 ~]# rpm -q php php-devel
未安装软件包 php
未安装软件包 php-devel
[root@host71 ~]#
[root@host71 ~]# rpm -q automake autoconf
未安装软件包 automack
未安装软件包 autoconf
[root@host71 ~]#
[root@host71 ~]# yum -y install php php-devel automake autoconf //安装依赖
]# tar -zxf php-redis-2.2.4.tar.gz //安装扩展包
]# cd phpredis-2.2.4/
]# phpize //生成配置文件php-config及 configure命令
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
]# ./configure --with-php-config=/usr/bin/php-config //配置
]# make //编译
]# make install //安装
2)修改php.ini文件
]#vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/" //模块文件目录
730 extension = "redis.so" //模块文件名
:wq
]# systemctl restart php-fpm //重启php-fpm服务
]# php -m | grep -i redis //查看已加载的模块
redis
步骤三:测试配置:编写网站脚本,把数据存储到redis服务器192.168.4.50
1)查看192.168.4.50主机的redis服务是否运行
[root@host50 ~]# netstat -utnlp | grep redis-server
tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11523/redis-server
[root@host50 ~]#
[root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 //访问服务
192.168.4.50:6350> ping
PONG
192.168.4.50:6350> exit
2)编写网站脚本
]# vim /usr/local/nginx/html/linkredis.php
<?php
$redis = new redis();
$redis->connect("192.168.4.50","6350");
$redis->auth("123456");
$redis->set("linux","redhat");
echo $redis->get("linux");
?>
:wq
3)访问网站脚本
]#curl http://localhost/linkredis.php //访问nginx服务
redhat
4)在192.168.4.50 服务器,查看数据
[root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 //连接redis服务
192.168.4.50:6350> keys * //查看变量
1) "linux"
192.168.4.50:6350>
192.168.4.50:6350> get linux //获取值
"redhat"
192.168.4.50:6350>
最后
以上就是醉熏芝麻为你收集整理的部署LNMP+Redis的全部内容,希望文章能够帮你解决部署LNMP+Redis所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复