LoginSignup
7
20

More than 5 years have passed since last update.

VagrantでCentOS7+Apache2.4+PHP7.3+Laravel5.7環境構築

Last updated at Posted at 2019-03-31

VagrantでCentOS7+Apache2.4+PHP7.3+Laravel5.7環境構築

環境

  • Vagrant 2.2.0
  • CentOS7
  • Apache 2.4
  • Composer 1.8.4
  • PHP 7.3.3
  • Laravel 5.7

VagrantでCentOSサーバー構築

Vagrant初期設定

  1. 公式のCentOS7イメージから作成

    https://app.vagrantup.com/centos/

    vagrant init centos/7
    
  2. Vagrantfileを設定

    ~
    config.vm.box = "centos/7"
    ~
    config.vm.network "private_network", ip: "192.168.33.10"

Vagrantの流れ

ローカルPC上で

  1. vagrant up で構築
  2. vagrant halt で停止
  3. vagrant up で起動(データが存在する場合は構築でなく起動になる)
  4. vagrant ssh でサーバーにログイン
  5. vagrant destroy で不要になった環境、検証により壊してしまった環境を削除
  6. vagrant halt && vagrant destroy -f && vagrant up で構築再構築

CentOSサーバー環境設定

CentOSサーバー内で

  # historyに日付が表示されるように設定
  echo "HISTTIMEFORMAT='%F %T '" >> ~/.bashrc

  # SELinuxを無効
  sudo setenforce 0

  # zipインストール
  sudo yum install -y zip

  # wgetインストール
  sudo yum install -y wget

  # Gitインストール
  sudo yum install -y git

Apacheインストール

Apacheは基本的に最新バージョンのインストール推奨
CentOSサーバー内で

  sudo yum install -y httpd

  # サーバー起動時にApache起動
  sudo systemctl enable httpd.service

  # Apache起動
  sudo systemctl restart httpd.service

  # Apache起動確認
  service httpd status

PHPインストール

CentOSサーバー内で

  1. yumにEPELリポジトリを追加

    sudo yum install -y epel-release
    
  2. yumにRemiリポジトリを追加

    sudo yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    

    参考:CentOSのyumレポジトリ: EPEL, Remi, Software Collectionsの違いとまとめ

  3. sudo yum update -y でyumを更新

  4. sudo yum repolist all で有効、無効の全リポジトリの一覧を表示

  5. Laravel 5.7 のサーバ要件を確認
    https://readouble.com/laravel/5.7/ja/installation.html

  6. PHP本体 + PHP関連のモジュールをインストール

    sudo yum install -y --enablerepo=remi,remi-php73 php php-zip php-devel php-mbstring php-pdo php-xml php-bcmath
    
  7. サーバ要件を満たしているか、モジュールを確認

    php -m | grep -e openssl -e PDO -e mbstring -e tokenizer -e ^xml$ -e ctype -e json -e bcmath
    
    # PHP本体と関連のパッケージを削除する場合
    sudo yum remove php-*
    
  8. php -v でPHPがインストールされている事を確認

Laravelインストール

CentOSサーバー内で

  1. composerのインストール

    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    
  2. composer -V でcomposerがインストールされていることを確認

  3. composerにLaravelのインストール

    composer global require laravel/installer
    
  4. 新しくLaravelプロジェクトを作成

    # 作成するディレクトリまで移動、権限付与0
    cd /var/www/html
    sudo chmod 777 -R /var/www/html
    
    # プロジェクトを作成
    composer create-project --prefer-dist laravel/laravel dev_laravel
    

Laravelプロジェクト環境設定

  1. 「C:\Windows\System32\drivers\etc\hosts」ファイルにホスト名追記

    192.168.33.10 dev.laravel
    
  2. Apacheのドキュメントルートを編集する。
    CentOSサーバー内で

    # バックアップ作成
    sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
    
    # ドキュメントルート編集
    sudo vi /etc/httpd/conf/httpd.conf 
    
    DocumentRoot "/var/www/html"
    > DocumentRoot "/var/www/html/dev_laravel/public"
    
  3. Laravelプロジェクトのパーミッション設定
    CentOSサーバー内で

    # SELinuxを無効
    sudo setenforce 0
    
    cd /var/www/html/dev_laravel
    sudo chmod 775 -R ./storage/
    sudo chmod 775 -R ./bootstrap/cache
    
  4. http://dev.laravel/」にアクセスできることを確認

WinScpから「root」でログイン出来るようにする。

  1. ローカルPCで公開鍵を作成する。(「%HOMEPATH%.ssh\id_rsa.pub」になければ作成)
    参考:ssh公開鍵認証を実装する

  2. ローカルPCの公開鍵「%HOMEPATH%.ssh\id_rsa.pub」をWinScpで「/tmp/id_rsa.pub」に仮配置

  3. vagrant ssh でログインし、「root」ユーザーで鍵を移動

    # rootでログイン
    su root
    Password:vagrant
    
    # .sshフォルダ作成
    mkdir -m 700 ~/.ssh
    
    # rootの.sshフォルダに公開鍵配置
    mv /tmp/id_rsa.pub ~/.ssh/
    cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    
  4. WinSCPから「root」でログイン

    • 転送プロトコル:SFTP
    • ホスト名:localhost
    • ユーザー名:root
    • パスワード:vagrant
    • [設定]→[SSH]→[認証]→秘密鍵:「%HOMEPATH%.ssh\id_rsa.ppk」を設定
  5. WinSCPで作成したプロジェクトをDLし、gitに登録、開発スタート

参考:Vagrantで作成したVMにWinSCPでログインする

7
20
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
7
20