LoginSignup
15
18

More than 5 years have passed since last update.

CentOS6にPHP7をソースからインストール

Posted at

環境構築につかうもの

CentOS 6.7にPHPをソースからインストールします。
環境構築には以下を使いました。

もちろんVagrantは使わなくてもかまいません。
Vagrantの操作方法は割愛しています。

Apacheをインストール

まずはApacheをインストールします。
Apacheはyumで入れます。

$ su -
# yum install -y httpd

設定ファイルを編集します。

vi /etc/httpd/conf/httpd.conf

↓以下を追記
ServerName localhost:80

立ち上がる事を確認します。

# service httpd start

http://192.168.33.10 にアクセスして、ページが表示されたら成功です。
IPは適宜読み替えてください。

OS起動時に自動起動するようにしておきます。

# chkconfig --add httpd

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

PHPをインストールする前に、必要なパッケージをインストールします。

# yum install -y httpd-devel libxml2-devel bzip2-devel curl-devel libjpeg-devel libpng-devel libXpm-devel freetype freetype-devel aspell-devel recode-devel openssl-devel gmp-devel readline-devel

http://qiita.com/soundws/items/cc84da42419f1ab3443b を参考にして、
yumのリポジトリを追加します。

# wget http://ftp.riken.jp/Linux/fedora/epel/RPM-GPG-KEY-EPEL-6
# rpm --import RPM-GPG-KEY-EPEL-6 
# rm -f RPM-GPG-KEY-EPEL-6 
# vi /etc/yum.repos.d/epel.repo
# 以下の内容で新規作成
[epel]
name=EPEL RPM Repository for Red Hat Enterprise Linux
baseurl=http://ftp.riken.jp/Linux/fedora/epel/6/$basearch/
gpgcheck=1
enabled=0

パッケージをインストールします。

# yum --enablerepo=epel install -y libmcrypt php-mcrypt libmcrypt-devel php-mcrypt-devel

PHPをダウンロード

# cd /usr/local/src
# wget -O php-7.0.6.bz2 http://jp2.php.net/get/php7.0.6.tar.bz2/from/this/mirror
# tar jvxf php-7.0.6.bz2

PHPをインストール

コンパイルします。

# cd php-7.0.6
# ./configure \
    --enable-mbstring \
    --enable-zip \
    --enable-bcmath \
    --enable-pcntl \
    --enable-ftp \
    --enable-exif \
    --enable-calendar \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-wddx \
    --with-curl \
    --with-mcrypt \
    --with-iconv \
    --with-gmp \
    --with-pspell \
    --with-gd \
    --with-jpeg-dir=/usr \
    --with-png-dir=/usr \
    --with-zlib-dir=/usr \
    --with-xpm-dir=/usr \
    --with-freetype-dir=/usr \
    --enable-gd-native-ttf \
    --enable-gd-jis-conv \
    --with-openssl \
    --with-gettext=/usr \
    --with-zlib=/usr \
    --with-bz2=/usr \
    --with-recode=/usr \
    --with-mysqli=mysqlnd \
    --enable-mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-readline \
    --with-apxs2=/usr/sbin/apxs

成功したらインストールをします。

# make && make install

以下のように表示されれば成功です。

You may want to add: /usr/local/lib/php to your php.ini include_path
/usr/local/src/php-7.0.6/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:           /usr/local/include/php/ext/pdo/

PHPの設定

php.iniを設置します。
/etcに置かれているので所定の位置にコピーします。

# cp /etc/php.ini /usr/local/lib
# vi /usr/local/lib/php.ini

以下を記述して保存します。
include_path = "/usr/local/lib/php"

Apacheの設定

ApacheでPHPを扱えるようにします。

# vi /etc/httpd/conf/httpd.conf

以下を追加
<IfModule mod_php7.c>
  AddType application/x-httpd-php .php
</IfModule>

以下を編集(index.phpを追加)
DirectoryIndex index.html index.html.var index.php

Apacheを再起動します。

# service httpd restart
# vi /var/www/html/info.php

以下を記述
<?php

phpinfo();

http://192.168.33.10/info.php を実行してみます。
phpinfoが表示されたら成功です。

PDOのテスト

PDOが使えるか確認します。devというDBと
usersというテーブルにデータがある前提で進めます。

# vi /var/www/html/pdotest.php

以下を記述
<?php

$dsn = 'mysql:dbname=dev; host=localhost; charset=utf8';
$name = 'ユーザー名';
$password = 'パスワード';

try{
    $db = new PDO($dsn, $name, $password);
    $stt = $db->prepare("select * from users");
    $stt->execute();
    while ($row = $stt->fetch(PDO::FETCH_ASSOC)) {
        var_dump($row);
    }
    echo "正常終了";
} catch(PDOException $e){
    echo $e->getMessage();
}

Permission Denied というエラーが出る際はSELinuxが有効である可能性があります。

# getenforce
Enforcing

# vi /etc/sysconfig/selinux
以下を記述して保存して、再起動
SELINUX=disabled

なお、MySQL(MariaDB)のインストールは http://qiita.com/d-dai/items/9b20c47402a8fc5f1a6a 
に記載しています。

15
18
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
15
18