Vagrant+AlmaLinuxでPHP8の開発環境を構築する
こんばんわ。
緊急事態宣言が発令され連休中はどこもいけず困り果てている、どうもみけぽんです。
元々2029年までサポートが保証されていたCentOS8が急遽2021年内で打ち切りになった通称「CentOS8ショック(勝手に名付けた)」ですが、CentOS8の代替としてUbuntuに移行された方も多いと思います。
私もその一人なのですが、個人的になかなかUbuntuを好きになれません。
実家にいるような安心感があったCentOSが忘れられないため、同じRHEL(レッドハット・エンタープライズ・リナックス)の「AlmaLinux」で開発環境を構築してみました。
Locky Linuxとどちらを使うか迷いましたが、2021/3に安定版がリリースされたということもあり、今回はAlmaLinuxを採用しました。
AlmaLinux+VagrantでPHPの開発環境を構築する
VagrantでAlmaLinuxの設定を行う
AlmaLinuxのBoxを追加
最初にAlmaLinuxのBoxを追加します。
Vagrantの公式ページを参照するとAlmaLinuxの公式Boxがありましたので、そちらを追加しました。
ちなみに私はWindowsマシンを使っているので、下記のコマンドはGitBashを使用しています。
途中で連携するプロバイダを聞かれますが、こちらは自分の環境に合わせて選択してください。
私はVagrantとVirtualBoxを主に連携させているため、2を選択しました。
$ vagrant box add almalinux/8
==> box: Loading metadata for box 'almalinux/8'
box: URL: https://vagrantcloud.com/almalinux/8
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) libvirt
2) virtualbox
3) vmware_desktop
Enter your choice: 2
==> box: Adding box 'almalinux/8' (v8.3.20210427) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/almalinux/boxes/8/versions/8.3.20210427/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3-accelerate.amazonaws.com
box:
box: Calculating and comparing box checksum...
==> box: Successfully added box 'almalinux/8' (v8.3.20210427) for 'virtualbox'!
Vagrantの初期設定を行う
作業ディレクトリへ移動し、先ほど追加したBoxでVagrantの初期設定を行います。
$ vagrant init almalinux/8
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
作成されたVagrantファイルは下記を書き換えてください。
Vagrantはフォルダ毎にパーミッションを変更できないので、開発環境ということもありここはセキュリティを気にせず権限は777にしてください。
↓sshキーの自動更新を防ぐため、config.vm.box = "box_name"の真下辺りに追記
config.ssh.insert_key = false
config.ssh.forward_agent = true
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10" ←コメントアウトを解除
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder "../data", "/vagrant_data" ←コメントアウトを解除し下記のように修正
↓
config.vm.synced_folder "./", "/home/vagrant/public_html",
:owner => "vagrant",
:group => "vagrant",
:mount_options => ["dmode=777,fmode=777"]
Vagrantの起動
Vagrantファイルを修正したら起動。
問題がなければ数分で起動できます。
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'almalinux/8'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'almalinux/8' version '8.3.20210427' is up to date...
==> default: Setting the name of the VM: skillsheet_default_1619787809804_29099
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
GuestAdditions are newer than your host but, downgrades are disabled. Skipping.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => C:/Users/user_name/project_name
default: /home/vagrant/public_html => C:/Users/user_name/project_name
==> default: Detected mount owner ID within mount options. (uid: 1000 guestpath: /home/vagrant/public_html)
==> default: Detected mount group ID within mount options. (gid: 1000 guestpath: /home/vagrant/public_html)
開発環境へSSH接続
Vagrantが起動されたら開発環境へSSH接続します。
$ vagrant ssh
[vagrant@localhost ~]$
これでAlmaLinuxをインストールした開発環境へアクセスできます。
AlmaLinuxの設定を行う
バージョンの確認
AlmaLinux 8.3であることが確認できました。
$ cat /etc/almalinux-release
AlmaLinux release 8.3 (Purple Manul)
rootユーザーに変更
この後のインストールはroot権限が必要なため、rootユーザーへ変更しておきます。
vagrantユーザーの場合は下記コマンドでパスワードなしでrootユーザーへ変更できます。
$ sudo su
[root@localhost vagrant]#
タイムゾーンの設定
まずは最初にタイムゾーンを日本時間に合わせます。
$ timedatectl set-timezone Asia/Tokyo
$ timedatectl
Local time: Fri 2021-04-30 22:36:42 JST
Universal time: Fri 2021-04-30 13:36:42 UTC
RTC time: Fri 2021-04-30 22:36:43
Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes
忘れずに再起動も行いましょう。
$ systemctl restart rsyslog.service
apacheのインストール
apache及び関連エクステンションをインストールします。
$ dnf -y install httpd httpd-tools httpd-devel mod_ssl
インストールをしたら起動させた上で、起動されていることを確認します。
$ systemctl start httpd
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-04-30 13:31:06 UTC; 14s ago
Docs: man:httpd.service(8)
Main PID: 5421 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 5994)
Memory: 24.9M
CGroup: /system.slice/httpd.service
├─5421 /usr/sbin/httpd -DFOREGROUND
├─5422 /usr/sbin/httpd -DFOREGROUND
├─5423 /usr/sbin/httpd -DFOREGROUND
├─5424 /usr/sbin/httpd -DFOREGROUND
└─5425 /usr/sbin/httpd -DFOREGROUND
再起動しても自動でapacheを起動するよう有効化もしておきます。
$ systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
PHPのインストール
今回は最新のPHP8をインストールしようと思います。
インストールできるPHPのバージョンを確認
$ dnf module list php
Last metadata expiration check: 0:28:07 ago on Fri 30 Apr 2021 10:28:47 PM JST.
AlmaLinux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
モジュールにPHP8は含まれていないため、Remiレポジトリを追加するところから始めます。
Remiレポジトリを追加し有効化する
まずはRemiレポジトリに依存関係があるEPELリポジトリをインストールします。
$ dnf install -y epel-release
$ dnf update epel-release
その後Remiレポジトリをインストール。
$ rpm -ivh http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Retrieving http://rpms.remirepo.net/enterprise/remi-release-8.rpm
warning: /var/tmp/rpm-tmp.0Qm7q3: Header V4 RSA/SHA256 Signature, key ID 5f11735a: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:remi-release-8.3-1.el8.remi ################################# [100%]
$ rpm --import http://rpms.remirepo.net/RPM-GPG-KEY-remi
Remiレポジトリをインストールしたら有効化します。
$ dnf config-manager --set-enabled remi
再度インストールできるPHPのバージョンを確認すると、PHP8も追加されていました。
$ dnf module list php
Remi's Modular repository for Enterprise Linux 8 - x86_64 150 kB/s | 749 kB 00:05
Safe Remi's RPM repository for Enterprise Linux 8 - x86_64 144 kB/s | 1.7 MB 00:11
Remi's RPM repository for Enterprise Linux 8 - x86_64 246 kB/s | 3.4 MB 00:13
Last metadata expiration check: 0:00:02 ago on Fri 30 Apr 2021 11:09:46 PM JST.
AlmaLinux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
Remi's Modular repository for Enterprise Linux 8 - x86_64
Name Stream Profiles Summary
php remi-7.2 common [d], devel, minimal PHP scripting language
php remi-7.3 common [d], devel, minimal PHP scripting language
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
PHP8のインストール
Remiレポジトリからphp:remi-8.0 モジュールをインストールするとPHP8も一緒にインストールされます。
$ dnf module install -y php:remi-8.0
バージョンを確認すると、PHP8がインストールできたことが確認できます。
$ php -v
PHP 8.0.5 (cli) (built: Apr 27 2021 12:23:55) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
必要に応じて依存関係にあるモジュールもインストールします。
$ dnf install -y php-devel php-bcmath php-mcrypt php-pdo php-xml php-tokenizer php-mysqlnd php-pecl-xdebug php-gd php-intl php-zip php-opcache php-mbstring
まとめ
将来的にはLocky Linuxに移行する可能性もありますが、AlmaLinuxはCentOS8とコマンドの互換性があるのがとても素晴らしいです。
今年中に少しずつ、個人の開発&本番環境をCentOS8からAlmaLinuxにリプレイスしていこうと思っています。