最近在研究laravel+swoole+nginx的环境的搭建。本文主要记录下过程中遇到的问题。
Swoole简介:
网上可以查到很多的相关资料,在此不再赘述,只说它是一个PHP的扩展。
Swoole扩展的安装
Linux 安装:
Linux的安装可以参考官方的WIKI
安装swoole:
pecl install swoole
php.ini增加swoole:
extension=swoole.so
Mac安装:
pecl安装:
pecl install swoole 会报错,安装失败
需源码安装,步骤如下
pecl download swoole
tar -xzvf swoole-4.3.0.tgz
cd swoole-4.3.0
phpize
./configure --enable-openssl --enable-http2 --enable-sockets --enable-mysqlnd -with-openssl-dir=/usr/local/Cellar/openssl/1.0.2q/
make clear && make && sudo make install
问题: thirdparty目录下multipart_parser.h、swoole_http_parser.h、php_streams_int.h等文件缺失。此时需要去git上对应的文件下载下来。地址:https://github.com/swoole/swoole-src/
后续同源码安装:
cd swoole-src
phpize
./configure --enable-openssl --enable-http2 --enable-sockets --enable-mysqlnd -with-openssl-dir=/usr/local/Cellar/openssl/1.0.2q/
make clear && make && sudo make install
swooletw/laravel-swoole插件安装
安装 :
composer install swooletw/laravel-swoole
#config/app中providers中增加
SwooleTW\Http\LaravelServiceProvider::class,
#config下生成swoole_http.conf和swoole_websocket.php以及routes下websocket.php
php artisan vendor:publish --tag=laravel-swoole
问题
Linx下可正常访问,如果需要后台以守护进程方式运行,需将swoole_http中的daemonize值设置为true。另本地开发需将host改为0.0.0.0。这些配置正常应该在.env文件中进行配置!!
-Mac OS 系统下你首先会听到自己的Mac本风扇的呼啸声。
究其原因当然是失败了,虽说在运行php artisan swoole:http start显示本地http正常启动。实际是swoole的进程在无限重启(ps aux | grep swoole 可验证 )。在storage/logs/swoole_http.log文件中可以看到一下错误:
WARNING swManager_check_exit_status: worker#2[pid=41017] abnormal exit, status=0, signal=11
A bug occurred in Swoole-v4.3.0, please report it.
The Swoole developers probably don't know about it,
and unless you report it, chances are it won't be fixed.
You can read How to report a bug doc before submitting any bug reports:
>> https://github.com/swoole/swoole-src/issues/2000
Please do not send bug reports in the mailing list or personal letters.
The issue page is also suitable to submit feature requests.
我会跟踪查看解决方式。
该问题已解决:
PHP配置文件php.ini中配置pcre.jit=0;
Nginx 配置
server {
listen 80;
server_name **.com;
root 项目目录;
index index.html index.htm index.php;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
# 无需用到 HTTPS 故注释
# listen 443 ssl http2;
# ssl_certificate /etc/nginx/ssl/xxx.com.crt;
# ssl_certificate_key /etc/nginx/ssl/xxx.com.key;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /usr/local/var/log/www-log/blog/blog-access.log;
error_log /usr/local/var/log/www-log/blog/blog-error.log error;
# 日志,指定路径后可选开启。末尾值可选 error|notice|info
# error_log /var/log/nginx/xxx.com-error.log error;
sendfile off;
location = /index.php {
try_files /not_exists @swoole;
}
location / {
try_files $uri $uri @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix "/";
}
proxy_set_header Host $host;
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;
proxy_pass http://127.0.0.1:1215$suffix;
}
location ~* \.(ico|jp?g|gif|png|bmp|swf|flv)(\?[0-9]?$) {
expires 2d;
log_not_found off;
access_log off;
}
location ~* \.(js|css)$ {
expires 1d;
log_not_found off;
access_log off;
}
location = /(favicon.ico|robots.txt) {
access_log off;
log_not_found off;
}
location ~ ^/images/.*\.(php|php5|php7|.sh|.p1|.py)$ {
deny all;
}
location ~ ^/static/.*\.(php|php5|php7|.sh|.p1|.py)$ {
deny all;
}
location ~* ^/data/.*\.(php|php5|php7|.sh|.p1|.py)$ {
deny all;
}
location ~* ^/data/.*\.(html|lock|csv|htm|js|scss)$ {
deny all;
}
location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$ {
deny all;
}
location ~ ^/WEB-INF/ {
deny all;
}
location ~ ^/storage/ {
deny all;
}
location = /robots.txt{
if ($http_user_agent !~* "spider|bot|Python-urllib|pycurl") {
return 403;
}
}
location ~ /\.ht {
deny all;
}
}