依赖:
php >= 5.5.9 推荐PHP7+ (我选择的php7.1)
Swoole >= 1.7.19 从2.0.12开始不再支持PHP5 推荐4.2.3+ (我选择的4.4.8)
Laravel/Lumen >= 5.1 推荐5.6+ (我选择的5.5)
1. 安装swoole
目前官方最高版本为4.4.8 (截至到2019年10月末) 可查看所有版本:
https://github.com/swoole/swoole-src/releases
wget https://github.com/swoole/swoole-src/archive/v4.4.8.tar.gz
tar -zxf v4.4.8.tar.gz //解压:
phpize (ubuntu 没有安装phpize可执行命令:sudo apt-get install php-dev来安装phpize)
./configure --with-php-config=/usr/local/php/bin/php-config
make
sudo make install
- 安装laravels (之前需安装好laravel)
composer require "hhxsv5/laravel-s:~3.5.0" -vvv
修改config/app.php
'providers' => [
//...
Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],
发布配置文件: php artisan laravels publish 会生成 config/laravels.php
修改config/laravels.php 参数(如需)
3.配置nginx
贴一个nginx代理给laravels,并且开启websocket的demo
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream swoole {
# 通过 IP:Port 连接
server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;
# 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能
#server unix:/xxxpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s;
#server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s;
#server 192.168.1.2:5200 backup;
keepalive 16;
}
server
{
listen 80;
#listen [::]:80;
server_name laravels.dongshixiao.cn ;
index index.html index.htm index.php default.html default.htm default.php;
root /storage/wwwroot/你的项目路径;
# Nginx处理静态资源(建议开启gzip),LaravelS处理动态资源。
location / {
try_files $uri @laravels;
}
#include rewrite/laravel.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
# Http和WebSocket共存,Nginx通过location区分
# !!! WebSocket连接时路径为/ws
# Javascript: var ws = new WebSocket("ws://laravels.com/ws");
location =/ws {
# proxy_connect_timeout 60s;
# proxy_send_timeout 60s;
# proxy_read_timeout:如果60秒内被代理的服务器没有响应数据给Nginx,那么Nginx会关闭当前连接;同时,Swoole的心跳设置也会影响连接的关闭
# proxy_read_timeout 60s;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header Server-Protocol $server_protocol;
proxy_set_header Server-Name $server_name;
proxy_set_header Server-Addr $server_addr;
proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://swoole;
}
location @laravels {
# proxy_connect_timeout 60s;
# proxy_send_timeout 60s;
# proxy_read_timeout 60s;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header Server-Protocol $server_protocol;
proxy_set_header Server-Name $server_name;
proxy_set_header Server-Addr $server_addr;
proxy_set_header Server-Port $server_port;
proxy_pass http://swoole;
}
#access_log /home/wwwlogs/xxxx.log;
}
- 安装redis 安装时更新到5.0.5 (截至到2019年10月末)
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make
src/redis-server //开启服务
src/redis-cli //打开客户端
测试
redis> set foo bar
OK
redis> get foo
"bar"
远程连接:
配置redis.conf
将 bind 127.0.0.1 使用#注释掉,改为# bind 127.0.0.1(bind配置的是允许连接的ip,默认只允许本机连接;若远程连接需注释掉,或改为0.0.0.0)
将 protected-mode yes 改为 protected-mode no(3.2之后加入的新特性,目的是禁止公网访问redis cache,增强redis的安全性)
将 requirepass foobared 注释去掉,foobared为密码,也可修改为别的值(可选,建议设置)
启动redis,并指定配置文件 ./redis-server ../redis.conf
客户端连接: ./redis-cli 之后 auth 密码(没有密码可不需要)
- 让laravel使用redis
A. 安装 phpredis: pecl install redis // 之后php -m 查看是否有redis扩展
B. 使用composer require predis/predis // 包
以上两种二选一 推荐A