sonumb

Go - php와 같은 동적 스크립트를 실행하는 간단 웹서버 본문

개발자 이야기/Go

Go - php와 같은 동적 스크립트를 실행하는 간단 웹서버

sonumb 2021. 11. 25. 15:43

개요

테스트 목적으로 심플한 php 스크립트를 구동하고 결과를 알고 싶은데, WAS나 웹서버를 설치하고 설정하는 것은 번거롭다.

 

go에서 "net/http"를 이용해 간단한 http 서버를 작성/구동할 수 있다.

https://jeonghwan-kim.github.io/dev/2019/02/07/go-net-http.html

 

Go net/http 패키지

웹 어플리케이션을 개발하려고 Go 언어를 살펴보기 시작했다. 앞서 정리한 몇 가지 기본 패키지는 net/http 패키지를 사용하기 위한 준비 과정이라 생각하자. 이번에는 네트웍 프로그래밍을 위한 n

jeonghwan-kim.github.io

 

그렇다면, php와 같은 스크립트 파일의 실행하며, 그 결과를 출력하는 httpd도 만들 수 있을까? 답은 그렇다이다.

 

정말정말 간단한 소스코드는 아래와 같다.

 

소스코드

아래 소스는 "주소:8080"의 경로는 "/"이며,  클라이언트가 해당 주소에 접속하면, 경로 "/" 에 대해  연결된 스크립트 파일을 실행하여 그 결과를 출력을 수행한다.

package main

import (
  "log"
  "net/http"
  "net/http/cgi"
)

func main() {
  http.Handle("/", &cgi.Handler{
    Path: "/usr/bin/php-cgi",
    Dir:  "./",
    Env: []string{
      "SCRIPT_FILENAME=F2_TBDEV3054.php5",
      "REDIRECT_STATUS=trash",
    },
  })

  // Assumes ../app/app is running in ../app directory
  //http.Handle("/go-fcgi", &fcgi.Handler{
  //  Dialer: &fcgi.NetDialer{
  //    Network: "unix",
  //    Address: "../app.socket",
  //  },
  //})
  //http.Handle("/go-cgi", &cgi.Handler{
  //  Path: "../app/app",
  //  Dir:  "../app/",
  //  Args: []string{"cgi"},
  //})
  //
  //// Assumes ../app/index.py is running in ../app directory
  //http.Handle("/py-fcgi", &fcgi.Handler{
  //  Dialer: &fcgi.NetDialer{
  //    Network: "unix",
  //    Address: "../app-py.socket",
  //  },
  //})
  //http.Handle("/py-cgi", &cgi.Handler{
  //  Path: "../app/index.py",
  //  Dir:  "../app/",
  //  Args: []string{"cgi"},
  //})

  log.Println("port:8080")
  err := http.ListenAndServe(":8080", nil)
  if err != nil {
    log.Fatal(err.Error())
  }
}

실행 및 결과

$ go run report_httpd.go
2021/11/25 15:19:31 port:8080
PHP Warning:  getdate(): It is not safe to rely on the system's timezone settings.
You are *required* to use the date.timezone setting or the date_default_timezone_set()
function. In case you used any of those methods and you are still getting this warning,
 you most likely misspelled the timezone identifier.
We selected the timezone 'UTC' for now, but please set date.timezone to select
your timezone. in /home/sonumb/testdir/public_html/F4_1.php5 on line 3

크롬으로 주소:8080으로 접속한 뒤 결과물은 아래와 같다.

반응형