갈루아의 반서재

도입


우분투 18.04 환경에서 클로저를 이용한 웹구현의 기본적인 모습을 살펴본다. 샘플 클로져 어플리케이션과 패키지를 생성하고, Supervisor 과 Nginx 를 이용한 서버상에 클로져 앱을 셋팅해본다. 

이를 위해서 먼저, 아래 2가지가 선행되어야 한다. 우분투 18.04 환경과 sudo 권한을 가진 non-root 사용자 계정이 그것이다.  

먼저 아나콘다를 이용해 가상환경을 만든다. 

1
fukaerii@***-***-***-**:~$ conda create --name dominika python=3
cs


1
2
3
4
5
6
7
8
9
$ conda env list
# conda environments:
#
base                  *  /home/fukaerii/anaconda3
dominika                 /home/fukaerii/anaconda3/envs/dominika
redsparrow               /home/fukaerii/anaconda3/envs/redsparrow
 
$ . activate dominika
(dominika) ~$
cs


Step 1 — 샘플 클로져 앱 생성 및 패키징


우선적으로 해야할 것은 git을 이용해 샘플 클로져 프로젝트를 가져오는 것이다. 먼저 패키지를 업데이트한다.

1
2
3
4
5
6
7
$ sudo apt-get update
[sudo] password for fukaerii:
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Reading package lists... Done
cs

서버에 git 을 설치한다.

1
2
3
4
5
6
$ sudo apt-get install git
Reading package lists... Done
Building dependency tree
Reading state information... Done
git is already the newest version (1:2.17.1-1ubuntu0.1).
0 upgraded, 0 newly installed, 0 to remove and 78 not upgraded.
cs

샘플 프로젝트 저장소를 복제한다.

1
2
3
4
5
6
7
$ pwd
/home/fukaerii
$ git clone https://github.com/do-community/do-clojure-web.git
Cloning into 'do-clojure-web'...
remote: Counting objects: 37, done.
remote: Total 37 (delta 0), reused 0 (delta 0), pack-reused 37
Unpacking objects: 100% (37/37), done.
cs

다음으로 자바를 설치한다. 터미널에  java -version이라고 입력한다. 자바가 설치되어 있지 않다면, 아래와 같이 보일 것이다. 

1
2
3
4
5
6
7
8
9
10
$ java -version
 
Command 'java' not found, but can be installed with:
 
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
apt install openjdk-9-jre-headless
 
Ask your administrator to install one of them.
cs

위의 명령문 중 하나를 이용해 자바를 설치한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(dominika)~$ apt install openjdk-11-jre-headless
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
 
~# apt install openjdk-11-jre-headless
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  ca-certificates-java java-common libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libjpeg-turbo8 libjpeg8 liblcms2-2 libpcsclite1
  libxi6 libxrender1 libxtst6 x11-common
Suggested packages:
  default-jre cups-common liblcms2-utils pcscd libnss-mdns fonts-dejavu-extra fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei
  | fonts-wqy-zenhei fonts-indic
The following NEW packages will be installed:
  ca-certificates-java java-common libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libjpeg-turbo8 libjpeg8 liblcms2-2 libpcsclite1
  libxi6 libxrender1 libxtst6 openjdk-11-jre-headless x11-common
0 upgraded, 15 newly installed, 0 to remove and 78 not upgraded.
Need to get 40.2 MB of archives.
After this operation, 186 MB of additional disk space will be used.
Do you want to continue? [Y/n]
cs

설치 후 다시  java -version을 입력하면 다음과 같이 설치된 자바의 버전을 확인할 수 있다.

1
2
3
4
(dominika) ~$ java -version
openjdk version "10.0.1" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)
cs

이제 Leiningen 설치 스크립트를 다운로드 받는다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
~$ sudo curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -o /usr/local/bin/lein
[sudo] password for fukaerii:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 12463  100 12463    0     0  72459      0 --:--:-- --:--:-- --:--:-- 72459
 
~$ cd /usr/local/bin
/usr/local/bin$ ls -al
total 24
drwxr-xr-x  2 root root  4096 Aug  4 15:56 .
drwxr-xr-x 10 root root  4096 Apr 27 04:07 ..
-rw-r--r--  1 root root 12463 Aug  4 15:56 lein
 
cs

누구나 lein utility 를 사용할 수 있도록 퍼미션을 조정한다. 

1
2
~$ sudo chmod a+x /usr/local/bin/lein
[sudo] password for fukaerii:
cs

이제 lein 을 이용하여 컴파일이 가능하다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
~$ cd ~/do-clojure-web
~/do-clojure-web$ lein uberjar
Retrieving lein-ring/lein-ring/0.8.13/lein-ring-0.8.13.pom from clojars
Retrieving org/clojure/clojure/1.2.1/clojure-1.2.1.pom from central
Retrieving org/clojure/data.xml/0.0.6/data.xml-0.0.6.pom from central
Retrieving org/clojure/pom.contrib/0.0.25/pom.contrib-0.0.25.pom from central
Retrieving leinjacker/leinjacker/0.4.1/leinjacker-0.4.1.pom from clojars
Retrieving org/clojure/core.contracts/0.0.1/core.contracts-0.0.1.pom from central
Retrieving org/clojure/pom.contrib/0.0.26/pom.contrib-0.0.26.pom from central
Retrieving org/clojure/core.unify/0.5.3/core.unify-0.5.3.pom from central
Retrieving org/clojure/clojure/1.4.0/clojure-1.4.0.pom from central
Retrieving org/clojure/clojure/1.2.1/clojure-1.2.1.jar from central
Retrieving org/clojure/data.xml/0.0.6/data.xml-0.0.6.jar from central
Retrieving org/clojure/core.contracts/0.0.1/core.contracts-0.0.1.jar from central
Retrieving org/clojure/core.unify/0.5.3/core.unify-0.5.3.jar from central
Retrieving leinjacker/leinjacker/0.4.1/leinjacker-0.4.1.jar from clojars
Retrieving lein-ring/lein-ring/0.8.13/lein-ring-0.8.13.jar from clojars
Retrieving org/clojure/clojure/1.5.1/clojure-1.5.1.pom from central
Retrieving compojure/compojure/1.3.1/compojure-1.3.1.pom from clojars
Retrieving org/clojure/tools.macro/0.1.5/tools.macro-0.1.5.pom from central
Retrieving clout/clout/2.1.0/clout-2.1.0.pom from clojars
Retrieving instaparse/instaparse/1.3.4/instaparse-1.3.4.pom from clojars
Retrieving medley/medley/0.5.3/medley-0.5.3.pom from clojars
Retrieving ring/ring-core/1.3.2/ring-core-1.3.2.pom from clojars
Retrieving org/clojure/tools.reader/0.8.1/tools.reader-0.8.1.pom from central
Retrieving ring/ring-codec/1.0.0/ring-codec-1.0.0.pom from clojars
Retrieving commons-codec/commons-codec/1.6/commons-codec-1.6.pom from central
Retrieving commons-io/commons-io/2.4/commons-io-2.4.pom from central
Retrieving org/apache/commons/commons-parent/25/commons-parent-25.pom from central
Retrieving commons-fileupload/commons-fileupload/1.3/commons-fileupload-1.3.pom from central
Retrieving org/apache/commons/commons-parent/28/commons-parent-28.pom from central
Retrieving org/apache/apache/13/apache-13.pom from central
Retrieving commons-io/commons-io/2.2/commons-io-2.2.pom from central
Retrieving org/apache/commons/commons-parent/24/commons-parent-24.pom from central
Retrieving clj-time/clj-time/0.6.0/clj-time-0.6.0.pom from clojars
Retrieving joda-time/joda-time/2.2/joda-time-2.2.pom from central
Retrieving crypto-random/crypto-random/1.2.0/crypto-random-1.2.0.pom from clojars
Retrieving crypto-equality/crypto-equality/1.0.0/crypto-equality-1.0.0.pom from clojars
Retrieving ring/ring-defaults/0.1.2/ring-defaults-0.1.2.pom from clojars
Retrieving ring/ring-core/1.3.1/ring-core-1.3.1.pom from clojars
Retrieving ring/ring-ssl/0.2.1/ring-ssl-0.2.1.pom from clojars
Retrieving ring/ring-core/1.3.0-RC1/ring-core-1.3.0-RC1.pom from clojars
Retrieving ring/ring-headers/0.1.1/ring-headers-0.1.1.pom from clojars
Retrieving ring/ring-core/1.3.0/ring-core-1.3.0.pom from clojars
Retrieving ring/ring-anti-forgery/1.0.0/ring-anti-forgery-1.0.0.pom from clojars
Retrieving hiccup/hiccup/1.0.5/hiccup-1.0.5.pom from clojars
Retrieving javax/servlet/servlet-api/2.5/servlet-api-2.5.pom from central
Retrieving hiccup/hiccup/1.0.2/hiccup-1.0.2.pom from clojars
Retrieving org/clojure/java.jdbc/0.2.3/java.jdbc-0.2.3.pom from central
Retrieving com/h2database/h2/1.3.170/h2-1.3.170.pom from central
Retrieving ring/ring-jetty-adapter/1.3.2/ring-jetty-adapter-1.3.2.pom from clojars
Retrieving ring/ring-servlet/1.3.2/ring-servlet-1.3.2.pom from clojars
Retrieving org/eclipse/jetty/jetty-server/7.6.13.v20130916/jetty-server-7.6.13.v20130916.pom from central
Retrieving org/eclipse/jetty/jetty-project/7.6.13.v20130916/jetty-project-7.6.13.v20130916.pom from central
Retrieving org/eclipse/jetty/jetty-parent/20/jetty-parent-20.pom from central
Retrieving org/eclipse/jetty/orbit/javax.servlet/2.5.0.v201103041518/javax.servlet-2.5.0.v201103041518.pom from central
Retrieving org/eclipse/jetty/orbit/jetty-orbit/1/jetty-orbit-1.pom from central
Retrieving org/eclipse/jetty/jetty-parent/18/jetty-parent-18.pom from central
Retrieving org/eclipse/jetty/jetty-continuation/7.6.13.v20130916/jetty-continuation-7.6.13.v20130916.pom from central
Retrieving org/eclipse/jetty/jetty-http/7.6.13.v20130916/jetty-http-7.6.13.v20130916.pom from central
Retrieving org/eclipse/jetty/jetty-io/7.6.13.v20130916/jetty-io-7.6.13.v20130916.pom from central
Retrieving org/eclipse/jetty/jetty-util/7.6.13.v20130916/jetty-util-7.6.13.v20130916.pom from central
Retrieving org/clojure/clojure/1.5.1/clojure-1.5.1.jar from central
Retrieving org/clojure/tools.macro/0.1.5/tools.macro-0.1.5.jar from central
Retrieving commons-io/commons-io/2.4/commons-io-2.4.jar from central
Retrieving org/clojure/tools.reader/0.8.1/tools.reader-0.8.1.jar from central
Retrieving commons-fileupload/commons-fileupload/1.3/commons-fileupload-1.3.jar from central
Retrieving joda-time/joda-time/2.2/joda-time-2.2.jar from central
Retrieving commons-codec/commons-codec/1.6/commons-codec-1.6.jar from central
Retrieving org/clojure/java.jdbc/0.2.3/java.jdbc-0.2.3.jar from central
Retrieving javax/servlet/servlet-api/2.5/servlet-api-2.5.jar from central
Retrieving com/h2database/h2/1.3.170/h2-1.3.170.jar from central
Retrieving org/eclipse/jetty/jetty-server/7.6.13.v20130916/jetty-server-7.6.13.v20130916.jar from central
Retrieving org/eclipse/jetty/orbit/javax.servlet/2.5.0.v201103041518/javax.servlet-2.5.0.v201103041518.jar from central
Retrieving org/eclipse/jetty/jetty-http/7.6.13.v20130916/jetty-http-7.6.13.v20130916.jar from central
Retrieving org/eclipse/jetty/jetty-continuation/7.6.13.v20130916/jetty-continuation-7.6.13.v20130916.jar from central
Retrieving org/eclipse/jetty/jetty-io/7.6.13.v20130916/jetty-io-7.6.13.v20130916.jar from central
Retrieving org/eclipse/jetty/jetty-util/7.6.13.v20130916/jetty-util-7.6.13.v20130916.jar from central
Retrieving compojure/compojure/1.3.1/compojure-1.3.1.jar from clojars
Retrieving clout/clout/2.1.0/clout-2.1.0.jar from clojars
Retrieving medley/medley/0.5.3/medley-0.5.3.jar from clojars
Retrieving ring/ring-core/1.3.2/ring-core-1.3.2.jar from clojars
Retrieving instaparse/instaparse/1.3.4/instaparse-1.3.4.jar from clojars
Retrieving crypto-random/crypto-random/1.2.0/crypto-random-1.2.0.jar from clojars
Retrieving crypto-equality/crypto-equality/1.0.0/crypto-equality-1.0.0.jar from clojars
Retrieving ring/ring-codec/1.0.0/ring-codec-1.0.0.jar from clojars
Retrieving ring/ring-defaults/0.1.2/ring-defaults-0.1.2.jar from clojars
Retrieving clj-time/clj-time/0.6.0/clj-time-0.6.0.jar from clojars
Retrieving ring/ring-headers/0.1.1/ring-headers-0.1.1.jar from clojars
Retrieving ring/ring-ssl/0.2.1/ring-ssl-0.2.1.jar from clojars
Retrieving ring/ring-anti-forgery/1.0.0/ring-anti-forgery-1.0.0.jar from clojars
Retrieving hiccup/hiccup/1.0.2/hiccup-1.0.2.jar from clojars
Retrieving ring/ring-servlet/1.3.2/ring-servlet-1.3.2.jar from clojars
Retrieving ring/ring-jetty-adapter/1.3.2/ring-jetty-adapter-1.3.2.jar from clojars
Compiling do-clojure-web.handler
Compiling do-clojure-web.handler
Created /home/fukaerii/do-clojure-web/target/do-clojure-web-0.1.0.jar
Created /home/fukaerii/do-clojure-web/target/do-clojure-web-0.1.0-standalone.jar
cs


Step 2 — 클로져 어플리케이션 환경 셋팅하기


이 어플리케이션이 정상적으로 작동하기 위해서는 3가지 요소 - Java, Supervisor, 그리고 Nginx 라는 3가지 요소가 필요하다. 앞서 Java 설치는 마쳤고, 이제 Supervisor 와 Nginx 를 설치해보자. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
~/do-clojure-web$ sudo apt-get install nginx supervisor
[sudo] password for fukaerii:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libnginx-mod-http-geoip libnginx-mod-http-image-filter
  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core python-meld3
  python-pkg-resources
Suggested packages:
  libgd-tools fcgiwrap nginx-doc ssl-cert python-setuptools supervisor-doc
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libnginx-mod-http-geoip libnginx-mod-http-image-filter
  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx nginx-common nginx-core python-meld3
  python-pkg-resources supervisor
0 upgraded, 19 newly installed, 0 to remove and 78 not upgraded.
Need to get 2,760 kB of archives.
After this operation, 9,848 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 fonts-dejavu-core all 2.37-1 [1,041 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/main amd64 fontconfig-config all 2.12.6-0ubuntu2 [55.8 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/main amd64 libfontconfig1 amd64 2.12.6-0ubuntu2 [137 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/main amd64 libjbig0 amd64 2.1-3.1build1 [26.7 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/main amd64 libtiff5 amd64 4.0.9-5 [152 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic/main amd64 libwebp6 amd64 0.6.1-2 [185 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/main amd64 libxpm4 amd64 1:3.5.12-1 [34.0 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/main amd64 libgd3 amd64 2.2.5-4 [118 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 nginx-common all 1.14.0-0ubuntu1 [37.0 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 libnginx-mod-http-geoip amd64 1.14.0-0ubuntu1 [10.8 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/main amd64 libnginx-mod-http-image-filter amd64 1.14.0-0ubuntu1 [14.2 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/main amd64 libnginx-mod-http-xslt-filter amd64 1.14.0-0ubuntu1 [12.6 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic/main amd64 libnginx-mod-mail amd64 1.14.0-0ubuntu1 [41.4 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic/main amd64 libnginx-mod-stream amd64 1.14.0-0ubuntu1 [63.3 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic/main amd64 nginx-core amd64 1.14.0-0ubuntu1 [412 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic/main amd64 nginx all 1.14.0-0ubuntu1 [3,596 B]
Get:17 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-pkg-resources all 39.0.1-2 [128 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python-meld3 all 1.0.2-2 [30.9 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic/universe amd64 supervisor all 3.3.1-1.1 [256 kB]
Fetched 2,760 kB in 7s (369 kB/s)
Preconfiguring packages ...
Selecting previously unselected package fonts-dejavu-core.
(Reading database ... 140089 files and directories currently installed.)
Preparing to unpack .../00-fonts-dejavu-core_2.37-1_all.deb ...
Unpacking fonts-dejavu-core (2.37-1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../01-fontconfig-config_2.12.6-0ubuntu2_all.deb ...
Unpacking fontconfig-config (2.12.6-0ubuntu2) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../02-libfontconfig1_2.12.6-0ubuntu2_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../03-libjbig0_2.1-3.1build1_amd64.deb ...
Unpacking libjbig0:amd64 (2.1-3.1build1) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../04-libtiff5_4.0.9-5_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.9-5) ...
Selecting previously unselected package libwebp6:amd64.
Preparing to unpack .../05-libwebp6_0.6.1-2_amd64.deb ...
Unpacking libwebp6:amd64 (0.6.1-2) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../06-libxpm4_1%3a3.5.12-1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.12-1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../07-libgd3_2.2.5-4_amd64.deb ...
Unpacking libgd3:amd64 (2.2.5-4) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../08-nginx-common_1.14.0-0ubuntu1_all.deb ...
Unpacking nginx-common (1.14.0-0ubuntu1) ...
Selecting previously unselected package libnginx-mod-http-geoip.
Preparing to unpack .../09-libnginx-mod-http-geoip_1.14.0-0ubuntu1_amd64.deb ...
Unpacking libnginx-mod-http-geoip (1.14.0-0ubuntu1) ...
Selecting previously unselected package libnginx-mod-http-image-filter.
Preparing to unpack .../10-libnginx-mod-http-image-filter_1.14.0-0ubuntu1_amd64.deb ...
Unpacking libnginx-mod-http-image-filter (1.14.0-0ubuntu1) ...
Selecting previously unselected package libnginx-mod-http-xslt-filter.
Preparing to unpack .../11-libnginx-mod-http-xslt-filter_1.14.0-0ubuntu1_amd64.deb ...
Unpacking libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1) ...
Selecting previously unselected package libnginx-mod-mail.
Preparing to unpack .../12-libnginx-mod-mail_1.14.0-0ubuntu1_amd64.deb ...
Unpacking libnginx-mod-mail (1.14.0-0ubuntu1) ...
Selecting previously unselected package libnginx-mod-stream.
Preparing to unpack .../13-libnginx-mod-stream_1.14.0-0ubuntu1_amd64.deb ...
Unpacking libnginx-mod-stream (1.14.0-0ubuntu1) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../14-nginx-core_1.14.0-0ubuntu1_amd64.deb ...
Unpacking nginx-core (1.14.0-0ubuntu1) ...
Selecting previously unselected package nginx.
Preparing to unpack .../15-nginx_1.14.0-0ubuntu1_all.deb ...
Unpacking nginx (1.14.0-0ubuntu1) ...
Selecting previously unselected package python-pkg-resources.
Preparing to unpack .../16-python-pkg-resources_39.0.1-2_all.deb ...
Unpacking python-pkg-resources (39.0.1-2) ...
Selecting previously unselected package python-meld3.
Preparing to unpack .../17-python-meld3_1.0.2-2_all.deb ...
Unpacking python-meld3 (1.0.2-2) ...
Selecting previously unselected package supervisor.
Preparing to unpack .../18-supervisor_3.3.1-1.1_all.deb ...
Unpacking supervisor (3.3.1-1.1) ...
Processing triggers for ufw (0.35-5) ...
Processing triggers for ureadahead (0.100.0-20) ...
Setting up libjbig0:amd64 (2.1-3.1build1) ...
Setting up fonts-dejavu-core (2.37-1) ...
Setting up python-meld3 (1.0.2-2) ...
Setting up nginx-common (1.14.0-0ubuntu1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
Setting up libtiff5:amd64 (4.0.9-5) ...
Setting up python-pkg-resources (39.0.1-2) ...
Setting up supervisor (3.3.1-1.1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/supervisor.service → /lib/systemd/system/supervisor.service.
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10) ...
Setting up libnginx-mod-mail (1.14.0-0ubuntu1) ...
Setting up libxpm4:amd64 (1:3.5.12-1) ...
Processing triggers for man-db (2.8.3-2) ...
Setting up libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1) ...
Setting up libnginx-mod-http-geoip (1.14.0-0ubuntu1) ...
Setting up libwebp6:amd64 (0.6.1-2) ...
Setting up fontconfig-config (2.12.6-0ubuntu2) ...
Setting up libnginx-mod-stream (1.14.0-0ubuntu1) ...
Setting up libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
Setting up libgd3:amd64 (2.2.5-4) ...
Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1) ...
Setting up nginx-core (1.14.0-0ubuntu1) ...
Setting up nginx (1.14.0-0ubuntu1) ...
Processing triggers for ureadahead (0.100.0-20) ...
Processing triggers for ufw (0.35-5) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
cs

클로져 웹어플리케이션과 로그 파일을 저장해둘 공간이 필요하다. 생성해보자. 

1
~/do-clojure-web$ sudo mkdir -p /var/www/do-clojure-web/app/db /var/www/logs
cs

이제 클로져 어플리케이션 파일과 데이터베이스 파일을 방금 만든 디렉토리로 이동할 수 있다.

1
2
~/do-clojure-web$ sudo cp ~/do-clojure-web/target/do-clojure-web-0.1.0-standalone.jar /var/www/do-clojure-web/app/
~/do-clojure-web$ sudo cp ~/do-clojure-web/db/do-clojure-web.h2.db /var/www/do-clojure-web/app/db/
cs

해당 어플리케이션은 www-data 사용자로 구동된다. 어플리케이션의 소유자를 www-data 로 변경하자. 그리고 클로져 어플리케이션 디렉토리도 변경한다. 

1
2
3
4
~/do-clojure-web$ sudo chown -R www-data /var/www/do-clojure-web/
~/do-clojure-web$ cd /var/www/do-clojure-web/app
/var/www/do-clojure-web/app$
 
cs


현재 구동중인 버전에 symlink 를 생성하여, 매번 시스템 설정이 변경되지 않도록 한다. 

1
2
3
4
5
6
7
/var/www/do-clojure-web/app$ ls -al
total 8620
drwxr-xr-x 3 www-data root    4096 Aug  4 17:19 .
drwxr-xr-x 3 www-data root    4096 Aug  4 17:15 ..
drwxr-xr-x 2 www-data root    4096 Aug  4 17:19 db
-rw-r--r-- 1 www-data root 8813412 Aug  4 17:19 do-clojure-web-0.1.0-standalone.jar
/var/www/do-clojure-web/app$ sudo ln -s do-clojure-web-0.1.0-standalone.jar do-clojure-web.jar
cs

이제 어플리케이션이 정상적으로 동작하는지 아래와 같이 확인해보자. 정상적으로 동작한다면 아래와 같은 결과를 얻게 된다. 

1
2
3
/var/www/do-clojure-web/app$ sudo java -jar do-clojure-web.jar
2018-08-04 08:40:52.736:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2018-08-04 08:40:52.805:INFO:oejs.AbstractConnector:Started SelectChannelConnector@127.0.0.1:5000
cs

CTRL+C 로 어플리케이션을 멈추고 다음 단계로 넘어가보자. 


Step 3 — 클로져 앱 구동을 위한 Supervisor 설정


어플리케이션을 서비스로 관리하기 위해서는 몇 가지 옵션이 필요하다. Supervisor 라고 불리는 것이 그것으로, 단순한 스크립트보다 관리하기 쉽고 다용도 활용이 가능하다. 하지만, 확장을 위해서는 uWSGI 문서를 참고해볼 필요가 있다. 

/etc/supervisor/conf.d/do-clojure-web.conf 파일을 생성한 후 편집하자. 

1
/var/www/do-clojure-web/app$ sudo nano /etc/supervisor/conf.d/do-clojure-web.conf
cs

편집창이 로딩되면 아래의 내용을 넣은 후 저장한다. 저장은 ^s 로 하면 된다.

1
2
3
4
5
6
7
8
9
[program:do-clojure-web]
command=/usr/bin/java -jar do-clojure-web.jar
directory=/var/www/do-clojure-web/app
user=www-data
autostart=true
autorestart=true
startretries=3
redirect_stderr=true
stdout_logfile=/var/www/logs/do-clojure-web.app.log
cs

설정이 매우 직관적인데, Supervisor 대몬 (서비스)는 어플리케이션을 /var/www/do-clojure-web/app 디렉토리로부터 구동시킨다. 그리고 로그를 /var/www/logs/do-clojure-web.app.log 에 남긴다는 것을 알 수 있다. 


Step 4 — Nginx 를 Proxy Server 로 설정하기


클로져 웹 어플리케이션은 로컬호스트 5000번 포트의 연결만 받기 때문에, 외부 접근을 위해 Nginx 와 같은 웹서버를 앞단에 두어야 한다. 이는 어플리케이션을 확장함에 따라 동적 자산을 관리하는데 있어서도 도움을 줄 수 있다. 

/etc/nginx/sites-available/default 파일을 편집해보자. 

1
/var/www/do-clojure-web/app$ sudo nano /etc/nginx/sites-available/default
cs

아래 빨간 색의 백엔드를 정의하는 부분을 삽입한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
 
# Default server configuration
#
upstream http_backend {
    server 127.0.0.1:5000;
    keepalive 32;
}
server {
    listen 80 default_server;
    listen [::]:80 default_server;
 
    # SSL configuration
    #
cs

다음으로 location / 로 시작되는 부분을 찾아보자. 찾아서 아래와 같이 각각의 줄 앞에 #을 넣어 주석처리한다.

1
2
3
4
5
6
7
 
    # location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
    # }
 
cs

그리고 바로 밑에 Nginx 가 일반적인 80 포트를 사용하는 웹서버처럼 동작하고 요청을 클로져 어플리케이션으로 전달한다는 점을 알려주는 다음 내용을 추가한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
    # location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
    # }
    
    location / {
        proxy_pass http://http_backend;
 
        proxy_http_version 1.1;
        proxy_set_header Connection "";
 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
 
        access_log /var/www/logs/do-clojure-web.access.log;
        error_log /var/www/logs/do-clojure-web.error.log;
    }
 
cs


Step 5 — 서비스 시작 및 데이터베이스 접근 테스트


이제 모든 것이 정상적으로 동작하는지 확인해볼 차례다. 클로져 어플리케이션이 동작할 수 있도록 Supervisor 대몬을 실행하자. 

1
/var/www/do-clojure-web/app$ sudo service supervisor start
cs

약 30초 가량 기다린 후 Nginx web server front end proxy 를 구동한다. 

1
/var/www/do-clojure-web/app$ sudo service nginx start
cs

웹브라우저를 열고 http://your_server_ip 를 입력하면, 샘플 클로져 어플리케이션 사이트가 아래와 같이 로딩됨을 볼 수 있다.

만약 다음과 같이 기본 Nginx 페이지가 보인다면, sudo service supervisor restart 로 Supervisor 를 재시작하고, 30초 기다렸다가 sudo service nginx restart 를 통해 Nginx 를 재시작한다. 

사이트가 로딩되면 상단의  Add a Location 링크를 눌러서 간단한 좌표를 입력해서 데이터베이스 접근 권한에 문제가 없는지 확인해보자. 예를 들어 다음과 같이 x 값에 4, y 값에 4 를 넣고 submit 하면 정상적으로 db 에 저장된 것을 확인할 수 있다.

이것으로 Lginingen, Supervisor, 그리고 Nginx 를 통해 클로져 어플리케이션을 배포하는 법을 알아보았다. 


https://www.digitalocean.com/community/tutorials/how-to-deploy-a-clojure-web-application-on-ubuntu-14-04