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以后提高了兩倍性能

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM