LoginSignup
14
15

More than 5 years have passed since last update.

さくらVPSでCakePHPを動かす。

Last updated at Posted at 2015-01-08

さくらVPSの初期設定からApache起動

さくらのVPS サーバの初期設定ガイド

PHPとApacheインストール

phpをインストールするとapacheもインストールされる。

yum install php

再起動時にapacheが起動するように設定する。

chkconfig httpd on

apacheを起動。

service httpd start

接続確認

http://標準ホスト名

※標準ホスト名はさくらVPSのサーバー情報で確認。

各種設定

apacheの設定

httpd.conf

/etc/httpd/conf/httpd.confのDocumentRootDirectoryを適宜変更する。

/var/www/htmlから/var/wwwにする場合


DocumentRoot "/var/www"

<Directory "/var/www">

NoneからAllにしておかないと、.htaccessで上書きできず、CakePHPにドキュメントルートの変更を教えられないので必要。

AllowOverride All

conf.d

/etc/httpd/conf.dに個別アプリ専用の設定を追加する。

fooアプリの場合

vi foo.conf

fooアプリのwebrootを設定する場合

Alias /foo /var/www/foo/app/webroot

<Location /foo>
    Order deny,allow
    Deny from all
    Allow from all
    AllowOverride all
</Location>

.htaccess

CakePHPにドキュメントルートの変更を教えるために、次の3つにRewriteBase /fooを追記する。

/var/www/foo/app/webroot/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /foo
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
~             

/var/www/foo/app/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase    /foo
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

/var/www/foo/.htaccess

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteBase    /foo
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

phpの設定

/etc/php.iniのタイムゾーンを設定する。

date.timezone = Asia/Tokyo

apache再起動

service httpd restart

デプロイ

アプリのアップロード

ftpをたてなくても、SSH(22ポート)でアクセスできるので、それで。
filezillaなら、MacでもWinでも使える。

場所は、ドキュメントルートの直下。
DocumentRoot "/var/www"で、fooアプリなら、/var/www/fooとなるように。

接続

http://標準ホスト名/アプリ名

PostgreSQL9.3.5のインストール

yumでインストールしたいけど、yumリポジトリにはまだ9.3系がないようなのでrpmでインストール。

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

yum install uuid
yum install libxslt

PostgreSQL9.3.5のインストール

バックスラッシュで改行してるけど、1行でもいいらしい。

rpm -ihv http://yum.postgresql.org/9.3/redhat/rhel-6.4-x86_64/postgresql93-9.3.5-1PGDG.rhel6.x86_64.rpm \
http://yum.postgresql.org/9.3/redhat/rhel-6.4-x86_64/postgresql93-server-9.3.5-1PGDG.rhel6.x86_64.rpm \
http://yum.postgresql.org/9.3/redhat/rhel-6.4-x86_64/postgresql93-libs-9.3.5-1PGDG.rhel6.x86_64.rpm \
http://yum.postgresql.org/9.3/redhat/rhel-6.4-x86_64/postgresql93-contrib-9.3.5-1PGDG.rhel6.x86_64.rpm \
http://yum.postgresql.org/9.3/redhat/rhel-6.4-x86_64/postgresql93-devel-9.3.5-1PGDG.rhel6.x86_64.rpm

環境設定

su - postgres
cd /var/lib/pgsql
echo "export PATH=$PATH:/usr/pgsql-9.3/bin" >> .bash_profile 
source .bash_profile

DBの起動

初期化

initdb

サービス起動

su -
service postgresql-9.3 start
service postgresql-9.3 status
chkconfig postgresql-9.3 on

接続確認

su - postgres
psql -U postgres -d postgres
¥q

データベース、テーブルの作成

データベース作成

createdb database

psql database
¥l

テーブル作成

PgAdmin3でGUIでつくって、スクリプトをみると楽。

CREATE TABLE table
(
  id serial NOT NULL,
  aaa integer,
  bbb character varying,
  CONSTRAINT table_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE table
  OWNER TO postgres;

¥d

CSVからデータをインサート

idはserialなので、抜かしているのがポイント。

COPY table (aaa, bbb)
    FROM '/var/www/sabae/DB/citizen_proposal.csv' WITH CSV;

select count(*) from table;
select min(id) from table;
select max(id) from table;

serialのリセット

インサートが途中でこけたりしてやり直すとき、serialをリセットする場合

select setval ('table_id_seq', 1, false);

PDOのインストール

これをいれておかないと、PDOなんてないよって怒られるのでインストール。

yum install php-pgsql
service httpd restart
14
15
2

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