在主機192.168.4.57部署LNMP 環(huán)境
配置PHP支持redis
編寫網(wǎng)站腳本,把數(shù)據(jù)存儲到redis服務器192.168.4.50
一:在主機192.168.4.57部署LNMP 環(huán)境
1)安裝源碼nginx軟件及php-fpm
[root@lnmp~] # yum -y install gcc pcre-devel zlib-devel //安裝依賴
[root@lnmp~] # tar -zxvf nginx-1.12.2.tar.gz //解壓
[root@lnmp~] # cd nginx-1.12.2 //進源碼目錄
[root@lnmp~] # ./configure //配置
[root@lnmp~] # make && make install //編譯安裝
[root@lnmp~] # ls /usr/local //查看安裝目錄
bin etc games include lib lib64 libexec nginx sbin share src
[root@lnmp~] #ls /usr/local/nginx //查看目錄列表
conf html logs sbin
[root@lnmp~] #yum -y install php-fpm //安裝php-fpm
2)修改配置nginx.conf
[root@lnmp~] # 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;
}
[root@lnmp~] # /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服務和啟動nginx服務
[root@lnmp~] # systemctl start php-fpm //啟動服務
[root@lnmp~] # netstat -utnlp | grep :9000 //查看端口
[root@lnmp~] # /usr/local/nginx/sbin/nginx
[root@lnmp~] # netstat -utnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23505/nginx: master
4)測試配置
[root@lnmp~] # vim /usr/local/nginx/html/test.php //編寫php文件
<?php
echo "hello world!!!";
?>
[root@lnmp~] # curl http://localhost/test.php //訪問nginx服務
二:配置PHP支持redis
1)安裝php擴展
[root@lnmp~] # yum -y install php php-devel automake autoconf //安裝依賴
[root@lnmp~] # tar -zxf php-redis-2.2.4.tar.gz //安裝擴展包
[root@lnmp~] # cd phpredis-2.2.4/
[root@lnmp~] # phpize //生成配置文件php-config及 configure命令
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@lnmp~] # ./configure --with-php-config=/usr/bin/php-config //配置
[root@lnmp~] # make //編譯
[root@lnmp~] # make install //安裝
2)修改php.ini文件
[root@lnmp~] #vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/" //模塊文件目錄
730 extension = "redis.so" //模塊文件名
[root@lnmp~] # systemctl restart php-fpm //重啟php-fpm服務
[root@lnmp~] # php -m | grep -i redis //查看已加載的模塊
redis
三:redis服務器配置
對Redis服務器192.168.4.50做如下配置: 端口號 6350 連接密碼 123456
1)修改配置文件
[root@redis utils]# /etc/init.d/redis_6379 stop
[root@redis utils]# vim /etc/redis/6379.conf
...
bind 192.168.4.50 //設(shè)置服務使用的ip
port 6350 //更改端口號
requirepass 123456 //設(shè)置密碼
2)修改啟動腳本
[root@redis ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.50 -p 6350 -a 123456 shutdown
3)啟動服務
[root@redis ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis ~]#
[root@redis ~]# netstat -utnlp | grep redis-server
tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11523/redis-server
4)測試配置,訪問服務存取數(shù)據(jù)
[root@redis ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 //訪問服務
192.168.4.50:6350> ping
PONG
192.168.4.50:6350> keys *
(empty list or set)
192.168.4.50:6350>
192.168.4.50:6350> set x 99
OK
192.168.4.50:6350>
192.168.4.50:6350> exit
四:測試配置:編寫網(wǎng)站腳本,把數(shù)據(jù)存儲到redis服務器192.168.4.50
1)查看192.168.4.50主機的redis服務是否運行
[root@redis~] # netstat -utnlp | grep redis-server
tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11523/redis-server
[root@redis~] # 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)編寫網(wǎng)站腳本
[root@lnmp~] # 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");
?>
3)訪問網(wǎng)站腳本
[root@lnmp~] #curl http://localhost/linkredis.php //訪問nginx服務
redhat
4)在192.168.4.50 服務器,查看數(shù)據(jù)
[root@redis~] # 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>