依賴:
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可執(zhí)行命令: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,
],
發(fā)布配置文件: php artisan laravels publish 會生成 config/laravels.php
修改config/laravels.php 參數(shù)(如需)
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處理靜態(tài)資源(建議開啟gzip),LaravelS處理動態(tài)資源。
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區(qū)分
# !!! 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秒內(nèi)被代理的服務(wù)器沒有響應(yīng)數(shù)據(jù)給Nginx,那么Nginx會關(guān)閉當(dāng)前連接;同時,Swoole的心跳設(shè)置也會影響連接的關(guān)閉
# 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 //開啟服務(wù)
src/redis-cli //打開客戶端
測試
redis> set foo bar
OK
redis> get foo
"bar"
遠(yuǎn)程連接:
配置redis.conf
將 bind 127.0.0.1 使用#注釋掉,改為# bind 127.0.0.1(bind配置的是允許連接的ip,默認(rèn)只允許本機(jī)連接;若遠(yuǎn)程連接需注釋掉,或改為0.0.0.0)
將 protected-mode yes 改為 protected-mode no(3.2之后加入的新特性,目的是禁止公網(wǎng)訪問redis cache,增強(qiáng)redis的安全性)
將 requirepass foobared 注釋去掉,foobared為密碼,也可修改為別的值(可選,建議設(shè)置)
啟動redis,并指定配置文件 ./redis-server ../redis.conf
客戶端連接: ./redis-cli 之后 auth 密碼(沒有密碼可不需要)
- 讓laravel使用redis
A. 安裝 phpredis: pecl install redis // 之后php -m 查看是否有redis擴(kuò)展
B. 使用composer require predis/predis // 包
以上兩種二選一 推薦A