Skip to content

Files

Latest commit

b8f67c6 · Apr 18, 2023

History

History

web-skeleton

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 23, 2023
Aug 3, 2021
Sep 18, 2021
Aug 3, 2021
Aug 3, 2021
Aug 3, 2021
Apr 18, 2023
Aug 3, 2021
Aug 3, 2021
Aug 3, 2021
Apr 2, 2022
Apr 19, 2022
Feb 23, 2023

Web development skeleton

帮助你快速搭建 Web 项目骨架,并指导你如何使用该骨架的细节,骨架默认开启了 SQL、Redis 日志,压测前请先关闭 .envAPP_DEBUG

安装

需要先安装 Swoole 或者 WorkerMan

composer create-project --prefer-dist mix/web-skeleton web

快速开始

启动 cli-server 开发服务 (零依赖)

composer run-script --timeout=0 cliserver:start

启动 Swoole 多进程服务

composer run-script --timeout=0 swoole:start

启动 Swoole 协程服务

composer run-script --timeout=0 swooleco:start

启动 Swow 协程服务

需自行安装 composer require swow/swow:develop 注意:swow 不支持 PHP 8.0 以下版本

composer run-script --timeout=0 swow:start

启动 WorkerMan 多进程服务

composer run-script --timeout=0 workerman:start

执行脚本

  • composer run-script 命令中的 --timeout=0 参数是防止 composer 执行超时
  • composer.json 定义了命令执行脚本,对应上面的执行命令
"scripts": {
    "cliserver:start": "php -S localhost:8000 -t public",
    "swoole:start": "php bin/swoole.php",
    "swooleco:start": "php bin/swooleco.php",
    "workerman:start": "php bin/workerman.php start",
    "cli:clearcache": "php bin/cli.php clearcache"
}

当然也可以直接下面这样启动,效果是一样的,但是 scripts 能帮你记录到底有哪些可用的命令,同时在IDE中调试更加方便。

php bin/swoole.php start

编写一个 Web 页面

首先修改根目录 .env 文件的数据库信息

然后在 routes/index.php 定义一个新的路由

$vega->handle('/', [new Hello(), 'index'])->methods('GET');

路由里使用了 Hello 控制器,我们需要创建他

<?php

namespace App\Controller;

use Mix\Vega\Context;

class Hello
{

    /**
     * @param Context $ctx
     */
    public function index(Context $ctx)
    {
        $ctx->HTML(200, 'index', [
            'title' => 'Hello, World!'
        ]);
    }

}

views 目录创建 index.php 视图文件

<html>
<h1>
    <?= $title ?>
</h1>
</html>

重新启动服务器后方可测试新开发的接口

实际开发中使用 PhpStorm 的 Run 功能,只需要点击一下重启按钮即可

// 查找进程 PID
ps -ef | grep swoole

// 通过 PID 停止进程
kill PID

// 重新启动进程
composer run-script swoole:start

// curl 测试
curl http://127.0.0.1:9501/

使用容器中的对象

容器采用了一个简单的单例模式,你可以修改为更加适合自己的方式。

DB::instance()
RDS::instance()
Logger::instance()
Config::instance()

部署

  • CLI

线上部署启动时,修改 shell/server.sh 脚本中的绝对路径和参数

php=/usr/local/bin/php
file=/project/bin/swoole.php
cmd=start
numprocs=1

启动管理

sh shell/server.sh start
sh shell/server.sh stop
sh shell/server.sh restart

使用 nginx 或者 SLB 代理到服务器端口即可

server {
    server_name www.domain.com;
    listen 80; 
    root /data/project/public;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "keep-alive";
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        if (!-f $request_filename) {
             proxy_pass http://127.0.0.1:9501;
        }
    }
}
  • PHP-FPM

和 Laravel、ThinkPHP 部署方法完全一致,将 public/index.phpnginx 配置 rewrite 重写即可

server {
    server_name www.domain.com;
    listen 80;
    root /data/project/public;
    index index.html index.php;

    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1 last;
        }
    }

    location ~ ^(.+\.php)(.*)$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

License

Apache License Version 2.0, http://www.apache.org/licenses/