使用laravel-swoole加速laravel框架(详细步骤)

6 篇文章 0 订阅
3 篇文章 0 订阅

步骤一:linux服务器安装PHP swoole扩展(swoole不能再Windows上运行))
为什么要安装swoole扩展,因为laravel-swoole需要依赖这个扩展
1、执行命令(安装)
pecl install swoole
2、修改 php.ini 配置文件,找到自己的php.ini文件在最后添加这句(我的php.ini路径 /etc/php.ini)
extension=swoole.so
3、重启PHP服务
service php-fpm restart (使用其他命令也可以)
4、查看是否安装成功
php -m
在这里插入图片描述
步骤二:安装 laraveltw/laravel-swoole
在项目根目录下使用 composer require swooletw/laravel-swoole -vvv 安装,最好加上 -vvv 参数,因为安装很慢,会给人一种没有安装被卡主的感觉(自己就经历过)
最好别再本地执行或者在 PHPstorm 中执行。因为 windows 不支持 laravel-swoole, 会有各种问题。可以在虚拟机的项目根目录下执行(安装时需要耐心)。
1、执行命令
composer require swooletw/laravel-swoole -vvv (时间很长,至少我执行的时间很长)
2、然后,添加服务提供商(它支持包自动发现。如果您的Laravel版本高于5.5,请跳过此步骤。)
则将Service Provider添加到providers数组中config/app.php:
[
‘providers’ => [
SwooleTW \ Http \ LaravelServiceProvider :: class,
],
]
3、如果要更改默认配置,请运行以下命令以生成配置文件,swoole_http.php并swoole_websocket.php在目录中/config:
php artisan vendor:publish --tag=laravel-swoole
参数说明:
swoole_http.php
在这里插入图片描述
swoole_websocket.php
在这里插入图片描述
4、启动swoole
php artisan swoole:http start
在这里插入图片描述
在这里插入图片描述

5、配置nginx
文档给的配置说明
在这里插入图片描述
以下是我的配置,正常的你的nginx配置的基础上进行修改,添加上swoole的配置略作修改
map $http_upgrade $connection_upgrade {
default upgrade;
‘’ close;
}

server {
listen 443 ssl http2;
server_name zsy.hzyxhfp.com;
charset utf-8;
index index.html index.htm index.php;
root /www/develop/crown/public;
#root /www/develop/zhang/crown/public;

ssl_certificate   /etc/nginx/conf.d/letsencrypt/zsy.hzyxhfp.com.pem;
ssl_certificate_key /etc/nginx/conf.d/letsencrypt/zsy.hzyxhfp.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

**#swoole
location = /index.php {
# Ensure that there is no such file named “not_exists”
# in your “public” directory.
try_files /not_exists @swoole;
}
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set s u f f i x " " ; i f ( suffix ""; if ( suffix"";if(uri = /index.php) {
set s u f f i x ? suffix ? suffix?query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

    # IF https
    # proxy_set_header HTTPS "on";
    proxy_pass http://127.0.0.1:1215$suffix;
}

#swoole**

***#这个是原本的配置,已改为上面swoole配置
#location / {
#   try_files $uri $uri/ /index.php$is_args$args;
#}***



location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

error_log    /etc/nginx/logs/crown_hzyxhfp_com_error.log    error;
#error_page 404 /index.php;
#error_page 404 405 500 502 504 /test.php;
location ~ /\. {
    deny all;
}

location ~ .*\.(woff|js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    try_files $uri =404;
}

location ~ .*\.(woff|jpg|jpeg|png|gif|ico|css|js)$ {
    expires 1h;
    add_header Cache-Control public;
    add_header Pragma public;
    add_header Vary Accept-Encoding;
}

location ~ \.php(.*)$ {
    fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
}
location ~ /\.(?!well-known).* {
    deny all;
}

# security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;

}

配置完成冲去nginx
service nginx restart
6、使用swoole注意事项
在这里插入图片描述
7、测试,添加一个测试路由,需要重启swoole,否则修改的代码不生效

在这里插入图片描述
在这里插入图片描述
关闭swoole之后
在这里插入图片描述
重启swoole又正常了,说明swoole已配置成功

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是详细步骤: 1. 安装 swoole 扩展 在终端中运行以下命令进行 swoole 扩展的安装: ``` pecl install swoole ``` 2. 创建 Laravel 项目 使用以下命令在终端中创建 Laravel 项目: ``` composer create-project --prefer-dist laravel/laravel chat-app ``` 3. 安装 Laravel-Swoole 在终端中运行以下命令进行 Laravel-Swoole 的安装: ``` composer require swooletw/laravel-swoole ``` 然后,运行以下命令生成配置文件: ``` php artisan vendor:publish --tag=laravel-swoole ``` 4. 创建聊天控制器 在终端中运行以下命令创建聊天控制器: ``` php artisan make:controller ChatController ``` 在 `ChatController` 中添加以下方法: ```php public function index() { return view('chat'); } public function sendMessage(Request $request) { $message = $request->input('message'); // 发送消息给所有连接的客户端 app('swoole')->sendMessage($message); return response()->json(['status' => 'success']); } ``` `index` 方法用于返回聊天页面视图,`sendMessage` 方法用于接收客户端发送的消息并将其发送给所有已连接的客户端。 5. 创建聊天视图 在 `resources/views` 目录下创建 `chat.blade.php` 文件,添加以下代码: ```html <!DOCTYPE html> <html> <head> <title>Laravel Swoole Chat App</title> </head> <body> <h1>Welcome to Laravel Swoole Chat App</h1> <div id="messages"></div> <input type="text" id="message" placeholder="Type your message here..."> <button id="send">Send</button> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> var ws = new WebSocket('ws://' + window.location.hostname + ':9501'); ws.onmessage = function(event) { var message = event.data; var div = document.createElement('div'); div.innerHTML = message; document.getElementById('messages').appendChild(div); }; $('#send').click(function() { var message = $('#message').val(); ws.send(message); $('#message').val(''); }); </script> </body> </html> ``` 6. 设置路由 在 `routes/web.php` 文件中添加以下代码: ```php use App\Http\Controllers\ChatController; Route::get('/chat', [ChatController::class, 'index']); Route::post('/chat/send-message', [ChatController::class, 'sendMessage']); ``` 7. 启动服务器 在终端中运行以下命令启动服务器: ``` php artisan swoole:http start ``` 现在,你可以在浏览器中访问 `http://localhost:1215/chat` 查看聊天页面了。可以在多个浏览器窗口中打开该页面并尝试发送消息,消息将会实时显示在其他窗口中。 希望这些步骤可以帮助到你实现 Laravel8.5 swoole 实现网页聊天。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值