開発メモ,主に補足by子連れ親父プログラマー

2012-08-27

CodeIgniterで list() っつー名前のメソッド名を作るとエラーになる。

CodeIgniterで list() っつー名前のメソッド名を作るとエラーになる。

こんな感じで、

public function index()
 {
  $this->load->view('welcome_message');
 }

 public function list()
 {
  echo "ok";
 }

list() メソッドを追加すると・・・、

Parse error: syntax error, unexpected T_LIST, expecting T_STRING in /Users/xxx/Sites/codeigniter/application/controllers/welcome.php on line 25


なんでやねん!

ドラゴンクエストX 目覚めし五つの種族 オンライン(通常版)

2012-08-19

OS X Mountaion Lion で MacPorts を使って開発環境を整える(postgres編)


OS X Mountaion Lion で MacPorts を使って開発環境を整える(postgres編)


まずは、

$ sudo port install postgresql90

とすると終わり際に、

To use the postgresql server, install the postgresql90-server port

と出るので、引き続き、

$ sudo port install postgresql90-server

を実行。終わり際に、

###########################################################
# A startup item has been generated that will aid in
# starting postgresql90-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load postgresql90-server
###########################################################

と、

To create a database instance, after install do
 sudo mkdir -p /opt/local/var/db/postgresql90/defaultdb
 sudo chown postgres:postgres /opt/local/var/db/postgresql90/defaultdb
 sudo su postgres -c '/opt/local/lib/postgresql90/bin/initdb -D /opt/local/var/db/postgresql90/defaultdb'

To tweak your DBMS, consider increasing kern.sysv.shmmax by adding an increased kern.sysv.shmmax .. to /etc/sysctl.conf

が出る。
順番にやっていく。

$ sudo port load postgresql90-server
$ sudo mkdir -p /opt/local/var/db/postgresql90/defaultdb
$ sudo chown postgres:postgres /opt/local/var/db/postgresql90/defaultdb
$ sudo su postgres -c '/opt/local/lib/postgresql90/bin/initdb -D /opt/local/var/db/postgresql90/defaultdb'

ここで、だーっと出る。

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale ja_JP.UTF-8.
The default database encoding has accordingly been set to UTF8.
initdb: could not find suitable text search configuration for locale ja_JP.UTF-8
The default text search configuration will be set to "simple".

fixing permissions on existing directory /opt/local/var/db/postgresql90/defaultdb ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers ... 2400kB
creating configuration files ... ok
creating template1 database in /opt/local/var/db/postgresql90/defaultdb/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

    /opt/local/lib/postgresql90/bin/postgres -D /opt/local/var/db/postgresql90/defaultdb
or
    /opt/local/lib/postgresql90/bin/pg_ctl -D /opt/local/var/db/postgresql90/defaultdb -l logfile start

最後のやつをやってみる。

$ /opt/local/lib/postgresql90/bin/postgres -D /opt/local/var/db/postgresql90/defaultdb

エラーになる。

postgres cannot access the server configuration file "/opt/local/var/db/postgresql90/defaultdb/postgresql.conf": Permission denied

パーミッションの問題か。
sudo su postgres して再トライ。

$ sudo su postgres
$ /opt/local/lib/postgresql90/bin/postgres -D /opt/local/var/db/postgresql90/defaultdb
LOG:  database system was shut down at 2012-08-11 04:10:00 JST
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections

動いたっぽい。

$ /opt/local/lib/postgresql90/bin/psql -l -U postgres                              List of databases
   Name    |  Owner   | Encoding | Collation | Ctype |   Access privileges  
-----------+----------+----------+-----------+-------+-----------------------
 postgres  | postgres | UTF8     | C         | C     |
 template0 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
(3 rows)

よさそうだ。
パスを通しておく。

$ vim .profile

export PATH=/opt/local/lib/postgresql90/bin:$PATH

テスト用DBを作っておく。

$ createdb sample_db -U postgres -E UTF-8

psqlして、

$ psql sample_db -U postgres

テーブルを作る。

sample_db=# CREATE TABLE users (
sample_db(# id SERIAL PRIMARY KEY,
sample_db(# name varchar(255)
sample_db(# );
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
CREATE TABLE

確認。

sample_db=# \d
              List of relations
 Schema |     Name     |   Type   |  Owner  
--------+--------------+----------+----------
 public | users        | table    | postgres
 public | users_id_seq | sequence | postgres
(2 rows)

sample_db=# \d users
                                 Table "public.users"
 Column |          Type          |                     Modifiers                     
--------+------------------------+----------------------------------------------------
 id     | integer                | not null default nextval('users_id_seq'::regclass)
 name   | character varying(255) |
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

データを入れておく。

sample_db=# INSERT INTO users (name) VALUES ('yamada');
INSERT 0 1

セレクトして確認。

sample_db=# select * from users;
 id |  name 
----+--------
  1 | yamada
(1 row)

再びportでインストール

$ sudo port install php5-postgresql

アパッチ再起動

$ sudo /opt/local/apache2/bin/apachectl restart

infophp()で確認。

簡単なphpを書いて接続テスト。

<?php
$conn = pg_pconnect("dbname=sample_db user=postgres");
if (!$conn) {
  echo "An error occured.\n";
  exit;
}

$result = pg_query($conn, "SELECT * FROM users");
if (!$result) {
  echo "An error occured.\n";
  exit;
}

while ($row = pg_fetch_row($result)) {
  echo "Id: $row[0]  Name: $row[1]";
  echo "<br />\n";
}

表示される。

Id: 1 Name: yamada

よさそうだ。

2012-08-18

OS X Mountaion Lion で MacPorts を使って開発環境を整える(perl編)

OS X Mountaion Lion で MacPorts を使って開発環境を整える(perl編)


使うことはないかもしれないが、使えないのも困るだろうということで、perlの設定もしておく。
まず、

$ sudo vim /opt/local/apache2/conf/httpd.conf

として、

<Directory "/opt/local/apache2/cgi-bin">
     AllowOverride None
     Options None
     Order allow,deny
     Allow from all
</Directory>

Options None のところを、コメントアウトして、
Options Indexes MultiViews ExecCGI FollowSymLinks Includes
に変更

<Directory "/opt/local/apache2/cgi-bin">
     AllowOverride None
     Options Indexes MultiViews ExecCGI FollowSymLinks Includes
    #Options None
     Order allow,deny
     Allow from all
</Directory>

あと、

AddHandler cgi-script .cgi

のコメントアウトを外す。

アパッチ再起動

$ sudo /opt/local/apache2/bin/apachectl restart

で、

#!/opt/local/bin/perl
print "Content-type: text/html\n\n";
print "ok";

とだけ書いたファイルを test.cgi として保存して、htdocs 以下に置く。
パーミッションの設定、

$ chmod 0755 test.cgi

をやっておく。
あとはブラウザから開ければOK。



2012-08-17

OS X Mountaion Lion で MacPorts を使って開発環境を整える(MySQL編)

OS X Mountaion Lion で MacPorts を使って開発環境を整える(MySQL編)


引き続きMySQLのインストールへ。
まずは、

$ sudo port install mysql5-server

終わり際に、こんなメッセージと、

###########################################################
# A startup item has been generated that will aid in
# starting mysql5-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load mysql5-server
###########################################################

こんなメッセージが出る。

If this is a new install, in order to setup the database you might want to run:
sudo -u _mysql mysql_install_db5

悩ましいね。
とりあえず、

$ sudo port load mysql5-server

なにも起きない。
続いて、

$ sudo -u _mysql mysql_install_db5

と入れてみると、

Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h bohr.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

だーっと何か出た。
さっぱりわからんのでとりあえず、

$ /opt/local/lib/mysql5/bin/mysql_secure_installation

をやってみる。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)

現在のrootのパスワードをいれろってメッセージが出るが設定してないじゃん。
設定してないから何もいれなくてもいいのか?
が、空でenterしても反応なし。
これは何かオカシイ。
なんか間違ってる。
ということでmac本体を再起動だ!
・・・・・
で、もう一回ここから。

$ /opt/local/lib/mysql5/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
OK, successfully used password, moving on...

やっぱり、ただエンターキーだけ打てばいいってことらしい。
進む。

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

引き続きrootのパスワード設定になったので、2回入れる。
成功!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

あとは、なんじゃようわからんが全部yesで完了!
いやあ、こんな設定初めてやったかも。

$ /opt/local/lib/mysql5/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.63 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

オッケーだ。
パスが通っていないので、

$ vim ~/.profile

して、

export PATH=/opt/local/lib/mysql5/bin:$PATH

を追加。

次へ。

$ sudo port install php5-mysql

終わり際に、以下のメッセージが出るので、

To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set
mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
to /opt/local/var/run/mysql5/mysqld.sock

php.ini を編集する。

$ sudo vim /opt/local/etc/php5/php.ini

で、default_socket の空になっている = に書きこむ。
PDO。

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

mysql。

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysql.default-socket
mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

mysqli。

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock

アパッチ再起動。

$ sudo /opt/local/apache2/bin/apachectl restart

infophp()でmysqlが入ってることを確認。
テスト用データベースを作成する。

mysql> CREATE DATABASE sample_db DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

ユーザー権限を設定。

mysql> GRANT ALL ON sample_db.* TO sample_user@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

データベースを指定して、

mysql> use sample_db
Database changed

テーブルを作成。

mysql> CREATE TABLE users (
    -> id INT AUTO_INCREMENT,
    -> name varchar(255),
    -> PRIMARY KEY  (id)
    -> );
Query OK, 0 rows affected (0.04 sec)

テーブルを確認。

mysql> show tables;
+---------------------+
| Tables_in_sample_db |
+---------------------+
| users               |
+---------------------+
1 row in set (0.00 sec)

一個データをインサート。

mysql> INSERT INTO users (name) VALUES ('yamada');
Query OK, 1 row affected (0.00 sec)

セレクトして確認。

mysql> select * from users;
+----+--------+
| id | name   |
+----+--------+
|  1 | yamada |
+----+--------+
1 row in set (0.00 sec)

DBに接続してセレクトする簡単なphpを書く。
php.netからそのまま。

<?php
$link = mysqli_connect('localhost', 'sample_user', 'password', 'sample_db');
if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";

if ($result = mysqli_query($link, "SELECT name FROM users")) {
    printf("Select returned %d rows.\n", mysqli_num_rows($result));

    /* 結果セットを開放します */
    mysqli_free_result($result);
}

mysqli_close($link);

画面に表示される。

Success... Localhost via UNIX socket Select returned 1 rows.

よさそうだ。


2012-08-10

OS X Mountaion Lion で MacPorts を使って開発環境を整える(apache, PHP編)

OS X Mountaion Lion で MacPorts を使って開発環境を整える(apache, PHP編)


という訳で無事 Mountaion Lion のクリーンインストールも完了し、開発環境を再構築しようってことで四苦八苦。
まずはMacPortsのこのページ


http://www.macports.org/install.php


から Mountaion Lion のリンクをクリックしてダウンロード。
MacPorts をインストール。
それから Apple の開発者ページ


https://developer.apple.com/downloads/index.action


へ行って Command Line Tools (OS X Mountain Lion) for Xcode をダウンロードしてインストール。
(実際にはここでログインが必要で、なんだっけな、iCloudのアカウントで入ってメールアドレスのVerifyがあって、そんでやっとダウンロードできる)


そんで、まずはアパッチ。


$ sudo port install apache2

終わり際に


###########################################################
# A startup item has been generated that will aid in
# starting apache2 with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load apache2
###########################################################

とかって出るので、以下を実行。


$ sudo port load apache2
$ port installed

として確認してみると、アパッチと一緒に


openssl @1.0.1c_0 (active)
 perl5 @5.12.4_0+perl5_12 (active)
 perl5.12 @5.12.4_1 (active)
 sqlite3 @3.7.13_0 (active)

この辺もインストールされているのが分かる。
で、次はphp。


$ sudo port install php5 +apache2 +pear

こちらも終わり間際で


To customize php, copy
/opt/local/etc/php5/php.ini-development (if this is a development server) or
/opt/local/etc/php5/php.ini-production (if this is a production server) to
/opt/local/etc/php5/php.ini and then make changes.

と表示されるので、


$ sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

とする。

ついでにphp.iniを編集して、


$ sudo vim /opt/local/etc/php5/php.ini

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Tokyo 

とタイムゾーンの設定を入れておく。
引き続き、apacheのhttpd.conf を編集。


$ sudo vim /opt/local/apache2/conf/httpd.conf

として、


# Virtual hosts
Include conf/extra/httpd-vhosts.conf

この部分のコメントアウトを外す。
さらに、


AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

となっているあたりに以下を追加。


AddType application/x-httpd-php .php

引き続き


$ sudo vim /opt/local/apache2/conf/extra/httpd-vhosts.conf

を編集。下の方を全部消して、以下のように変更


<VirtualHost *:80>
   DocumentRoot "/opt/local/apache2/htdocs"
   ServerName localhost
 </VirtualHost>
          
 <VirtualHost *:80>
     DocumentRoot "/Users/xxxx/htdocs"
     ServerName vhost
     <Directory "/Users/xxxx/htdocs">
       order deny,allow
       allow from All
       Options All
       AllowOverride All
     </Directory>
</VirtualHost>

自分のユーザーフォルダーの直下にhtdocsを作っておく。


$ mkdir /Users/xxxx/htdocs

その中に、適当な index.html を入れておく。
ついでに


<?php
phpinfo();

とだけ書いてあるinfo.phpファイルを作って入れておく。

あと、


$ sudo vim /private/etc/hosts

として


127.0.0.1   vhost

を追加。


$ sudo /opt/local/apache2/bin/apachectl restart

でアパッチ再起動。ブラウザで見てみる。


http://vhost/
http://vhost/info.php

OKなら mbstring とかも入れる。


$ sudo port install php5-mbstring
$ sudo /opt/local/apache2/bin/apachectl restart

とかやって。phpinfo() でちゃんと反映されているか確認する。
ここまでで一週間くらいかかった。(笑)
先は長いって。



このブログを検索

Powered by Blogger.

ラベル

php (17) jQuery (13) OSX (10) MySQL (8) Javascript (7) Postgres (7) port (7) apache (6) Java (3) Smarty (2) html (2) pear (2) FCKEditor (1) XAMPP (1) css (1) git (1) perl (1) ruby (1)

Facebookバナー