環境を作る度に色んなサイト見て調べてって・・・大変なので、コピペできるように纏めました。
マシンスペック
- Mac mini 2018
- macOS Catalina(10.15.x)
- Intel Core-i7 3.2GHz 6コア
- メモリ 32GB
- SSD 512GB
VM環境
- CentOS 7.x
- Nginx 1.x
- PHP(PHP-FPM)7.3.x
- MariaDB 10.3.x
- Laravel 6.x
- IPアドレス
- 192.168.56.104
- ユーザー/パスワード
- root/vagrant
- vagrant/vagrant
- VM名
- CentOS7
やること
- VirtualBoxに色々インストール
- VagrantのBox作成
- 他
補足
各環境を行ったり来たりするので、下記のように記載します。
[VM]# VirtualBoxの中でroot権限で作業
[Cent]$ MacのターミナルからCentOSに接続して作業
[Cent]# MacのターミナルからCentOSに接続してroot権限で作業
[Mac]$ MacのターミナルでMac内の作業
前提
- VirtualBoxはインストール済み
- Vagrantはインストール済み
※参考
Vagrant+VirtualBoxのインストール(Mac)
VirtualBoxの設定
名前はCentOS7で作成。
NATとホストオンリーアダプターを設定する
macでVirtualBoxにCentOS7をいれてssh接続する
CentOSのインストール
ISOは下記を使用
http://ftp.iij.ad.jp/pub/linux/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso
インストール手順
CentOS 7.6をインストール Linuxのインストールは難しくありません
ネットワーク設定変更
[VM]# cd /etc/sysconfig/network-scripts
[VM]# vi ifcfg-enp0s3
★下記の点線内を編集
---
ONBOOT=yes
---
:wq
[VM]# vi ifcfg-enp0s8
★下記の点線内を編集
---
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.56.104
---
:wq
[VM]# systemctl restart network
ターミナルから接続
[Mac]$ ssh root@192.168.56.104
password:vagrant
NIC名を変更
※CentOS6みたいに、デバイス名をifcfg-eth0とifcfg-eth1にしたかっただけの設定なので、したくなければ不要です。
[Cent]# cd /etc/default
[Cent]# cp -p grub grub_yyyymmdd
[Cent]# vi grub
★下記の点線内をコピペ
---
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet biosdevname=0 net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"
---
:wq
[Cent]# cp -p /boot/grub2/grub.cfg /boot/grub2/grub.cfg_yyyymmdd
[Cent]# grub2-mkconfig -o /boot/grub2/grub.cfg
[Cent]# cd /etc/sysconfig/network-scripts
[Cent]# mv ifcfg-enp0s3 ifcfg-eth0
[Cent]# mv ifcfg-enp0s8 ifcfg-eth1
[Cent]# vi ifcfg-eth0
★enp0s3をeth0に置換
:%s/enp0s3/eth0/g
:wq
[Cent]# vi ifcfg-eth1
★enp0s8をeth1に置換
:%s/enp0s8/eth1/g
:wq
[Cent]# shutdown -r now
※参考
【CentOS7】NICのデバイス名をeth0へ変更
selinuxの無効化
[Cent]# vi /etc/sysconfig/selinux
★下記の点線内を編集
---
SELINUX=disabled
---
ファイアーウォールの無効化
[Cent]# systemctl stop firewalld
[Cent]# systemctl disable firewalld
IPV6の無効化
[Cent]# vi /etc/default/grub
★GRUB_CMDLINE_LINUXに「ipv6.disable=1」を追記
GRUB_CMDLINE_LINUX="ipv6.disable=1 crashkernel=auto spectre_v2=retpoline rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet biosdevname=0 net.ifnames=0"
:wq
[Cent]# grub2-mkconfig -o /boot/grub2/grub.cfg
[Cent]# shutdown -r now
※参考
CentOS 7~Stream 8 ネットワーク設定、及び IPv6 無効化
sudoの設定
[Cent]# visudo
★下記の点線内を追記
---
vagrant ALL=(ALL) NOPASSWD: ALL
---
:wq
Vagrantユーザーの作成と確認
[Cent]# useradd -m vagrant
[Cent]# passwd vagrant
[Cent]# su - vagrant
[Cent]$ sudo ls -l /root
[Cent]$ exit
※参考
VagrantでオリジナルBoxを作成する#vagrantユーザになって確認
yumリポジトリの登録とパッケージ更新
[Cent]# yum -y install epel-release
[Cent]# yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
[Cent]# yum -y update
[Cent]# VirtualBox Guest Additionsのインストール
CDイメージを挿入
VirtualBox Guest Additionsの導入方法をみてみよう!
[Cent]# yum -y install kernel kernel-devel perl gcc
[Cent]# yum -y install bzip2
[Cent]# mkdir /media/cdrom
[Cent]# mount -r /dev/cdrom /media/cdrom
[Cent]# sh /media/cdrom/VBoxLinuxAdditions.run
[Cent]# umount /media/cdrom
※参考
VagrantでオリジナルBoxを作成する#virtualbox-guest-additionsのインストール
Nginxのインストール
[Cent]# vi /etc/yum.repos.d/nginx.repo
★点線内をコピペ
---
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
enabled=0
gpgcheck=0
---
:wq
[Cent]# yum -y --enablerepo=nginx install nginx
PHPのインストール
[Cent]# yum -y install --enablerepo=remi,remi-php73 php php-devel php-mbstring php-pdo php-gd php-xml php-mcrypt php-mysqlnd php-fpm php-pecl-xdebug php-pecl-zip php-bcmath
設定ファイルを編集
PHP設定
[Cent]# vi /etc/php.ini
★点線内を末尾(Gで移動)にコピペ
---
xdebug.remote_enable = On
xdebug.remote_autostart = On
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.scream = 0
xdebug.show_local_vars = 1
xdebug.idekey=PHPSTORM
---
:wq
PHP-FPM設定
[Cent]# vi /etc/php-fpm.d/www.conf
★下記を点線内に編集
user = apache
group = apache
listen = 127.0.0.1:9000
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0660
↓
---
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
---
:wq
Nginx設定
[Cent]# cd /etc/nginx
[Cent]# cp nginx.conf nginx.conf.org
[Cent]# cd /etc/nginx/conf.d
[Cent]# cp default.conf default.conf.org
[Cent]# vi /etc/nginx/conf.d/default.conf
★全行を削除
:%d
★点線内をコピペ
---
server {
listen 80;
server_name laravel6.local;
charset UTF-8;
access_log /var/log/nginx/access.log;
location / {
root /home/vagrant/laravel6/public;
try_files $uri /index.php?$query_string;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /home/vagrant/laravel6/public;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
---
:wq
PHP-FPMとNginxの起動および自動起動設定
[Cent]# systemctl start php-fpm
[Cent]# systemctl enable php-fpm
[Cent]# systemctl start nginx
[Cent]# systemctl enable nginx
※参考
[さくらVPS]CentOS7にphp7 + php-fpm + nginxをインストールする
MariaDBのインストール
[Cent]# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --mariadb-server-version=mariadb-10.3
[Cent]# yum -y install MariaDB-server MariaDB-client
[Cent]# systemctl start mariadb
[Cent]# systemctl enable mariadb
[Cent]# mysql -u root -p
Enter password: パスワードなし、そのままEnter
★DB:hoge、USER:fuga、PASSWORD:hidebu(※好きなのに変える)
MariaDB [(none)]> create database hoge;
MariaDB [(none)]> CREATE USER fuga@'%' IDENTIFIED BY 'hidebu';
MariaDB [(none)]> GRANT ALL ON hoge.* TO fuga@'%';
MariaDB [(none)]> CREATE USER fuga@'localhost' IDENTIFIED BY 'hidebu';
MariaDB [(none)]> GRANT ALL ON hoge.* TO fuga@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit
[Cent]# exit
※参考
CentOS7にyumでMariaDB最新版インストール
ターミナルから接続
[Mac]$ ssh vagrant@192.168.56.104
password:vagrant
Composerのインストール
[Cent]$ curl https://getcomposer.org/installer | php
[Cent]$ sudo mv -i composer.phar /usr/local/bin/composer
Laravelの導入
[Cent]$ sudo yum -y install unzip
[Cent]$ composer create-project --prefer-dist laravel/laravel laravel6 "6.*"
[Cent]$ cd laravel6
[Cent]$ composer require --dev barryvdh/laravel-ide-helper
[Cent]$ composer require --dev squizlabs/php_codesniffer
[Cent]$ chmod -R 777 storage
[Cent]$ chmod -R 777 bootstrap/cache
[Cent]$ vi .env
★下記を点線内に編集
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
↓
---
DB_DATABASE=hoge
DB_USERNAME=fuga
DB_PASSWORD=hidebu
---
:wq
[Cent]$ php artisan migrate
[Cent]$ php artisan db:seed
[Cent]$ mkdir bin
[Cent]$ vi bin/clear-laravel.sh
★下記を点線内をコピペ
---
# !/bin/bash
php artisan view:clear
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan clear-compiled
php artisan config:cache
composer dump-autoload
php artisan ide-helper:generate
php artisan ide-helper:models -N
php artisan ide-helper:meta
find . -name '._.DS_Store' -type f -ls -delete
---
:wq
[Cent]$ chmod 755 bin/clear-laravel.sh
[Cent]$ bin/clear-laravel.sh
※参考
composer install と composer updateの違い
Nginxが/home/vagrant配下を実行できるようにする
[Cent]$ chmod 701 /home/vagrant
動作確認(Macのhosts編集)
[Mac]$ sudo vi /private/etc/hosts
★下記の点線内を追記
---
192.168.56.104 laravel6.local
---
:wq
ブラウザで確認
http://laravel6.local/
VM環境その他いろいろ
VM接続ターミナルに戻る
Gitのインストール
[Cent]$ sudo yum -y install git
vagrant up時の「Warning: Authentication failure. Retrying...」対応
[Cent]$ sudo yum -y install wget
[Cent]$ mkdir -p /home/vagrant/.ssh
[Cent]$ wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
[Cent]$ chmod 0700 /home/vagrant/.ssh
[Cent]$ chmod 0600 /home/vagrant/.ssh/authorized_keys
[Cent]$ chown -R vagrant /home/vagrant/.ssh
※参考
vagrant upコマンド実行時にAuthentication failure.エラーが発生する
その他
★udevのルールの削除
[Cent]$ su -
[Cent]# rm -f /etc/udev/rules.d/70-persistent-net.rules
[Cent]# rm -rf /dev/.udev/
[Cent]# rm -f /lib/udev/rules.d/75-persistent-net-generator.rules
★Box作成のための最適化
[Cent]# yum clean all
★フラグメンテーションの解消
[Cent]# rm -f /EMPTY
[Cent]# shutdown -h now
起動中のVirtualBoxも終了する
VagrantのBox作成
[Mac]$ cd <Boxを作成するディレクトリ>
[Mac]$ vagrant package --base CentOS7 --output CentOS7.box
[Mac]$ vagrant box add CentOS7 CentOS7.box
[Mac]$ vagrant init CentOS7
作成された Vagrantfile を開いて編集
config.vm.network "private_network", ip: "192.168.56.104", auto_config:false
vagrant-vbguestのインストール
[Mac]$ vagrant plugin install vagrant-vbguest
※参考
Vagrantでマウントエラーが発生したときの解消方法
開発環境を起動
[Mac]$ vagrant up
Vagrant環境をマウント
FUSE・SSHFSのインストール
マウントポイントを作成
[Mac]$ mkdir -p ~/MountPoint/mount
マウント
[Mac]$ sshfs vagrant@192.168.56.104:/home/vagrant ~/MountPoint/mount
password:vagrant
アンマウント
[Mac]$ umount -f ~/MountPoint/mount
その他
- PhpStormの設定(PhpStormを利用する場合)
- Laravelを使って開発をするときに入れておきたいPhpStormのプラグイン
- PhpStorm から Vagrant VM の PHP アプリケーションをリモートデバッグする(Web & CLI)
- Gitリポジトリのhttps/sshを切り替えたい
- githubから httpsでcloneしてしまったリポジトリの originを sshに変更する
- SSHFSでアンマウントできなくなった場合
- Macのsshfsでunmountできなくなった時の対処