개요

code-server는 브라우저를 통해서 언제든 VS Code에 엑세스 할 수 있는 기능을 제공한다. 마이크로소프트에서 제공하는 vscode.dev 로도 사용할 수 있지만 ssh 터널을 사용할 수 없고 원하는 Extentions을 설치할 수 없는 단점이 있어서 사용하고 있다.

더 자세한 정보는 아래 링크에서 확인할 수 있다. 여기서는 Amazon LightSail에 code-server를 설치하는 방법을 다루고자 한다.

Amazon LightSail

Amazon LightSail는 AWS에서 제공하는 개발자용 호스팅 서비스 입니다. 서비스 프레임워크를 설치하고 구축하는데 많은 시간을 들이지 않도록 미리 구성된 이미지를 제공한다. Amazon EC2에는 여러 서비스가 혼합되어 있으며 단일 아키텍처를 생성하는 데 사용되는 고유한 개별 기능을 가지고 있어서 복잡한 아키텍처에 적합하지만, Lightsail은 기능이 간소화되어 소규모 또는 개발용으로 적합합니다[1]. 여기서는 VS Code를 브라우저를 통해 운영하고 개발용으로 사용할 것이므로 Amazon LightSail에 적합하다고 판단할 수 있다.

요구사항

code-server를 설치하기 위해서는 아래 요구사항을 충족해야 한다[2]. Amazon LightSail에서 아래 최소 요구사항에 맞는 인스턴스를 선택하자.

  • 1 GB of RAM
  • 2 CPU cores

설치

code-server를 설치

인스턴스를 생성했다면 SSH로 접속해서 아래 명령으로 code-server를 설치할 수 있다.

curl -fsSL https://code-server.dev/install.sh | sh

설정

설치 후 설정 파일을 수정해서 공개될 아이피를 변경해서 외부에서 접속이 가능하게 변경할 수 있다. 또한, 접속 비밀번호도 설정하자.

~/.config/code-server/config.yaml

ind-addrpassword를 변경한다.

ind-addr: 0.0.0.0:8080
auth: password
password: [사용 할 비밀번호 입력]
cert: false

자동 재시작 등록

서버 재부팅 시 자동으로 실행하고 싶다면 아래와 같이 실행한다.

sudo systemctl enable --now code-server@$USER

등록 후 잘 등록됐는지 확인한다.

sudo systemctl list-units code-server@$USER*

아래와 같이 등록내역이 확인되면 잘 등록된 것이다.

$ sudo systemctl list-units code-server*

UNIT                         LOAD   ACTIVE SUB     DESCRIPTION
code-server@ec2-user.service loaded active running code-server

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

서비스 재시작

등록이 됐다면 재시작하여 서비스를 실행해 본다.

$ sudo service code-server@$USER

Redirecting to /bin/systemctl status code-server@ec2-user.service
● code-server@ec2-user.service - code-server
   Loaded: loaded (/usr/lib/systemd/system/code-server@.service; enabled; vendor preset: d
isabled)
   Active: active (running) since Thu 2022-12-22 00:00:53 UTC; 3min 6s ago
 Main PID: 28755 (node)
   CGroup: /system.slice/system-code\x2dserver.slice/code-server@ec2-user.service
           ├─28755 /usr/lib/code-server/lib/node /usr/lib/code-server
           └─28777 /usr/lib/code-server/lib/node /usr/lib/code-server/out/node/entry

Dec 22 00:00:53 ip-0... systemd[1]: Started code...
Hint: Some lines were ellipsized, use -l to show in full.

Nginx 설치

code-server 단독으로 실행할 수도 있지만, SSL을 적용하지 않았으므로 보안 및 일부 Extensions을 사용하는데 제약이 있을 수 있다. 그래서 nginx를 설치하고 별도 도메인을 연결 후 SSL을 적용하고자 한다.

이미 설치된 이미지로 Lightsail을 시작할 수 도 있지만, 그렇지 않다면 아래 명령으로 설치할 수 있다.

$ sudo yum install nginx
$ sudo service nginx restart

Nginx 설정

설치가 되었다면 Nginx을 설정을 위해 아래 파일을 생성한다.

/etc/nginx/conf.d/code-server.conf

파일을 다음과 같이 수정한다. {YOUR_DOMAIN}은 사용한 도메인으로 변경하고 proxy_pass http://localhost:8080/ 부분은 위 code-server에 설정된 포트와 동일한 포트로 설정한다.

server {
  listen 80;
  server_name {YOUR_DOMAIN};

	location / {
		proxy_pass http://localhost:8080/;
		proxy_set_header Host $host;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection upgrade;
		proxy_set_header Accept-Encoding gzip;
	}
}

설정이 모두 완료 되었다면 nginx의 설정을 reload 한다.

$ sudo service nginx reload

이제 {YOUR_DOMAIN}으로 설정한 도메인으로 접속 가능이 가능하다.

Nginx 패스워드 설정 (옵션)

code-server에도 비밀번호 기능이 있어서 이 섹션을 진행하지 않아도 되지만, 보안이 염려된다면 Nginx에 별도 비밀번호를 한번 더 설정해도 좋다.

htpasswd가 설치되있지 않은 경우

htpasswd가 설치 되있지 않은 경우 아래 명령을 통해서 먼저 필요한 툴을 설치한다.

$ sudo yum install httpd-tools

password 설정

{YOUR_ID}에 사용할 아이디를 입력하고 이후 프롬프트에 비밀번호를 입력해 .htpasswd를 설정한다.

$ sudo htpasswd -c /etc/nginx/.htpasswd {YOUR_ID}

basic auth 설정

위에서 설정한 /etc/nginx/conf.d/code-server.conf를 다시 수정한다.

server {
  listen 80;
  server_name {YOUR_DOMAIN};

	location / {
		auth_basic "dev";
		auth_basic_user_file /etc/nginx/.htpasswd;

		proxy_pass http://localhost:8080/;
		proxy_set_header Host $host;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection upgrade;
		proxy_set_header Accept-Encoding gzip;
	}
}

설정이 모두 완료 되었다면 nginx의 설정을 반영한다.

$ sudo service nginx reload

SSL 적용 (letsencrypt 사용)

certbot 설치

letsencrypt를 이용해 인증서를 발급받으려면 certbot을 설치해야 한다. 여기서는 awslinux2[3] 이미지를 이용한다고 가정하고 다음 내용을 진행한다.

먼저 아래 명령으로 certbot 설치를 위해 필요한 저장소를 설정한다.

$ sudo wget -r --no-parent -A 'epel-release-*.rpm' https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
$ sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
$ sudo yum-config-manager --enable epel*
$ sudo yum repolist all

certbot을 설치한다.

$ sudo amazon-linux-extras install epel -y
$ sudo yum install certbot python2-certbot-nginx

certbot 설정

certbot 실행

$ sudo certbot

사용자 이메일 입력

$ sudo certbot

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel):

이용 동의 (Y)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:
install: missing destination file operand after certbot

이용 동의 (Y)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:

연결할 도메인 선택 (숫자 입력, 1)

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: YOUR_DOMAIN
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

인증서 발행

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://YOUR_DOMAIN
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Subscribe to the EFF mailing list (email: YOUR_EMAIL).

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/YOUR_DOMAIN/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/YOUR_DOMAIN/privkey.pem
   Your certificate will expire on 2023-03-21. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again with the "certonly" option. To non-interactively
   renew *all* of your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

발행 실패의 경우 (80포트가 외부로 열려 있지 않은 경우), lightsail 방화벽을 확인해서 외부에서 80포트 접속이 가능하도록 설정한다.

Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Requesting a certificate for YOUR_DOMAIN
Performing the following challenges:
http-01 challenge for YOUR_DOMAIN
Waiting for verification...
Challenge failed for domain YOUR_DOMAIN
http-01 challenge for YOUR_DOMAIN
Cleaning up challenges
Some challenges have failed.

인증서 만료시 자동으로 갱신되도록 crontab에 등록한다. 아래는 매일 01:39와 13:39에 실행되도록 예약한다. 시차 또는 실패의 이유로 갱신이 안되는 경우도 있을 수 있기에 하루에 두번 실행하는 것이 좋다.

39      1,13    *       *       *       root    certbot renew --no-self-upgrade

crond 재시작

$ sudo systemctl restart crond

장점

  • VS Code와 거의 동일한 개발 환경을 제공한다. VS Code에 익숙한 개발자라면 어려움 없이 그대로 사용할 수 있다.
  • 언제나 같은 개발환경을 제공한다. 이전 개발했던 환경을 그대로 이어서 진행할 수 있어서 개발흐름에 발해가 되지 않는다.
  • 테블릿(iPad 등)이나 급하게 외부에서 개발환경이 필요한 경우 사용할 수 있다. 특히 SSH를 이용할 수 있어서 vscode.dev 보다는 할수있는 것들이 많다.

단점

  • nginx에 인증, code-server에 인증 그리고 방화벽을 통한 인증을 설정했지만 중요한 정보와 중요 서버에 경유해서 접속해야 하는 경우 여전히 보안이 걱정된다.
  • 설정 sync를 지원하지 않아서 초기 환경을 만드는데 불편하다. (개인 설정 및 설치해서 사용했던 Extensions을 모두 다시 설치해야한다.) 하지만 곧 지원예정이다[4].
  • 서버 개발을 위해서는 더 높은 사양의 인스턴스가 필요하다.

References

](https://aws.amazon.com/ko/premiumsupport/knowledge-center/lightsail-differences-from-ec2/) [^2]: https://github.com/coder/code-server/blob/main/docs/requirements.md [^3]: https://docs.aws.amazon.com/ko_kr/AWSEC2/latest/UserGuide/SSL-on-amazon-linux-2.html [^4]: https://coder.com/docs/code-server/latest/FAQ