0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu 24.04.2 LTS 上の Vagrant + VirtualBox の中にNginx サーバを構築し、ローカルネットワークから VM に SSH 接続する

Last updated at Posted at 2025-04-14

Nginxの検証環境が必要になったため構築。LXDやMultipassはネットワークブリッジの設定が煩雑なため、今回はVagrantを用いた。

Vagrantのインストール

sudo apt update
sudo apt install virtualbox
curl -LO https://releases.hashicorp.com/vagrant/2.4.1/vagrant_2.4.1-1_amd64.deb
sudo dpkg -i vagrant_2.4.1-1_amd64.deb
mkdir -p ~/vagrant/nginx-ubuntu
cd ~/vagrant/nginx-ubuntu
sudo vi Vagrantfile

IP固定のための設定

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64" # Ubuntu 22.04 LTS
  config.vm.hostname = "nginx-ubuntu"

  # 固定IP設定
  config.vm.network "public_network",
    ip: "192.168.3.30", # ルータのIPに応じて設定
    bridge: "enp8s0" # ホストのネットワークインターフェース名(変更が必要かも)

  # リソース設定
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048
    vb.cpus = 1
  end
end

vagrant建てる

# 起動
vagrant up

# 接続
vagrant ssh

VM内で設定

# ユーザー作成
sudo adduser yourname

# sudoグループに追加
sudo usermod -aG sudo yourname

# yournameとしてログイン
sudo -i -u yourname

# .sshディレクトリ作成(ない場合)
mkdir -p ~/.ssh && chmod 700 ~/.ssh

# 鍵生成(パスフレーズなし)
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N ""

# 公開鍵をauthorized_keysに登録
cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# 一時的にvagrantユーザーの共有フォルダにコピー
sudo cp ~/.ssh/id_ed25519 /vagrant/nginx-ubuntu_id_ed25519
sudo chmod 600 /vagrant/nginx-ubuntu_id_ed25519

#Windowsから接続
ssh -i C:\Users\yourname\.ssh\nginx-ubuntu_id_ed25519 yourname@192.168.3.30

Nginxをインストールし、TCP Exposerを使って公開

TCP Exposerのアカウントはあらかじめ作って設定しておく

sudo apt update
sudo apt upgrade -y

sudo apt install nginx certbot python3-certbot-nginx -y

ssh-keygen -f yourkey -t ed25519

# 鍵をコピー
sudo cp yourkey.pub /vagrant/yourkey.pub

# これで動く (設定のため一度動かすこと)
ssh -i yourkey -R yourname:80:localhost:80 yourdomain@tcpexposer.com

sudo apt update
sudo apt install autossh -y

TCP Exposerのサービス設定

sudo vi /etc/systemd/system/tcpexposer-tunnel.service
[Unit]
Description=tcpexposer-tunnel
After=network.target

[Service]
ExecStart=/usr/bin/autossh -M 0 -i /home/yourname/yourkey -R yourname:80:localhost:80 yourdomain@tcpexposer.com
Restart=always
User=yourname
Environment=PATH=/usr/bin:/bin
WorkingDirectory=/home/yourname
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=tcpexposer-tunnel

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable tcpexposer-tunnel.service
sudo systemctl start tcpexposer-tunnel.service

Vagrantのサービス設定

sudo vi /etc/systemd/system/vagrant-nginx-vm.service
[Unit]
Description=Vagrant Nginx VM
After=network.target network-online.target
Wants=network-online.target

[Service]
Type=forking
User=yourname
WorkingDirectory=/home/yourname/vagrant/nginx-ubuntu
ExecStartPre=/bin/sleep 30
ExecStart=/usr/bin/vagrant up --provider=virtualbox
ExecStop=/usr/bin/vagrant halt
RemainAfterExit=true

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable vagrant-nginx-vm
sudo systemctl start vagrant-nginx-vm
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?