部署支持php和Redis的Nginx服务器
一.安裝并配置Nginx服務器
- 在nginx1(192.168.1.10)上安裝nginx
# 安裝編譯器
[root@nginx1 ~]# yum install -y gcc pcre-devel zlib-devel
# 編譯安裝nginx
[root@nginx1 ~]# tar xf nginx-1.12.2.tar.gz
[root@nginx1 ~]# cd nginx-1.12.2
[root@nginx1 nginx-1.12.2]# ./configure
[root@nginx1 nginx-1.12.2]# make && make install
```
- 安裝php-fpm
[root@nginx1 ~]# yum install -y php-fpm
[root@nginx1 ~]# systemctl start php-fpm
[root@nginx1 ~]# ss -tlnp | grep :9000
LISTEN???? 0????? 128??? 127.0.0.1:9000???????????????????? *:*?????????????????? users:(("php-fpm",pid=11310,fd=0),("php-fpm",pid=11309,fd=0),("php-fpm",pid=11308,fd=0),("php-fpm",pid=11307,fd=0),("php-fpm",pid=11306,fd=0),("php-fpm",pid=11305,fd=6))
```
- 修改配置文件
[root@nginx1 ~]# vim +65 /usr/local/nginx/conf/nginx.conf
??????? location ~ \.php$ {
??????????? root?????????? html;
??????????? fastcgi_pass?? 127.0.0.1:9000;
??????????? fastcgi_index? index.php;
??????? #??? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
??????????? include??????? fastcgi.conf;
??????? }
```
- 啟動nginx服務
[root@nginx1 ~]# /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
[root@nginx1 ~]# /usr/local/nginx/sbin/nginx
[root@nginx1 ~]# ss -tlnp | grep :80
LISTEN???? 0????? 128????????? *:80?????????????????????? *:*?????????????????? users:(("nginx",pid=11376,fd=6),("nginx",pid=11375,fd=6))
```
- 測試
[root@nginx1 ~]# vim /usr/local/nginx/html/test.php
<?php
??? echo "Hello World!\n";
??? phpinfo();
?>
[root@nginx1 ~]# curl http://127.0.0.1/test.php
Hello World!
... ...
```
二.安裝redis
[root@redis1 ~]# yum install -y gcc
[root@redis1 ~]# wget https://download.redis.io/releases/redis-4.0.8.tar.gz
[root@redis1 ~]# tar xf redis-4.0.8.tar.gz
[root@redis1 ~]# cd redis-4.0.8
# 修改安裝目錄為/usr/local/redis
[root@redis1 redis-4.0.8]# vim +27 src/Makefile
PREFIX?=/usr/local/redis
# 編譯安裝
[root@redis1 redis-4.0.8]# make && make install
# 將redis命令目錄添加至PATH環境變量
[root@redis1 redis-4.0.8]# vim /etc/bashrc?? # 尾部追加
export PATH=$PATH:/usr/local/redis/bin
[root@redis1 redis-4.0.8]# source /etc/bashrc
# 初始化redis服務
[root@redis1 redis-4.0.8]# ./utils/install_server.sh? # 全部問題直接回車采用默認值
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/redis/bin/redis-server]
Selected config:
Port?????????? : 6379
Config file??? : /etc/redis/6379.conf
Log file?????? : /var/log/redis_6379.log
Data dir?????? : /var/lib/redis/6379
Executable???? : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
三.配置PHP支持Redis
- 安裝php擴展
[root@nginx1 ~]# yum install -y php-devel automake autoconf
```
- 安裝php-redis
下載地址
http://windows.php.net/downloads/pecl/snaps/redis/2.2.5/
[root@nginx1 ~]# tar xf redis-cluster-4.3.0.tgz
[root@nginx1 ~]# cd redis-4.3.0/
[root@nginx1 redis-4.3.0]# phpize
[root@nginx1 redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config
[root@nginx1 redis-4.3.0]# make && make install
[root@nginx1 redis-4.3.0]# ls /usr/lib64/php/modules/redis.so
/usr/lib64/php/modules/redis.so
?
- 修改php配置文件并重啟服務
[root@nginx1 redis-4.3.0]# vim /etc/php.ini?? # 在730行下添加
extension_dir = "/usr/lib64/php/modules"
extension = "redis.so"
[root@nginx1 ~]# systemctl restart php-fpm
```
- 測試
# 創建腳本,寫入redis數據庫
[root@nginx1 ~]# vim /usr/local/nginx/html/set_redis.php
<?php
??? $redis = new redis();
??? $redis->connect("192.168.1.11", "6379");
??? $redis->auth("tedu.cn");
??? $redis->set("username", "tom");
?>
# 創建腳本,讀取redis數據
[root@nginx1 ~]# vim /usr/local/nginx/html/get_redis.php
<?php
??? $redis = new redis();
??? $redis->connect("192.168.1.11", "6379");
??? $redis->auth("tedu.cn");
??? echo $redis->get("username");
?>
# 訪問測試頁面
[root@nginx1 ~]# curl http://127.0.0.1/set_redis.php
[root@nginx1 ~]# curl http://127.0.0.1/get_redis.php
tom
# 在redis數據庫上查看
[root@redis1 ~]# redis-cli -a tedu.cn
127.0.0.1:6379> KEYS *
1) "username"
127.0.0.1:6379> GET username
"tom"
總結
以上是生活随笔為你收集整理的部署支持php和Redis的Nginx服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis集群的基本配置
- 下一篇: PyCharm 安装详细图片(linux