みそしりんぐ

現在進行形みそしる

LAMP環境(CentOS 7, Apache, MariaDB, PHP 7)を構築する

参考サイトの内容を自分のやりたいことにあわせて詰め込んだまとめ.
やってることは参考元に詳しいです.

目次

  1. Summary
    • ポイント
    • OS
    • 結果
  2. インストール
  3. phpMyAdmin
    • 準備
    • インストール
    • アクセス制限
  4. DB
    • ログイン
    • phpMyAdmin
    • アプリケーション
  5. 蛇足
  6. 参考

Summary

ポイント

  • Cent OSの公式リポジトリではPHP 5.4が提供されている
    • remiリポジトリから最新バージョンをインストールしましょう
  • DB接続はPDO
  • phpMyAdminも導入
  • DBのセッティング,Composerの導入など,実用で使う直前まで

OS

Vagrant上でCent OSを動かしました.
boxはbento/centos-7.2を使用.

# Vagrantfile

Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/centos-7.2" # ←変更
......
$ cat /etc/redhat-release
# CentOS Linux release 7.2.1511 (Core) 

結果

$ httpd -v
# Server version: Apache/2.4.6 (CentOS)
# Server built:   Nov 19 2015 21:43:13

$ mysql --version
# mysql  Ver 15.1 Distrib 5.5.44-MariaDB, for Linux (x86_64) using readline 5.1

$ php -v
# PHP 7.0.4 (cli) (built: Mar  2 2016 18:03:26) ( NTS )
# Copyright (c) 1997-2016 The PHP Group
# Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

インストール

とりあえずすべてインストールし,起動や設定を軽く行います.

準備

$ sudo yum upgrade

Apache

$ sudo yum install httpd
$ sudo systemctl start httpd
ファイアーウォール

httpdを許可しましょう.

$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --reload

……と参考元にあったのですが,私は

# FirewallD is not running

と言われてしまいました.boxでもともと設定してあるのかな.

設定

.htaccessを許可

# /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html"
......
<Directory "/var/www/html">
    AllowOverride FileInfo Options
    #  AllowOverride All でもOK
......

ドキュメントルート変更

# /etc/httpd/conf/httpd.conf

# DocumentRoot "/var/www/html"
DocumentRoot "/var/www/test/public_html"
......
<Directory "/var/www/test/public_html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
......

設定が終わったら再起動しましょう.

$ sudo systemctl restart httpd

MySQLMariaDB

$ sudo yum install mariadb mariadb-server mariadb-devel
設定

文字コード

# /etc/my.cnf

[mysqld]
character-set-server=utf8

[client]
default-character-set=utf8

起動 & 初期設定

$ sudo systemctl start mariadb
$ mysql_secure_installation

コマンドを打った後は,指示通りにすすめればOKです.
わたしはすべてyesで設定しました.

PHP

MariaDB + PDO使用なので,php-mysqlndを追加.また,一緒にComposerもインストール.

$ sudo rpm --import https://raw.githubusercontent.com/remicollet/remirepo/master/remi-release/RPM-GPG-KEY-remi
$ sudo yum install yum-utils http://remi.kazukioishi.net/enterprise/remi-release-7.rpm
$ sudo yum-config-manager --enable remi-php70
$ sudo yum install php php-mbstring php-intl php-gmp php-mysqlnd composer
設定
# /etc/php.ini

[PHP]
error_reporting = E_ALL | E_STRICT
display_errors on

[Date]
date.timezone="Asia/Tokyo"

[mbstring]
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = auto
mbstring.http_output = UTF-8
mbstring.encoding_translation = On
mbstring.detect_order = auto

phpMyAdmin

準備

$ sudo yum install yum-plugin-priorities
# /etc/yum.repos.d/remi.repo

[remi]
priority=100

[remi-test]
priority=100

インストール

$ sudo yum-config-manager --enable remi remi-test
$ sudo yum install phpMyAdmin

アクセス制限

ローカル以外のブラウザからもアクセス可能にする.

# /etc/httpd/conf.d/phpMyAdmin.conf

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     Require all granted # ← 変更
   </IfModule>
......

DB

今後つくるであろうアプリケーションやphpMyAdminのため,データベースを作ります.
また,ユーザの追加や権限の変更なども行っていきます.

ログイン

MariaDB [(none)]>から始まるコマンドは,以下のようにMariaDBにログインして実行してください.

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.44-MariaDB MariaDB Server

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

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

MariaDB [(none)]> ......

phpMyAdmin

アカウント名 pma
パスワード pma_pass

で設定していきます.お好みで変更してください.

phpMyAdmin用データベース・ユーザの作成
-- /usr/share/phpMyAdmin/sql/create_tables.sql

-- --------------------------------------------------------

--
-- Privileges
--
-- (activate this statement if necessary)
GRANT SELECT, INSERT, DELETE, UPDATE, ALTER ON `phpmyadmin`.* TO
  'pma'@localhost;
-- ↑コメントアウトを外す --

-- --------------------------------------------------------
MariaDB [(none)]> source /usr/share/phpMyAdmin/sql/create_tables.sql
MariaDB [(none)]> set password for pma@localhost = password('pma_pass');
MariaDB [(none)]> flush privileges;
設定
<!-- /etc/phpMyAdmin/config.inc.php -->

<?php
......
/**
 * phpMyAdmin configuration storage settings.
 */

/* User used to manipulate with storage */
// $cfg['Servers'][$i]['controlhost'] = '';
// $cfg['Servers'][$i]['controlport'] = '';
$cfg['Servers'][$i]['controluser'] = 'pma';
$cfg['Servers'][$i]['controlpass'] = 'pma_pass';

/* Storage database and tables */
$cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
$cfg['Servers'][$i]['tracking'] = 'pma__tracking';
$cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
$cfg['Servers'][$i]['recent'] = 'pma__recent';
$cfg['Servers'][$i]['favorite'] = 'pma__favorite';
$cfg['Servers'][$i]['users'] = 'pma__users';
$cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
$cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
$cfg['Servers'][$i]['central_columns'] = 'pma__central_columns';
$cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings';
$cfg['Servers'][$i]['export_templates'] = 'pma__export_templates';

アプリケーション

データベース名 test
アカウント名 test_user
パスワード test_pass

で設定していきます.お好みで変更してください.

データベース
-- データベース一覧
MariaDB [(none)]> show databases;

-- 作成
MariaDB [(none)]> create database test;
ユーザ
-- ユーザ一覧
MariaDB [(none)]> select user,host,user from mysql.user;

-- 作成
MariaDB [(none)]> create user 'test_user'@'localhost' identified by 'test_pass';

-- 権限変更
MariaDB [(none)]> grant select,index on test.* to 'test_user'@'localhost';

-- phpMyAdminのアカウントにも権限を与えておく
MariaDB [(none)]> grant all on test.* to 'pma'@'localhost';

権限は以下のページなどを参考にどうぞ.
www.dbonline.jp


蛇足

文字化けするのでロケールを変えてみたものの,治らず諦めました.
LANG=C付きで実行すればとりあえずはいけます!
以下ロケール変更方法.

$ sudo localectl set-locale LANG=en_US.utf8

zero-config.com