LoginSignup
3
7

More than 5 years have passed since last update.

CentOS上でApacheとPostgreSQLを使ってCodeIgniterを動かす

Last updated at Posted at 2016-03-11

ローカルのMAMP環境ではそんなに苦労しなくても環境が作れた気がするけど、
CentOS上で作ったWebアプリを動かそうとしたらハマったのでメモ。
SELinuxの停止やPostgreSQLのIPv6の無効化に気付かず、だいぶ時間かかりました...。

環境

OS:CentOS 6.7
PostgreSQL:9.4
CodeIgniter:3

構築手順

1 Apacheインストール(+自動起動設定)

$ yum install httpd
$ chkconfig httpd on

2 PHPインストール

$ sudo yum install  php-cli php-fpm php-devel php-gd php-mbstring php-pdo php-pear php-xml php-imap php-pecl php-mcrypt php-common php-pgsql

3 PHP確認

echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

http://IP/phpinfo.php
へアクセスしてPHPの詳細が表示されることを確認する

4 SELinux を止める
/etc/sysconfig/selinuxを編集

SELINUX=disabled

disabledに変更

5 PostgreSQLのインストール
標準リポジトリのPostgreSQLはバージョンが古いので、
標準リポジトリを参照した際にPostgreSQLは参照しない設定にする。

/etc/yum.repos.d/CentOS-Base.repoを開く

[base]
exclude=postgresql*  ←追加

[updates]
exclude=postgresql*  ←追加

公式の yum リポジトリを追加

$ yum -y localinstall http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.noarch.rpm
$ yum -y install postgresql94-server

6 PostgreSQLの初期化

$ service postgresql-9.4 initdb

7 PostgreSQLの自動起動設定

$ chkconfig postgresql-9.4 on

8 PostgreSQLの起動

$ service postgresql-9.4 start

9 postgreユーザでログイン

$su postgres
$psql -d postgres

10 開発用のロール(オーナー)を追加(例. オーナー名:test)

  • OSのログインユーザと同じユーザ名でロールを追加する
postgres=# CREATE ROLE test WITH SUPERUSER CREATEROLE CREATEDB LOGIN;

11 開発用オーナーを指定してDBを作成

create database ’DB名’ owner test;

12 一旦postgresユーザからログアウト

13 DBにログイン

psql -d ’DB名’

14 テーブル作成等
DBログイン後に、作成するWebアプリに合わせてテーブル作成やカラム追加を行う。

15 Codeigniterのdatabase.confを修正

  • ユーザ名、パスワード名、データベース名を任意の値に修正
  • dbdriverをpostgreに変更
$db['default'] = array(
    'dsn'   => '',
    'hostname' => '127.0.01',
    'username' => 'ユーザ名',    ←ここ
    'password' => 'パスワード',   ←ここ
    'database' => 'データベース名', ←ここ
    'dbdriver' => 'postgre',    ←ここ
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

16 PostgreSQLのIPv6を無効にする
postgresql.confを編集

listen_addresses = 'localhost' 

listen_addresses = '0.0.0.0'

17 PostgreSQLを再起動


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