Linux

CentOS7+Apache+PHP+Redisセッションストア

CentOS7 ApacheでRedisをセッションストアとして利用する

寄稿しました。

 

 

WEB

 

[centos@ip-172-31-17-156 ~]$ sudo su -

[root@ip-172-31-17-156 ~]#

 

# vi /etc/sysconfig/selinux


SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

 

# reboot now

 

# yum install httpd httpd-devel php php-mysql mysql mariadb-server mariadb

 

# systemctl start httpd
# systemctl enable httpd


# systemctl start mariadb
# systemctl enable mariadb

 

# mkdir -p /var/www/vhosts/sg-test.local/httpdocs

 

# mysql -u root

MariaDB [(none)]> CREATE DATABASE testdb;

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO testdbuser@localhost IDENTIFIED BY 'testdbpassword';

MariaDB [(none)]> exit

 

# vi sg-test.local.conf


<VirtualHost *:80>
   ServerName sg-test.local
   DocumentRoot /var/www/vhosts/sg-test.local/httpdocs
   ErrorLog logs/virtual.host-error_log
   CustomLog logs/virtual.host-access_log combined
   <Directory "/var/www/vhosts/sg-test.local/httpdocs">
       AllowOverride All
   </Directory>
</VirtualHost>

 

# httpd -t
Syntax OK

# systemctl restart httpd

 

http://sg-test.local/

 

# yum install wget unzip

# pwd
/var/www/vhosts/sg-test.local/httpdocs


# wget https://ja.wordpress.org/wordpress-4.9.1-ja.zip

# unzip wordpress-4.9.1-ja.zip

 

# cd /var/www/vhosts/sg-test.local/httpdocs/wordpress

# cp wp-config-sample.php wp-config.php


# vi wp-config.php

/** WordPress のためのデータベース名 */
define('DB_NAME', 'testdb');

/** MySQL データベースのユーザー名 */
define('DB_USER', 'testdbuser');

/** MySQL データベースのパスワード */
define('DB_PASSWORD', 'testdbpassword');

 

http://sg-test.local/wordpress/wp-admin/install.php

 

WEB1のスナップショットからWEB2を作成する

 

# yum install epel-release

# yum install --enablerepo=epel php-pecl-redis

 

 

# vi /etc/httpd/conf.d/php.conf

(略)

#php_value session.save_handler "files"
#php_value session.save_path    "/var/lib/php/session"
php_value session.save_handler "redis"
php_value session.save_path    "tcp://172.31.20.193:6379?auth=PassWordDeath"
php_value soap.wsdl_cache_dir  "tcp://172.31.20.193:6379?auth=PassWordDeath"

 

# systemctl restart httpd

 

# vi /var/www/vhosts/sg-test.local/httpdocs/wordpress/sessiontest.php

<?php
session_start();

echo "save_handler=" . ini_get("session.save_handler") . "\n";
echo "save_path=" . ini_get("session.save_path") . "\n";
echo "session_id=" . session_id() . "\n";

$_SESSION['libname'] = "PhpRedis";

 

 

web1
http://13.230.193.6/wordpress/sessiontest.php

 

web2
http://13.230.251.13/wordpress/sessiontest.php

 

 

Redis

 

[centos@ip-172-31-20-193 ~]$ sudo su -
[root@ip-172-31-20-193 ~]#


# yum install unzip wget

# yum groupinstall "Development Tools" "Base"

# yum install gcc gcc-c++ pcre-devel zlib-devel make wget openssl-devel libxml2 libxml2-devel libxslt-devel libxslt libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools-devel flex

# yum install gcc tcl readline-devel

 

# getenforce
Enforcing

 

# vi /etc/sysconfig/selinux

SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

 

# vi /etc/sysctl.conf
 
 
※下記を追記
 
#Redis用設定
vm.overcommit_memory = 1
net.core.somaxconn = 1024

 

# sysctl -p
vm.overcommit_memory = 1
net.core.somaxconn = 1024

 

# vi /etc/rc.local
 
# that this script will be executed during boot.
 
・
・
touch /var/lock/subsys/local
 
 
※以下を追記
 
#Redis用設定
echo never > /sys/kernel/mm/transparent_hugepage/enabled

 

# chmod +x /etc/rc.d/rc.local

 

# reboot now

 

# cd /usr/local/src

# wget http://download.redis.io/releases/redis-4.0.6.tar.gz

# tar zxvf redis-4.0.6.tar.gz

# cd redis-*

# make distclean

# make && make install

 

# mkdir /etc/redis

# mkdir -p /var/redis/6379

# cp utils/redis_init_script /etc/init.d/redis_6379

# cp redis.conf /etc/redis/6379.conf

 

# vi /etc/redis/6379.conf
 
 
daemonize no
 
↓変更
 
#daemonize no
daemonize yes
 
 
pidfile /var/run/redis_6379.pid
 
 
 
logfile ""
 
↓変更
 
#logfile ""
logfile /var/log/redis/redis_6379.log
 
 
 
dir ./
 
↓変更
 
#dir ./
dir /var/redis/6379



bind 127.0.0.1

↓変更

#bind 127.0.0.1



#protected-mode yes
protected-mode no


requirepass PassWordDeath

 

 

ログディレクトリ

# mkdir /var/log/redis

 

 

# vi /etc/init.d/redis_6379


#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
 
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
 
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
 
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
 
start(){
    if [ -f $PIDFILE ]
    then
            echo "$PIDFILE exists, process is already running or crashed"
    else
            echo "Starting Redis server..."
            $EXEC $CONF
    fi
}
stop(){
    if [ ! -f $PIDFILE ]
    then
            echo "$PIDFILE does not exist, process is not running"
    else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT shutdown
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis to shutdown ..."
                sleep 1
            done
            echo "Redis stopped"
    fi
}
 
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

 

# chkconfig --add redis_6379

# chkconfig redis_6379 on

# /etc/init.d/redis_6379 start
Starting Redis server...

 

# netstat -an | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN

 

# redis-cli ping
PONG


# redis-cli info | grep redis_version
redis_version:4.0.6

 

# tail -n 25 /var/log/redis/redis_6379.log

6309:C 27 Dec 04:16:21.599 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6309:C 27 Dec 04:16:21.599 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=6309, just started
6309:C 27 Dec 04:16:21.599 # Configuration loaded
6310:M 27 Dec 04:16:21.600 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6310
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

6310:M 27 Dec 04:16:21.601 # Server initialized
6310:M 27 Dec 04:16:21.601 * Ready to accept connections

 

[root@ip-172-31-20-193 ~]# redis-cli

127.0.0.1:6379> AUTH PassWordDeath
OK

127.0.0.1:6379> monitor
OK

1514443961.347753 [0 172.31.20.250:39628] "AUTH" "PassWordDeath"
1514443961.348116 [0 172.31.20.250:39628] "GET" "PHPREDIS_SESSION:g688feh8inohm6igec5dmbfp20"
1514443966.656988 [0 172.31.17.156:38718] "AUTH" "PassWordDeath"
1514443966.657294 [0 172.31.17.156:38718] "GET" "PHPREDIS_SESSION:a03cbo3khnap0es4sf04s62vl5"



127.0.0.1:6379> exit
[root@ip-172-31-20-193 redis-4.0.6]#

 

 

 

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)