在 homestead 中创建 webman 站点

Linux系统
355
0
0
2022-09-04

1. 在 Homestead/scripts/site-types/ 目录下创建 webman.sh 脚本文件。

在 homestead 中创建 webman 站点

内容如下:

#!/usr/bin/env bash

declare -A params=$6       # Create an associative array
declare -A headers=${9}    # Create an associative array
declare -A rewrites=${10}  # Create an associative array
paramsTXT=""
if [ -n "$6" ]; then 
   for element in "${!params[@]}" 
   do
      paramsTXT="${paramsTXT}
      fastcgi_param ${element} ${params[$element]};" 
   done
fi
headersTXT=""
if [ -n "${9}" ]; then 
   for element in "${!headers[@]}" 
   do
      headersTXT="${headersTXT}
      add_header ${element} ${headers[$element]};" 
   done
fi
rewritesTXT=""
if [ -n "${10}" ]; then 
   for element in "${!rewrites[@]}" 
   do
      rewritesTXT="${rewritesTXT}
      location ~ ${element} { if (!-f \$request_filename) { return 301 ${rewrites[$element]}; } }" 
   done
fi

if [ "$7" = "true" ]
then configureXhgui="
location /xhgui {
        try_files \$uri \$uri/ /xhgui/index.php?\$args;
}
"
else configureXhgui=""
fi

block="upstream webman {
    server 127.0.0.1:${11};
}

server {
    listen ${3:-80};
    listen ${4:-443} ssl http2;
    server_name .$1;
    root \"$2\";

    index index.html index.htm index.php;

    charset utf-8;

    $rewritesTXT

    location / {
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header Host \$host;
        $headersTXT

        if (!-f \$request_filename){
          proxy_pass http://webman;
        }
    }

    $configureXhgui

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/$1-error.log error;

    sendfile off;

    location ~ /\.ht {
        deny all;
    }

    ssl_certificate     /etc/ssl/certs/$1.crt;
    ssl_certificate_key /etc/ssl/certs/$1.key;
}
"

echo "$block" > "/etc/nginx/sites-available/$1"
ln -fs "/etc/nginx/sites-available/$1" "/etc/nginx/sites-enabled/$1"

2. 编辑 Homestead/scripts/homestead.rb,新增 upstreamPort 配置,传递给脚本的第 11 个变量。在 378 行左右。

在 homestead 中创建 webman 站点

<...>
          # Convert the site & any options to an array of arguments passed to the 
          # specific site type script (defaults to laravel)
          s.path = script_dir + "/site-types/#{type}.sh"
          s.args = [
              site['map'],                # $1
              site['to'],                 # $2
              site['port'] ||= http_port, # $3
              site['ssl'] ||= https_port, # $4
              site['php'] ||= '8.0',      # $5
              params ||= '',              # $6
              site['xhgui'] ||= '',       # $7
              site['exec'] ||= 'false',   # $8
              headers ||= '',             # $9
              rewrites ||= '',            # $10 注意,这里要增加 ","
              site['upstream_port'] ||= '8787',   # $11 这里是新增的
          ]
<...>

3. 在 Homestead.yaml 创建自己的站点,示例如下

<...>

sites: 
    <...>

    - map: webman.test 
      to: /home/vagrant/code/php/webman-test/public 
      php: "8.0" 
      type: webman 
      upstream_port: 8788 # 此处指定 upstream 中的 port;

<...>

4. 在 hosts 文件中新增站点域名

<...>

192.168.10.10 webman.test

<...>

5. 在 Homestead 目录下执行 vagrant provision,重新生成站点配置。

6. 修改 webman 项目配置的启动文件:webman/config/server.php,监听端口需与 upstreamPort 相同。

在 homestead 中创建 webman 站点

7. 进入 homestead 虚拟机。到项目下执行 php start.php start,启动项目。

8. 在宿主机打开浏览器,访问 webman.test,查看项目是否配置成功。

此处可查看:www.workerman.net/a/1287