部署LNMP+Redis(mongodb,并测试远程写入mysql数据主机)

系统环境

L---这里用的是rhel-server-7.4-x86_64-dvd.iso

N---nginx-1.12.2.tar.gz

M---MySQL5.7.17或者mariadb、mariadb-server

P---PHP-fpm-5.4.16-42.el7.x86_64.rpm

 

安装步骤

lnmp服务端(nginx,mysql,php-fpm,redis)  用192.168.4.52主机
192.168.4.50 为redis主机。

1.安装软件包:

依赖包:gcc  gcc-c++.x86_64  pcre-devel  zlib-devel (默认被依赖安装)  openssl-devel

nginx包: nginx-1.12.2.tar.gz 源码包 (编译安装)

数据库: mysql或者mariadb,mariadb-server

php包  :php-fpm-5.4.16-42.el7.x86_64.rpm

[root@mysql52 ~]# yum -y install gcc  gcc-c++.x86_64  pcre-devel  zlib-devel openssl-devel
[root@mysql52 ~]# tar -zxf nginx-1.12.2.tar.gz
[root@mysql52 lnmp]# cd nginx-1.12.2/
[root@mysql52 nginx-1.12.2]# ./configure
[root@mysql52 nginx-1.12.2]# make
[root@mysql52 nginx-1.12.2]# make install
[root@mysql52 ~]#yum -y install mariadb mariadb-server
[root@mysql52 ~]# systemctl restart mysqld
[root@mysql52 ~]# systemctl enable  mysqld
[root@mysql52 ~]# yum  -y install php-fpm-5.4.16-42.el7.x86_64.rpm 
[root@mysql52 ~]# systemctl start php-fpm.service
[root@mysql52 ~]# systemctl enable  php-fpm.service -f 

2.修改nginx支持php,测试能否解释php

[root@mysql52 ~]# vim /usr/local/nginx/conf/nginx.conf  
              ....
 65          location ~ \.php$ {                     //将65-71行注释去掉,69行不用
 66              root           html;
 67              fastcgi_pass   127.0.0.1:9000;
 68              fastcgi_index  index.php;
 69         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_s    cript_name;
 70              include        fastcgi.conf;     //将fastcgi_param 改为fastcgi.conf
 71          }       
              ....

[root@mysql52 ~]# /usr/local/nginx/sbin/nginx       //启动服务
[root@mysql52 ~]# netstat -pantul | grep 80
tcp        0      0 0.0.0.0:80         0.0.0.0:*         LISTEN   4613/nginx: master  

[root@mysql52 ~]# vim /usr/local/nginx/html/test.php
<?php
echo "hello!The young man!!!";
?>
[root@mysql52 ~]# curl 192.168.4.52/test.php
hello!The young man

3.安装redis(编译安装,初始化)

[root@mysql52 ~]# tar -zxf redis-4.0.8.tar.gz 
[root@mysql52 ~]# cd redis-4.0.8/
[root@mysql52 redis-4.0.8]# make 
[root@mysql52 redis-4.0.8]# make  install
[root@mysql52 redis-4.0.8]# ./utils/install_server.sh    //初始化
[root@mysql52 redis-4.0.8]# redis-cli                    //测试链接
127.0.0.1:6379> 

4.安装php扩展(mysql redis)

[root@mysql52 ~]# yum -y install php
[root@mysql52 ~]# yum -y install php-mysql.x86_64
已安装:
  php-mysql.x86_64 0:5.4.16-42.el7                                 

作为依赖被安装:
  php-pdo.x86_64 0:5.4.16-42.el7 
[root@mysql52 ~]# yum -y install php-devel-5.4.16-42.el7.x86_64.rpm 
 
已安装:
  php-devel.x86_64 0:5.4.16-42.el7                                 

作为依赖被安装:
  autoconf.noarch 0:2.69-11.el7   automake.noarch 0:1.13.4-3.el7  
  m4.x86_64 0:1.4.16-10.el7    
[root@mysql52 ~]# tar -zxf php-redis-2.2.4.tar.gz
[root@mysql52 ~]# cd phpredis-2.2.4/
[root@mysql52 phpredis-2.2.4]# /usr/bin/phpize 
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
[root@mysql52 phpredis-2.2.4]# ./configure  --with-php-config=/usr/bin/php-config
[root@mysql52 phpredis-2.2.4]# make
[root@mysql52 phpredis-2.2.4]# make install
Installing shared extensions:     /usr/lib64/php/modules/             //模块路径
[root@mysql52 phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so      mysqli.so     pdo.so         redis.so
fileinfo.so  mysql.so      pdo_sqlite.so  sqlite3.so
json.so      pdo_mysql.so  phar.so        zip.so
[root@mysql52 phpredis-2.2.4]# php -m | grep -i mysql                 //查看模块是否加载
mysql
mysqli
pdo_mysql

[root@mysql52 phpredis-2.2.4]# vim /etc/php.ini 
    ...
 728  extension_dir = "/usr/lib64/php/modules/"                 //模块路径
    
 730  extension = "redis.so"                                     //模块名
    ...
[root@mysql52 phpredis-2.2.4]# php -m | grep -i redis
redis

5.测试配置

[root@mysql52 phpredis-2.2.4]# vim /usr/local/nginx/html/redis.php
<?php
$redis = new redis();
$redis->connect('127.0.0.1',6379);          
$redis->auth('123456')                           //如果设置密码,写此项
$redis->set('redistest','66666');
echo $redis->get('redistest');        
echo "
" ;
?>
[root@mysql52 phpredis-2.2.4]# curl 192.168.4.52/redis.php
[root@mysql52 phpredis-2.2.4]# redis-cli 
127.0.0.1:6379> keys *
"redistest"

6.用不同主机验证redis(远端存储在50主机)

[root@mysql52 html]# cp  redis.php  link50.php 
[root@mysql52 html]# vim link50.php 
<?php
$redis = new redis();
$redis->connect('192.168.4.50',6350);
$redis->auth('123456');
$redis->set('link50','123456');
echo $redis->get('link50');
echo "
" ;
?>

[root@mysql52 html]# curl 192.168.4.52/link50.php
123456

把redis换成MongoDB同样部署

1.在192.168.4.51启动MongoDB服务

[root@mongo50 ~]# netstat -pntul | grep mongo
tcp        0      0 192.168.4.50:27050      0.0.0.0:*               LISTEN      1497/mongod    

2.在52安装php扩展MongoDB(编译安装同redis)

[root@mysql52 ~]# tar -zxf mongo-1.6.16.tgz    //解压进行编译安装,同redis,略
...
[root@mysql52 ~]#php -m | grep -i mongo
mongo

3.测试配置

[root@mysql52 ~]# vim /usr/local/nginx/html/linkmongo.php
<?php
$m = new Mongo("mongodb://192.168.4.50:27050");
$db = $m->db3;
$c = $db->t1;
$data=array("name"=>"bob","age"=>19);
$c->insert($data);
echo "data ok";
?>
[root@mysql52 ~]#curl 192.168.4.51/linkmongo.php     //网页测试
data ok

4.51上看是否写入

[root@mongo51 ~]# /usr/local/mongodb/bin/mongo --host 192.168.4.51 --host 27051
> show dbs
admin    0.000GB
config   0.000GB
db3      0.000GB
...
local    0.000GB
> use db3
switched to db db3
> show tables
t1
> db.t1.find()
{ "_id" : ObjectId("5c2446e21d6b27c4408b4567"), "name" : "bob", "age" : NumberLong(19) }
{ "_id" : ObjectId("5c244d381d6b27c5408b4567"), "name" : "bob", "age" : NumberLong(19) }

远程数据写入mysql主机

1.在192.168.4.53启动mysql服务,并建库和表,授权用户

[root@mysql53 ~]# systemctl restart mysqld
[root@mysql53 ~]# netstat -pntul | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      1583/mysqld  
[root@mysql53 ~]# mysql -uroot -p123456 -h192.168.4.53
mysql> create database gamedb;
mysql> use gamedb;
mysql> create table user(
    -> name char(30),
    -> pass char(10));
mysql> desc user;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(30) | YES  |     | NULL    |       |
| pass  | char(10) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> grant select,insert on gamedb.user to webuser@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.03 sec)

2.在52安装php扩展(mysql)

[root@mysql52 ~]# yum -y install php-mysql
[root@mysql52 ~]# php -m | grep -i mysql
mysql
mysqli
pdo_mysql

3.测试配置

[root@mysql52 ~]# vim  /usr/local/nginx/html/mysql.html
<html>
<form action="mysql.php" method="post">
<h5> mysql login user info:</h5>
<h3>username</h3>
<input type="text" name="name" size="30" maxlenth="255"/>
<br />
<br />
<h5>password:</h5>
<input type="text" name="pass" size="10" maxlenth="255"/>
<br />
<br />
<h5><input type="submit" name="submit" value="submit" /></h5></p>
</form>
</html>

[root@mysql52 ~]# vim /usr/local/nginx/html/mysql.php
<?php
$servername = "192.168.4.53";
$username = "webuser";
$password = "123456";
$dbname = "gamedb";

$conn = new mysqli($servername,$username,$password,$dbname);

if ($conn->connect_error) {
   die("link fail:" . $conn->connect_error);
}
else{
echo "link mysql ok";
echo "  ";
}

$username=$_POST['name'];
$userpassword=$_POST['pass'];
$sql="insert into user (name,pass) values
('$username','$userpassword')";
if($conn->query($sql)){echo "insert data ok"; }
$conn->close();
?>

[root@mysql52 ~]# firefox 192.168.4.52/mysql.html
 mysql login user info:
username

输入名字:比如xiaoming
password:
输入密码:比如123456
submit    //提交

然后网页跳转到192.168.4.52/mysql.php
link mysql ok insert data ok

4.在53上查看

mysql> select  * from gamedb.user;
+----------+--------+
| name     | pass   |
+----------+--------+
| xiaoming | 123456 |
+----------+--------+
1 rows in set (0.00 sec)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值