centos 安装 redis及简单配置

下载redis并安装

http://redis.io/download

  1. wget http://download.redis.io/releases/redis-3.2.3.tar.gz
  2. tar xzf redis-3.2.3.tar.gz
  3. cd redis-3.2.3
  4. make

配置redis

移动或者复制 redis-3.2.3/src 目录到其他位置

  1. mv redis-3.2.3/src /usr/local/redis
  2. # 复制配置文件
  3. cp redis-3.2.3/redis.conf /usr/local/redis

修改redis.conf 配置使进程在后台运行

  1. vim /usr/local/redis/redis.conf
  2. # 修改参数 daemonize yes

配置init 脚本

vim /etc/init.d/redis

  1. # description: Start and Stop redis
  2. PATH=/usr/local/bin:/sbin:/usr/bin:/bin
  3. REDISPORT=6379
  4. EXEC=/usr/local/redis/redis-server
  5. REDIS_CLI=/usr/local/redis/redis-cli
  6. PIDFILE=/var/run/redis_6379.pid
  7. CONF="/usr/local/redis/redis.conf"
  8. case "$1" in
  9. start)
  10. if [ -f $PIDFILE ]
  11. then
  12. echo "$PIDFILE exists, process is already running or crashed."
  13. else
  14. echo "Starting Redis server..."
  15. $EXEC $CONF
  16. fi
  17. if [ "$?"="0" ]
  18. then
  19. echo "Redis is running..."
  20. fi
  21. ;;
  22. stop)
  23. if [ ! -f $PIDFILE ]
  24. then
  25. echo "$PIDFILE exists, process is not running."
  26. else
  27. PID=$(cat $PIDFILE)
  28. echo "Stopping..."
  29. $REDIS_CLI -p $REDISPORT SHUTDOWN
  30. sleep 2
  31. while [ -x $PIDFILE ]
  32. do
  33. echo "Waiting for Redis to shutdown..."
  34. sleep 1
  35. done
  36. echo "Redis stopped"
  37. fi
  38. ;;
  39. restart|force-reload)
  40. ${0} stop
  41. ${0} start
  42. ;;
  43. *)
  44. echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
  45. exit 1
  46. esac

配置好init 脚本就可以使用 service redis start 命令来启动reids 了

设置redis 开机启动

  1. chkconfig redis on

centos6.8 yum 安装 lamp

安装 EPEL源

https://fedoraproject.org/wiki/EPEL

找到相应的centos版本

If you are running an EL6 version, please visit here to get the newest ‘epel-release’ package for EL6: The newest version of ‘epel-release’ for EL6

  1. rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

安装 remi 源

http://rpms.famillecollet.com/

找到相应的centos版本

Enterprise Linux 6 – repository auto-configuration package : remi-release-6.rpm

  1. rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

安装mysql

  1. yum --enablerepo=remi install mysql mysql-server

可能因为网络问题,连接不上remi源,先用vpn:https://www.flyvpn.com/cn/

配置mysql

  1. #配置 mysqld 开机自启动
  2. chkconfig mysqld on
  3. #启动 mysql
  4. service mysqld start
  5. #更改 root 密码
  6. mysql -uroot
  7. #进入 mysql,然后
  8. update mysql.user SET password=password('123456') WHERE user='root';
  9. # 让外网可以访问
  10. update mysql.user set host = '%' where user ='root';
  11. flush privileges;
  12. quit;

安装php

  1. yum --enablerepo=remi-php56 install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml php-soap php-fpm php-cli php-pdo

php可用源版本选择

http://rpms.famillecollet.com/enterprise/6/

设置php-fpm 开机启动

  1. chkconfig --add php-fpm
  2. chkconfig php-fpm on

安装nginx

http://nginx.org/en/linux_packages.html

创建nginx的yum 安装源
/etc/yum.repos.d/nginx.repo

  1. [nginx]
  2. name=nginx repo
  3. baseurl=http://nginx.org/packages/centos/6/$basearch/
  4. gpgcheck=0
  5. enabled=1
  1. #安装
  2. yum install --enablerepo=nginx nginx
  3. #启动
  4. service nginx start
  5. #设置为开机启动:
  6. chkconfig --add nginx
  7. chkconfig nginx on

nginx配置:/etc/nginx/conf.d/default.conf

  1. server {
  2. listen 80;
  3. server_name foo.com;
  4. root /www;
  5. index index.html index.htm index.php;
  6. location / {
  7. try_files $uri $uri/ /index.php;
  8. }
  9. location ~ \.php$ {
  10. try_files $uri =404;
  11. fastcgi_index index.php;
  12. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  13. include fastcgi_params;
  14. fastcgi_pass 127.0.0.1:9000;
  15. }
  16. }

phalcon 笔记(不定期更新)

1. model 初始化配置

public function initialize()
	{
		$this->useDynamicUpdate(true); // 动态更新字段,非全部更新
	}

2.数据库配置,记录查询sql

$di->setShared('db', function () use ($config,$di) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;
    /** @var Phalcon\Db\Adapter\Pdo\Mysql $connection */
    $connection =  new $class($dbConfig);
    /** @var Phalcon\Events\Manager $eventsManager */
    $eventsManager = $di->get("eventsManager");
    $logger = new \Phalcon\Logger\Adapter\File(__DIR__."/../logs/debugs.log");
    $eventsManager->attach('db', function($event, $connection) use ($logger) {
        if ($event->getType() == 'beforeQuery') {
            $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
        }
        if ($event->getType() == 'beforeSave') {
            $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
        }
    });
    $connection->setEventsManager($eventsManager);

    return $connection;
});

2.禁用或启用特性
https://docs.phalconphp.com/en/latest/reference/models.html#disabling-enabling-features
phalcon的orm有坑,如果数据库某个字段为null,那么update,save的时候会报错,因为phalcon默认检测字段是否Not null。
应该在model将此特性关闭。将notNullValidations设置为false

 

   public function initialize()
    {
        $this->setup(['notNullValidations'=>FALSE]);
    }

phalcon数据库配置添加表前缀

Phalcon默认居然不能设置数据表前缀,常用的框架都支持。
修改方法一:
新建一个基础模型,然后所有的模型在该类上继承即可

<?php
class BaseModel extends \Phalcon\Mvc\Model {
    public function getSource()
    {
        return 'gw_'.strtolower(get_class($this));
    }
}

但是此种方法,用phalcon devtools命令行模式生成model文件,文件名称是有表前缀的

修改方法二:
2.1 先在app/config/config.php 配置文件加上数据库前缀配置tablePrefix

    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'root',
        'password'    => '',
        'dbname'      => 'test',
        'charset'     => 'utf8',
        'port' => '3306',
        'tablePrefix' => 'gw_'
    ),

2.2 修改phalcon devtools
代码phalcon\devtools\scripts\Phalcon\Builder\Model.php
在220行$table = $this->options->get(‘name’);之后加上代码

$table = $this->options->get('name');
if(isset($config->database->tablePrefix)){
    $table = $config->database->tablePrefix.$table;
}

在480行
$methodRawCode[] = $this->snippet->getModelSource($this->options->get(‘name’));
修改代码为:
$methodRawCode[] = $this->snippet->getModelSource($table);

此时使用工具命令

phalcon model user

生成model :
app/models/User.php
内容如下,不会提示table不存在了:

<?php

use Phalcon\Mvc\Model\Validator\Email as Email;

class User extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $username;

    /**
     *
     * @var string
     */
    public $password;

    /**
     *
     * @var integer
     */
    public $status;

    /**
     *
     * @var string
     */
    public $real_name;

    /**
     *
     * @var string
     */
    public $mobile;

    /**
     *
     * @var string
     */
    public $email;

    /**
     *
     * @var integer
     */
    public $sex;

    /**
     *
     * @var integer
     */
    public $logins;

    /**
     *
     * @var integer
     */
    public $create_time;

    /**
     * Validations and business logic
     *
     * @return boolean
     */
    public function validation()
    {
        $this->validate(
            new Email(
                array(
                    'field'    => 'email',
                    'required' => true,
                )
            )
        );

        if ($this->validationHasFailed() == true) {
            return false;
        }

        return true;
    }

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource("gw_user");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'gw_user';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return User[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return User
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

}

 

php7.04 编译安装

安装参考:http://www.tuicool.com/articles/bi2iYj3

编译参数:

./configure
--prefix=/usr/local/php7
--exec-prefix=/usr/local/php7
--bindir=/usr/local/php7/bin
--sbindir=/usr/local/php7/sbin
--includedir=/usr/local/php7/include
--libdir=/usr/local/php7/lib/php
--mandir=/usr/local/php7/php/man
--with-config-file-path=/usr/local/php7/etc
--with-mysql-sock=/var/run/mysql/mysql.sock
--with-mcrypt=/usr/include
--with-mhash
--with-openssl
--with-mysqli=shared,mysqlnd
--with-pdo-mysql=shared,mysqlnd
--with-gd
--with-iconv
--with-zlib
--enable-zip
--enable-inline-optimization
--disable-debug
--disable-rpath
--enable-shared
--enable-xml
--enable-bcmath
--enable-shmop
--enable-sysvsem
--enable-mbregex
--enable-mbstring
--enable-ftp
--enable-gd-native-ttf
--enable-pcntl
--enable-sockets
--with-xmlrpc
--enable-soap
--without-pear
--with-gettext
--enable-session
--with-curl
--with-jpeg-dir
--with-freetype-dir
--enable-opcache
--enable-fpm
--with-fpm-user=nginx
--with-fpm-group=nginx
--without-gdbm 
--disable-fileinfo

 

要把 编译参数弄成一行再执行

编译如果报错,使用 ./configure –help 查看哪些参数是能用的,每个版本可能都不一样

使用 apache 还需要添加参数 –with-apxs2=/usr/sbin/apxs