步骤一: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已配置成功