部署LNMP+Redis(mongodb,並測試遠端寫入mysql資料主機)
阿新 • • 發佈:2019-01-04
系統環境
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
[[email protected] ~]# yum -y install gcc gcc-c++.x86_64 pcre-devel zlib-devel openssl-devel
[[email protected] ~]# tar -zxf nginx-1.12.2.tar.gz
[[email protected] lnmp]# cd nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure
[[email protected] nginx-1.12.2]# make
[[email protected] nginx-1.12.2]# make install
[[email protected] ~]#yum -y install mariadb mariadb-server
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# systemctl enable mysqld
[[email protected] ~]# yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm
[[email protected] ~]# systemctl start php-fpm.service
[[email protected] ~]# systemctl enable php-fpm.service -f
2.修改nginx支援php,測試能否解釋php
[[email protected] ~]# 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 }
....
[[email protected] ~]# /usr/local/nginx/sbin/nginx //啟動服務
[[email protected] ~]# netstat -pantul | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4613/nginx: master
[[email protected] ~]# vim /usr/local/nginx/html/test.php
<?php
echo "hello!The young man!!!";
?>
[[email protected] ~]# curl 192.168.4.52/test.php
hello!The young man
3.安裝redis(編譯安裝,初始化)
[[email protected] ~]# tar -zxf redis-4.0.8.tar.gz
[[email protected] ~]# cd redis-4.0.8/
[[email protected] redis-4.0.8]# make
[[email protected] redis-4.0.8]# make install
[[email protected] redis-4.0.8]# ./utils/install_server.sh //初始化
[[email protected] redis-4.0.8]# redis-cli //測試連結
127.0.0.1:6379>
4.安裝php擴充套件(mysql redis)
[[email protected] ~]# yum -y install php
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# tar -zxf php-redis-2.2.4.tar.gz
[[email protected] ~]# cd phpredis-2.2.4/
[[email protected] phpredis-2.2.4]# /usr/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[[email protected] phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
[[email protected] phpredis-2.2.4]# make
[[email protected] phpredis-2.2.4]# make install
Installing shared extensions: /usr/lib64/php/modules/ //模組路徑
[[email protected] 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
[[email protected] phpredis-2.2.4]# php -m | grep -i mysql //檢視模組是否載入
mysql
mysqli
pdo_mysql
[[email protected] phpredis-2.2.4]# vim /etc/php.ini
...
728 extension_dir = "/usr/lib64/php/modules/" //模組路徑
730 extension = "redis.so" //模組名
...
[[email protected] phpredis-2.2.4]# php -m | grep -i redis
redis
5.測試配置
[[email protected] 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 "
" ;
?>
[[email protected] phpredis-2.2.4]# curl 192.168.4.52/redis.php
[[email protected] phpredis-2.2.4]# redis-cli
127.0.0.1:6379> keys *
"redistest"
6.用不同主機驗證redis(遠端儲存在50主機)
[[email protected] html]# cp redis.php link50.php
[[email protected] 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 "
" ;
?>
[[email protected] html]# curl 192.168.4.52/link50.php
123456
把redis換成MongoDB同樣部署
1.在192.168.4.51啟動MongoDB服務
[[email protected] ~]# netstat -pntul | grep mongo
tcp 0 0 192.168.4.50:27050 0.0.0.0:* LISTEN 1497/mongod
2.在52安裝php擴充套件MongoDB(編譯安裝同redis)
[[email protected] ~]# tar -zxf mongo-1.6.16.tgz //解壓進行編譯安裝,同redis,略
...
[[email protected] ~]#php -m | grep -i mongo
mongo
3.測試配置
[[email protected] ~]# 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";
?>
[[email protected] ~]#curl 192.168.4.51/linkmongo.php //網頁測試
data ok
4.51上看是否寫入
[[email protected] ~]# /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服務,並建庫和表,授權使用者
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# netstat -pntul | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 1583/mysqld
[[email protected] ~]# 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 [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.03 sec)
2.在52安裝php擴充套件(mysql)
[[email protected] ~]# yum -y install php-mysql
[[email protected] ~]# php -m | grep -i mysql
mysql
mysqli
pdo_mysql
3.測試配置
[[email protected] ~]# 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>
[[email protected] ~]# 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();
?>
[[email protected] ~]# 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)