Vagrant で CentOS7.2 + nginx1.10 + php7 で phpinfo するまで

  • VirtualBoxのインストール
  • vagrant のインストール
  • ゲストOS起動

    # BOX追加
    > vagrant box add --insecure centos72 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
    # 初期化
    > vagrant init centos72
    # 起動
    > vagrant up
    

    Teratermなどでログイン

    HOST: 192.168.33.10
    ID: vagrant
    PW: vagrant
    

    nginxインストール

    $ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    $ sudo yum install nginx
    バージョン
    $ nginx -v
    起動テスト
    $ sudo nginx -t
    起動
    $ sudo systemctl start nginx
    自動起動設定
    $ sudo systemctl enable nginx
    

    ブラウザからアクセスしnginxのデフォルトページが表示されることを確認する。

    続いてfastcgi(php-fpm)へ処理を渡すためのNginxの設定

    $ sudo vi /etc/nginx/conf.d/myapp.conf
    server {
      listen 80;
      server_name 192.168.33.10;
      root /var/www/myapp/public;
      index index.php index.html;
    
      # '/'で始まる全てのURIに一致
      location / {
        # リクエストURI, /index.phpの順に処理を試みる
        try_files $uri $uri/ /index.php?$query_string;
      }
    
      location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    
        include fastcgi_params;
      }
    }
    
    # rootディレクトリに動作確認用ファイルを作成
    $ sudo mkdir -p /var/www/myapp/public
    
    # 動作確認用のファイルを作成
    $ sudo vi /var/www/myapp/public/index.html
    +test
    $ sudo vi /var/www/myapp/public/index.php
    +
    

    ブラウザでindex.htmlにアクセスするとブラウザ内に表示されるが、index.phpにアクセスすると502が帰ってくることを確認する。

    php7インストール

    必要そうなパッケージをインストール

    $ sudo yum install epel-release
    $ wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
    $ sudo rpm -ivh remi-release-7.rpm
    $ sudo yum install --enablerepo=epel,remi,remi-php70 php70 php70-php-mcrypt php70-php-mbstring php70-php-fpm php70-php-gd php70-php-pecl-xdebug php70-php-pecl-redis php70-php-pecl-imagick-devel php70-php-pecl-imagick php70-php-mysqlnd php70-php-intl php70-php-bcmath php70-php-pecl-zip php70-php-xmlrpc php70-php-xml  php70-php-pecl-http php70-php-pecl-http-devel php70-php-opcache
    

    PHPのパスを通す

    $ cat /opt/remi/php70/enable
    export PATH=/opt/remi/php70/root/usr/bin:/opt/remi/php70/root/usr/sbin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/opt/remi/php70/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
    export MANPATH=/opt/remi/php70/root/usr/share/man:${MANPATH}
    

    .bashrcに追記

    $ vi ~/.bashrc
    export PATH=/opt/remi/php70/root/usr/bin:/opt/remi/php70/root/usr/sbin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/opt/remi/php70/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
    export MANPATH=/opt/remi/php70/root/usr/share/man:${MANPATH}
    

    反映
    $ source ~/.bashrc
    バージョン確認
    $ php -v

    /etc/profileにも追記
    $ sudo vi /etc/profile
    反映
    $ source /etc/profile

    php-fpmの設定

    $ sudo vi /etc/opt/remi/php70/php-fpm.d/www.conf
    +user = nginx
    +group = nginx
    +listen = /var/run/php-fpm.sock
    +listen.owner = nginx
    +listen.group = nginx
    +listen.mode = 0666
    

    php-fpm設定ファイル作成

    $ sudo vi /etc/systemd/system/php-fpm.service
    +[Unit]
    +Description=The PHP FastCGI Process Manager
    +After=syslog.target network.target
    +
    +[Service]
    +Type=simple
    +PIDFile=/var/run/php-fpm.pid
    +ExecStart=/opt/remi/php70/root/usr/sbin/php-fpm --nodaemonize --fpm-config /etc/opt/remi/php70/php-fpm.conf
    +ExecReload=/bin/kill -USR2 $MAINPID
    +
    +[Install]
    +WantedBy=multi-user.target
    起動
    $ sudo systemctl start php-fpm
    自動起動
    $ sudo systemctl enable php-fpm
    phpinfo確認
    $ sudo vi /var/www/myapp/public/index.php
    +
    

    参考
    http://qiita.com/yudsuzuk/items/94fdc3dff58d37a9806f
    http://dqn.sakusakutto.jp/2015/12/centos7_yum_install_php70.html
    VagrantにてCentOS7にNginx+php-fpm+PHP7でLaravelの開発環境構築(前編)
    ありがとうございます!