# hyperf-admin **Repository Path**: wei-kongqin/hyperf-admin ## Basic Information - **Project Name**: hyperf-admin - **Description**: hyperf+vue后台 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-28 - **Last Updated**: 2025-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Hyperf 管理系统 ## 简介 这是一个基于 [Hyperf](https://hyperf.wiki) 框架的管理系统骨架应用,适用于快速搭建后台管理系统。该系统包含管理员管理、角色管理、权限管理、菜单管理、日志记录等基础功能,适合用于企业级后台系统的开发。 前端使用了 [Vue Admin](https://github.com/PanJiaChen/vue-admin) 模板,后端基于 Hyperf 框架实现,前后端分离,便于维护和扩展。 ## 功能模块 - **管理员管理**:支持管理员信息的增删改查、禁用、分配角色等。 - **角色管理**:支持角色的创建、编辑、删除、权限分配。 - **权限管理**:支持权限的创建、编辑、删除。 - **菜单管理**:支持菜单的增删改查、权限绑定。 - **操作日志**:记录管理员的操作行为。 - **RBAC 权限模型**:基于角色的权限控制,支持细粒度权限管理。 - **Token 认证**:使用 `MD5Token` 实现管理员登录认证和 Token 管理。 ## 技术架构 - **后端**:使用 Hyperf 框架,基于 Swoole 的高性能 PHP 协程服务。 - **前端**:使用 Vue.js + Element UI 搭建的后台管理系统模板。 - **数据库**:MySQL,使用 Hyperf ORM 操作数据库。 - **缓存**:使用 Redis 存储 Token 和权限信息。 - **权限控制**:基于中间件和注解实现权限验证。 ## 安装与部署 ### 环境要求 - PHP >= 8.1 - Swoole 扩展 >= 5.0 或 Swow 扩展 >= 1.3 - Redis 扩展 - MySQL 扩展(如使用数据库) - Docker(可选) ### 安装步骤 1. **克隆项目** ```bash git clone https://gitee.com/wei-kongqin/hyperf-admin.git cd hyperf-admin ``` 2. **安装依赖** ```bash composer install ``` 3. **配置数据库和 Redis** 修改 `config/autoload/databases.php` 和 `config/autoload/redis.php` 文件,配置数据库和 Redis 连接信息。 4. **导入数据库** 使用 `app/Databases/*.sql` 文件导入数据库结构。 5. **启动服务** ```bash php bin/hyperf.php start ``` 6. **启动前端** 进入 `vue-admin` 目录,安装依赖并启动开发服务器: ```bash cd vue-admin npm install npm run dev ``` 访问 `http://localhost:9501` 即可进入后台管理系统。 ## 使用说明 ### 管理员登录 - 登录接口:`POST /admin/auth/login` - 请求参数: - `username` - `password` 登录成功后会返回 Token,后续请求需携带该 Token。 ### 权限控制 - 使用 `VerifyTokenMiddleware` 验证 Token。 - 使用 `PermissionMiddleware` 控制接口权限。 - 权限定义在 `app/Constants/ErrorCode.php` 中。 ### 操作日志 - 所有管理员操作都会通过 `ActionLogListener` 记录到数据库。 - 日志接口:`GET /admin/logs` ### Token 管理 - 使用 `MD5Token` 类实现 Token 的生成、验证、刷新、注销等操作。 - Token 信息存储在 Redis 中,支持 Token 过期、刷新、多设备登录控制等。 ## 项目结构 ### 后端结构 ``` app/ ├── Controller/ # 控制器 │ └── Admin/ # 管理后台控制器 ├── Logic/ # 业务逻辑 │ └── Admin/ # 管理后台业务逻辑 ├── Model/ # 数据模型 ├── Service/ # 服务类 ├── Constants/ # 常量定义 ├── Exception/ # 异常处理 ├── Middleware/ # 中间件 ├── Listener/ # 事件监听器 ├── Utils/ # 工具类 ├── Event/ # 自定义事件 ``` ### 前端结构 ``` vue-admin/ ├── src/ │ ├── api/ # 接口请求 │ ├── components/ # 公共组件 │ ├── layout/ # 页面布局 │ ├── router/ # 路由配置 │ ├── store/ # Vuex 状态管理 │ ├── views/ # 页面视图 │ └── utils/ # 工具类 ``` ## 开发规范 - 使用 PSR-12 编码规范。 - 使用 Hyperf 的依赖注入和 AOP 特性。 - 使用 Redis 缓存 Token 和权限信息。 - 使用中间件进行权限控制和 Token 验证。 - 使用事件监听器记录操作日志。 ## 扩展建议 - 可以扩展 `app/Service` 中的服务类,实现更多业务逻辑。 - 可以在 `vue-admin/src/views` 中添加新的管理页面。 - 可以通过 `app/Logic/Admin/PermissionsLogic.php` 扩展权限控制逻辑。 ## 贡献指南 欢迎提交 Pull Request,贡献代码。请遵循以下规范: - 提交前确保代码通过 PHPStan 和 PHPUnit 测试。 - 保持代码简洁,符合 PSR-12 规范。 - 提交时附带清晰的 commit message。 ## 许可证 本项目使用 [MIT License](https://opensource.org/licenses/MIT) 开源协议。 # 新购服务器部署 ### 宝塔海外版安装: ``` URL=https://www.aapanel.com/script/install_6.0_en.sh && if [ -f /usr/bin/curl ];then curl -ksSO "$URL" ;else wget --no-check-certificate -O install_6.0_en.sh "$URL";fi;bash install_6.0_en.sh 66959f96 ``` ### 安装docker: - 由于apt官方库里的docker版本可能比较旧,所以先卸载可能存在的旧版本: ``` sudo apt-get remove docker docker-engine docker-ce docker.io ``` - 更新apt包索引: ``` sudo apt-get update ``` - 安装以下包以使apt可以通过HTTPS使用存储库(repository): ``` sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common ``` - 添加Docker官方的GPG密钥: ``` curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - ``` - 使用下面的命令来设置stable存储库: ``` sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" ``` - 再更新一下apt包索引: ``` sudo apt-get update ``` - 安装最新版本的Docker CE: ``` sudo apt install docker.io ``` - 查看docker服务是否启动: ``` systemctl status docker ``` - 若未启动,则启动docker服务: ``` sudo systemctl start docker ``` - 加入开机自启: ``` systemctl enable docker.service ``` - 经典的hello world: ``` sudo docker run hello-world ``` ### 由于docker默认安装路径是/var/lib/docker下,占用磁盘内存较大,购买了新云盘后,如需更换安装目录,请按照以下步骤: ### #### 新购云盘尚未挂载,先进行挂载步骤: #### - 查看硬盘信息 ``` fdisk -l ``` - 输入 ``` fdisk /dev/vdb ``` - 然后根据下图的提示,输入n,p,1,回车,回车,wq,保存退出。 ``` mkfs.ext3 /dev/vdb1 echo '/dev/vdb1 /www ext3 defaults 0 0' >> /etc/fstab mkdir -p /www mount /dev/vdb1 /www ``` #### 云盘已挂载www目录,直接迁移安装目录: #### - 运行以下命令查看root安装路径 ``` docker info ``` - 停止docker运行 ``` systemctl stop docker.socket ``` - 然后移动整个/var/lib/docker目录到目的路径: ``` mv /var/lib/docker /www/docker ``` - 软连接 ``` ln -s /www/docker /var/lib/docker ``` - 最后启动docker ``` systemctl start docker systemctl daemon-reload ``` # 站点部署相关 - docker创建网络 ```gherkin docker network create --subnet=172.22.0.0/16 znet ``` - hyperf项目启动容器: ```gherkin docker run -d --name {{容器名称,例:project}} -v /www/wwwroot/{{项目}}:/www/project -v /mnt:/mnt -p 9501:9501 -it --privileged -u root --network znet --ip {{指定IP,例:172.22.0.2}} -e TZ=Asia/Shanghai --entrypoint /bin/sh hyperf/hyperf:8.1-alpine-v3.18-swoole ``` ```gherkin docker exec -it {{容器名称,例:project}} bash cd /www/project composer update cp .env.example .env nohup php bin/hyperf.php start > /dev/null 2>&1 & #守护进程后台启动 php bin/hyperf.php start #调试模式 ``` - laravel项目启动容器: ```gherkin cd /www/wwwroot/{{项目名称,例:project}} docker build -t {{镜像名,例:project}} . docker run -d -p 81:80 -p 9000:9000 -p 4403:443 --name {{容器名,例:project}} --network znet --ip {{指定IP,例:172.22.0.2}} -e TZ=Asia/Shanghai -v /www/wwwroot/{{项目名称,例:project}}:/app {{镜像名,例:project}} ``` - 反向代理形式nginx配置(适用于docker部署): ```gherkin log_format {{info日志变量名}} '"$remote_addr" - "$remote_user" [$time_local] Url:"$request" RequestId:"$request_id" Status:"$status" Size:"$body_bytes_sent" Referer:"$http_referer" "$http_user_agent" - "$scheme" "$http_x_forwarded_for" - "$upstream_addr" "$upstream_status" - ResponseTime:"$upstream_response_time" - RequestTime:"$request_time" - Accept-Language:"$http_accept_language"'; log_format {{post日志变量名}} escape=json '"$remote_addr" - "$remote_user" [$time_local] "$request" Status:"$status" - RequestId:"$request_id" - RequestBody:"$request_body"'; server { listen 80; listen 443 ssl http2; server_name {{域名}}; index index.php index.html index.htm default.php default.htm default.html; root {{项目路径}}; #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 ssl_certificate /www/server/panel/vhost/cert/{{域名}}/fullchain.pem; ssl_certificate_key /www/server/panel/vhost/cert/{{域名}}/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; error_page 497 https://$host$request_uri; #error_page 404/404.html; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-00.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/{{域名}}.conf; #REWRITE-END #### 真实IP ##### set_real_ip_from 0.0.0.0/0; #如果不确定真实上游IP段,只能指定全部IP范围 real_ip_header X-Forwarded-For; real_ip_recursive off; #取信任X-Forwarded-For数组中,信任IP提供的最后一个IP #real_ip_recursive on; #取信任X-Forwarded-For数组中,信任IP以外的第一个IP #### 异常处理 ##### include comm-header-handler.conf; include comm-error-json.conf; #### 公共 ##### include comm-cors.conf; location ~* \.(php|jsp|cgi|asp|aspx)$ { proxy_pass http://127.0.0.1:9527;#代理转发docker内网访问的项目 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; #### 公共 ##### include comm-header-handler.conf; include comm-cors.conf; } location / { proxy_pass http://127.0.0.1:9527;#代理转发docker内网访问的项目 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; add_header X-Cache $upstream_cache_status; ### 所有location 里面都要加这段 #### 公共 ##### include comm-header-handler.conf; include comm-cors.conf; ### 所有location 里面都要加这段 } location /api { proxy_pass http://127.0.0.1:9527;#代理转发docker内网访问的项目 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; add_header X-Cache $upstream_cache_status; ### 所有location 里面都要加这段 #### 公共 ##### include comm-header-handler.conf; include comm-cors.conf; ### 所有location 里面都要加这段 } #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log off; access_log /dev/null; } location ~ .*\.(js|css)?$ { expires 12h; error_log off; access_log /dev/null; } error_log /www/wwwlogs/{{域名}}.error.log; access_log /www/wwwlogs/{{域名}}.more2.log {{自定义变量命名:gateway_log}}; access_log /www/wwwlogs/{{域名}}.post.log {{自定义变量命名:gateway_log_postdata}}; } ``` - nginx配置(适合PFM框架): ```gherkin log_format {{info日志变量名}} '"$remote_addr" - "$remote_user" [$time_local] Url:"$request" RequestId:"$request_id" Status:"$status" Size:"$body_bytes_sent" Referer:"$http_referer" "$http_user_agent" - "$scheme" "$http_x_forwarded_for" - "$upstream_addr" "$upstream_status" - ResponseTime:"$upstream_response_time" - RequestTime:"$request_time" - Accept-Language:"$http_accept_language"'; log_format {{post日志变量名}} escape=json '"$remote_addr" - "$remote_user" [$time_local] "$request" Status:"$status" - RequestId:"$request_id" - RequestBody:"$request_body"'; server { listen 80; listen 443 ssl http2; server_name {{域名}}; index index.php index.html index.htm default.php default.htm default.html; root {{项目路径}}; #SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules #error_page 404/404.html; ssl_certificate /etc/nginx/vhost/cert/{{域名}}/fullchain.pem; ssl_certificate_key /etc/nginx/vhost/cert/{{域名}}/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2; ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; add_header Strict-Transport-Security "max-age=31536000"; error_page 497 https://$host$request_uri; #SSL-END #### 真实IP ##### set_real_ip_from 0.0.0.0/0; #如果不确定真实上游IP段,只能指定全部IP范围 real_ip_header X-Forwarded-For; real_ip_recursive off; #取信任X-Forwarded-For数组中,信任IP提供的最后一个IP #real_ip_recursive on; #取信任X-Forwarded-For数组中,信任IP以外的第一个IP #### 异常处理 ##### include comm-header-handler.conf; include comm-error-json.conf; #### 公共 ##### include comm-cors.conf; # Forbidden files or directories location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) { return 404; } # Directory verification related settings for one-click application for SSL certificate location ~ \.well-known{ allow all; } #Prohibit putting sensitive files in certificate verification directory if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) { return 403; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log /dev/null; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; error_log /dev/null; access_log off; } location / { try_files $uri $uri/ /index.php$is_args$query_string; } error_log /www/wwwlogs/{{域名}}.error.log; access_log /www/wwwlogs/{{域名}}.more2.log {{自定义变量命名:gateway_log}}; access_log /www/wwwlogs/{{域名}}.post.log {{自定义变量命名:gateway_log_postdata}}; } ``` - comm-cors.conf ```gherkin #所有location均需要包含此配置 add_header Access-Control-Allow-Origin $http_origin always; add_header Access-Control-Allow-Credentials true always; add_header Access-Control-Expose-Headers "X-Request-ID,Authorization" always; add_header Access-Control-Allow-Methods '*' always; add_header Access-Control-Allow-Headers 'Content-Type,Access-Control-Allow-Headers,Content-Length,Accept,Authorization,X-Requested-With,Referer,User-Agent,Cookie,Set-Cookie,X-Request-ID,X-BLM-FRID,X-BLM-CID,Timestamp,Signature,Token' always; # 对于预检请求直接返回成功状态码 if ($request_method = 'OPTIONS') { return 204; } ``` - comm-header-handler.conf ```gherkin #给客户端返回X-Request-ID add_header "X-Request-ID" $request_id always; #传递X-Request-ID给下游 proxy_set_header X-Request-ID $request_id; ``` - comm-error-json.conf ```gherkin proxy_intercept_errors on; #### 50X异常 ##### error_page 500 = @error_json500; error_page 503 = @error_json503; error_page 504 = @error_json504; location @error_json500 { # 定义 JSON 格式的错误响应 default_type application/json; include comm-cors.conf; include comm-header-handler.conf; set $error_message "Internal Server Error"; return 500 '{"code": 500, "message": "$error_message", "Request-ID": "$request_id"}'; } location @error_json503 { # 定义 JSON 格式的错误响应 default_type application/json; include comm-cors.conf; include comm-header-handler.conf; set $error_message "Service Unavailable"; return 503 '{"code": 503, "message": "$error_message", "Request-ID": "$request_id"}'; } location @error_json504 { # 定义 JSON 格式的错误响应 default_type application/json; include comm-cors.conf; include comm-header-handler.conf; set $error_message "Gateway Timeout"; return 504 '{"code": 504, "message": "$error_message", "Request-ID": "$request_id"}'; } ``` ## 补充给站点添加BA校验 - 安装htpasswd命令 ```js sudo apt-get install apache2-utils//ubuntu安装命令 yum -y install httpd-tools//centos安装命令 ``` - 添加用户生成密文文件 ```js htpasswd -b -c {{密文文件路径,例子:/www/nginx_auth_passwd}} {{自定义用户名:admin}} {{自定义密码:1234565@@}} ``` - 最后nginx中server添加 ```js //示例: location /admin { auth_basic "admin";//自定义名称 auth_basic_user_file /www/nginx_auth_passwd;//密文文件 proxy_pass http://127.0.0.1:81/admin;#验证通过后跳转 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; add_header X-Cache $upstream_cache_status; } ``` ## htpasswd命令 htpasswd命令是Apache的Web服务器内置工具,用于创建和更新储存用户名、域和用户基本认证的密码文件。 - 语法 htpasswd(选项)(参数) 选项: ```js -c:创建一个加密文件; -n:不更新加密文件,只将加密后的用户名密码显示在屏幕上; -m:默认采用MD5算法对密码进行加密; -d:采用CRYPT算法对密码进行加密; -p:不对密码进行进行加密,即明文密码; -s:采用SHA算法对密码进行加密; -b:在命令行中一并输入用户名和密码而不是根据提示输入密码; -D:删除指定的用户。 ``` - 参数 用户:要创建或者更新密码的用户名; 密码:用户的新密码。 - 实例 利用htpasswd命令添加用户 ```js htpasswd -b -c {{密文文件路径,例子:/www/nginx_auth_passwd}} {{自定义用户名:admin}} {{自定义密码:1234565@@}} ``` 在bin目录下生成一个.passwd文件,用户名net,密码:php,默认采用MD5加密方式。 在原有密码文件中增加下一个用户 ```js htpasswd -b {{密文文件路径,例子:/www/nginx_auth_passwd}} Jack 123456 ``` 去掉-c选项,即可在第一个用户之后添加第二个用户,依此类推。 不更新密码文件,只显示加密后的用户名和密码 ```js htpasswd -nb nginx ninx12 nginx:eVdgwLGfhJ8U6 #显示的密码 ``` 只是显示加密后密码是什么样的 不更新.passwd文件,只在屏幕上输出用户名和经过加密后的密码。 利用htpasswd命令删除用户名和密码 ```js htpasswd -D .passwd tom ``` 利用htpasswd命令修改密码 ```js htpasswd -D .passwd tom htpasswd -b .passwd tom 123456 ``` 即先使用htpasswd删除命令删除指定用户,再利用htpasswd添加用户命令创建用户即可实现修改密码的功能。 ## 安装SSL证书部署工具 ``` cd ~ git clone https://gitee.com/neilpang/acme.sh.git cd acme.sh ./acme.sh --install -m 978952042@qq.com ``` - 切换到root, 这是推荐的, 以免cron任务执行权限遇到问题 ``` sudo su ``` - 创建证书目录 ``` mkdir /etc/nginx/vhost/cert chmod -R 777 /etc/nginx/vhost/cert ``` - 安装, 指定证书全部放到 /etc/nginx/vhost/cert下, 方便管理 ``` ./acme.sh --install --cert-home /etc/nginx/vhost/cert --email 978952042@qq.com ``` - 部署证书命令 ``` /root/.acme.sh/acme.sh --force --issue -d {{站点域名}} -w nginx:"/etc/nginx/vhost/{{站点域名}}.conf" --fullchain-file /etc/nginx/vhost/cert/{{站点域名}}/fullchain.pem --key-file /etc/nginx/vhost/cert/{{站点域名}}/privkey.pem ``` - nginx配置文件中启用证书 ``` ##以下是SSL证书配置(开始) ssl_certificate /etc/nginx/vhost/cert/{{站点域名}}/fullchain.pem; ssl_certificate_key /etc/nginx/vhost/cert/{{站点域名}}/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; error_page 497 https://$host$request_uri; ##以下是SSL证书配置(结束) ```