Linux 用 Web サーバーとしてのインストール
このガイドでは、実行方法について説明します。CODESYS 4 Linux上のサーバーとして。
互換性
CODESYS 4 Linux版は現時点ではDebianのみ公式にサポートされています。Ubuntu、Kubuntuなど、他のDebianベースのディストリビューションでも動作するようにすることは可能ですが、サポート範囲は限定的です。
予備的考察
Linuxシステム管理の基礎
Linuxにおけるユーザー管理(PAM、LDAP、IPAなど)
Debianにおけるパッケージ管理
TLS証明書管理に関する知識、可能であれば関連するインフラストラクチャに関する知識
Docker、nginx、その他のサーバー技術に関する知識があれば尚可
HTTPS/TLS
CODESYS 4ブラウザ API を使用しますが、これはセキュアなコンテキストでのみ利用可能です。これは、アクセスが次の方法で行われる場合に常に当てはまります。localhost。
作りたいと思ったらすぐにCODESYS 4ただし、ネットワーク上の他のコンピュータからアクセス可能なサーバーとして、HTTPS経由での通信が絶対に必要です。そうでない場合、アプリケーションは正しく動作しません。
なぜならCODESYS 4それ自体はまだHTTPS経由の通信をサポートしていないため、これを行うにはアップストリームプロキシを設定する必要があります。詳細については、セクション全体をお読みください。アップストリームTLSリバースプロキシの設定気をつけて。
ランタイムとゲートウェイ
Linux パッケージはCODESYS 4現在含まれていないCODESYSゲートウェイまたはCODESYSランタイム。
対応するダウンロードは以下にありますCODESYS Store。
プロジェクト内で通信設定(ゲートウェイ)を適切に構成し、サーバーからコントローラーにアクセスできるようにしてください。
サーバー上で作業するユーザーは、以下の方法でのみアクセスできます。CODESYSサーバーからアクセス可能なゲートウェイとコントローラー。サーバーが必要なゲートウェイにアクセスできることを確認するか、サーバー上でローカルゲートウェイを直接利用できるようにしてください。
準備:ユーザーアカウントの作成
デフォルトでは、ユーザー管理はCODESYS 4サーバーは Debian システムのユーザー管理に基づいています。codesys-4グループはログインできますCODESYS 4デフォルトではサーバーが起動します。ただし、オプションを指定してサーバーを起動することもできます。--login-groups=first,second,third代わりにログインを許可すべき他のグループを1つ以上指定します。
作成する
codesys-4グループ。これは一度だけ必要で、Debianパッケージのインストール時に自動的に実行されます。
別のグループを使用したい場合は、ここで独自のグループを指定してください。
sudo addgroup codesys-4
新しいユーザーを作成する
User1もしそれがまだ存在しない場合。(ユーザー情報の入力を求めるプロンプトなど)
FullName、Room Numberなどについては、空欄のままにしておくことで省略できます。sudo adduser User1
追加
User1へcodesys-4グループ。独自のグループを使用している場合は、ユーザーを別のグループに割り当ててください。(これにより、そのユーザーはサーバーにログインする権限を得ます。)
sudo adduser User1 codesys-4
任意の数のユーザーを作成できます。パスワードを使用してログインでき、メンバーであるすべてのユーザーcodesys-4グループはログインできますCODESYS 4。
systemd を介してサーバーとして動作
CODESYS 4サーバーは専用のシステムユーザーで実行する必要があるため、
準備
CODESYS 4ウェブサーバーにインストールする必要があります。そのためには、CODESYS 4章のガイドによるとLinux 用デスクトップアプリケーションとしてのインストール(「Debianパッケージのインストール」のセクションまでを含む)。
テスト専用のシステムユーザーを作成します。
> sudo useradd --system --create-home c4-server
ディレクトリ内
/etc/systemd/system/codesys-4.service設定するService Unitファイルsystemd[Unit] Description=CODESYS 4 Server [Service] Type=exec WorkingDirectory=/opt/codesys-4/ ExecStart=/opt/codesys-4/c4-server --port 8080 Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=codesys-4-server User=c4-server Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
サービスを開始するには
systemd。$> sudo systemctl start codesys-4
このサービスはHTTP経由で利用可能になります
localhostポート8080で。
Docker経由でサーバーとして動作
標準のDockerイメージではありません
CODESYS 4現在、Dockerイメージとして配布されていません。そのため、本番環境での使用を目的とした公式のDockerイメージは存在しません。CODESYS 4。
ただし、独自のDockerイメージを構築することも可能です。その手順については、このセクションの後半で説明します。
事前の知識とサポート
このユースケースを正しく設定するには、Dockerに関する基本的な知識が必須です。Dockerの基本的な使い方に関するサポートは提供しておりません。
# Official ASP.NET 8.0 runtime base image FROM mcr.microsoft.com/dotnet/aspnet:8.0 WORKDIR /opt/codesys-4 # The default port is 8080 EXPOSE 8080 # We have to be root to install the package, switch back to app after USER root # Install the Debian package for CODESYS 4. # We set ACCEPT_CODESYS_EULA=true to skip the interactive prompt to accept the EULA during package installation. # Building and executing this Dockerfile therefore means you accept the terms and condition of the CODESYS Engineering EULA! RUN --mount=type=bind,source=output/,target=/tmp/output/ <<EOF ACCEPT_CODESYS_EULA=true dpkg -i /tmp/output/codesys-4*.deb EOF # The server should run with the unprivileged system user "app", see # https://learn.microsoft.com/en-us/dotnet/core/compatibility/containers/8.0/app-user # For security reasons, c4-server will refuse to start as root. USER app:app ENTRYPOINT ["/opt/codesys-4/c4-server"]
イメージをビルドするには、まず以下のDebianパッケージをダウンロードする必要があります。CODESYS 4そしてそれを保存output/ Dockerfile があるサブフォルダーに移動します。その後、通常どおりイメージをビルドできます。docker buildx build指示。
コンテナ内でイメージを起動する場合、コンテナ内に存在し、かつメンバーであるユーザーのみが対象となります。codesys-4グループはサーバーにログインできます(第〇章と比較してください)準備:ユーザーアカウントの作成 )
ユーザーは、コンテナ内の適切なメカニズム(例:PAM)にアクセスできる必要があります。ホームディレクトリは、可能な限りコンテナ内の永続ボリュームとしてマウントする必要があります。--login-groupsこのオプションは、Dockerfile の ENTRYPOINT 定義の引数として指定するか、コンテナ起動時に引数として渡すことができます。
テスト目的で、コンテナの起動直後にホスト上の既存ユーザーを同期できます。docker-test-example.shスクリプト:
(もちろん、タグ識別子や独自のDockerレジストリなど、ご自身の環境に合わせてスクリプトを調整する必要があります。)
# This script is used to start our CODESYS 4 docker containers in our
# development and test environments (RasPi, WSL, Linux VM).
# It's not regarded as safe for production use!
# The name of our container
CONTAINER=codesys-4
# The repository to fetch the image from
URL="dockerhost.example.com:1234/codesys-images/codesys-4:develop"
# Stop and clean up any running container.
if docker inspect "$CONTAINER" > /dev/null 2>&1; then
echo Trying to clean up
docker stop "$CONTAINER"
docker rm "$CONTAINER"
fi
# stop and rm may fail when the container does not exist,
# but from here on, we want to abort on first error
set -e
echo Downloading "$URL"...
docker pull "$URL"
echo starting image...
# Starting the docker image.
# We mount the /home folder. We listen on port 8080.
# The option "--add-host host.docker.internal:host-gateway" allows us to access
# a CODESYS gateway running on the host machine via the hostname
# "host.docker.internal" from within the container.
docker run --restart=unless-stopped --detach \
--volume /home:/home \
-p127.0.0.1:8080:8080 \
-e CBE_PORT=8080 \
--add-host host.docker.internal:host-gateway \
--name "$CONTAINER" \
"$URL"
# output the version and build info, with some newlines, so it's easier readable.
echo -e \\n CODESYS 4 image build info: $(docker exec codesys-4 cat /opt/codesys-4/dist/version.json) \\n
# Ensure we have a home directory the app user can use, to write the C4 log files.
# The base image already contains /home/app, but it's shadowed by mounting our
# /home into the container, so we need to create the folder if it doesn't exist.
# Strictly speaking, this is only necessary once on a given host (because /home
# has been mounted from the host), but if we run it always, we can be sure that
# this script will also work on fresh machines.
docker exec --user 0 "$CONTAINER" bash -c "mkdir -v -p /home/app ; chown -v app:app /home/app ; chmod -v og-rwx /home/app"
# Synchronize the actual users into the container. We use a very hackish approach
# here, not recommended for production use, it just works for the dev environment.
# WARNING: Synchronizing will only work when:
# 1) The users do not yet exist within the container
# 2) The numeric user and group IDs are not yet occupied within the container.
# Also, it's recommended to configure sudo so it caches the password using
# timestamp-timeout, or even NOPASSWD if you want to take the risk.
# Only the groups codesys-4 and the user personal group will be synchronized.
echo synchronizing group codesys-4
getent group codesys-4| docker exec --user 0 -i "$CONTAINER" /bin/sh -c "cat >>/etc/group"
sudo getent gshadow codesys-4| docker exec --user 0 -i "$CONTAINER" /bin/sh -c "cat >>/etc/gshadow"
# get all users in group codesys-4
C4_USERS=$(getent group codesys-4| awk -F':' '{print $4}' | tr ',' ' ')
for CURRENT in $C4_USERS ; do
echo synchronizing user $CURRENT
getent passwd $CURRENT | docker exec --user 0 -i "$CONTAINER" /bin/sh -c "cat >>/etc/passwd"
sudo getent shadow $CURRENT | docker exec --user 0 -i "$CONTAINER" /bin/sh -c "cat >>/etc/shadow"
# we also need to synchronize the user specific group
getent group $CURRENT | docker exec --user 0 -i "$CONTAINER" /bin/sh -c "cat >>/etc/group"
sudo getent gshadow $CURRENT | docker exec --user 0 -i "$CONTAINER" /bin/sh -c "cat >>/etc/gshadow"
done
echo finished.TLSリバースプロキシの事前配置
CODESYS 4ブラウザ API を使用しますが、これはセキュアなコンテキストでのみ利用可能です。これは、アクセスが次の方法で行われる場合に常に当てはまります。localhost。 出来るだけ早くCODESYS 4サーバーはネットワーク上の他のコンピューターからアクセス可能であるため、使用するブラウザが信頼するサーバー証明書を持つTLS暗号化を必ず設定する必要があります。
CODESYS 4現時点ではTLS暗号化を実装していません。CODESYS 4ポータルプロキシの背後で実行され、TLS暗号化を維持できます。そうでない場合は、リバースプロキシなどnginx事前に簡単に配置でき、TLS 暗号化を維持します。特に、proxy_set_headerそしてproxy_cache_bypass指示は、すべてが正しく機能するために必要です(WebSocketも含む)。
SSL証明書
このユースケースでは、有効なSSL証明書、または組織内で信頼済みとして分類されている自己署名SSL証明書のいずれかが絶対に必要です。
IT管理者と相談し、事前の知識なしに自分の証明書を信頼できるものとして分類しようとしないでください。
テスト目的で証明書を発行したい場合で、手順を理解している場合は、次のセクションのガイドに従ってください。テスト目的でTLS証明書を使用するこれらの証明書は、決して本番環境で使用してはなりません。
リバースプロキシとしてのNginx
以下の例は、TLS暗号化を維持するためにnginxをリバースプロキシとして構成する方法を示しています。CODESYS 4以下のファイルを保存できます。/etc/nginx/sites-available/codesys-4また、以下のパスも調整する必要があります。ssl_certificateそしてssl_certificate_key証明書の実際の場所へ移動します。その後、symlink以下の設定を有効にする/etc/nginx/sites-enabled/そしてnginxを再起動してください。
# See https://docs.microsoft.com/en-us/troubleshoot/developer/webapps/aspnetcore/practice-troubleshoot-linux/2-2-install-nginx-configure-it-reverse-proxy
# for more information.
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/ssl/certs/codesys-4-certificate.crt;
ssl_certificate_key /etc/ssl/private/codesys-4.key;
#server_name _;
server_tokens off; # see https://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens
location / {
rewrite ^/$ /index.html last;
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
#hsts header
add_header Strict-Transport-Security "max-age=31536000" always;
}
}
# Redirect unencrypted http access to encrypted https access.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name _;
return 301 https://$host$request_uri;
}テスト目的のTLS証明書
セキュリティに関するお知らせ
警告:独自の証明書を発行して信頼することは、重大なセキュリティリスクをもたらす可能性があります。以下の手順は、IT管理者の許可を得ている場合にのみ実行してください。これらの手順を実行するために、既存のグループポリシーやその他のセキュリティ対策を回避しようとしないでください。
実運用においては、ブラウザが認識する公式認証局(CA)発行の証明書、または社内インフラ内で認識される証明書の使用を強く推奨します。不明な点がある場合は、IT部門にご相談ください。
必ず秘密鍵ファイルを保管してくださいexample.keyそして特にexampleca.keyこれらのファイルは安全に保管され、他の誰にもアクセスできないようにしてください。これらのファイルにアクセスした者は、それらを使って無数の証明書を偽造し、あなたやあなたの組織に対して中間者攻撃を仕掛ける可能性があります。
使用できますopenssl以下に示すように、テスト目的で証明書を生成するコマンドを実行します。その前に、example_cert.extファイルでは、証明書が有効となるホスト名を編集する必要があります。また、--subj具体的な使用状況に応じて、コマンド内のパラメータを調整してください。
authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment extendedKeyUsage=serverAuth subjectAltName = @alt_names [alt_names] DNS.1=localhost # adjust these to your needs DNS.2=first.host.example.com DNS.3=other.host.example.com
# Generate development/testing certificates for CODESYS 4. # Add the hostnames to example_cert.ext with your favourite text editor. # Creating the CA: openssl genrsa -out exampleca.key 2048 openssl req -new -x509 -days 365 -key exampleca.key -subj "/C=ZZ/ST=Example Kingdom/L=Example City/O=Example Organization/CN=Example Test CA" -out exampleca.crt # Creating the Certificate: openssl genrsa -out example.key 2048 openssl req -new -nodes -out example.csr -key example.key -subj "/C=ZZ/ST=Example Kingdom/L=Example City/O=Example Organization/CN=Example Test Server" openssl x509 -req -days 365 -in example.csr -CA exampleca.crt -CAkey exampleca.key -out codesys-4-development-certificate.crt -extfile example_cert.ext -CAcreateserial
重要
互換性
Cygwin ベースのシェル (例えば Git bash) を使用すると、openssl reqコマンドが失敗する可能性があります。これは、Cygwin に関連する OpenSSL の既知の問題です。詳細については、以下を参照してください。 https://github.com/openssl/openssl/issues/8795。
この問題が発生するのは、正規のLinuxシェル(WSLとも呼ばれる)でOpenSSLを実行する場合、またはCMDやPowerShellのコマンドライン経由で実行する場合です。
ルート証明書exampleca.crt次に、それぞれのブラウザで信頼済みとして登録する必要があります。実際の証明書codesys-4-development-certificate.crtおよびそれぞれの秘密鍵example.keyサーバーにインストールし、サーバー構成で参照する必要があります。たとえば、nginx をこの目的で使用できます (セクションを参照) TLSリバースプロキシの事前配置。
取り外すことを忘れないでくださいexampleca.crtテスト段階終了後、ブラウザの信頼済み証明書リストから証明書を削除します。