PHP

首页 -  PHP  -  Laravel通过Swoole提升性能

Laravel通过Swoole提升性能

Laravel通过Swoole提升性能

1、安装配置laravel

1.1、composer下载laravel

composer create-project --prefer-dist laravel/laravel blog "5.5.*"

1.2、给storage 目录和 bootstrap/cache 目录配置读写权限

chmod -R 777  storage
chmod -R 777  bootstrap/cache

1.3、配置.env文件的数据库信息

DB_HOST=服务器ip
DB_DATABASE=数据库名
DB_USERNAME=用户名
DB_PASSWORD=密码

2、安装配置nginx

2.1、安装nignx

Centos7Yum安装配置指定版本nginx

2.2、配置nginx

#强制跳转https
server {        listen
80;        server_name www.xxxxx.com;        rewrite ^(.*) https://$server_name$1 permanent;    
      } server {         listen
443;         ssl on;         server_name     www.xxxxx.com;         index index.html index.htm index.php;         root    /usr/share/nginx/html/code/blog/public/;         ssl_certificate   cert/xxxx.pem;         ssl_certificate_key  cert/xxxx.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;
     #优雅链接        location
/ {              try_files $uri $uri/ /index.php?$query_string;        }      #将php请求交给php-fpm处理         location ~ \.php {            fastcgi_index index.php;            #fastcgi_pass web_server;            fastcgi_pass 127.0.0.1:9000;            include      fastcgi_params;            set $path_info "";            set $real_script_name $fastcgi_script_name;            fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/code/xxxx/public/$real_script_name;            fastcgi_param SCRIPT_NAME $real_script_name;            fastcgi_param PATH_INFO $path_info;        } }
 

3、输入域名即可看到Laravel欢迎页面:

4、编译安装Swoole

git clone https://github.com/swoole/swoole-src.git && \cd swoole-src && \
phpize && \
./configure && \
make && make install

php.ini中添加extension=swoole.so 并重启php

 5、安装Laravels拓展包

直接在Laravel项目中集成Swoole,不用改底层代码

5.1、通过composer安装Laravels

composer require "hhxsv5/laravel-s:~3.0" -vvv

5.2、Laravel: 修改文件config/app.phpLaravel 5.5+支持包自动发现,可跳过这步

'providers' => [    //...
    Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],

5.3、发布配置文件(每次升级LaravelS后,建议重新发布一次配置文件

php artisan laravels publish

5.4、修改配置config/laravels.php:监听的IP、端口等,参考配置项

5.5、启动Laravels

在运行之前,请先仔细阅读:注意事项

php artisan laravels  start         //启动php artisan laravels  start   -d   //守护进程模式运行

5.6、配置Nginx,由laravels接管php-fpm处理php文件

gzip on;
gzip_min_length 1024;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
upstream laravels {
    # 通过 IP:Port 连接
    server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;
    keepalive 16;
}

server {
        listen 80;
        server_name www.xxxxx.com;
        rewrite ^(.*) https://$server_name$1 permanent;    }

server {
         listen 443;
         ssl on;
         server_name     www.xxxxx.com;
         index index.html index.htm index.php;
         root    /usr/share/nginx/html/code/blog/public/;
         ssl_certificate   cert/xxx.pem;
         ssl_certificate_key  cert/xxx.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;
         #关闭目录浏览
         autoindex off;

         # Nginx处理静态资源(建议开启gzip),LaravelS处理动态资源。
         location / {
                try_files $uri @laravels;
         }

         location @laravels {
         # proxy_connect_timeout 60s;
         # proxy_send_timeout 60s;
         # proxy_read_timeout 120s;
         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://laravels;     }

}

 经测试,ab压测同一个接口,使用Swoole以后提高了两倍性能


(0)
分享:

本文由:郁冬 博客园 作者:郁冬 博客园发表,转载请注明来源!

标签:

相关阅读