CentOS7 LAMP环境安装

httpd

$ yum install -y httpd

开启防火墙http与https的端口

$ firewall-cmd --permanent --zone=public --add-service=http
$ firewall-cmd --permanent --zone=public --add-service=https

mariadb

$ yum install -y mariadb mariadb-server

初始化 mariadb 的账户配置

$ mysql_secure_installation

使用合适的配置文件

$ ls /usr/share/mysql/my-*
my-huge.cnf  my-innodb-heavy-4G.cnf  my-large.cnf  my-medium.cnf  my-small.cnf
$ mv /etc/my.cnf /usr/share/mysql/my.default.cnf # 备份默认配置
$ cp /usr/share/mysql/my-small.cnf /etc/my.cnf  # 复制新的配置文件

php56

# 安装 yum 支持库
$ rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
# 查看可安装的 php 列表
$ yum list php*
# 安装 php
$ yum install -y php56w php56w-mysql php56w-gd libjpeg* php56w-ldap php56w-odbc php56w-pear php56w-xml php56w-xmlrpc php56w-mbstring php56w-bcmath

配置默认时区

$ vi /etc/php.ini
# 修改行内容如下
date.timezone = PRC

服务的启动与关闭

$ systemctl start [server name]  // 启动
$ systemctl stop [server name] // 停止
$ systemctl restart [server name] // 重启
$ systemctl enable [server name] // 启用开机启动
$ systemctl disable [server name]// 禁用开机启动

laravel 5.1 安装跨域插件,开启跨域API

composer https://packagist.org/packages/barryvdh/laravel-cors
请根据composer文档安装适合laravel版本的插件

laravel 5.1 版本安装

$ composer require barryvdh/laravel-cors 0.9.2

添加到config/app.php$providers数组

Barryvdh\Cors\ServiceProvider::class,

全局使用

开启全部路由的跨域,把 HandleCors类 添加 app/Http/Kernel.php 的 $middleware 属性中

protected $middleware = [
 // ...
 \Barryvdh\Cors\HandleCors::class,
];

组中间件

如果您想要在特定的中间件组或路由上允许CORS,请将HandleCors中间件添加到您的组中:

protected $middlewareGroups = [
 'web' => [
 // ...
 ],
 'api' => [
 // ...
 \Barryvdh\Cors\HandleCors::class,
 ],
];

添加配置文件

$ php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"

注:当使用自定义header头,如:X-Auth-Token或者X-Requested-With,你必须设置allowedHeaders包含这些头。您也可以将其设置array('*')为允许所有自定义header
注意:如果您明确列入白名单,则必须包含Origin或请求将无法识别为CORS。

return [
 /*
 |--------------------------------------------------------------------------
 | Laravel CORS
 |--------------------------------------------------------------------------
 |
 | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
 | to accept any value.
 |
 */
 'supportsCredentials' => false,
 'allowedOrigins' => ['*'],
 'allowedHeaders' => ['Content-Type', 'X-Requested-With'],
 'allowedMethods' => ['*'], // ex: ['GET', 'POST', 'PUT', 'DELETE']
 'exposedHeaders' => [],
 'maxAge' => 0,
]

也可以引用env文件配置

return [
    /*
     |--------------------------------------------------------------------------
     | Laravel CORS
     |--------------------------------------------------------------------------
     |
     | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
     | to accept any value.
     |
     */
    'supportsCredentials' => false,
    'allowedOrigins' => explode(',', env('HTTP_ORIGIN', '*')),
    'allowedHeaders' => explode(',', env('HTTP_HEADERS', 'Origin,Content-Type,Cookie,Accept,SJ-Auth-Token')),
    'allowedMethods' => explode(',', env('HTTP_METHODS', '*')),
    'exposedHeaders' => [],
    'maxAge' => 0,
];

JSON对象使用 json_decode() 解码时慎转数组

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

$assoc 当该参数为 TRUE 时,将返回array 而非object 。
假设JSON对象为

{
  name: 'Empty Object'
    data: {}
}

经 json_decode(json,true) 转换

array(
    name => 'Empty Object',
    data => array()
)

此时的php数组将无法用json_encode()正确的转换为原本的JSON对象

{
  name: 'Empty Object'
    data: []
}

laravel 关于一个优雅的框架安装 (mac)

安装composer
准备好homebrew工具
brew install composer #安装composer
不是mac os 只要想办法安装上composer就可以继续

php54-mcrypt 模块
brew install homebrew/php/php54-mcrypt #安装
缺少该模块会导致报错

用composer安装laravel
composer create-project laravel/laravel –prefer-dist
完成后的记录
Generating autoload files
> php artisan clear-compiled
> php artisan optimize
Generating optimized class loader
Compiling common classes
> php -r “copy(‘.env.example’, ‘.env’);”
> php artisan key:generate
Application key [0DfkzQ8kYye9tBbtz8zi2xPkeEKfpwk9] set successfully.

laravel下的storage 和 vendor 目录要让服务器有写入的权限
chmod -R 777 storage
chmod -R 777 vendor

Apache 开启 mod_rewrite 模块
在 httpd.conf 中找到 #LoadModule rewrite_module 去掉注释