Ubuntu20.04にCode-Serverを簡単にインストールできます

Ubuntu20.04にCode-Serverを簡単にインストールできます

この記事では、Ubuntu20.04にCode-Serverをインストールする方法を説明します。

Code-serverは、任意のマシンでVS Codeを提供し、ブラウザーでアクセスするオープンソースサーバーアプリケーションです。強力なクラウドサーバーを活用して、コンパイルとテストを高速化し、外出先でバッテリーを節約できます。

前提条件

  • Ubuntu20.04が最小でVPSをインストールしました。推奨:4GB RAM、2vCPU。
  • VPSを指す有効なドメイン名。
  • rootユーザーアクセスまたは管理者権限を持つ通常のユーザー。

Ubuntu20.04にCode-Serverをインストールする

1.サーバーを更新します

サーバーを最新の状態に保ちます。

# apt update -y

# apt upgrade -y

2.Code-Serverをインストールします

次のコマンドを使用して、公式リポジトリからコードサーバーをダウンロードします。

# cd /home
# wget https://github.com/cdr/code-server/releases/download/v3.11.0/code-server-3.11.0-linux-amd64.tar.gz

tarファイルを抽出し、次のコマンドを使用してコードサーバーで名前を変更します

# tar xzf code-server-3.11.0-linux-amd64.tar.gz && rm code-server-3.11.0-linux-amd64.tar.gz
# mv code-server-3.11.0-linux-amd64 code-server

コードサーバーのバイナリを実行可能にします。

# cd code-server

code-serverを実行します。

./code-server --port 8000

コンピューターのブラウザーを開き、http:// YOUR_IP_ADDRESS:8000に移動します。パスワードの入力を求められます。SSH端末に表示されているパスワードを使用してください。Visual StudioCodeインターフェイスが開きます。サーバーを停止するに は、SSHターミナルでCTRL + Cを押します 。

3.システムスタートアップサービスを作成します

Linuxのsystemdサービスマネージャーを使用してサービスを作成できます。次の手順を実行します:

次のコマンドを使用して、code-server.serviceという名前で新しいファイルを作成します。

# vi /lib/systemd/system/code-server.service

以下の内容をコピー&ペーストし、設定したいパスワードに置き換えてください。これは、コードサーバーへのログインに使用するパスワードになります。

[Unit]
Description=Code Server Service
After=network.target

[Service]
Type=simple
Restart=on-failure
RestartSec=10
WorkingDirectory=/home/code-server
Environment='PASSWORD='
ExecStart=/home/code-server/code-server --port 8000
StandardOutput=file:/var/log/code-server-output.log
StandardError=file:/var/log/code-server-error.log

[Install]
WantedBy=multi-user.target

ファイルを保存して閉じます。

次に、次のコマンドを使用してcode-server.serviceを起動して有効にします。

# systemctl daemon-reload

# systemctl start code-server

# systemctl enable code-server

コンピューターのブラウザーを開き、http:// YOUR_IP_ADDRESS:8000に移動します。パスワードの入力を求められます。code-server.serviceファイルに記載されているパスワードを使用してください。

4.Nginxをインストールして構成します

ドメイン名を介してインストールにアクセスし、SSL証明書を追加するには、リバースプロキシを設定することをお勧めします。このデモンストレーションの目的で、NginxWebサーバーをリバースプロキシとして使用しています。

Nginxをインストールするには、次のコマンドを使用します。

# apt-get install nginx -y

デフォルトのNginxサイト構成ファイルを無効にします。

# rm /etc/nginx/sites-enabled/default

code-server用の新しいNginxサイト構成ファイルを作成します。

# vi /etc/nginx/sites-available/code-server

次のスニペットをファイルに貼り付け、example.comを独自のドメイン名に置き換えます。

server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

構成を有効にして、NginxWebサーバーを再起動します。

# ln -s /etc/nginx/sites-available/code-server /etc/nginx/sites-enabled/

Nginx構成ファイルをテストします。

# nginx -t

エラーが発生しない場合は、nginxサービスを再起動します。

# systemctl restart nginx

UFWをファイアウォールとして使用している場合は、ファイアウォールを介したSSHおよびNginx接続を許可し、ファイアウォールを有効にします。これにより、コードサーバーのインストールにNginxリバースプロキシ経由でのみアクセスできるようになります。

# ufw allow ssh
# ufw allow 'Nginx Full'
# ufw enable

ドメイン名に移動して、インストールをテストします。

5. Let's EncryptSSLをインストールします

サーバーとエンドユーザー間の接続を安全に保ちます。SSL証明書をインストールすることを強くお勧めします。Let's Encryptは無料のSSL証明書で、有効期間は90日で、90日ごとに更新されます。certbotを使用してSSL証明書をインストールしています。

Certbotをダウンロードしてインストールします。

# apt-get install python3-certbot-nginx

次のコマンドを実行して、Certbotのコマンドラインインストーラーを起動し、example.comを独自のドメイン名に置き換えます。

# certbot --nginx -d example.com

インタラクティブインストーラーを続行します。HTTPトラフィックをHTTPSにリダイレクトするかどうかを尋ねられたら、リダイレクトすることを選択します。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you’re confident your site works on HTTPS. You can undo this
change by editing your web server’s configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/code-server
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/code-server

6. fail2banをインストールします(推奨)

Fail2banは、ログを解析してサーバーへの自動攻撃を検出して軽減するアプリケーションです。事前定義された回数の失敗したログイン試行が検出されると、fail2banはサーバーのiptablesを変更して、事前定義された時間、攻撃者をブロックします。

setuptoolsをインストールします。これは、fail2banをインストールするために必要です。

# apt-get install python3-setuptools -y

fail2banをダウンロードしてインストールします。

git clone https://github.com/fail2ban/fail2ban.git
cd fail2ban
sudo python3 setup.py install
cp build/fail2ban.service /lib/systemd/system/fail2ban.service
cd .. && rm -rf fail2ban

デフォルトの構成ファイルjails.confをjails.localにコピーし、コピーしたファイルをテキストエディターで開きます。

# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# vi /etc/fail2ban/jail.local

次のスニペットをファイルの下部に貼り付けます。maxretry、findtime、およびbantimeパラメーターを変更できます。

[code-server]
enabled = true
logpath = /var/log/code-server-output.log
# maxretry: The amount of unsuccessful login attempts after which a ban is issued.
maxretry = 5
# findtime: The amount of time within which the login attempts must occur.
findtime = 10m
# bantime: The amount of time for which an IP is banned from accessing the server.
bantime = 10m

新しいフィルター構成ファイルを作成してフィルターパターンを定義し、コードサーバーログで失敗したログイン試行を見つけます。

# vi /etc/fail2ban/filter.d/code-server.conf

次のスニペットをファイルに貼り付けます。

[Definition]
failregex = Failed login attempt {\'xForwardedFor\':\'\'.*
ignoreregex =
datepattern = "timestamp':{EPOCH}}$

rsyslogサービスを再起動し、fail2banサービスを有効にして開始します。

# systemctl restart rsyslog.service

# systemctl start fail2ban.service
# systemctl enable fail2ban.service

それでおしまい。インストールは正常に完了しました。

この記事では、Ubuntu20.04にCode-Serverをインストールする方法について説明しました。