PHP laravel framework combined with MySQL and Redis database deployment _php Instance

Source: Internet
Author: User
Tags chmod documentation install php pack php code aliyun

Compared to the official documents, it is more important to put the framework environment together.
0. Environment Introduction

    • Operating system: CentOS
    • Database: MySQL 5.6 (Aliyun rds)
    • PHP 5.4.4 (>=5.4)
    • Laravel 5.0

I. Installation of LNMP
before installing Laravel, you need to build a Linux + Nginx + Mysql + PHP environment. The concrete steps are no longer detailed here.
P.S.

    • The Linux Aliyun has been brought in, and this article uses the CentOS 6.5 64-bit ECS
    • About the choice of Nginx and Apache to see their preferences, this article is using the reverse agent expert Nginx
    • Whether to install MySQL also depends on your situation, such as you use Aliyun rds, there is no need to install

Second, install composer
Composer is the tool used to manage PHP package dependencies, and Laravel is using this tool to come into dependency management. There are two ways to install

Local Installation
Global installation, which is available in any directory of the system. This article only describes this installation method. Official Installation Documentation

Execute the following two commands separately

Curl-ss Https://getcomposer.org/installer | PHP
MV Composer.phar/usr/local/bin/composer

Install complete, use the following command to see if the installation is successful

Composer-v

The version number appears, indicating that the installation was successful

Iii. installation of Laravel
according to Laravel's official documentation, it is recommended to use the "Install tool through Laravel", there is no pit, skip here
Tip: Because Laravel also relies on some PHP extensions, use Yum to install

sudo install yum php-mysql php-mcrypt php-mbstring Php-tokenizer

After the installation is complete, add the following configuration at the bottom of the Nginx configuration file (typically/etc/nginx/conf.d/default.conf)

Location/{
  try_files $uri $uri//index.php $query _string;
}

Come to your Laravel engineering directory, see the storage and Vendor folders, use the following command to modify their file read and write permissions, so that nginx users can read and write it

sudo chmod-r 766 storage
sudo chmod-r 766 vendor

Four, let MVC run!
before that, you should read the official document routing, controller, database usage basics, eloquent ORM
At this point, you can start coding, develop an MVC demo, this demo is the function of the database table Tbl_item read out from the database, and in JSON format to respond to the browser.
Let's say you've initialized your web App with Laravel new demo.

    • Database to build (demo), build table (Tbl_item), (field arbitrarily set)
    • Configuration Profile config/database.php
    • Directly manipulate the database and insert a piece of data into the Tbl_item.
    • Start coding

Add the following code at the bottom of the demo/app/http/routes.php:

Route::get ('/item/{id} ', ' Itemcontroller@showitem ');

demo/app/http/controllers/directory new Add File itemcontroller.php, the code is as follows:

<?php namespace App\http\controllers;

Use Illuminate\http\request;
Use App\item as Item;

Class Itemcontroller extends Controller {

  private $model;

  Public function __construct ()
  {
    $this->model = new Item ();
  }

  Public Function ShowItem ($id)
  {
    $users = $this->model->fetchall ();
    echo Json_encode ($users);
    Log::info (' Get user list, through msyql ');
  }


demo/app/directory new file item.php code below

<?php namespace App;

Use Illuminate\database\eloquent\model;

Class Item extends Model {

  protected $fillable = [' name ', ' Price '];
  protected $guarded = [' id '];

  /**
   * The database table used by the model.
   * Default:tbl_items
   * @var string
  ///protected $table = ' tbl_items ';

  Public Function Fetchall () {
    $items = $this->all ()->tojson ();
    return $items;
  }


All item data can be listed by using a browser to access HTTP://YOURIP/ITEM/1

V. Laravel combination of Redis
Direct-DB is not enough, soon database access will become the bottleneck of the system. We introduce cache Redis. Or the same idea, let the system run up first.
1. Installation Start Redis

Installation

$ wget http://download.redis.io/releases/redis-3.0.1.tar.gz
$ tar xzf redis-3.0.1.tar.gz
$ cd redis-3.0.1
$ make

Start

$ src/redis-server

To view the official download and installation documentation, you need only a few commands


2. Install PHP Predis
Predis is a PHP access Redis expansion pack, only need to download the original code, you do not need to install PHP extensions (such as php-redis.so). Before this, however, a composer is introduced, because Laravel is used to install a Third-party package (administrative dependencies).

CD to the path to your app, modify Composer.json, add "Predis/predis" in the Require field, "~1.0.1", and then the current directory sudo composer update, this time will automatically download the package needed expansion pack, These expansion packs will be placed in the vendor directory. If there is not enough memory of these errors, now appears to be the cause of insufficient memory allocation, restart the server can be completely resolved to modify the server configuration, but I do not know where to change, follow-up and repair

Configure the related configuration and view the official documentation. Mainly configure the config/database.php

' Redis ' => array (
  ' cluster ' => false,
  ' default ' => array (' Host ' => ' 127.0.0.1 ', ' Port ' => 6379) 
   )


3, coding

<?php namespace App\http\controllers;

Use Illuminate\http\request;
Use App\user as User;
Use Illuminate\support\facades\redis as Redis;

Class Usercontroller extends Controller {

  //use User;

  Private $model;
  /**
   * Create a new controller instance.
   *
   * @return void
   *
  /Public Function __construct ()
  {
    $this->model = new User ();
  }

  /**
   * Show 
   * *
   @return Response
  /Public Function Showuser ($id)
  {
    $redis = Redis: : Connection (' Default ');
    $cacheUsers = $redis->get (' userlist ');

    if ($cacheUsers) {
      $users = $cacheUsers;
      Print_r ($users);
      Log::info (' Get user list, via Redis ');
    } else{
      $users = $this->model->fetchall ();
      $redis->set (' userlist ', $users);
      Print_r ($users);
      Log::info (' Get user list, pass msyql ');}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.