LoginSignup
4
9

More than 5 years have passed since last update.

個人的ローカル開発環境(LAMP環境・PHP7.1)構築メモ

Posted at

いつもGoogleドキュメントに個人的にまとめてあるものを使っていたのですが、せっかくQiita始めたのでこちらに移植。
Qiita初投稿。

前提

  • Vagrant+VirtualBox
  • リモートOSはCentOS7
  • rootユーザーで作業します
  • 最低限UNIXコマンドが扱えること

Vagrantfileの設定

デフォルトのままでは外部→リモートOSにアクセス出来ないので、下記の設定をします。

# 下記の行の先頭についている#を消して、コメントアウトを解除します。
config.vm.network "private_network", ip: "192.168.33.10"

Apacheのインストール

まずはyum install。

yum -y install httpd

常時起動にしてしまいます。

systemctl enable httpd.service

httpd.confの設定

最低限AllowOverrideの設定とDirectoryIndexの設定はしておくと良いかもです。
前者は.htaccessを使うため、後者はPHPをトップページに使えるようにするため。

/etc/httpd/conf/httpd.conf
<Directory "/var/www">
    # None→Allにします
    AllowOverride All
    Require all granted
</Directory>

~中略~

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    # None→Allにします
    AllowOverride All
    Require all granted
</Directory>

~中略~

<IfModule dir_module>
    # index.phpを追加する
    DirectoryIndex index.php index.html
</IfModule>

いちいちApacheの再起動を掛けるのも面倒なので、ここではまだ起動しないでおきます。

PHP7.1のインストール

これを書いている2017年2月現在の最新版PHP7.1の場合。
PHPに特化したリポジトリであるWebtaticを使っちゃいます。

Webtaticリポジトリの追加

Webtatic公式サイトに導入方法が書いてあるので、これに乗っ取ります。
Webtatic Yum Repository
今回はCentOS7なので、「Webtatic EL7 for CentOS/RHEL 7」で。

PHPモジュールのインストール

yumでPHP7.1をインストールしていきます。
これも公式サイトに対応モジュール一覧が記載されているので、必要なものをインストールしていきます。
個人的な設定は以下の通り。

yum -y install php71w php71w-cli php71w-common php71w-devel php71w-fpm php71w-gd php71w-mbstring php71w-mcrypt php71w-opcache php71w-pdo php71w-pear php71w-pecl-imagick php71w-soap php71w-xml

XDebugを導入したい場合

これを執筆している2017年2月現在、なんとWebtaticでXDebugが提供されていません。
しょうがないので自前でコンパイルします。
なお、XDebugの導入には「php-devel」と「php-pear」のインストールが前提です。
※上記のコマンドでは両方とも入ってます。

まずはCのコンパイラとmakeコマンドをインストールします。
※この後に使うpeclコマンドで、C言語のコンパイラが走るようなので。

yum -y install gcc make

そうしたら、pecl install。

pecl install xdebug

php -vをして「with Xdebug」と表示されていればインストール成功です。

sh-4.2# php -v
PHP 7.1.0 (cli) (built: Dec  3 2016 11:17:43) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.1.0, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans

XDebugの設定

このままではXDebugが用を成さないので設定ファイルに設定を書き込みます。
個人的な設定は下記の通り。

/etc/php.d/xdebug.ini
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.idekey=netbeans-xdebug

私はNetbeans使いなのでidekeyが「netbeans-xdebug」となっていますが、お使いのIDEに合わせて設定してください。
ポート番号も同様です。

MariaDBのインストール

CentOS7では、デフォルトのDBにMariaDBが指定されています。
なのでMySQLと違い、リポジトリを指定すること無く普通にyumでインストールが出来ます。
なんと便利な。

私の場合はそのままこのMariaDBを使っちゃいます。

yum -y install mariadb mariadb-server

MariaDBの設定

デフォルトではクエリログの設定がされていないので、このタイミングでしてしまいます。
[mysqld]から[mysqld_safe]の間に下記を追記します。

/etc/my.cnf
character-set-server = utf8

# query log
general-log = 1
general-log-file = /var/log/mariadb/query.log

# slow query
slow-query-log = 1
slow-query-log-file = /var/log/mariadb/slow_query.log
long-query-time = 3

大丈夫かとは思いますが、念のため先にファイルを作っておきます。
所有者は「mysql」ユーザー、及び「mysql」グループにしておきます。

cd /var/log/mariadb
touch query.log slow_query.log
chown mysql:mysql query.log slow_query.log

ここまで来たらMariaDBを常駐させ、起動します。

systemctl enable mariadb.service
systemctl start mariadb.service

MariaDBの確認

念のため一度ログインしておきます。
下記のようになっていれば大丈夫です。

sh-4.2# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Apacheの起動

ここまで来たらそろそろApacheを起動します。

systemctl start httpd.service

確認

PHPファイルを作成して、「Panzer Vor!」と表示されるか確認します。

/var/www/html/index.php
<?php
echo 'Panzer Vor!';

ブラウザに「192.168.33.10」でアクセスして、「Panzer Vor!」と表示されていれば全て完了です。

ぱんつぁーふぉー!

4
9
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
4
9