hyperf运行环境搭建

介绍
Hyperf 是基于 Swoole 4.4+ 实现的高性能、高灵活性的 PHP 协程框架,内置协程服务器及大量常用的组件,性能较传统基于 PHP-FPM 的框架有质的提升,提供超高性能的同时,也保持着极其灵活的可扩展性,标准组件均基于 PSR 标准 实现,基于强大的依赖注入设计,保证了绝大部分组件或类都是 可替换 与 可复用 的。
官方文档https://www.bookstack.cn/read/Hyperf-1.1.1
本文docker安装mysql+hyperf+nginx
安装提前学习docker容器相关知识,熟悉docker search、docker pull、docker ps、docker ps -a、docker rm 等命令
hyperf安装

# 下载并运行 hyperf/hyperf 镜像,并将镜像内的项目目录绑定到宿主机的 /tmp/skeleton 目录
docker run -v /home/rabbit/skeleton:/hyperf-skeleton -p 9501:9501 -it --entrypoint /bin/sh hyperf/hyperf:7.2-alpine-cli
# 镜像容器运行后,在容器内安装 Composer
wget https://github.com/composer/composer/releases/download/1.8.6/composer.phar
chmod u+x composer.phar
mv composer.phar /usr/local/bin/composer
# 将 Composer 镜像设置为阿里云镜像,加速国内下载速度
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
# 通过 Composer 安装 hyperf/hyperf-skeleton 项目
composer create-project hyperf/hyperf-skeleton hyperf-skeleton
# 进入安装好的 Hyperf 项目目录
cd hyperf-skeleton
# 启动 Hyperf
php bin/hyperf.php start
##上述在composer可能会报错一些版本错误,可以修改composer.json文件中版本,手动composer下来
##-v /tmp/skeleton:/hyperf-skeleton这个是映射宿主文件地址:docker中地址
##-p 9501:9501 是端口映射宿主端口:docker里面端口

启动后 curl 172.16.177.138:9501访问成功。
运行还需要安装mysql
docker启动mysql
-e设置密码

docker run -p 3306:3306 --nam-e mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

docker安装nginx
先创建文件夹nginx里面有子文件夹conf.d(存放vhost)、logs(日志文件),编辑nginx.conf文件

mkdir  -p /home/rabbit/nginx/conf.d
mkdir  -p /home/rabbit/nginx/logs
vi  /home/rabbit/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
  #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

在文件中创建vhost文档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;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

启动nginx

docker run --name nginx -p 80:80 -v /home/rabbit/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/rabbit/nginx/conf.d:/etc/nginx/conf.d -v /home/rabbit/nginx/logs:/var/log/nginx  -v /home/rabbit/skeleton:/hyperf-skeleton -d nginx

启动成功验证,在浏览器输入172.16.177.138出来nginx页面
配置nginx反向代理访问

# 至少需要一个 Hyperf 节点,多个配置多行
upstream hyperf {
    # Hyperf HTTP Server 的 IP 及 端口
    server 172.16.177.138:9501;  #上面配置好的服务器
}
server {
    # 监听端口
    listen 80; 
    # 绑定的域名,填写您的域名,你想通过什么域名访问到hyperf
    server_name proxy.hyperf.io;
    location / {
        # 将客户端的 Host 和 IP 信息一并转发到对应节点  
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 执行代理访问真实服务器,无需改变
        proxy_pass http://hyperf;
    }
}

编写代码验证访问
hyperf运行环境搭建_第1张图片

你可能感兴趣的:(语言专栏,php,hyper-v,docker,nginx)