CentOS8搭建Web服务器笔记2 Swoole4+Laravel8+LaravelS3

运维 专栏收录该内容
7 篇文章 0 订阅

1. Swoole PHP 协程框架

Swoole 使 PHP 开发人员可以编写高性能高并发的 TCP、UDP、Unix Socket、HTTP、 WebSocket 等服务,让 PHP 不再局限于 Web 领域。

1.1 查询最新发布版本

https://github.com/swoole/swoole-src/releases
在这里插入图片描述

1.2 下载源码包

https://github.com/swoole/swoole-src/archive/v4.6.3.tar.gz

[root@VM-0-16-centos downloads]# wget -O swoole4.6.3.tar.gz https://github.com/swoole/swoole-src/archive/v4.6.3.tar.gz 
[root@VM-0-16-centos downloads]# ls
php-8.0.2  php-8.0.2.tar.gz  nginx-1.19.7  nginx-1.19.7.tar.gz  swoole4.6.3.tar.gz
  • 解压安装文件到downloads目录
    tar -zxvf /usr/local/downloads/swoole4.6.3.tar.gz -C /usr/local/downloads
[root@VM-0-16-centos downloads]# tar -zxvf /usr/local/downloads/swoole4.6.3.tar.gz -C /usr/local/downloads

1.3 安装依赖

yum install libcurl-devel glibc-headers gcc-c++ pcre-devel
yum install automake autoconf bison

1.4 编译安装

  • 编译生成配置命令configure
[root@VM-0-16-centos downloads]# cd /usr/local/downloads/swoole-src-4.6.3
[root@VM-0-16-centos swoole-src-4.6.3]# ls
clear.sh        CODE-STYLE.md  examples          include        mascot.png    src           tools
CMakeLists.txt  config.m4      ext-src           LICENSE        package.xml   SUPPORTED.md  travis
code_format.sh  CPPLINT.cfg    gdbinit           Makefile.frag  README-CN.md  tests
code_stats.sh   CREDITS        google-style.xml  make.sh        README.md     thirdparty

存在configure命令文件就跳过

[root@VM-0-16-centos swoole-src-4.6.3]# phpize
Configuring for:
PHP Api Version:         20200930
Zend Module Api No:      20200930
Zend Extension Api No:   420200930
[root@VM-0-16-centos swoole-src-4.6.3]# ls
autom4te.cache  code_stats.sh  configure.ac  gdbinit           make.sh       run-tests.php  tools
build           CODE-STYLE.md  CPPLINT.cfg   google-style.xml  mascot.png    src            travis
clear.sh        config.h.in    CREDITS       include           package.xml   SUPPORTED.md
CMakeLists.txt  config.m4      examples      LICENSE           README-CN.md  tests
code_format.sh  configure      ext-src       Makefile.frag     README.md     thirdparty

成功生成configure命令

  • 配置参数

[root@VM-0-16-centos swoole-src-4.6.3]# ./configure  \
 --enable-openssl  \
--enable-swoole-json  \
--enable-swoole-curl

参数详情参考官方说明
https://wiki.swoole.com/#/environment?id=%e7%bc%96%e8%af%91%e9%80%89%e9%a1%b9

  • 编译安装
    make && make install
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/downloads/swoole-src-4.6.3/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/
Installing header files:          /usr/local/php8/include/php/

查看安装的扩展文件

[root@VM-0-16-centos swoole-src-4.6.3]# cd /usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/
[root@VM-0-16-centos no-debug-non-zts-20200930]# ls
mysqli.so  opcache.so  pdo_mysql.so  swoole.so

1.5 配置php启用swoole扩展

1.5.1 查看 php.ini 位置

php -i | grep php.ini

[root@VM-0-16-centos ~]# php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/php8/etc
Loaded Configuration File => /usr/local/php8/etc/php.ini

1.5.1 配置php扩展

vi /usr/local/php8/etc/php.ini

Dynamic Extensions 配置项中加入extension=swoole.so

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename
;
; For example:
;
;   extension=mysqli
;
; When the extension library to load is not located in the default extension
; directory, You may specify an absolute path to the library file:
;
;   extension=/path/to/extension/mysqli.so
;
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
; 'extension='php_<ext>.dll') is supported for legacy reasons and may be
; deprecated in a future PHP major version. So, when it is possible, please
; move to the new ('extension=<ext>) syntax.
;
; Notes for Windows environments :
;
; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)
;   extension folders as well as the separate PECL DLL download (PHP 5+).
;   Be sure to appropriately set the extension_dir directive.
extension=swoole.so

1.5.3 查看php加载swoole扩展情况

  • 重启fpm
    systemctl restart php-fpm
  • 命令行查看swoole加载情况
    php -m | grep swoole
[root@VM-0-16-centos ~]# php -m | grep swoole
swoole

php已经成功加载swoole扩展

  • phpinfo页面查看swoole加载情况
    在这里插入图片描述

1.6 配置Nginx转发Swoole

1.6.1 查看nginx站点配置文件

[root@VM-0-16-centos ~]# find /etc/nginx/conf.d -name *.conf
/etc/nginx/conf.d/default.conf
[root@VM-0-16-centos ~]# vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

1.6.2 配置Nginx负载均衡与转发

1.6.2.1 Nginx负载均衡

upstream  swoole_http{
	#ip_hash;
	server  127.0.0.1:9501 weight=3 max_conns=2000;
	server  127.0.0.2:9501 weight=2 max_conns=2000 down;
	#server  127.0.0.3:9501 backup max_conns=1000;
}
  • weight
    权重数值越大,负载分配几率越大
    不设权重,默认把请求轮循负载到各个服务器
  • max_conns
    最大连接数
    限制服务器允许的最大连接数量,超过的连接不会分配,轮询分配到其他服务器。默认为0为不限制。
  • backup
    后备服务器
    只有其他所有swoole服务器都负载饱和或当机才被启用加与负载
  • down
    当机标记
    有服务器当机,nginx自动标记服务器,排除down服务器的负载分配
  • ip_hash
    哈希表分配负载,根据散列算法分配不同用户固定访问一个后端服务器,解决session一致问题
    有条件架构独立Redis服务器,缓存所有后端服务器的session,也能解决session一致问题
    ip_hash,轮询,权重三种分配方式不能同时生效,三选一
  • max_fails 单位时间内重试连接次数
  • fail_timeout 允许重试连接的单位时间和请求失败等待轮询的单位时间
    在单位周期fail_timeout内,重试连接达到max_fails次数,nginx把此服务器节点标记为宕机不可用(down),将连接转到其他服务器节点;
    有新的连接符合下一个周期(单位时间为fail_timeout)再一次去请求,判断是否连接是否成功,恢复此节点。

1.6.2.2 反向代理转发Swoole

  • PHP系统自带FPM模式的php请求配置
	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    # php-fpm 管理模式fastcgi处理php请求配置
	#location ~ \.php$ {
    #    root           /usr/share/nginx/html;
    #    fastcgi_pass   unix:/dev/shm/php-fpm.sock;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
  • Swoole 引擎的php请求配置
	# pass the PHP scripts to Swoole server listening on 127.0.0.1:9501
    # 反向代理转发 swoole引擎处理php请求配置
    location ~ \.php$ {
        proxy_http_version 1.1;
        proxy_set_header Connection "keep-alive";
        proxy_set_header X-Real-IP $remote_addr;
        # 转发请求到upstream节点
        proxy_pass http://swoole_http; 
        # 转发请求到IP:Port
        #proxy_pass http://127.0.0.1:9501; 
    }

1.6.2.3 测试Swoole http服务

[root@VM-0-16-centos ~]# mkdir /opt/swoole
[root@VM-0-16-centos ~]# cd /opt/swoole
[root@VM-0-16-centos swoole]# vi http_test.php
  • 创建swoole http服务器测试脚本
<?php
$http = new Swoole\Http\Server('0.0.0.0', 9501);
$http->on('Request', function ($request, $response) {
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end('<h1>这是由Swoole服务:<br>' . rand(1000, 9999) . '</h1>');
});
$http->start();
?>
  • 启动http服务器测试脚本
[root@VM-0-16-centos swoole]# php http_test.php
  • 浏览器访问
    在这里插入图片描述

1.6.2.4 Swoole http简单php请求处理功能

[root@VM-0-16-centos swoole]# vi http_server.php
  • 创建php请求处理服务器脚本
<?php
// 创建监听
$http = new swoole_http_server('0.0.0.0', 9501);
// 收到HTTP 请求,回调此函数
$http->on('request', function($request, $response) {
	// 时区设置
	date_default_timezone_set('PRC');
	// 命令行输出请求时间和IP
    echo "\n".date('Y-m-d H:i:s',time())." Receive from ".$request->server['remote_addr'];
    // 请求路径
    $pathInfo = $request->server['path_info'];
    // 运行当前http服务器脚本目录下的php
    //$file = __DIR__ . $pathInfo;
    //指定站点根目录
    $file = "/usr/share/html" . $pathInfo;
    echo "  page :".$pathInfo;
    //判断是否文件
    if(is_file($file)) {
        //判断文件后缀名
        if(pathinfo($pathInfo)['extension'] == 'php') {
        	// 处理php文件
            ob_start();
            	include $file;
            	$content = ob_get_contents();
            ob_end_clean();
            //输出到浏览器
            $response->end($content);
        } else {
            //处理其他文件
        }
    } else {
        $response->status(404);
        $response->end('php not found');
    }
});
echo 'start http server at '.date('Y-m-d H:i:s',time());
$http->start();
?>
  • 启动http服务器脚本
[root@VM-0-16-centos swoole]# php http_server.php
start http server at 2021-02-22 18:51:27
2021-02-23 02:51:33 Receive from 127.0.0.1  page :/info.php
  • 浏览器访问info.php返回的phpinfo()数据
    在这里插入图片描述

2. Laravel PHP Web开发框架

Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。

2.1 安装laravel

2.1.1 Composer依赖管理器

Composer 是 PHP 的一个依赖管理工具。它允许你申明项目所依赖的代码库,基于每个项目的基础上进行管理,在你项目目录中(例如 vendor)进行安装。默认情况下它不会在全局安装任何东西。

  • 全局安装

下载并移动到CentOS程序目录,重命名去扩展名方便调用

[root@VM-0-16-centos downloads]# curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...

Composer (version 2.0.9) successfully installed to: /usr/local/downloads/composer.phar
Use it: php composer.phar

[root@VM-0-16-centos downloads]# ls -sl
composer.phar  php-8.0.2  php-8.0.2.tar.gz  swoole4.6.3.tar.gz  swoole-src-4.6.3
[root@VM-0-16-centos downloads]# mv composer.phar /usr/local/bin/composer

2.1.2 安装Laravel框架安装器

2.1.2.1 全局安装laravel安装器

composer global require "laravel/installer"

[root@VM-0-16-centos downloads]# composer global require "laravel/installer"
Changed current directory to /root/.config/composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? y
Using version ^4.1 for laravel/installer
./composer.json has been created
Running composer update laravel/installer
Loading composer repositories with package information
Updating dependencies
Lock file operations: 12 installs, 0 updates, 0 removals
  - Locking laravel/installer (v4.1.1)
  - Locking psr/container (1.0.0)
  - Locking symfony/console (v5.2.3)
  - Locking symfony/polyfill-ctype (v1.22.1)
  - Locking symfony/polyfill-intl-grapheme (v1.22.1)
  - Locking symfony/polyfill-intl-normalizer (v1.22.1)
  - Locking symfony/polyfill-mbstring (v1.22.1)
  - Locking symfony/polyfill-php73 (v1.22.1)
  - Locking symfony/polyfill-php80 (v1.22.1)
  - Locking symfony/process (v5.2.3)
  - Locking symfony/service-contracts (v2.2.0)
  - Locking symfony/string (v5.2.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 12 installs, 0 updates, 0 removals
  - Downloading symfony/polyfill-php80 (v1.22.1)
  - Downloading symfony/process (v5.2.3)
  - Downloading symfony/polyfill-mbstring (v1.22.1)
  - Downloading symfony/polyfill-intl-normalizer (v1.22.1)
  - Downloading symfony/polyfill-intl-grapheme (v1.22.1)
  - Downloading symfony/polyfill-ctype (v1.22.1)
  - Downloading symfony/string (v5.2.3)
  - Downloading psr/container (1.0.0)
  - Downloading symfony/service-contracts (v2.2.0)
  - Downloading symfony/polyfill-php73 (v1.22.1)
  - Downloading symfony/console (v5.2.3)
  - Downloading laravel/installer (v4.1.1)
  - Installing symfony/polyfill-php80 (v1.22.1): Extracting archive
  - Installing symfony/process (v5.2.3): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.22.1): Extracting archive
  - Installing symfony/polyfill-intl-normalizer (v1.22.1): Extracting archive
  - Installing symfony/polyfill-intl-grapheme (v1.22.1): Extracting archive
  - Installing symfony/polyfill-ctype (v1.22.1): Extracting archive
  - Installing symfony/string (v5.2.3): Extracting archive
  - Installing psr/container (1.0.0): Extracting archive
  - Installing symfony/service-contracts (v2.2.0): Extracting archive
  - Installing symfony/polyfill-php73 (v1.22.1): Extracting archive
  - Installing symfony/console (v5.2.3): Extracting archive
  - Installing laravel/installer (v4.1.1): Extracting archive
6 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
10 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

2.1.2.2 配置运行环境变量

vim /etc/profile.d/laravel.sh

export PATH="$PATH:~/.config/composer/vendor/bin"

立即生效配置
source /etc/profile

2.2 创建Laravel项目

2.2.1 设置项目父目录

设置/home为项目父目录

[root@VM-0-16-centos downloads]# cd /home

2.2.2 使用Laravel安装器创建项目

在当前目录下创建myapp项目
laravel new myapp

[root@VM-0-16-centos home]# laravel new myapp

2.2.3 使用Composer管理器创建项目

2.2.3.1 创建项目

2.2.3.1.1 当前目录下创建myweb项目

composer create-project laravel/laravel myweb --prefer-dist

[root@VM-0-16-centos home]# composer create-project laravel/laravel myweb --prefer-dist
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? y
Creating a "laravel/laravel" project at "./myweb"
Installing laravel/laravel (v8.5.10)
  - Downloading laravel/laravel (v8.5.10)
  - Installing laravel/laravel (v8.5.10): Extracting archive
Created project in /home/myweb
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
Lock file operations: 105 installs, 0 updates, 0 removals
  - Locking asm89/stack-cors (v2.0.2)
  - Locking brick/math (0.9.2)
  - Locking dnoegel/php-xdg-base-dir (v0.1.1)
  - Locking doctrine/inflector (2.0.3)
  - Locking doctrine/instantiator (1.4.0)
  - Locking doctrine/lexer (1.2.1)
  - Locking dragonmantank/cron-expression (v3.1.0)
  - Locking egulias/email-validator (2.1.25)
  - Locking facade/flare-client-php (1.4.0)
  - Locking facade/ignition (2.5.13)
  - Locking facade/ignition-contracts (1.0.2)
  - Locking fakerphp/faker (v1.13.0)
  - Locking fideloper/proxy (4.4.1)
  - Locking filp/whoops (2.9.2)
  - Locking fruitcake/laravel-cors (v2.0.3)
  - Locking graham-campbell/result-type (v1.0.1)
  - Locking guzzlehttp/guzzle (7.2.0)
  - Locking guzzlehttp/promises (1.4.0)
  - Locking guzzlehttp/psr7 (1.7.0)
  - Locking hamcrest/hamcrest-php (v2.0.1)
  - Locking laravel/framework (v8.29.0)
  - Locking laravel/sail (v1.3.1)
  - Locking laravel/tinker (v2.6.0)
  - Locking league/commonmark (1.5.7)
  - Locking league/flysystem (1.1.3)
  - Locking league/mime-type-detection (1.7.0)
  - Locking mockery/mockery (1.4.2)
  - Locking monolog/monolog (2.2.0)
  - Locking myclabs/deep-copy (1.10.2)
  - Locking nesbot/carbon (2.45.1)
  - Locking nikic/php-parser (v4.10.4)
  - Locking nunomaduro/collision (v5.3.0)
  - Locking opis/closure (3.6.1)
  - Locking phar-io/manifest (2.0.1)
  - Locking phar-io/version (3.1.0)
  - Locking phpdocumentor/reflection-common (2.2.0)
  - Locking phpdocumentor/reflection-docblock (5.2.2)
  - Locking phpdocumentor/type-resolver (1.4.0)
  - Locking phpoption/phpoption (1.7.5)
  - Locking phpspec/prophecy (1.12.2)
  - Locking phpunit/php-code-coverage (9.2.5)
  - Locking phpunit/php-file-iterator (3.0.5)
  - Locking phpunit/php-invoker (3.1.1)
  - Locking phpunit/php-text-template (2.0.4)
  - Locking phpunit/php-timer (5.0.3)
  - Locking phpunit/phpunit (9.5.2)
  - Locking psr/container (1.0.0)
  - Locking psr/event-dispatcher (1.0.0)
  - Locking psr/http-client (1.0.1)
  - Locking psr/http-message (1.0.1)
  - Locking psr/log (1.1.3)
  - Locking psr/simple-cache (1.0.1)
  - Locking psy/psysh (v0.10.6)
  - Locking ralouphie/getallheaders (3.0.3)
  - Locking ramsey/collection (1.1.3)
  - Locking ramsey/uuid (4.1.1)
  - Locking sebastian/cli-parser (1.0.1)
  - Locking sebastian/code-unit (1.0.8)
  - Locking sebastian/code-unit-reverse-lookup (2.0.3)
  - Locking sebastian/comparator (4.0.6)
  - Locking sebastian/complexity (2.0.2)
  - Locking sebastian/diff (4.0.4)
  - Locking sebastian/environment (5.1.3)
  - Locking sebastian/exporter (4.0.3)
  - Locking sebastian/global-state (5.0.2)
  - Locking sebastian/lines-of-code (1.0.3)
  - Locking sebastian/object-enumerator (4.0.4)
  - Locking sebastian/object-reflector (2.0.4)
  - Locking sebastian/recursion-context (4.0.4)
  - Locking sebastian/resource-operations (3.0.3)
  - Locking sebastian/type (2.3.1)
  - Locking sebastian/version (3.0.2)
  - Locking swiftmailer/swiftmailer (v6.2.5)
  - Locking symfony/console (v5.2.3)
  - Locking symfony/css-selector (v5.2.3)
  - Locking symfony/deprecation-contracts (v2.2.0)
  - Locking symfony/error-handler (v5.2.3)
  - Locking symfony/event-dispatcher (v5.2.3)
  - Locking symfony/event-dispatcher-contracts (v2.2.0)
  - Locking symfony/finder (v5.2.3)
  - Locking symfony/http-client-contracts (v2.3.1)
  - Locking symfony/http-foundation (v5.2.3)
  - Locking symfony/http-kernel (v5.2.3)
  - Locking symfony/mime (v5.2.3)
  - Locking symfony/polyfill-ctype (v1.22.1)
  - Locking symfony/polyfill-iconv (v1.22.1)
  - Locking symfony/polyfill-intl-grapheme (v1.22.1)
  - Locking symfony/polyfill-intl-idn (v1.22.1)
  - Locking symfony/polyfill-intl-normalizer (v1.22.1)
  - Locking symfony/polyfill-mbstring (v1.22.1)
  - Locking symfony/polyfill-php72 (v1.22.1)
  - Locking symfony/polyfill-php73 (v1.22.1)
  - Locking symfony/polyfill-php80 (v1.22.1)
  - Locking symfony/process (v5.2.3)
  - Locking symfony/routing (v5.2.3)
  - Locking symfony/service-contracts (v2.2.0)
  - Locking symfony/string (v5.2.3)
  - Locking symfony/translation (v5.2.3)
  - Locking symfony/translation-contracts (v2.3.0)
  - Locking symfony/var-dumper (v5.2.3)
  - Locking theseer/tokenizer (1.2.0)
  - Locking tijsverkoyen/css-to-inline-styles (2.2.3)
  - Locking vlucas/phpdotenv (v5.3.0)
  - Locking voku/portable-ascii (1.5.6)
  - Locking webmozart/assert (1.9.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 105 installs, 0 updates, 0 removals
  - Downloading doctrine/inflector (2.0.3)
  - Downloading doctrine/lexer (1.2.1)
  - Downloading symfony/polyfill-ctype (v1.22.1)
  - Downloading webmozart/assert (1.9.1)
  - Downloading dragonmantank/cron-expression (v3.1.0)
  - Downloading symfony/polyfill-php80 (v1.22.1)
  - Downloading symfony/polyfill-mbstring (v1.22.1)
  - Downloading symfony/var-dumper (v5.2.3)
  - Downloading symfony/polyfill-intl-normalizer (v1.22.1)
  - Downloading symfony/polyfill-intl-grapheme (v1.22.1)
  - Downloading symfony/string (v5.2.3)
  - Downloading psr/container (1.0.0)
  - Downloading symfony/service-contracts (v2.2.0)
  - Downloading symfony/polyfill-php73 (v1.22.1)
  - Downloading symfony/console (v5.2.3)
  - Downloading psr/log (1.1.3)
  - Downloading monolog/monolog (2.2.0)
  - Downloading voku/portable-ascii (1.5.6)
  - Downloading phpoption/phpoption (1.7.5)
  - Downloading graham-campbell/result-type (v1.0.1)
  - Downloading vlucas/phpdotenv (v5.3.0)
  - Downloading symfony/css-selector (v5.2.3)
  - Downloading tijsverkoyen/css-to-inline-styles (2.2.3)
  - Downloading symfony/deprecation-contracts (v2.2.0)
  - Downloading symfony/routing (v5.2.3)
  - Downloading symfony/process (v5.2.3)
  - Downloading symfony/polyfill-php72 (v1.22.1)
  - Downloading symfony/polyfill-intl-idn (v1.22.1)
  - Downloading symfony/mime (v5.2.3)
  - Downloading symfony/http-foundation (v5.2.3)
  - Downloading symfony/http-client-contracts (v2.3.1)
  - Downloading psr/event-dispatcher (1.0.0)
  - Downloading symfony/event-dispatcher-contracts (v2.2.0)
  - Downloading symfony/event-dispatcher (v5.2.3)
  - Downloading symfony/error-handler (v5.2.3)
  - Downloading symfony/http-kernel (v5.2.3)
  - Downloading symfony/finder (v5.2.3)
  - Downloading symfony/polyfill-iconv (v1.22.1)
  - Downloading egulias/email-validator (2.1.25)
  - Downloading swiftmailer/swiftmailer (v6.2.5)
  - Downloading ramsey/collection (1.1.3)
  - Downloading brick/math (0.9.2)
  - Downloading ramsey/uuid (4.1.1)
  - Downloading psr/simple-cache (1.0.1)
  - Downloading opis/closure (3.6.1)
  - Downloading symfony/translation-contracts (v2.3.0)
  - Downloading symfony/translation (v5.2.3)
  - Downloading nesbot/carbon (2.45.1)
  - Downloading league/mime-type-detection (1.7.0)
  - Downloading league/flysystem (1.1.3)
  - Downloading league/commonmark (1.5.7)
  - Downloading laravel/framework (v8.29.0)
  - Downloading filp/whoops (2.9.2)
  - Downloading facade/ignition-contracts (1.0.2)
  - Downloading facade/flare-client-php (1.4.0)
  - Downloading facade/ignition (2.5.13)
  - Downloading fakerphp/faker (v1.13.0)
  - Downloading fideloper/proxy (4.4.1)
  - Downloading asm89/stack-cors (v2.0.2)
  - Downloading fruitcake/laravel-cors (v2.0.3)
  - Downloading psr/http-message (1.0.1)
  - Downloading psr/http-client (1.0.1)
  - Downloading ralouphie/getallheaders (3.0.3)
  - Downloading guzzlehttp/psr7 (1.7.0)
  - Downloading guzzlehttp/promises (1.4.0)
  - Downloading guzzlehttp/guzzle (7.2.0)
  - Downloading laravel/sail (v1.3.1)
  - Downloading nikic/php-parser (v4.10.4)
  - Downloading dnoegel/php-xdg-base-dir (v0.1.1)
  - Downloading psy/psysh (v0.10.6)
  - Downloading laravel/tinker (v2.6.0)
  - Downloading hamcrest/hamcrest-php (v2.0.1)
  - Downloading mockery/mockery (1.4.2)
  - Downloading nunomaduro/collision (v5.3.0)
  - Downloading phpdocumentor/reflection-common (2.2.0)
  - Downloading phpdocumentor/type-resolver (1.4.0)
  - Downloading phpdocumentor/reflection-docblock (5.2.2)
  - Downloading sebastian/version (3.0.2)
  - Downloading sebastian/type (2.3.1)
  - Downloading sebastian/resource-operations (3.0.3)
  - Downloading sebastian/recursion-context (4.0.4)
  - Downloading sebastian/object-reflector (2.0.4)
  - Downloading sebastian/object-enumerator (4.0.4)
  - Downloading sebastian/global-state (5.0.2)
  - Downloading sebastian/exporter (4.0.3)
  - Downloading sebastian/environment (5.1.3)
  - Downloading sebastian/diff (4.0.4)
  - Downloading sebastian/comparator (4.0.6)
  - Downloading sebastian/code-unit (1.0.8)
  - Downloading sebastian/cli-parser (1.0.1)
  - Downloading phpunit/php-timer (5.0.3)
  - Downloading phpunit/php-text-template (2.0.4)
  - Downloading phpunit/php-invoker (3.1.1)
  - Downloading phpunit/php-file-iterator (3.0.5)
  - Downloading theseer/tokenizer (1.2.0)
  - Downloading sebastian/lines-of-code (1.0.3)
  - Downloading sebastian/complexity (2.0.2)
  - Downloading sebastian/code-unit-reverse-lookup (2.0.3)
  - Downloading phpunit/php-code-coverage (9.2.5)
  - Downloading doctrine/instantiator (1.4.0)
  - Downloading phpspec/prophecy (1.12.2)
  - Downloading phar-io/version (3.1.0)
  - Downloading phar-io/manifest (2.0.1)
  - Downloading myclabs/deep-copy (1.10.2)
  - Downloading phpunit/phpunit (9.5.2)
  - Installing doctrine/inflector (2.0.3): Extracting archive
  - Installing doctrine/lexer (1.2.1): Extracting archive
  - Installing symfony/polyfill-ctype (v1.22.1): Extracting archive
  - Installing webmozart/assert (1.9.1): Extracting archive
  - Installing dragonmantank/cron-expression (v3.1.0): Extracting archive
  - Installing symfony/polyfill-php80 (v1.22.1): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.22.1): Extracting archive
  - Installing symfony/var-dumper (v5.2.3): Extracting archive
  - Installing symfony/polyfill-intl-normalizer (v1.22.1): Extracting archive
  - Installing symfony/polyfill-intl-grapheme (v1.22.1): Extracting archive
  - Installing symfony/string (v5.2.3): Extracting archive
  - Installing psr/container (1.0.0): Extracting archive
  - Installing symfony/service-contracts (v2.2.0): Extracting archive
  - Installing symfony/polyfill-php73 (v1.22.1): Extracting archive
  - Installing symfony/console (v5.2.3): Extracting archive
  - Installing psr/log (1.1.3): Extracting archive
  - Installing monolog/monolog (2.2.0): Extracting archive
  - Installing voku/portable-ascii (1.5.6): Extracting archive
  - Installing phpoption/phpoption (1.7.5): Extracting archive
  - Installing graham-campbell/result-type (v1.0.1): Extracting archive
  - Installing vlucas/phpdotenv (v5.3.0): Extracting archive
  - Installing symfony/css-selector (v5.2.3): Extracting archive
  - Installing tijsverkoyen/css-to-inline-styles (2.2.3): Extracting archive
  - Installing symfony/deprecation-contracts (v2.2.0): Extracting archive
  - Installing symfony/routing (v5.2.3): Extracting archive
  - Installing symfony/process (v5.2.3): Extracting archive
  - Installing symfony/polyfill-php72 (v1.22.1): Extracting archive
  - Installing symfony/polyfill-intl-idn (v1.22.1): Extracting archive
  - Installing symfony/mime (v5.2.3): Extracting archive
  - Installing symfony/http-foundation (v5.2.3): Extracting archive
  - Installing symfony/http-client-contracts (v2.3.1): Extracting archive
  - Installing psr/event-dispatcher (1.0.0): Extracting archive
  - Installing symfony/event-dispatcher-contracts (v2.2.0): Extracting archive
  - Installing symfony/event-dispatcher (v5.2.3): Extracting archive
  - Installing symfony/error-handler (v5.2.3): Extracting archive
  - Installing symfony/http-kernel (v5.2.3): Extracting archive
  - Installing symfony/finder (v5.2.3): Extracting archive
  - Installing symfony/polyfill-iconv (v1.22.1): Extracting archive
  - Installing egulias/email-validator (2.1.25): Extracting archive
  - Installing swiftmailer/swiftmailer (v6.2.5): Extracting archive
  - Installing ramsey/collection (1.1.3): Extracting archive
  - Installing brick/math (0.9.2): Extracting archive
  - Installing ramsey/uuid (4.1.1): Extracting archive
  - Installing psr/simple-cache (1.0.1): Extracting archive
  - Installing opis/closure (3.6.1): Extracting archive
  - Installing symfony/translation-contracts (v2.3.0): Extracting archive
  - Installing symfony/translation (v5.2.3): Extracting archive
  - Installing nesbot/carbon (2.45.1): Extracting archive
  - Installing league/mime-type-detection (1.7.0): Extracting archive
  - Installing league/flysystem (1.1.3): Extracting archive
  - Installing league/commonmark (1.5.7): Extracting archive
  - Installing laravel/framework (v8.29.0): Extracting archive
  - Installing filp/whoops (2.9.2): Extracting archive
  - Installing facade/ignition-contracts (1.0.2): Extracting archive
  - Installing facade/flare-client-php (1.4.0): Extracting archive
  - Installing facade/ignition (2.5.13): Extracting archive
  - Installing fakerphp/faker (v1.13.0): Extracting archive
  - Installing fideloper/proxy (4.4.1): Extracting archive
  - Installing asm89/stack-cors (v2.0.2): Extracting archive
  - Installing fruitcake/laravel-cors (v2.0.3): Extracting archive
  - Installing psr/http-message (1.0.1): Extracting archive
  - Installing psr/http-client (1.0.1): Extracting archive
  - Installing ralouphie/getallheaders (3.0.3): Extracting archive
  - Installing guzzlehttp/psr7 (1.7.0): Extracting archive
  - Installing guzzlehttp/promises (1.4.0): Extracting archive
  - Installing guzzlehttp/guzzle (7.2.0): Extracting archive
  - Installing laravel/sail (v1.3.1): Extracting archive
  - Installing nikic/php-parser (v4.10.4): Extracting archive
  - Installing dnoegel/php-xdg-base-dir (v0.1.1): Extracting archive
  - Installing psy/psysh (v0.10.6): Extracting archive
  - Installing laravel/tinker (v2.6.0): Extracting archive
  - Installing hamcrest/hamcrest-php (v2.0.1): Extracting archive
  - Installing mockery/mockery (1.4.2): Extracting archive
  - Installing nunomaduro/collision (v5.3.0): Extracting archive
  - Installing phpdocumentor/reflection-common (2.2.0): Extracting archive
  - Installing phpdocumentor/type-resolver (1.4.0): Extracting archive
  - Installing phpdocumentor/reflection-docblock (5.2.2): Extracting archive
  - Installing sebastian/version (3.0.2): Extracting archive
  - Installing sebastian/type (2.3.1): Extracting archive
  - Installing sebastian/resource-operations (3.0.3): Extracting archive
  - Installing sebastian/recursion-context (4.0.4): Extracting archive
  - Installing sebastian/object-reflector (2.0.4): Extracting archive
  - Installing sebastian/object-enumerator (4.0.4): Extracting archive
  - Installing sebastian/global-state (5.0.2): Extracting archive
  - Installing sebastian/exporter (4.0.3): Extracting archive
  - Installing sebastian/environment (5.1.3): Extracting archive
  - Installing sebastian/diff (4.0.4): Extracting archive
  - Installing sebastian/comparator (4.0.6): Extracting archive
  - Installing sebastian/code-unit (1.0.8): Extracting archive
  - Installing sebastian/cli-parser (1.0.1): Extracting archive
  - Installing phpunit/php-timer (5.0.3): Extracting archive
  - Installing phpunit/php-text-template (2.0.4): Extracting archive
  - Installing phpunit/php-invoker (3.1.1): Extracting archive
  - Installing phpunit/php-file-iterator (3.0.5): Extracting archive
  - Installing theseer/tokenizer (1.2.0): Extracting archive
  - Installing sebastian/lines-of-code (1.0.3): Extracting archive
  - Installing sebastian/complexity (2.0.2): Extracting archive
  - Installing sebastian/code-unit-reverse-lookup (2.0.3): Extracting archive
  - Installing phpunit/php-code-coverage (9.2.5): Extracting archive
  - Installing doctrine/instantiator (1.4.0): Extracting archive
  - Installing phpspec/prophecy (1.12.2): Extracting archive
  - Installing phar-io/version (3.1.0): Extracting archive
  - Installing phar-io/manifest (2.0.1): Extracting archive
  - Installing myclabs/deep-copy (1.10.2): Extracting archive
  - Installing phpunit/phpunit (9.5.2): Extracting archive
74 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
74 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan key:generate --ansi
Application key set successfully.
[root@VM-0-16-centos home]# ls
myweb

myweb项目创建完成!

2.2.3.1.1 设置storage目录权限

此步骤是必要条件!

chmod -R 777 /home/myweb/storage

2.2.3.2 解决问题

2.2.3.2.1 问题 require ext-fileinfo *
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - laravel/framework[v8.12.0, ..., 8.x-dev] require league/flysystem ^1.1 -> satisfiable by league/flysystem[1.1.0, ..., 1.x-dev].
    - league/flysystem[1.1.0, ..., 1.x-dev] require ext-fileinfo * -> it is missing from your system. Install or enable PHP's fileinfo extension.
    - Root composer.json requires laravel/framework ^8.12 -> satisfiable by laravel/framework[v8.12.0, ..., 8.x-dev].

To enable extensions, verify that they are enabled in your .ini files:
    - /usr/local/php8/etc/php.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
  • 编译安装fileinfo扩展
[root@VM-0-16-centos home]# find / -name fileinfo
/usr/local/downloads/php-8.0.2/ext/fileinfo
[root@VM-0-16-centos home]# cd /usr/local/downloads/php-8.0.2/ext/fileinfo
[root@VM-0-16-centos fileinfo]# ls
config.m4             CREDITS             fileinfo.c         libmagic         Makefile.frag
config.w32            data_file.c         fileinfo.stub.php  libmagic.patch   php_fileinfo.h
create_data_file.php  fileinfo_arginfo.h  generate_patch.sh  magicdata.patch  tests
[root@VM-0-16-centos fileinfo]# phpize && ./configure --with-php-config=/usr/local/php8/bin/php-config
[root@VM-0-16-centos fileinfo]# make && make install
  • 编辑php.ini启用fileinfo扩展
    vim /usr/local/php8/etc/php.ini
extension=fileinfo
2.2.3.2.2 问题 to retrieve a token. Token (hidden):
  • GitHub注册获取Token
  • https://github.com/settings/tokens
  • 登录或注册GitHub帐号
  • Personal Access Tokens页面点击Generate new token
  • Note 里面填写自定义令牌名称
  • 点击Generate token生成token
  • 复制token码
  • 设置composer全局token
    composer config --global github-oauth.github.com <token>
[root@VM-0-16-centos home]# composer config --global github-oauth.github.com abcdefghijk1234567

在这里插入图片描述

2.2.3.2.3 问题 Install of fakerphp/faker failed
Failed to clone https://github.com/FakerPHP/Faker.git, git was not found, check that it is ins  
  talled and in your PATH env. 
  • 延长处理超时时间
[root@VM-0-16-centos home]# composer config -g process-timeout 600
  • 配置国内镜像下载源
[root@VM-0-16-centos home]# composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
  • 重新创建项目
[root@VM-0-16-centos home]# rm -rf myweb
[root@VM-0-16-centos home]# composer create-project laravel/laravel myweb --prefer-dist

2.3 配置nginx启用laravel框架

  • 核心配置
	# laravel部署必要URL规则,解决除了根目录都报404 
	location / {
		try_files $uri $uri/ /index.php?$query_string;
	}
  • 详细配置注释
    vim /etc/nginx/conf.d/default.conf
server
{
	# 监听端口
	listen       80; 
	# 服务器域名
	server_name localhost; 
	# 指定首页文件
	index index.html index.htm index.php; 
	# 指定字符集
	charset utf-8; 
	
	# laravel部署项目入口目录public
	root /home/myweb/public; 
	# laravel部署必要URL规则,解决除了根目录都报404 
	location / {
		try_files $uri $uri/ /index.php?$query_string;
	}
	# laravel部署重写url规则
	if (!-e $request_filename)
	{
		rewrite ^/(.*)$ /index.php?/$1 last;
		break;
	}
    
	# 去除url末尾的斜杠,便于SEO
	if (!-d $request_filename)
	{
		rewrite ^/(.+)/$ /$1 permanent;
	}
	# 去除index action
	if ($request_uri ~* index/?$)
	{
		rewrite ^/(.*)/index/?$ /$1 permanent;
	}
	
	# 处理php请求
	location ~ \.php$ {
		fastcgi_pass	unix:/dev/shm/php-fpm.sock; 
		fastcgi_index  	index.php;
		fastcgi_param  	SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include			fastcgi_params;
	}

	# 禁用favicon.ico和robots.txt报错日志
	location = /favicon.ico { access_log off; log_not_found off; }
	location = /robots.txt { access_log off; log_not_found off; }
	
	# 错误转静态页
	error_page   500 502 503 504  /50x.html;
	location = /50x.html {
		root   /usr/share/nginx/html;
	}
	# 图片缓存有效期
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp3|wma)$
	{
		expires      7d;
	}
	# JS和CSS缓存有效期
	location ~ .*\.(js|css)$
	{
		expires      1h;
	}
	# 字体缓存有效期
	location ~ .*\.(eot|ttf|otf|woff|svg)$ {
	    expires 30d;
	    access_log off;
	}
}
  • 浏览器成功访问
    在这里插入图片描述

3. LaravelS集成

LaravelS是laravel的第三方swoole扩展包,快速集成 Swoole到 Laravel,减轻Swoole需要自行编写服务器脚本的工作,既用上Swoole异步高性能处理并发功能,又发挥出Laravel框架处理PHP业务便捷能力。

3.1 查看LaravelS最新版本

https://packagist.org/packages/hhxsv5/laravel-s
在这里插入图片描述

3.2 Composer 安装

  • 必须确保进入Laravel项目根目录,安装器将读取项目composer.json文件
[root@VM-0-16-centos ~]# cd /home/myweb
[root@VM-0-16-centos myweb]# ls -sl
total 360
  4 drwxr-xr-x  7 root root   4096 Feb 17 00:58 app
  4 -rwxr-xr-x  1 root root   1686 Feb 17 00:58 artisan
  4 drwxr-xr-x  3 root root   4096 Feb 17 00:58 bootstrap
  4 -rw-r--r--  1 root root   1685 Feb 24 03:47 composer.json
292 -rw-r--r--  1 root root 298884 Feb 24 03:47 composer.lock
  4 drwxr-xr-x  2 root root   4096 Feb 17 00:58 config
  4 drwxr-xr-x  5 root root   4096 Feb 17 00:58 database
  4 -rw-r--r--  1 root root    473 Feb 17 00:58 package.json
  4 -rw-r--r--  1 root root   1202 Feb 17 00:58 phpunit.xml
  4 drwxr-xr-x  2 root root   4096 Feb 17 00:58 public
  4 -rw-r--r--  1 root root   3780 Feb 17 00:58 README.md
  4 drwxr-xr-x  6 root root   4096 Feb 17 00:58 resources
  4 drwxr-xr-x  2 root root   4096 Feb 17 00:58 routes
  4 -rw-r--r--  1 root root    563 Feb 17 00:58 server.php
  4 drwxrwxrwx  5 root root   4096 Feb 17 00:58 storage
  4 drwxr-xr-x  4 root root   4096 Feb 17 00:58 tests
  4 drwxr-xr-x 46 root root   4096 Feb 24 03:47 vendor
  4 -rw-r--r--  1 root root    559 Feb 17 00:58 webpack.mix.js
[root@VM-0-16-centos myweb]# 
  • 安装laravel-s
    composer require "hhxsv5/laravel-s:~3.7.16" -vvv

确保你的composer.lock文件是在版本控制中

[root@VM-0-16-centos myweb]# composer require "hhxsv5/laravel-s:~3.7.16" -vvv
Reading ./composer.json (/home/myweb/composer.json)
Loading config file /root/.config/composer/config.json
Loading config file /root/.config/composer/auth.json
Loading config file ./composer.json (/home/myweb/composer.json)
Checked CA file /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem: valid
Executing command (/home/myweb): git branch -a --no-color --no-abbrev -v
Executing command (/home/myweb): git describe --exact-match --tags
Executing command (CWD): git --version
Executing command (/home/myweb): git log --pretty="%H" -n1 HEAD
Executing command (/home/myweb): hg branch
Executing command (/home/myweb): fossil branch list
Executing command (/home/myweb): fossil tag list
Executing command (/home/myweb): svn info --xml
Reading /root/.config/composer/composer.json
Loading config file /root/.config/composer/config.json
Loading config file /root/.config/composer/auth.json
Loading config file /root/.config/composer/composer.json (/root/.config/composer/composer.json)
Loading config file /root/.config/composer/auth.json
Reading /root/.config/composer/auth.json
Reading /home/myweb/vendor/composer/installed.json
Reading /root/.config/composer/vendor/composer/installed.json
Running 2.0.9 (2021-01-27 16:09:27) with PHP 8.0.2 on Linux / 4.18.0-193.6.3.el8_2.x86_64
... 此处省略过长安装信息
Downloading https://packagist.org/downloads/
[201] https://packagist.org/downloads/
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> post-autoload-dump: Illuminate\Foundation\ComposerScripts::postAutoloadDump
> post-autoload-dump: @php artisan package:discover --ansi
Executing command (CWD): '/usr/local/php8/bin/php' -d allow_url_fopen='1' -d disable_functions='' -d memory_limit='1536M' artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: hhxsv5/laravel-s
Discovered Package: laravel/sail
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
75 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

3.3 注册服务模块

Laravel 5.5+ 以上版本跳过此步骤,安装器自动寻找项目目录下composer.json注册。

修改Laravel工程根目录下config/app.php文件

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

3.4 发布laravelS配置

LaravelS 每次升级,都需重新发布配置到项目中
php artisan laravels publish

配置文件: config/laravels.php
二进制文件: bin/laravels bin/fswatch bin/inotify

[root@VM-0-16-centos myweb]# php artisan laravels publish
Copied file [/home/myweb/vendor/hhxsv5/laravel-s/config/laravels.php] To [/home/myweb/config/laravels.php]
Linked file [/home/myweb/vendor/hhxsv5/laravel-s/bin/laravels] To [/home/myweb/bin/laravels]
Linked file [/home/myweb/vendor/hhxsv5/laravel-s/bin/fswatch] To [/home/myweb/bin/fswatch]
Linked file [/home/myweb/vendor/hhxsv5/laravel-s/bin/inotify] To [/home/myweb/bin/inotify]

3.5 Laravel 和 LaravelS配置项

3.5.1 Laravel 环境变量 ( Laravel框架配置 )

(注意此处是Laravel框架配置,不是LaravelS扩展包配置)

进入Laravel 工程根目录下.env
vim /home/myweb/.env

LARAVELS_LISTEN_IP=localhost
LARAVELS_DAEMONIZE=true
  • LARAVELS_LISTEN_IP
    LaravelS 监听地址
    如果config/laravels.php中socket_type为SWOOLE_UNIX_STREAM
    监听改为socket文件路径,如
    LARAVELS_LISTEN_IP=unix:/dev/shm/laravels.sock
  • LARAVELS_DAEMONIZE
    后台方式运行LaravelS服务

3.5.2 LaravelS 配置文件( LaravelS扩展包配置 )

  • (注意此处是LaravelS扩展包配置)

Laravel工程根目录下config/laravels.php文件
vim /home/myweb/config/laravels.php

  • 监听配置
'listen_ip' => env('LARAVELS_LISTEN_IP', '127.0.0.1'),
'listen_port' => env('LARAVELS_LISTEN_PORT', 5200),
'socket_type' => defined('SWOOLE_SOCK_TCP') ? SWOOLE_SOCK_TCP : 1,

listen_ip 监听地址
设置默认地址127.0.0.1
env(‘LARAVELS_LISTEN_IP’, ‘127.0.0.1’), 优先读取判断Laravel的.env中是否设置了监听地址,如果没有使用这里设置的127.0.0.1地址

  • IPv4:
    “127.0.0.1” 监听本地地址
    “0.0.0.0” 监听所有地址
  • IPv6:
    "::1"监听本地地址
    "::"监听所有地址

listen_port 监听端口
设置默认端口5200
env(‘LARAVELS_LISTEN_PORT’, 5200),优先读取判断Laravel的.env中是否设置了监听地址,如果没有使用这里设置的127.0.0.1地址

socket_type 套接字类型
如果使用SOCK_UNIX_STREAM类型,监听设置为socket文件路径,如
unix:/dev/shm/laravels.sock

  • SWOOLE_SOCK_TCP: TCP
  • SWOOLE_SOCK_TCP6: TCP IPv6
  • SWOOLE_SOCK_UDP: UDP
  • SWOOLE_SOCK_UDP6: UDP IPv6
  • SWOOLE_UNIX_DGRAM: Unix socket dgram
  • SWOOLE_UNIX_STREAM: Unix socket stream
  • Enable SSL: $sock_type | SWOOLE_SSL.
  • swoole 配置
'swoole' => [
        'daemonize'          => env('LARAVELS_DAEMONIZE', false),
        'dispatch_mode'      => 2,
        'reactor_num'        => env('LARAVELS_REACTOR_NUM', function_exists('swoole_cpu_num') ? swoole_cpu_num() : 8),
        'worker_num'         => env('LARAVELS_WORKER_NUM', function_exists('swoole_cpu_num') ? swoole_cpu_num() : 8),
        //'task_worker_num'    => env('LARAVELS_TASK_WORKER_NUM', function_exists('swoole_cpu_num') ? swoole_cpu_num() * 2 : 8),
        'task_ipc_mode'      => 1,
        'task_max_request'   => env('LARAVELS_TASK_MAX_REQUEST', 100000),
        'task_tmpdir'        => @is_writable('/dev/shm/') ? '/dev/shm' : '/tmp',
        'max_request'        => env('LARAVELS_MAX_REQUEST', 100000),
        'open_tcp_nodelay'   => true,
        'pid_file'           => storage_path('laravels.pid'),
        'log_level'          => 4,
        'log_file'           => storage_path(sprintf('logs/swoole-%s.log', date('Y-m'))),
        'document_root'      => base_path('public'),
        'buffer_output_size' => 2 * 1024 * 1024,
        'socket_buffer_size' => 8 * 1024 * 1024,
        'package_max_length' => 4 * 1024 * 1024,
        'reload_async'       => true,
        'max_wait_time'      => 60,
        'enable_reuse_port'  => true,
        'enable_coroutine'   => false,
        'upload_tmp_dir'     => @is_writable('/dev/shm/') ? '/dev/shm' : '/tmp',
        'http_compression'   => false,
    ],
  • daemonize 守护进程
    设置 true 时,程序将转入后台作为守护进程长时间运行。
    如果不启用守护进程,当 ssh 终端退出后,程序将被终止运行。

  • 更多swoole参数设置参考 https://wiki.swoole.com/#/server/setting

3.6 配置nginx启用LaravelS

  • 复制nginx配置
[root@VM-0-16-centos myweb]# cd /etc/nginx/conf.d
[root@VM-0-16-centos conf.d]# cp default.conf laravels_nginx.conf
[root@VM-0-16-centos conf.d]# ls
default.conf  laravels_nginx.conf
  • 编辑配置
    vi /etc/nginx/conf.d/laravels_nginx.conf
[root@VM-0-16-centos conf.d]# vi laravels_nginx.conf
# 负载均衡
upstream laravels{
    #server localhost:5200 weight=5 max_fails=3 fail_timeout=30s;
    server unix:/dev/shm/laravels.sock weight=5 max_fails=3 fail_timeout=30s;
    keepalive 16;
}
# 服务器
server
{
        # 监听端口
        listen       80;
        # 服务器域名
        server_name localhost;
        # 指定首页文件
        index index.html index.htm index.php;
        # 指定字符集
        charset utf-8;
        # 根目录指向laravel项目入口目录public
        root /home/myweb/public;

        # laravel部署必要URL规则,解决除了根目录都报404
        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }
        # 根据laravel规则进行url重写
        if (!-e $request_filename)
        {
                rewrite ^/(.*)$ /index.php?/$1 last;
                break;
        }
    
        # 去除url末尾的斜杠,便于SEO
        if (!-d $request_filename)
        {
                rewrite ^/(.+)/$ /$1 permanent;
        }

        # 去除index action
        if ($request_uri ~* index/?$)
        {
                rewrite ^/(.*)/index/?$ /$1 permanent;
        }

        # 处理php请求
        location ~ \.php$ {
                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; 
        }

        # 禁用favicon.ico和robots.txt报错日志
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt { access_log off; log_not_found off; }

        # 错误转静态页
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/share/nginx/html;
        }
        # 图片缓存有效期
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp3|wma)$
        {
                expires      7d;
        }
        # JS和CSS缓存有效期
        location ~ .*\.(js|css)$
        {
                expires      1h;
        }
        # 字体缓存有效期
        location ~ .*\.(eot|ttf|otf|woff|svg)$ {
            expires 30d;
            access_log off;
        }
        location ~ /\.ht {
                deny all;
        }
}

3.7 启动LaravelS服务

  • 命令格式
    php bin/laravels {start|stop|restart|reload|info|help}

LaravelS启动时自动执行 php artisan laravels config 并生成相关文件

  • storage/laravels.conf LaravelS 的运行时配置文件
  • storage/laravels.pid Master 进程的 PID 文件
  • storage/laravels-timer-process.pid 定时器 Timer 进程的 PID 文件
  • storage/laravels-custom-processes.pid 所有自定义进程的 PID 文件
  • ssh 在线模式运行,TCP监听模式
[root@VM-0-16-centos myweb]# php bin/laravels start
 _                               _  _____ 
| |                             | |/ ____|
| |     __ _ _ __ __ ___   _____| | (___  
| |    / _` | '__/ _` \ \ / / _ \ |\___ \ 
| |___| (_| | | | (_| |\ V /  __/ |____) |
|______\__,_|_|  \__,_| \_/ \___|_|_____/ 
                                           
Speed up your Laravel/Lumen
>>> Components
+---------------------------+---------+
| Component                 | Version |
+---------------------------+---------+
| PHP                       | 8.0.2   |
| Swoole                    | 4.6.3   |
| LaravelS                  | 3.7.16  |
| Laravel Framework [local] | 8.29.0  |
+---------------------------+---------+
>>> Protocols
+-----------+--------+-------------------+-----------------------+
| Protocol  | Status | Handler           | Listen At             |
+-----------+--------+-------------------+-----------------------+
| Main HTTP | On     | Laravel Framework | http://127.0.0.1:5200 |
+-----------+--------+-------------------+-----------------------+
>>> Feedback: https://github.com/hhxsv5/laravel-s
[2021-02-23 20:38:08] [TRACE] Swoole is running, press Ctrl+C to quit.
  • 驻后台运行,Socket监听模式
[root@VM-0-16-centos myweb]# php bin/laravels start                             
 _                               _  _____ 
| |                             | |/ ____|
| |     __ _ _ __ __ ___   _____| | (___  
| |    / _` | '__/ _` \ \ / / _ \ |\___ \ 
| |___| (_| | | | (_| |\ V /  __/ |____) |
|______\__,_|_|  \__,_| \_/ \___|_|_____/ 
                                           
Speed up your Laravel/Lumen
>>> Components
+---------------------------+---------+
| Component                 | Version |
+---------------------------+---------+
| PHP                       | 8.0.2   |
| Swoole                    | 4.6.3   |
| LaravelS                  | 3.7.16  |
| Laravel Framework [local] | 8.29.0  |
+---------------------------+---------+
>>> Protocols
+-----------+--------+-------------------+------------------------------------+
| Protocol  | Status | Handler           | Listen At                          |
+-----------+--------+-------------------+------------------------------------+
| Main HTTP | On     | Laravel Framework | http://unix:/dev/shm/laravels.sock |
+-----------+--------+-------------------+------------------------------------+
>>> Feedback: https://github.com/hhxsv5/laravel-s
[2021-02-24 17:36:37] [TRACE] Swoole is running in daemon mode, see "ps -ef|grep laravels".

到此,Laravel项目站点myweb,成功启动Swoole异步多线程服务,代替了FPM进程管理服务。
在这里插入图片描述


本章完

CentOS8搭建Web服务器笔记1 Nginx+Php8基础环境
CentOS8搭建Web服务器笔记3 Mysql8+Redis6数据库

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值