Ubuntu11.04にSymfony2をインストール

Symfony2をインストールした際のメモです。

sudo apt-get install apache2
sudo apt-get install mysql-server
sudo apt-get install php5 php5-mysql php-pear php-apc php5-intl
sudo apt-get install sqlite3 php5-sqlite
sudo apt-get install make libpcre3-dev                 

mkdir ~/installed
wget -O ~/installed/Symfony_Standard_2.0.4.tgz "http://symfony.com/download?v=Symfony_Standard_2.0.4.tgz" 
tar -xzpf ~/installed/Symfony_Standard_2.0.4.tgz

cd /var/www
sudo ln -s /home/user/installed/Symfony

sudo vi /etc/php5/apache2/php.ini
sudo nano /etc/php5/conf.d/global.ini
date.timezone = "Asia/Tokyo"
short_open_tag = Off

php app/check.php
********************************
*                              *
*  Symfony requirements check  *
*                              *
********************************

php.ini used by PHP: /etc/php5/cli/php.ini

** WARNING **
*  The PHP CLI can use a different php.ini file
*  than the one used with your web server.
*  If this is the case, please ALSO launch this
*  utility from your web server.
** WARNING **

** Mandatory requirements **

  OK        Checking that PHP version is at least 5.3.2 (5.3.5-1ubuntu7.3 installed)
  OK        Checking that the "date.timezone" setting is set
  OK        Checking that app/cache/ directory is writable
  OK        Checking that the app/logs/ directory is writable
  OK        Checking that the json_encode() is available
  OK        Checking that the SQLite3 or PDO_SQLite extension is available
  OK        Checking that the session_start() is available
  OK        Checking that the ctype_alpha() is available
  OK        Checking that the token_get_all() is available
  OK        Checking that the APC version is at least 3.0.17

** Optional checks **

  OK        Checking that the PHP-XML module is installed
  OK        Checking that the token_get_all() function is available
  OK        Checking that the mb_strlen() function is available
  OK        Checking that the iconv() function is available
  OK        Checking that the utf8_decode() is available
  OK        Checking that the posix_isatty() is available
  OK        Checking that the intl extension is available
  OK        Checking that the intl ICU version is at least 4+
  OK        Checking that a PHP accelerator is installed
  OK        Checking that php.ini has short_open_tag set to off
  OK        Checking that php.ini has magic_quotes_gpc set to off
  OK        Checking that php.ini has register_globals set to off
  OK        Checking that php.ini has session.auto_start set to off

** Optional checks (Doctrine) **

  OK        Checking that PDO is installed
  OK        Checking that PDO has some drivers installed: mysql, sqlite, sqlite2


php bin/vendors install --reinstall
http://(サーバーアドレス)/Symfony/web/config.php


localhost以外のアスセスして、以下のエラーが出た場合は、

error: This script is only accessible from localhost.
nano /var/www/Symfony/web/config.php
nano /var/www/Symfony/web/app_dev.php

# 以下削除
if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
    '127.0.0.1',
    '::1',
))) {
    header('HTTP/1.0 403 Forbidden');
    die('This script is only accessible from localhost.');
}

f:id:t-taira:20111030114104p:image

CakePHPでGmailを使ってメール送信

必要なライブラリを用意


以下のライブラリをDLして、app/contollers/componentsに配置します。

・qdmail
 ダウンロードdownload - Qdmail - PHP::Mail Library , Quick and Detailed for Multibyte
・qdsmtp
 ダウンロード - Qdsmtp-Simple SMTP Mailer for PHP

コントローラ


app/app_controller.php

<?php
class AppController extends Controller {
	var $mail_param = array(
		'host' => 'ssl://smtp.gmail.com',
		'port' => 465,
		'from' => 'admin@gmail.com',
		'protocol' => 'SMTP_AUTH',
		'user' => 'sample@gmail.com', // Gmailのメールアドレス
		'pass' => 'password',  // Googleのアカウントのパスワード
	);
}
?>


app/controllers/mails_controller.php

<?php
class MailsController extends AppController {
	// (中略)
	var $components = array('Qdmail');	
	function send() {
		$this->Qdmail->smtp(true);
		$this->Qdmail->smtpServer($this->mail_param);
		$this->Qdmail->to('yamada@gmail.com', '山田太郎様');
		$this->Qdmail->subject('ご注文ありがとうございます!');
		$this->Qdmail->from('admin@gmail.com', 'XXXオンラインショップ');
		$text = 'これはメール配信テストです。';
		$this->Qdmail->text($text);
		$this->Qdmail->send();
	}
}
動作確認


http://localhost/mails/send でメール送信されればOKです。

Vimプラグインの管理

以前、Vimプラグインの管理でpathogenについて書きましたが(Vimで快適なRails開発をはじめるまで - t.taira blog)、
Vundleの方が簡単だったので乗り換えました。

$ cd ~
$ mv .vim .vim_bk
$ mkdir .vim
$ cd .vim
$ git clone git://github.com/gmarik/vundle.git

※Macの場合、gitコマンドはXcodeをインストールすると使用できるようになります。


~/.vimrc

set nocompatible
filetype off

set rtp+=~/.vim/vundle/
call vundle#rc()

" original repos on github
Bundle 'tpope/vim-fugitive'
Bundle 'tpope/vim-haml'
Bundle 'tpope/vim-surround'
Bundle 'tpope/vim-endwise'
Bundle 'tpope/vim-rails'
Bundle 'scrooloose/nerdtree'
Bundle 'scrooloose/nerdcommenter'
Bundle 'Shougo/neocomplcache'
Bundle 'Shougo/unite.vim'
Bundle 'csexton/rvm.vim'
Bundle 'altercation/vim-colors-solarized'
Bundle 'cucumber/cucumber'
Bundle 'kchmck/vim-coffee-script'
Bundle 'thinca/vim-quickrun'
Bundle 'tomasr/molokai'
Bundle 'Shougo/vimshell'
Bundle 'Shougo/vimproc'
Bundle 'leshill/vim-json'
Bundle 'mattn/zencoding-vim'
Bundle 'gmarik/snipmate.vim'
Bundle 'pix/vim-align'
Bundle 'tsaleh/vim-tcomment'

filetype plugin indent on

vimを起動して、「:BundleInstall」でプラグインを取得してインストールしてくれます。
すごく簡単!


:Source
Vim-users.jp - Hack #215: Vundle で plugin をモダンに管理する

はじめてのCakePHPアプリでハマった箇所の対応方法

第5回 CakePHPで作るToDoアプリ(1)|gihyo.jp … 技術評論社
を参考にCakaPHPでアプリを作ってみましたが、古い記事だったので結構はまりました。
対応した内容をメモしておきます。

・環境

Ubuntu11.04-server, Apache2.2.17, PHP5.3.5

・app/tmpディレクトリの権限エラーが出たので、権限を追加しました。
$ chmod -R 777 app/tmp
・以下の警告が出たので、core.phpのSecurity.salt, Security.cipherSeedを変更しました。

app/config/core.php

Notice (1024): Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your application [CORE/cake/libs/debugger.php, line 694]
・以下のエラーが出たので、mod_rewriteの設定をしました。(これが一番ハマった...)
URL rewriting is not properly configured on your server.

1. Help me configure it
2. I don't / can't use URL rewriting

mod_rewriteの設定

$ sudo a2enmod rewrite
Enabling module rewrite.
Run '/etc/init.d/apache2 restart' to activate new configuration!
$ sudo /etc/init.d/apache2 restart

※ 私の場合、mod_rewriteの設定は有効になっていたのですが、なぜかうまく動かない…。
原因はDLしたCakePHP.zipを解凍して、プロジェクトディレクトリにコピーしたのですが、
その際に「.htaccess」がコピーできていませんでした。 orz
.htaccess」を作成してあげたら、無事動きましたー。

SQL Errorが発生、findAllメソッドは非推奨になったのでfindメソッドを使いました。
SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'findAll' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 684]

app/controllers/tasks_controllers.php

class TasksController extends AppController {
    var $name = 'Tasks';
    var $uses = array('Task');
    
    function index() {
	// $this->set('tasks', $this->Task->findAll(null, null, 'Task.created ASC'));
        $this->set('tasks', $this->Task->find('all', array('order'=>'Task.created ASC')));
    }
}
・Viewの日本語が文字化けしたので、DB設定でコメントアウトされていた「'encoding' => 'utf8'」

を有効にしました。

app/config/database.php

	var $default = array(
		'driver' => 'mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'user',
		'password' => 'password',
		'database' => 'todo',
		'prefix' => '',
		'encoding' => 'utf8',
	);
これでようやく、一覧画面が表示できるようになりました。

f:id:t-taira:20110921122643p:image

MySQLでリモートアクセスと日本語(UTF-8)の設定

リモートアクセス
# bind-address = 127.0.0.1

ローカルホストからのアクセスのみ許可する設定になっているので、リモートアクセスできるように上記箇所をコメントアウトします。

日本語(UTF-8)


0) 設定前(文字コードの確認)

mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+


1) デフォルトの文字コードの設定を変更する
sudo nano /etc/mysql/my.cnf

[mysqld]
.....
default-character-set=utf8
character-set-client-handshake
.....

[mysqldump]
.....
default-character-set=utf8

[mysql]
.....
default-character-set=utf8


2) 再起動

sudo /etc/init.d/mysql restart


3) 設定後(設定反映の確認)

mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+


※ 既存のデータベース(UTF-8設定前にlatin1で作成してしまったデータベース)
文字コードを変更する

mysql> alter database [データベース名] default character set utf8;
Query OK, 0 rows affected (0.00 sec)


:Source
MySQLのcharsetをutf8に変更する方法
404 Error - Not Found

Ubuntu11.04にPHP環境を構築するメモ

Apache, PHP, MySQLのインストール

$ sudo apt-get install apache2 php5 php5-gd mysql-server php5-mysql phpmyadmin

バーチャルホストの設定

http://example.jp」で「/home/taira/sites/example.jp」以下のファイルが表示されるようにします。


必要なディレクトリを作成します。

$ mkdir -p /home/taira/sites/example.jp


バーチャルホストの設定ファイルを作成し、hostsに設定を1行追加すればOKです。
・/etc/apache2/sites-available/example.jp

<VirtualHost *:80>
    ServerName  example.jp
    DocumentRoot /home/taira/sites/example.jp
    <Directory "/home/taira/sites/example.jp">
        DirectoryIndex index.html
        AllowOverride all
        Options +ExecCGI +Includes -MultiViews
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log combined
</VirtualHost>


・/etc/hosts

127.0.0.1   example.jp


設定を反映します。

$ sudo a2ensite example.jp
$ sudo /etc/init.d/apache2 reload
$ sudo /etc/init.d/apache2 restart

動作確認


以下の確認用ファイルを作成します。
・/home/taira/sites/example.jp/index.php

<? phpinfo(); ?>


作成できたら、http://example.jp/index.phpにアクセスして以下のように表示されればOK!
f:id:t-taira:20110830092033p:image


:Source
Ubuntu で PHP + MySQL + バーチャルホストでの開発環境を最速で作る方法 | ウェブル